antlr-rust-codegen 0.32.0

ANTLR v4 grammar compiler and Rust source generator
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
pub(crate) fn render_generated_rule_method(
    out: &mut String,
    rule: &GeneratedParserRule,
    step_render_context: GeneratedStepRenderContext<'_>,
) {
    let index = rule.rule_index;
    if rule.left_recursive {
        writeln!(
            out,
            "\n    #[allow(dead_code)]\n    fn parse_generated_rule_{index}(&mut self, allow_fallback: bool) -> Result<antlr4_runtime::ParseTree, GeneratedRuleError> {{"
        )
        .expect("writing to a string cannot fail");
        writeln!(
            out,
            "        self.parse_generated_rule_{index}_precedence(0, allow_fallback)"
        )
        .expect("writing to a string cannot fail");
        writeln!(out, "    }}").expect("writing to a string cannot fail");
        writeln!(
            out,
            "\n    #[allow(dead_code)]\n    fn parse_generated_rule_{index}_precedence(&mut self, __precedence: i32, allow_fallback: bool) -> Result<antlr4_runtime::ParseTree, GeneratedRuleError> {{"
        )
        .expect("writing to a string cannot fail");
    } else {
        writeln!(
            out,
            "\n    #[allow(dead_code)]\n    fn parse_generated_rule_{index}(&mut self, __precedence: i32, allow_fallback: bool) -> Result<antlr4_runtime::ParseTree, GeneratedRuleError> {{"
        )
        .expect("writing to a string cannot fail");
        writeln!(out, "        let _ = __precedence;").expect("writing to a string cannot fail");
    }
    render_generated_rule_lifecycle(out, rule, step_render_context);
    writeln!(out, "    }}").expect("writing to a string cannot fail");
}

fn render_generated_rule_lifecycle(
    out: &mut String,
    rule: &GeneratedParserRule,
    step_render_context: GeneratedStepRenderContext<'_>,
) {
    let index = rule.rule_index;
    let entry_state = rule.entry_state;
    writeln!(
        out,
        "        antlr4_runtime::__antlr4_rust_generated_rule! {{"
    )
    .expect("writing to a string cannot fail");
    if rule.left_recursive {
        writeln!(
            out,
            "            recursive self, {entry_state}isize, {index}, __precedence, allow_fallback, atn(), GeneratedRuleError::Fatal;"
        )
        .expect("writing to a string cannot fail");
    } else {
        writeln!(
            out,
            "            ordinary self, {entry_state}isize, {index}, allow_fallback, atn(), GeneratedRuleError::Fatal;"
        )
        .expect("writing to a string cannot fail");
    }
    if step_render_context
        .adaptive_atn_preferred_rule_slots
        .iter()
        .any(Option::is_some)
    {
        writeln!(
            out,
            "            retry [self.adaptive_atn_retry_slot.is_some() => GeneratedRuleError::AdaptiveRetry];"
        )
        .expect("writing to a string cannot fail");
    } else {
        writeln!(out, "            retry [none];").expect("writing to a string cannot fail");
    }
    writeln!(
        out,
        "            bind (__ctx, __rule_start, __consumed_eof, __sync_error);"
    )
    .expect("writing to a string cannot fail");
    let mut setup = String::new();
    render_portable_local_declarations(&mut setup, index, step_render_context, 4);
    render_embedded_attrs_local(&mut setup, index, step_render_context, 4);
    render_embedded_init_entry(&mut setup, index, step_render_context, 4);
    render_generated_rule_section(out, "setup", &setup);
    writeln!(out, "            body {{").expect("writing to a string cannot fail");
    render_generated_steps(out, &rule.steps, 4, step_render_context);
    writeln!(out, "            }}").expect("writing to a string cannot fail");
    let mut success = String::new();
    render_embedded_after_and_seal(&mut success, index, step_render_context, true, 4);
    render_generated_rule_section(out, "success", &success);
    let mut recovery = String::new();
    render_embedded_after_and_seal(&mut recovery, index, step_render_context, false, 4);
    render_generated_rule_section(out, "recovery", &recovery);
    writeln!(out, "        }}").expect("writing to a string cannot fail");
}

fn render_generated_rule_section(out: &mut String, name: &str, body: &str) {
    if body.is_empty() {
        writeln!(out, "            {name} {{}}").expect("writing to a string cannot fail");
        return;
    }
    writeln!(out, "            {name} {{").expect("writing to a string cannot fail");
    out.push_str(body);
    writeln!(out, "            }}").expect("writing to a string cannot fail");
}

fn render_generated_steps(
    out: &mut String,
    steps: &[GeneratedParserStep],
    indent: usize,
    render_context: GeneratedStepRenderContext<'_>,
) {
    for step in steps {
        render_generated_step(out, step, indent, render_context);
    }
}

pub(crate) fn render_generated_step(
    out: &mut String,
    step: &GeneratedParserStep,
    indent: usize,
    render_context: GeneratedStepRenderContext<'_>,
) {
    let pad = "    ".repeat(indent);
    match step {
        GeneratedParserStep::MatchToken {
            token_type,
            follow_state,
        } => {
            writeln!(
                out,
                "{pad}self.base.match_token_into({token_type}, {follow_state}, atn(), &mut __ctx, &mut __consumed_eof)?;"
            )
            .expect("writing to a string cannot fail");
        }
        GeneratedParserStep::MatchSet {
            token_set,
            intervals,
            follow_state,
        } => {
            if let Some(token_set) = token_set {
                writeln!(
                    out,
                    "{pad}self.base.match_token_set_into(atn().token_set({token_set}).expect(\"generated parser token-set index\"), {follow_state}, atn(), &mut __ctx, &mut __consumed_eof)?;"
                )
                .expect("writing to a string cannot fail");
            } else {
                let intervals = render_i32_ranges(intervals);
                writeln!(
                    out,
                    "{pad}self.base.match_set_into(&{intervals}, {follow_state}, atn(), &mut __ctx, &mut __consumed_eof)?;"
                )
                .expect("writing to a string cannot fail");
            }
        }
        GeneratedParserStep::MatchNotSet {
            token_set,
            intervals,
            follow_state,
        } => {
            if let Some(token_set) = token_set {
                writeln!(
                    out,
                    "{pad}self.base.match_not_token_set_into(atn().token_set({token_set}).expect(\"generated parser token-set index\"), 1, atn().max_token_type(), {follow_state}, atn(), &mut __ctx, &mut __consumed_eof)?;"
                )
                .expect("writing to a string cannot fail");
            } else {
                let intervals = render_i32_ranges(intervals);
                writeln!(
                    out,
                    "{pad}self.base.match_not_set_into(&{intervals}, 1, atn().max_token_type(), {follow_state}, atn(), &mut __ctx, &mut __consumed_eof)?;"
                )
                .expect("writing to a string cannot fail");
            }
        }
        GeneratedParserStep::MatchWildcard { follow_state } => {
            // A wildcard matches any single token. Model it as a not-set with an
            // empty exclusion set (every token in 1..=max), reusing the recovering
            // match so a wildcard at EOF performs ANTLR's single-token insertion
            // (`<missing ...>` error node) and lets the rule continue, instead of
            // aborting the remaining steps.
            writeln!(
                out,
                "{pad}self.base.match_not_set_into(&[], 1, atn().max_token_type(), {follow_state}, atn(), &mut __ctx, &mut __consumed_eof)?;"
            )
            .expect("writing to a string cannot fail");
        }
        GeneratedParserStep::Precedence(precedence) => {
            writeln!(out, "{pad}if !self.base.precpred({precedence}) {{")
                .expect("writing to a string cannot fail");
            writeln!(
                out,
                "{pad}    return Err(self.base.failed_predicate_error(\"precpred(_ctx, {precedence})\"));"
            )
            .expect("writing to a string cannot fail");
            writeln!(out, "{pad}}}").expect("writing to a string cannot fail");
        }
        GeneratedParserStep::Predicate {
            rule_index,
            pred_index,
        } => {
            if let Some((condition, message)) = render_context
                .portable_locals
                .and_then(|portable| portable.predicates.get(&(*rule_index, *pred_index)))
            {
                writeln!(out, "{pad}if !({condition}) {{")
                    .expect("writing to a string cannot fail");
                match message {
                    Some(message) => writeln!(
                        out,
                        "{pad}    return Err(self.base.failed_predicate_option_error({rule_index}, \"{}\".to_owned()));",
                        rust_string(message)
                    )
                    .expect("writing to a string cannot fail"),
                    None => writeln!(
                        out,
                        "{pad}    return Err(self.base.failed_predicate_error(\"semantic predicate\"));"
                    )
                    .expect("writing to a string cannot fail"),
                }
                writeln!(out, "{pad}}}").expect("writing to a string cannot fail");
                return;
            }
            if let Some(embedded) = render_context.embedded {
                let (condition, message) =
                    embedded_predicate_condition_and_message(&embedded, *rule_index, *pred_index);
                writeln!(out, "{pad}if !({condition}) {{")
                    .expect("writing to a string cannot fail");
                match message {
                    Some(message) => writeln!(
                        out,
                        "{pad}    return Err(self.base.failed_predicate_option_error({rule_index}, \"{}\".to_owned()));",
                        rust_string(&message)
                    )
                    .expect("writing to a string cannot fail"),
                    None => writeln!(
                        out,
                        "{pad}    return Err(self.base.failed_predicate_error(\"semantic predicate\"));"
                    )
                    .expect("writing to a string cannot fail"),
                }
                writeln!(out, "{pad}}}").expect("writing to a string cannot fail");
                return;
            }
            writeln!(
                out,
                "{pad}if !self.base.parser_semantic_ir_predicate_matches_with_context_and_local(parser_semantics(), {rule_index}, {pred_index}, &__ctx, __precedence) {{"
            )
            .expect("writing to a string cannot fail");
            writeln!(
                out,
                "{pad}    if let Some(__message) = self.base.parser_semantic_ir_predicate_failure_message({rule_index}, {pred_index}, parser_semantics()) {{"
            )
            .expect("writing to a string cannot fail");
            writeln!(
                out,
                "{pad}        return Err(self.base.failed_predicate_option_error({rule_index}, __message));"
            )
            .expect("writing to a string cannot fail");
            writeln!(out, "{pad}    }}").expect("writing to a string cannot fail");
            writeln!(
                out,
                "{pad}    return Err(self.base.failed_predicate_error(\"semantic predicate\"));"
            )
            .expect("writing to a string cannot fail");
            writeln!(out, "{pad}}}").expect("writing to a string cannot fail");
        }
        GeneratedParserStep::CallRule {
            source_state,
            rule_index,
            precedence,
        } => {
            let has_embedded_call_arg = render_context
                .embedded
                .and_then(|embedded| embedded.call_args.get(source_state))
                .is_some();
            if has_embedded_call_arg {
                writeln!(
                    out,
                    "{pad}let __invoking_marker = self.base.push_invoking_state({source_state}isize);"
                )
                .expect("writing to a string cannot fail");
                let expression = render_context
                    .embedded
                    .expect("has_embedded_call_arg guard ensures embedded is Some")
                    .call_args
                    .get(source_state)
                    .expect("has_embedded_call_arg guard ensures call_arg exists");
                writeln!(
                    out,
                    "{pad}self.__embedded_pending_arg = Some(i64::from({expression}));"
                )
                .expect("writing to a string cannot fail");
            }
            let precedence = match precedence {
                GeneratedRuleCallPrecedence::Literal(value) => value.to_string(),
                GeneratedRuleCallPrecedence::InheritLocal => "__precedence".to_owned(),
            };
            let from_generated_call =
                format!("self.parse_rule_precedence_from_generated({rule_index}, {precedence})");
            let mut probes_enclosing_candidate = false;
            let generated_child_call = if render_context
                .direct_generated_rule_calls
                .get(*rule_index)
                .copied()
                .unwrap_or_default()
            {
                let probe_slots = render_context
                    .adaptive_atn_probe_rule_slots
                    .get(*rule_index)
                    .map(Vec::as_slice)
                    .unwrap_or_default();
                let caller_candidate_slot = render_context
                    .adaptive_atn_preferred_rule_slots
                    .get(render_context.current_rule_index)
                    .copied()
                    .flatten();
                probes_enclosing_candidate =
                    caller_candidate_slot.is_some_and(|slot| probe_slots.contains(&slot));
                let dispatch = if probes_enclosing_candidate {
                    "adaptive_probe_dispatch"
                } else {
                    "dispatch"
                };
                format!(
                    "self.parse_generated_rule_{rule_index}_{dispatch}({precedence}, false).map_err(GeneratedRuleError::into_error)"
                )
            } else {
                from_generated_call.clone()
            };
            let child_call = if render_context
                .atn_preferred_rule_calls
                .get(*rule_index)
                .copied()
                .unwrap_or_default()
            {
                from_generated_call
            } else if probes_enclosing_candidate {
                generated_child_call
            } else if let Some(slot) = render_context
                .adaptive_atn_preferred_rule_slots
                .get(*rule_index)
                .copied()
                .flatten()
            {
                format!(
                    "if self.adaptive_atn_preferred_rules[{slot}] {{ {from_generated_call} }} else {{ self.parse_generated_rule_{rule_index}_adaptive_dispatch({precedence}, false, Some({source_state}isize)).map_err(GeneratedRuleError::into_error) }}"
                )
            } else {
                generated_child_call
            };
            if has_embedded_call_arg {
                writeln!(out, "{pad}let __child = {child_call};")
                    .expect("writing to a string cannot fail");
                writeln!(
                    out,
                    "{pad}self.base.discard_invoking_state(__invoking_marker);"
                )
                .expect("writing to a string cannot fail");
                writeln!(out, "{pad}let __child = __child?;")
                    .expect("writing to a string cannot fail");
                writeln!(out, "{pad}self.base.add_parse_child(&mut __ctx, __child);")
                    .expect("writing to a string cannot fail");
            } else {
                writeln!(
                    out,
                    "{pad}antlr4_runtime::__antlr4_rust_invoke_subrule!(self, {source_state}isize, {child_call}, __ctx);"
                )
                .expect("writing to a string cannot fail");
            }
        }
        GeneratedParserStep::Action {
            source_state,
            rule_index,
            action_index,
        } => {
            if let Some(action_index) = action_index {
                writeln!(
                    out,
                    "{pad}let action = self.base.parser_action_at_current_indexed({source_state}, {rule_index}, {action_index}, __rule_start, __consumed_eof);"
                )
                .expect("writing to a string cannot fail");
            } else {
                writeln!(
                    out,
                    "{pad}let action = self.base.parser_action_at_current({source_state}, {rule_index}, __rule_start, __consumed_eof);"
                )
                .expect("writing to a string cannot fail");
            }
            if let Some(statement) = render_context.inline_action_statements.get(source_state) {
                if !statement.is_empty() {
                    writeln!(out, "{pad}{statement}").expect("writing to a string cannot fail");
                }
            }
            if render_context.embedded.is_some() {
                // Embedded actions executed inline just above, at their
                // ANTLR-correct point in the rule body.
                writeln!(out, "{pad}let _ = &action;").expect("writing to a string cannot fail");
                return;
            }
            writeln!(out, "{pad}let _ = action;").expect("writing to a string cannot fail");
        }
        GeneratedParserStep::Decision {
            state,
            decision,
            track_alt_number,
            allow_semantic_context,
            force_context,
            fast_path,
            alts,
        } => {
            render_generated_decision(
                out,
                DecisionRender {
                    state: *state,
                    decision: *decision,
                    track_alt_number: *track_alt_number,
                    allow_semantic_context: *allow_semantic_context,
                    force_context: *force_context,
                    fast_path: fast_path.as_ref(),
                    alts,
                },
                indent,
                render_context,
            );
        }
        GeneratedParserStep::StarLoop {
            state,
            decision,
            enter_alt,
            exit_alt,
            track_alt_number,
            allow_semantic_context,
            force_context,
            plus_loop,
            fast_path,
            body,
        } => {
            render_generated_star_loop(
                out,
                StarLoopRender {
                    state: *state,
                    decision: *decision,
                    alts: (*enter_alt, *exit_alt),
                    track_alt_number: *track_alt_number,
                    allow_semantic_context: *allow_semantic_context,
                    force_context: *force_context,
                    plus_loop: *plus_loop,
                    fast_path: fast_path.as_ref(),
                    body,
                },
                indent,
                render_context,
            );
        }
        GeneratedParserStep::LeftRecursiveLoop {
            state,
            decision,
            enter_alt,
            exit_alt,
            rule_index,
            entry_state,
            body,
            ..
        } => {
            render_generated_left_recursive_loop(
                out,
                LeftRecursiveLoopRender {
                    state: *state,
                    decision: *decision,
                    alts: (*enter_alt, *exit_alt),
                    rule: (*rule_index, *entry_state),
                    body,
                },
                indent,
                render_context,
            );
        }
    }
}