expressir 2.4.50-aarch64-linux → 2.4.52-aarch64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/expressir.gemspec +1 -1
  3. data/grammar-parg/atoms_to_parg.rb +136 -14
  4. data/grammar-parg/atoms_to_parg_jit.rb.txt.bak +315 -0
  5. data/grammar-parg/deepest.rb +21 -0
  6. data/grammar-parg/express-grammar.artifact.json +1 -1
  7. data/grammar-parg/express-grammar.parg +145 -295
  8. data/grammar-parg/express_keywords.parg +123 -130
  9. data/grammar-parg/express_literals.parg +5 -11
  10. data/grammar-parg/express_operators.parg +23 -32
  11. data/grammar-parg/express_remarks.parg +5 -17
  12. data/grammar-parg/parity-model.rb +3 -1
  13. data/grammar-parg/parity.sh +11 -7
  14. data/lib/expressir/3.2/expressir_core.so +0 -0
  15. data/lib/expressir/3.3/expressir_core.so +0 -0
  16. data/lib/expressir/3.4/expressir_core.so +0 -0
  17. data/lib/expressir/4.0/expressir_core.so +0 -0
  18. data/lib/expressir/commands/base.rb +13 -0
  19. data/lib/expressir/commands/validate.rb +6 -0
  20. data/lib/expressir/commands/validate_load.rb +23 -15
  21. data/lib/expressir/commands/validate_remarks.rb +16 -4
  22. data/lib/expressir/express/core.rb +7 -0
  23. data/lib/expressir/express/grammar/express-grammar.artifact.json +1 -1
  24. data/lib/expressir/express/grammar/parg/express_keywords.parg +125 -0
  25. data/lib/expressir/express/grammar/parg/express_literals.parg +8 -0
  26. data/lib/expressir/express/grammar/parg/express_operators.parg +25 -0
  27. data/lib/expressir/express/grammar/parg/express_remarks.parg +7 -0
  28. data/lib/expressir/express/grammar/parser.rb +10 -1
  29. data/lib/expressir/express/remark_scanner.rb +21 -0
  30. data/lib/expressir/version.rb +1 -1
  31. metadata +9 -5
  32. data/grammar-parg/express.parg +0 -57
  33. data/grammar-parg/express_structure.parg +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d60df346073fe612dc3cd5dcaec14f1919f8b1bdd54b2204d13a571e47515c10
4
- data.tar.gz: be77a6f1630a16759a76aef415e68f531d04db487f010a69e4f17aff83b89070
3
+ metadata.gz: 18611e8470ca1f9076fb837e6ff8f36e175fa4b74842429987e9625e2e709f95
4
+ data.tar.gz: b83c73cf8fee8aff2af39ce77105e5d8d0f377999de787a1dcabcc5d8da9c97f
5
5
  SHA512:
6
- metadata.gz: 621e0958c852e81bdde31a1c8a4cb064a8a90d0eb039d117d380a3e346bcdcb089cd0b19d9088046b9cd7c9365d1ce1713b620322a031ae6cd587eda7b5f4e90
7
- data.tar.gz: 0da3497e3e48f7198407efb6380577809a8af192c1a59814419b512a06a9adf0742da1d8f31cc5018a05439401949262d6c712f4169c9d857d6162679f093cd1
6
+ metadata.gz: c4eb03b8330a16c5391a090b42965f9153b3c03863ea86612665cf6fd30469cc67b56b0a4451147e2618a0a764f27ab6a2a419b1c5c3a9d5708f69cedec45d99
7
+ data.tar.gz: 4188439837356c2a59981e6a53384f31aa18c5461ab63355057274077b1cc0084c077568a6448e3cb2d52a25ea7d959e7b50fb1fcb18a0e557d22e6f0f8ffb90
data/expressir.gemspec CHANGED
@@ -53,7 +53,7 @@ Gem::Specification.new do |spec|
53
53
  spec.add_dependency "lutaml-model", "~> 0.8.159"
54
54
  spec.add_dependency "moxml", "~> 0.6.0"
55
55
  spec.add_dependency "paint"
56
- spec.add_dependency "parsanol", "~> 1.3.123"
56
+ spec.add_dependency "parsanol", "~> 1.3.130"
57
57
  spec.add_dependency "ruby-progressbar", "~> 1.11"
58
58
  spec.add_dependency "rubyzip", "~> 3.7"
59
59
  spec.add_dependency "table_tennis"
@@ -37,7 +37,7 @@ REJECTED = Hash.new(0)
37
37
  # the RemarkScanner), and parsanol 1.3.123's TriviaCapture#record
38
38
  # crashes on this grammar (`lstrip` for nil) — plain Trivia keeps the
39
39
  # artifact lane on the backtracking, capture-free wrapper instead.
40
- SKIP_DECL = "skip = trivia"
40
+ SKIP_DECL = "skip = express_remarks.trivia"
41
41
 
42
42
  class AtomRenderer
43
43
  def initialize(rejected, alt_rules)
@@ -206,25 +206,147 @@ end
206
206
  alt_rules = rules.select { |_, body| body.is_a?(Parsanol::Atoms::Alternative) }.keys
207
207
  warn "rules: #{rules.size}/#{RULE_NAMES.uniq.size}"
208
208
 
209
- # -- emit --
210
- out = []
211
- out << "grammar ExpressSyntax version \"0.1.0\" edition 2 {"
212
- out << " # The DSL's trivia channel (match[\" \\r\\n\\t\\f\"].repeat(1)) is"
213
- out << " # emitted below from the live rule; remarks stay product data."
209
+ # -- component split (issue: maintainable multi-file grammar) --
210
+ # The leaf vocabularies live in their own files, composed by the main
211
+ # grammar with top-level `use` declarations. Cross-component rule
212
+ # references render namespaced (`express_keywords.tABS`); captures
213
+ # keep their own names (the import prefix never touches `as`).
214
+ COMPONENTS = {
215
+ "express_keywords" => ->(name) { name.start_with?("t") && name.match?(/\At[A-Z]/) },
216
+ "express_operators" => ->(name) {
217
+ %w[addLikeOp andOp binaryTypeOp? bitwiseOp? compoundOp? divOp? exponentOp?
218
+ infixOp? logicalOp? modOp? multiplicationLikeOp orOp plusLikeOp
219
+ relOp unaryOp xorOp? op_colon op_colon_equal op_comma op_dot op_equal
220
+ op_ge op_gt op_le op_lt op_minus op_not_equal op_plus op_star op_slash
221
+ op_delim op_delim2 op_leftparen op_rightparen op_decl
222
+ op_asterisk op_colon_equals_colon op_colon_less_greater_colon
223
+ op_double_pipe op_equals op_greater_equal op_greater_than
224
+ op_less_equal op_less_greater op_less_than].include?(name)
225
+ },
226
+ "express_remarks" => ->(name) {
227
+ %w[embeddedRemark tailRemark space spaces trivia].include?(name)
228
+ },
229
+ # Pure lexical literals: identifiers, numbers, strings. The TYPE
230
+ # grammar rules (simpleTypes family) stay in the main file — they are
231
+ # syntax, not vocabulary, and would drag expression rules into this
232
+ # component.
233
+ "express_literals" => ->(name) {
234
+ %w[simpleId unguardedId digits simpleStringLiteral
235
+ anyKeyword digit].include?(name)
236
+ },
237
+ }.freeze
214
238
 
215
- renderer = AtomRenderer.new(REJECTED, alt_rules)
216
- rules.sort_by { |name, _| name }.each do |name, body|
239
+ def component_of(name, components, _rules)
240
+ components.each do |component, matches|
241
+ return component if matches.call(name)
242
+ end
243
+ nil
244
+ end
245
+
246
+ def collect_rule_refs(atom, out) # rubocop:disable Metrics/MethodLength -- a uniform tree walker
247
+ case atom
248
+ when Parsanol::Atoms::Entity then out << atom.rule_name.to_s
249
+ when Parsanol::Atoms::Named, Parsanol::Atoms::Repetition
250
+ collect_rule_refs(atom.parslet, out)
251
+ when Parsanol::Atoms::Sequence then atom.parslets.each { |c| collect_rule_refs(c, out) }
252
+ when Parsanol::Atoms::Alternative then atom.alternatives.each { |c| collect_rule_refs(c, out) }
253
+ when Parsanol::Atoms::Lookahead then collect_rule_refs(atom.bound_parslet, out)
254
+ end
255
+ end
256
+
257
+ # Assign every rule to its component (nil = the main grammar).
258
+ rule_component = {}
259
+ rules.each_key do |name|
260
+ rule_component[name] = component_of(name, COMPONENTS, rules)
261
+ end
262
+
263
+ # Qualification happens in the Entity case: the component-aware
264
+ # renderer prefixes cross-component references.
265
+ qualified_renderer = Class.new(AtomRenderer) do
266
+ attr_accessor :current_component, :rule_component
267
+
268
+ def render(atom)
269
+ if atom.is_a?(Parsanol::Atoms::Entity)
270
+ name = atom.rule_name.to_s
271
+ target = @rule_component[name]
272
+ if target && target != @current_component
273
+ return "#{target}.#{name}"
274
+ end
275
+
276
+ return name
277
+ end
278
+ super
279
+ end
280
+ end
281
+
282
+ emit_component = lambda do |component, names|
283
+ r = qualified_renderer.new(REJECTED, alt_rules)
284
+ r.current_component = component
285
+ r.rule_component = rule_component
286
+ body = []
287
+ names.sort.each do |name|
288
+ atom = rules[name]
289
+ decl = ATOMIC_RULES.include?(name) ? "atomic #{name} =" : "#{name} ="
290
+ body << " #{decl} #{r.render(atom)}"
291
+ end
292
+ body
293
+ end
294
+
295
+ main_names = rules.keys.select { |name| rule_component[name].nil? }
296
+ main_refs = {}
297
+ main_names.each do |name|
298
+ refs = []
299
+ collect_rule_refs(rules[name], refs)
300
+ main_refs[name] = refs
301
+ end
302
+
303
+ require "fileutils"
304
+ out_dir = File.dirname(out_path)
305
+ FileUtils.mkdir_p(out_dir)
306
+
307
+ # The leaf components: plain grammar blocks (the import prefixes their
308
+ # rules on merge).
309
+ COMPONENTS.each_key do |component|
310
+ names = rules.keys.select { |name| rule_component[name] == component }
311
+ next if names.empty?
312
+
313
+ component_out = []
314
+ component_out << "grammar Express#{component.split('_')[1].capitalize} version \"0.1.0\" edition 2 {"
315
+ component_out.concat(emit_component.call(component, names))
316
+ component_out << "}"
317
+ path = File.join(out_dir, "#{component}.parg")
318
+ File.write(path, "#{component_out.join("\n")}\n")
319
+ warn "wrote #{path} (#{component_out.size} rules)"
320
+ end
321
+
322
+ # The main grammar: top-level `use` the components (the parsers take
323
+ # use declarations before the grammar header), then the structural
324
+ # rules with qualified cross-component references.
325
+ main_out = []
326
+ main_out << "# The structural grammar proper. The leaf vocabularies compose"
327
+ main_out << "# via top-level use declarations:"
328
+ COMPONENTS.each_key { |component| main_out << "use #{component}" }
329
+ main_out << ""
330
+ main_out << "grammar ExpressSyntax version \"0.1.0\" edition 2 {"
331
+ main_out << " # The DSL's trivia channel (match[\" \\r\\n\\t\\f\"].repeat(1)) is"
332
+ main_out << " # emitted below from the live rule; remarks stay product data."
333
+
334
+ r = qualified_renderer.new(REJECTED, alt_rules)
335
+ r.current_component = nil
336
+ r.rule_component = rule_component
337
+ main_names.sort.each do |name|
338
+ atom = rules[name]
217
339
  decl = ATOMIC_RULES.include?(name) ? "atomic #{name} =" : "#{name} ="
218
- out << " #{decl} #{renderer.render(body)}"
340
+ main_out << " #{decl} #{r.render(atom)}"
219
341
  end
220
342
 
221
- out << " #{SKIP_DECL}"
343
+ main_out << " #{SKIP_DECL}"
222
344
 
223
- out << " entry syntax: syntax"
224
- out << "}"
345
+ main_out << " entry syntax: syntax"
346
+ main_out << "}"
225
347
 
226
- File.write(out_path, "#{out.join("\n")}\n")
227
- warn "wrote #{out_path} (#{out.size} lines)"
348
+ File.write(out_path, "#{main_out.join("\n")}\n")
349
+ warn "wrote #{out_path} (#{main_out.size} rules)"
228
350
  unless REJECTED.empty?
229
351
  warn "REJECTED (#{REJECTED.values.sum}):"
230
352
  REJECTED.first(10).each { |k, n| warn " #{n}x #{k}" }
@@ -0,0 +1,315 @@
1
+ # frozen_string_literal: true
2
+
3
+ # STEP 1 (issue #578): serialize the built DSL atom tree — Parsanol::
4
+ # Native.serialize_grammar's dump ({atoms: [...], root: N}) — to PARG
5
+ # text at edition v2. The dump materializes all 253 DSL rules, including
6
+ # the dynamic KEYWORD expansion; Named atoms carry both rule definitions
7
+ # (referenced by Entity atoms) and inline captures (.as), disambiguated
8
+ # by reference: a Named whose id is an Entity target is a rule.
9
+ #
10
+ # Output: express-grammar.parg. Compile with the parsanol exe; the
11
+ # corpus parity loop (DSL vs artifact over examples/*.exp) gates it.
12
+
13
+ require "json"
14
+
15
+ dump_path = ARGV[0] || File.join(__dir__, "dsl-dump.json")
16
+ out_path = ARGV[1] || File.join(__dir__, "express-grammar.parg")
17
+
18
+ dump = JSON.parse(File.read(dump_path))
19
+ ATOMS = dump["atoms"]
20
+ ROOT_ID = dump["root"]
21
+
22
+ # The DSL skip configuration (parser.rb): skip(:trivia, whitespace:
23
+ # :space, exempt: %i[simpleId digits spaces space embeddedRemark
24
+ # tailRemark]) — the skip channel is the `space` rule, with these rules
25
+ # atomic (self-managing trivia).
26
+ SKIP_RULE = "space"
27
+ ATOMIC_RULES = %w[simpleId digits spaces space embeddedRemark tailRemark].freeze
28
+
29
+ RULE_NAMES = File.read(File.join(__dir__, "RULE-INVENTORY.txt"))
30
+ .scan(/^\[ \] (\w+)$/).flatten + ["syntax"]
31
+
32
+ # KEYWORDS for the simpleId guard: parsanol's serialize_grammar DROPS
33
+ # the DSL's leading `anyKeyword.absent?` lookahead on simpleId (audit:
34
+ # the other four absent? sites survive; only this one is lost) — the
35
+ # guard is restored here from the DSL keyword list.
36
+ load File.join(__dir__, "generate-terminals.rb") rescue nil
37
+ KEYWORD_LIST = (defined?(KEYWORDS) ? KEYWORDS : []).freeze
38
+
39
+ REJECTED = Hash.new(0)
40
+
41
+ class AtomRenderer
42
+ def initialize(atoms, rejected, rule_def_ids, alt_rules)
43
+ @atoms = atoms
44
+ @rejected = rejected
45
+ @rule_def_ids = rule_def_ids
46
+ @alt_rules = alt_rules
47
+ @expanding = []
48
+ @grouped_wrapped = {}
49
+ end
50
+
51
+ def grouped_wrapped(id)
52
+ @grouped_wrapped[id]
53
+ end
54
+
55
+ def mark_grouped(id)
56
+ @grouped_wrapped[id] = true
57
+ end
58
+
59
+ def render(id, depth = 0)
60
+ return "<deep>" if depth > 200
61
+
62
+ a = @atoms[id]
63
+ type = a.keys.first
64
+ v = a[type]
65
+ case type
66
+ when "Str" then literal(v["pattern"])
67
+ when "Re"
68
+ simple_id_run(v["pattern"]) || hex_range(v["pattern"]) || begin
69
+ @rejected["Re #{v['pattern'].inspect}"] += 1
70
+ "<UNLOWERABLE-RE>"
71
+ end
72
+ when "Entity"
73
+ t = v["atom"]
74
+ target = @atoms[t]
75
+ if (nm = target["Named"]) && @rule_def_ids.include?(t)
76
+ # a reference to a rule whose body is an alternation must be
77
+ # parenthesized: ungrouped, the repetition/sequence it sits in
78
+ # captures only the first branch (0* useClause-branch escapes)
79
+ name = nm["name"]
80
+ @alt_rules.include?(name) ? "(#{name})" : name
81
+ elsif @expanding.include?(t)
82
+ @rejected["cycle at atom #{t}"] += 1
83
+ "<CYCLE>"
84
+ else
85
+ # parsanol inlines non-capturing rules into callers: the target
86
+ # body lives in-pool, so expand it in place — parenthesized when
87
+ # the expansion is a sequence or alternative (a grouped class's
88
+ # leading paren is not grouping; only structure counts)
89
+ @expanding.push(t)
90
+ r = render(t, depth + 1)
91
+ @expanding.pop
92
+ if %w[Alternative Sequence].include?(@atoms[t].keys.first) &&
93
+ !@grouped_wrapped.include?(t)
94
+ r = "(#{r})"
95
+ end
96
+ r
97
+ end
98
+ when "Named"
99
+ "(#{render(v['atom'], depth + 1)}) as #{v['name']}"
100
+ when "Sequence"
101
+ v["atoms"].map { |c| child_seq(c, depth) }.join(" ")
102
+ when "Alternative"
103
+ v["atoms"].map { |c| child_alt(c, depth) }.join(" / ")
104
+ when "Repetition"
105
+ mx = v["max"]
106
+ "#{v['min']}#{mx ? "*#{mx}" : "*"} #{child_rep(v['atom'], depth)}"
107
+ when "Lookahead"
108
+ inner = render(v["atom"], depth + 1)
109
+ # keep the child's own grouping: ungrouped ranges inside !() do
110
+ # not parse (probe-verified); !((group)) does
111
+ v["positive"] ? "&(#{inner})" : "!(#{inner})"
112
+ else
113
+ @rejected["atom type #{type}"] += 1
114
+ "<REJECTED-#{type.upcase}>"
115
+ end
116
+ end
117
+
118
+ private
119
+
120
+ # simpleId's single quantified Re (parsanol GH-69 — one Re consumes
121
+ # the whole identifier in one step): lower to a lead class + tail run.
122
+ def simple_id_run(pattern)
123
+ return nil unless pattern == "[a-zA-Z_][a-zA-Z0-9_]*"
124
+
125
+ lead = "%x41-5A / %x61-7A / %x5F"
126
+ "(#{lead}) *(%x41-5A / %x61-7A / %x30-39 / %x5F)"
127
+ end
128
+
129
+ private
130
+
131
+ def child_seq(id, depth)
132
+ a = @atoms[id]
133
+ raw = render(id, depth + 1)
134
+ a.keys.first == "Alternative" ? "(#{raw})" : raw
135
+ end
136
+
137
+ def child_alt(id, depth)
138
+ a = @atoms[id]
139
+ raw = render(id, depth + 1)
140
+ a.keys.first == "Sequence" && !raw.start_with?("(") ? "(#{raw})" : raw
141
+ end
142
+
143
+ def child_rep(id, depth)
144
+ a = @atoms[id]
145
+ raw = render(id, depth + 1)
146
+ if %w[Sequence Alternative].include?(a.keys.first) && !raw.start_with?("(")
147
+ "(#{raw})"
148
+ else
149
+ raw
150
+ end
151
+ end
152
+
153
+ # Lower a single-character-class regex (match["..."] / /./) to PARG
154
+ # %x alternatives. Returns nil for anything it cannot lower exactly.
155
+ def hex_range(pattern)
156
+ case pattern
157
+ when "." then "%x01-10FFFF"
158
+ else
159
+ m = pattern.match(/\A\[(.*)\]\z/m) or return nil
160
+ body = m[1]
161
+ negated = body.sub!(/\A\^/, "")
162
+ alts = []
163
+ i = 0
164
+ while i < body.length
165
+ ch = body[i]
166
+ if ch == "\\" && i + 1 < body.length
167
+ nxt = body[i + 1]
168
+ codes = { "r" => 0x0D, "n" => 0x0A, "t" => 0x09, "f" => 0x0C }
169
+ if (code = codes[nxt])
170
+ alts << "%x#{format('%02X', code)}"
171
+ elsif nxt == "s"
172
+ alts.concat(%w[%x20 %x09 %x0A %x0D %x0C])
173
+ else
174
+ return nil
175
+ end
176
+ i += 2
177
+ elsif i + 2 < body.length && body[i + 1] == "-"
178
+ return nil if ch == "\\"
179
+
180
+ alts << "%x#{format('%02X', ch.ord)}-#{format('%02X', body[i + 2].ord)}"
181
+ i += 3
182
+ elsif ch == '"'
183
+ alts << "%x22"
184
+ i += 1
185
+ elsif ch == "/"
186
+ alts << "%x2F"
187
+ i += 1
188
+ else
189
+ printable = ch.match?(/[[:print:]]/) && !ch.match?(/[[:space:]]/)
190
+ alts << (printable ? "\"#{ch}\"" : "%x#{format('%02X', ch.ord)}")
191
+ i += 1
192
+ end
193
+ end
194
+ return nil if alts.empty?
195
+
196
+ joined = alts.join(" / ")
197
+ # parenthesize multi-alternative classes: inside a sequence, a
198
+ # bare `a / b` would split the sequence across the alternatives
199
+ grouped = alts.size > 1 ? "(#{joined})" : joined
200
+ mark_grouped(joined) if alts.size > 1
201
+ if negated
202
+ # PARG negation does not take ranges — emit the numeric
203
+ # complement over [0x01, 0x10FFFF] as explicit ranges
204
+ pairs = []
205
+ i = 0
206
+ while i < body.length
207
+ ch = body[i]
208
+ if ch == "\\" && i + 1 < body.length
209
+ nxt = body[i + 1]
210
+ codes = { "r" => 0x0D, "n" => 0x0A, "t" => 0x09, "f" => 0x0C }
211
+ if (code = codes[nxt])
212
+ pairs << [code, code]
213
+ elsif nxt == "s"
214
+ pairs.concat([[0x20, 0x20], [0x09, 0x09], [0x0A, 0x0A],
215
+ [0x0D, 0x0D], [0x0C, 0x0C]])
216
+ end
217
+ i += 2
218
+ elsif i + 2 < body.length && body[i + 1] == "-"
219
+ pairs << [ch.ord, body[i + 2].ord]
220
+ i += 3
221
+ else
222
+ pairs << [ch.ord, ch.ord]
223
+ i += 1
224
+ end
225
+ end
226
+ lo = 1
227
+ ranges = []
228
+ pairs.sort.each do |plo, phi|
229
+ next if phi < lo
230
+
231
+ ranges << "%x#{format('%02X', lo)}-#{format('%02X', plo - 1)}" if plo > lo
232
+ lo = [lo, phi + 1].max
233
+ end
234
+ ranges << "%x#{format('%02X', lo)}-10FFFF" if lo <= 0x10FFFF
235
+ "(#{ranges.join(' / ')})"
236
+ else
237
+ grouped
238
+ end
239
+ end
240
+ end
241
+
242
+ # PARG string literal for a printable literal; %xNN for the lexer's
243
+ # problem characters (its escaped forms are rejected).
244
+ def literal(pattern)
245
+ pattern.chars.map do |c|
246
+ case c.ord
247
+ when 0x22 then "%x22"
248
+ when 0x5C then "%x5C"
249
+ when 0x2F then "%x2F"
250
+ else
251
+ c.match?(/[[:print:]]/) ? "\"#{c}\"" : "%x#{format('%02X', c.ord)}"
252
+ end
253
+ end.join(" ")
254
+ end
255
+ end
256
+
257
+ # -- rule discovery --
258
+ # A Named whose name is in the inventory defines that rule; wrapper
259
+ # duplicates collapse to the first. parsanol inlines non-capturing
260
+ # rules into callers, so some inventory names have no Named at all —
261
+ # their bodies render inline inside their callers.
262
+ rules = {}
263
+ ATOMS.each_with_index do |a, i|
264
+ next unless (nm = a["Named"]) && RULE_NAMES.include?(nm["name"])
265
+
266
+ rules[nm["name"]] = i unless rules.key?(nm["name"])
267
+ end
268
+ rule_def_ids = rules.values.to_set
269
+ # rules whose body is a direct alternative (after the Named wrapper):
270
+ # references to them need grouping at every use site
271
+ alt_rules = rules.select do |_, id|
272
+ body_id = ATOMS[id]["Named"]["atom"]
273
+ # follow the rule's own .as wrapper chain before the type check
274
+ while ATOMS[body_id].keys.first == "Named"
275
+ body_id = ATOMS[body_id]["Named"]["atom"]
276
+ end
277
+ ATOMS[body_id].keys.first == "Alternative"
278
+ end.keys
279
+
280
+ inlined = RULE_NAMES.uniq - rules.keys
281
+ warn "rules: #{rules.size} defined, #{inlined.size} inlined by parsanol"
282
+ warn "inlined: #{inlined.first(10).inspect}" if inlined.any?
283
+
284
+ # -- emit --
285
+ out = []
286
+ out << "grammar ExpressSyntax version \"0.1.0\" edition 2 {"
287
+ renderer = AtomRenderer.new(ATOMS, REJECTED, rule_def_ids, alt_rules)
288
+ # The DSL's skip(:trivia, whitespace: :space, exempt: ...) — `space`
289
+ # (whitespace | embeddedRemark | tailRemark) is the channel; remarks are
290
+ # consumed by the skip and attached under :space for RemarkScanner.
291
+ # parsanol inlines the space rule (no Named), so the body is emitted
292
+ # from the known DSL definition.
293
+ out << " # whitespace channel rule (the DSL's match[\" \\r\\n\\t\\f\"]; the"
294
+ out << " # skip declaration accepts rule references only)"
295
+ out << " space_ws = (%x20 / %x0D / %x0A / %x09 / %x0C)"
296
+ out << " skip = (space_ws / embeddedRemark / tailRemark) capture: space"
297
+ rules.sort_by { |_, id| id }.each do |name, id|
298
+ next if name == "simpleId" # hand-emitted above with the restored guard
299
+
300
+ body_id = ATOMS[id]["Named"]["atom"]
301
+ decl = ATOMIC_RULES.include?(name) ? "atomic #{name} =" : "#{name} ="
302
+ # (space is inlined by parsanol, so it is not in ATOMIC_RULES here)
303
+ out << " #{decl} #{renderer.render(body_id)}"
304
+ end
305
+
306
+ out << " entry syntax: syntax"
307
+ out << "}"
308
+
309
+ File.write(out_path, out.join("\n") + "\n")
310
+ warn "wrote #{out_path} (#{out.size} lines)"
311
+ unless REJECTED.empty?
312
+ warn "REJECTED (#{REJECTED.values.sum}):"
313
+ REJECTED.sort_by { |_, n| -n }.first(10).each { |k, n| warn " #{n}x #{k}" }
314
+ end
315
+ exit REJECTED.empty? ? 0 : 1
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+ require "expressir"
5
+ require "parsanol"
6
+
7
+ src = File.read(File.expand_path("../spec/syntax/syntax.exp", __dir__))
8
+ begin
9
+ Expressir::Express::Grammar::Parser.cached_parser.parse(src)
10
+ puts "OK"
11
+ rescue Parsanol::ParseFailed => e
12
+ puts "FAIL: #{e.message[0, 200]}"
13
+ # find the deepest cited line/char and show the source there
14
+ if e.message =~ /line (\d+) char (\d+)/
15
+ line = Regexp.last_match(1).to_i
16
+ char = Regexp.last_match(2).to_i
17
+ text = src.lines[line - 1].to_s
18
+ puts "line #{line}: #{text.inspect[0, 140]}"
19
+ puts " #{' ' * [char - 1, 0].max}^"
20
+ end
21
+ end