aboutsummaryrefslogtreecommitdiffstats
path: root/spec/scripts/gen_abnf.py
blob: 9569195ad6f924dc452988cbcebba0d4966cf4c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
"""Generate UGI_GRAMMAR_v0.abnf from ugi_registry_v0.json."""

import json
import os
import sys


def load_registry(path):
    with open(path, "r") as f:
        return json.load(f)


def hex_char(c):
    """Return ABNF hex notation for a character."""
    return f"%x{ord(c):02X}"


def hex_range(start, end):
    """Return ABNF hex range."""
    return f"%x{ord(start):02X}-{ord(end):02X}"


def collect_sub_ids(f):
    """Collect all sub-ID codes from a field."""
    ids = []
    if "sub_ids" in f:
        ids.extend(f["sub_ids"].keys())
    if "sub_id_groups" in f:
        for group in f["sub_id_groups"].values():
            if "sub_ids" in group:
                ids.extend(group["sub_ids"].keys())
    return ids


def gen_sub_id_rule(name, sub_ids):
    """Generate ABNF rule matching any of the given sub-ID strings."""
    # Quote each as case-insensitive string
    parts = []
    for sid in sorted(set(sub_ids)):
        # ABNF quoted strings are case-insensitive by default
        parts.append(f'"{sid}"')
    return f"{name} = " + " / ".join(parts)


def main():
    if len(sys.argv) != 3:
        print(f"Usage: {sys.argv[0]} <registry.json> <output.abnf>", file=sys.stderr)
        sys.exit(1)

    registry_path, output_path = sys.argv[1], sys.argv[2]
    reg = load_registry(registry_path)
    fields = reg["fields"]

    lines = []
    lines.append("; UGI Grammar v0 — RFC 5234 ABNF")
    lines.append("; Generated from ugi_registry_v0.json")
    lines.append(";")
    lines.append("; Compatible with abnf.dev/abnf2svg")
    lines.append("")

    # ── Top-level ──
    lines.append("; === Top-level ===")
    lines.append("")
    lines.append('ugi-string = "ugi:" version "@" handle ":" field-list')
    lines.append("")
    lines.append('version = 1*DIGIT ["/" 1*DIGIT]')
    lines.append("")
    lines.append("; handle: alphanumeric, hyphens, underscores, dots")
    lines.append("handle = 1*(ALPHA / DIGIT / %x2D / %x5F / %x2E)")
    lines.append("")
    lines.append('field-list = field *("," field)')
    lines.append("")

    # ── Field dispatch ──
    lines.append("; === Field dispatch ===")
    lines.append("")

    # Build field alternatives
    field_rules = []
    for code in sorted(fields.keys()):
        field_rules.append(f"field-{code}")

    lines.append("field = " + " / ".join(field_rules))
    lines.append("")

    # ── Common rules ──
    lines.append("; === Common rules ===")
    lines.append("")
    lines.append("ODIGIT = %x30-37")
    lines.append("")
    lines.append('paid = "$"')
    lines.append("")
    lines.append('aspire = "+" ODIGIT')
    lines.append("")
    lines.append("paid-aspire = paid [aspire]")
    lines.append("")
    lines.append("modifier = paid-aspire / aspire")
    lines.append("")
    lines.append('alternative = "/" ODIGIT')
    lines.append("")
    lines.append('; custom sub-ID: ~ followed by 2+ alpha chars')
    lines.append('custom-sub = "~" 2*ALPHA')
    lines.append("")
    lines.append("; sub-ID: 1-4 alpha chars")
    lines.append("sub-id = 1*4ALPHA")
    lines.append("")

    # ── Per-field rules ──
    lines.append("; === Per-field rules ===")
    lines.append("")

    for code in sorted(fields.keys()):
        f = fields[code]
        ftype = f["type"]
        mods = f.get("modifiers", {})
        has_paid = mods.get("paid", False)
        has_aspire = mods.get("aspire", False)
        has_alt = mods.get("alternative", False)

        hex_code = hex_char(code)
        lines.append(f"; {code}{f['name']} ({ftype})")

        if ftype == "direct":
            # Build modifier part
            mod_parts = []
            if has_alt:
                mod_parts.append("alternative")
            if has_paid and has_aspire:
                mod_parts.append("modifier")
            elif has_paid:
                mod_parts.append("paid")
            elif has_aspire:
                mod_parts.append("aspire")

            if mod_parts:
                mod_str = " [" + " / ".join(mod_parts) + "]"
            else:
                mod_str = ""

            lines.append(f"field-{code} = {hex_code} ODIGIT{mod_str}")

        elif ftype == "multi":
            # Entry: sub-id + digit + optional modifier
            mod_parts = []
            if has_paid and has_aspire:
                mod_parts.append("modifier")
            elif has_paid:
                mod_parts.append("paid")
            elif has_aspire:
                mod_parts.append("aspire")

            if mod_parts:
                mod_str = " [" + " / ".join(mod_parts) + "]"
            else:
                mod_str = ""

            entry_rule = f"field-{code}-entry"
            if code == "l":
                # l uses 2-char ISO 639-1 codes instead of generic sub-ids
                lines.append(
                    f"{entry_rule} = 2ALPHA ODIGIT{mod_str}"
                )
            else:
                lines.append(
                    f"{entry_rule} = (sub-id / custom-sub) ODIGIT{mod_str}"
                )
            lines.append(
                f"field-{code} = {hex_code} 1*{entry_rule}"
            )

        elif ftype == "single":
            mod_parts = []
            if has_paid and has_aspire:
                mod_parts.append("modifier")
            elif has_paid:
                mod_parts.append("paid")
            elif has_aspire:
                mod_parts.append("aspire")

            if mod_parts:
                mod_str = " [" + " / ".join(mod_parts) + "]"
            else:
                mod_str = ""

            lines.append(
                f"field-{code} = {hex_code} (sub-id / custom-sub) ODIGIT{mod_str}"
            )

        elif ftype == "special":
            if code == "g":
                # g: domains separated by /, $ after domain, ~ for custom, no digits
                lines.append(f'g-domain = 2*4ALPHA ["$"]')
                lines.append(f'g-custom = "~" 2*ALPHA')
                lines.append(
                    f'field-g = {hex_code} (g-domain / g-custom) *("/" (g-domain / g-custom))'
                )
            elif code == "b":
                # b: digit/digit, optional aspire on each
                aspire_str = " [aspire]" if has_aspire else ""
                lines.append(
                    f'field-{code} = {hex_code} ODIGIT{aspire_str} "/" ODIGIT{aspire_str}'
                )
            elif code == "v":
                lines.append(
                    f'field-{code} = {hex_code} ODIGIT "/" ODIGIT'
                )
            else:
                # Fallback
                lines.append(f"field-{code} = {hex_code} 1*(ALPHA / DIGIT / %x24 / %x2B / %x2F / %x7E)")

        lines.append("")

    # ── Block format ──
    lines.append("; === Block format ===")
    lines.append("")
    lines.append(
        'ugi-block = block-header CRLF block-first-line CRLF '
        '1*(block-field-line CRLF) block-footer'
    )
    lines.append("")
    lines.append('block-header = "------- BEGIN UGI BLOCK -------"')
    lines.append('block-footer = "-------- END UGI BLOCK --------"')
    lines.append("")
    lines.append(
        'block-first-line = "v:" version SP "@" handle [SP block-geek-field]'
    )
    lines.append("")
    lines.append('; In block format, G field uses uppercase and appears on first line')
    lines.append(
        'block-geek-field = "G" (g-domain / g-custom) *("/" (g-domain / g-custom))'
    )
    lines.append("")
    lines.append(
        'block-field-line = block-field *(SP block-field)'
    )
    lines.append("")
    lines.append(
        "; Block fields use uppercase code prefix repeated per sub-ID"
    )
    lines.append(
        'block-field = ALPHA 1*(ALPHA / DIGIT / %x24 / %x2B / %x2F / %x7E)'
    )
    lines.append("")

    # ── Core rules ──
    lines.append("; === Core rules (RFC 5234) ===")
    lines.append("")
    lines.append("ALPHA = %x41-5A / %x61-7A")
    lines.append("DIGIT = %x30-39")
    lines.append("SP = %x20")
    lines.append("CRLF = %x0D.0A / %x0A")
    lines.append("")

    output = "\n".join(lines)

    os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
    with open(output_path, "w") as f:
        f.write(output)

    print(f"Generated {output_path}")


if __name__ == "__main__":
    main()