parol 4.5.0

LL(k) and LALR(1) parser generator for Rust
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
use crate::CommonGeneratorConfig;
use crate::generators::lexer_ir::build_scanner_mode_data;
use crate::generators::{GrammarConfig, NamingHelper};
use anyhow::Result;
use scnr2_generate::character_classes::CharacterClasses;
use scnr2_generate::dfa::Dfa;
use scnr2_generate::nfa::Nfa;
use scnr2_generate::pattern::{Lookahead as ScnrLookahead, Pattern};
use scnr2_generate::scanner_data::TransitionToNumericMode;
use scnr2_generate::scanner_mode::ScannerMode as ScnrScannerMode;

use std::fmt::Write;

/// Generates the lexer part of the parser output file for C#.
pub fn generate_lexer_source<C: CommonGeneratorConfig>(
    grammar_config: &GrammarConfig,
    config: &C,
) -> Result<String> {
    let terminal_names =
        crate::generators::lexer_generator::generate_terminal_names(grammar_config);
    generate_lexer_source_with_terminal_names(grammar_config, config, &terminal_names)
}

pub(crate) fn generate_lexer_source_with_terminal_names<C: CommonGeneratorConfig>(
    grammar_config: &GrammarConfig,
    config: &C,
    terminal_names: &[String],
) -> Result<String> {
    let mut source = String::new();
    let _scanner_type_name = NamingHelper::to_upper_camel_case(config.user_type_name()) + "Scanner";

    writeln!(source, "using System;")?;
    writeln!(source, "using System.Collections.Generic;")?;
    writeln!(source, "using Parol.Runtime.Scanner;")?;
    writeln!(source)?;
    writeln!(source, "namespace {} {{", config.user_type_name())?;

    source.push_str(&generate_scanner_data_with_terminal_names(
        grammar_config,
        config,
        terminal_names,
    )?);

    writeln!(source, "}}")?;

    Ok(source)
}

/// Generates the scanner data class for C#.
pub fn generate_scanner_data<C: CommonGeneratorConfig>(
    grammar_config: &GrammarConfig,
    config: &C,
) -> Result<String> {
    let terminal_names =
        crate::generators::lexer_generator::generate_terminal_names(grammar_config);
    generate_scanner_data_with_terminal_names(grammar_config, config, &terminal_names)
}

pub(crate) fn generate_scanner_data_with_terminal_names<C: CommonGeneratorConfig>(
    grammar_config: &GrammarConfig,
    config: &C,
    terminal_names: &[String],
) -> Result<String> {
    let mode_data = build_scanner_mode_data(grammar_config, terminal_names)
        .map_err(|e| anyhow::anyhow!(e.to_string()))?;
    let mode_indices = mode_data
        .iter()
        .enumerate()
        .map(|(i, mode)| (mode.scanner_name.clone(), i))
        .collect::<std::collections::HashMap<_, _>>();

    let mut scanner_modes = Vec::new();
    for mode in mode_data {
        let sc_name = &mode.scanner_name;

        let mut patterns = Vec::new();
        for (rx, terminal_index, lookahead, _) in mode.terminal_mappings {
            let scnr_lookahead =
                match lookahead {
                    Some((true, pattern)) => ScnrLookahead::positive(pattern)
                        .map_err(|e| anyhow::anyhow!(e.to_string()))?,
                    Some((false, pattern)) => ScnrLookahead::negative(pattern)
                        .map_err(|e| anyhow::anyhow!(e.to_string()))?,
                    None => ScnrLookahead::None,
                };
            patterns.push(
                Pattern::new(rx, (terminal_index as u32).into()).with_lookahead(scnr_lookahead),
            );
        }

        let mut transitions = Vec::new();
        for (terminal_index, state_switch) in mode.transitions {
            let transition = match state_switch {
                crate::parser::parol_grammar::ScannerStateSwitch::SwitchPush(mode_name, _) => {
                    let mode_index = *mode_indices.get(mode_name.as_str()).unwrap();
                    TransitionToNumericMode::PushMode(terminal_index as usize, mode_index)
                }
                crate::parser::parol_grammar::ScannerStateSwitch::SwitchPop(_) => {
                    TransitionToNumericMode::PopMode(terminal_index as usize)
                }
                crate::parser::parol_grammar::ScannerStateSwitch::Switch(mode_name, _) => {
                    let mode_index = *mode_indices.get(mode_name.as_str()).unwrap();
                    TransitionToNumericMode::SetMode(terminal_index as usize, mode_index)
                }
            };
            transitions.push(transition);
        }
        transitions.sort_by_key(|t| t.token_type());

        scanner_modes.push(ScnrScannerMode::new(sc_name, patterns, transitions));
    }

    let skip_tokens_by_mode = scanner_modes
        .iter()
        .map(|mode| {
            grammar_config
                .scanner_configurations
                .iter()
                .find(|sc| sc.scanner_name == mode.name)
                .map(|sc| sc.skip_tokens.clone())
                .unwrap_or_default()
        })
        .collect::<Vec<Vec<parol_runtime::TerminalIndex>>>();

    // Build DFAs and CharacterClasses
    let mut nfas = scanner_modes
        .iter()
        .map(|mode| {
            Nfa::build_from_patterns(&mode.patterns).map_err(|e| anyhow::anyhow!(e.to_string()))
        })
        .collect::<Result<Vec<_>>>()?;

    let mut character_classes = CharacterClasses::new();
    for nfa in &nfas {
        nfa.collect_character_classes(&mut character_classes);
    }
    character_classes.create_disjoint_character_classes();
    for nfa in &mut nfas {
        nfa.convert_to_disjoint_character_classes(&character_classes);
    }

    let dfas = nfas
        .into_iter()
        .map(|nfa| Dfa::try_from(&nfa).map_err(|e| anyhow::anyhow!(e.to_string())))
        .collect::<Result<Vec<_>>>()?;

    // Generate C# source
    let mut source = String::new();
    let scanner_type_name = NamingHelper::to_upper_camel_case(config.user_type_name()) + "Scanner";

    writeln!(source, "    /// <summary>")?;
    writeln!(
        source,
        "    /// Scanner tables and helper functions generated by parol for this grammar."
    )?;
    writeln!(source, "    /// </summary>")?;
    writeln!(
        source,
        "    public static class {}Data {{",
        scanner_type_name
    )?;

    // Terminal Names
    writeln!(source, "        /// <summary>")?;
    writeln!(
        source,
        "        /// Ordered terminal names used by scanner and parser diagnostics."
    )?;
    writeln!(source, "        /// </summary>")?;
    writeln!(
        source,
        "        public static readonly string[] TerminalNames = ["
    )?;
    for name in terminal_names {
        writeln!(source, "            \"{}\",", name)?;
    }
    writeln!(source, "        ];")?;
    writeln!(source)?;

    // Match Function
    generate_match_function(&mut source, &character_classes)?;

    // Scanner Modes
    writeln!(source, "        /// <summary>")?;
    writeln!(
        source,
        "        /// Scanner mode table consumed by the scanner runtime."
    )?;
    writeln!(source, "        /// </summary>")?;
    writeln!(
        source,
        "        public static readonly ScannerMode[] ScannerModes = {{"
    )?;
    for (i, mode) in scanner_modes.iter().enumerate() {
        let dfa = &dfas[i];
        generate_scanner_mode(&mut source, mode, dfa, character_classes.intervals.len())?;
    }
    writeln!(source, "        }};")?;
    writeln!(source)?;

    writeln!(source, "        /// <summary>")?;
    writeln!(
        source,
        "        /// Scanner-mode-specific token type indices that are skipped by the scanner runtime."
    )?;
    writeln!(source, "        /// </summary>")?;
    writeln!(
        source,
        "        public static readonly int[][] SkipTokensByScannerMode = {{"
    )?;
    for skip_tokens in &skip_tokens_by_mode {
        let tokens = skip_tokens
            .iter()
            .map(std::string::ToString::to_string)
            .collect::<Vec<_>>()
            .join(", ");
        writeln!(source, "            [{}],", tokens)?;
    }
    writeln!(source, "        }};")?;

    writeln!(source, "    }}")?;

    Ok(source)
}

fn generate_match_function(
    source: &mut String,
    character_classes: &CharacterClasses,
) -> Result<()> {
    writeln!(source, "        /// <summary>")?;
    writeln!(
        source,
        "        /// Maps an input character to its scanner character class index."
    )?;
    writeln!(source, "        /// </summary>")?;
    writeln!(
        source,
        "        /// <param name=\"c\">Character to classify.</param>"
    )?;
    writeln!(
        source,
        "        /// <returns>The class index, or <c>null</c> if no class matches.</returns>"
    )?;
    writeln!(
        source,
        "        public static int? MatchFunction(char c) {{"
    )?;
    writeln!(
        source,
        "            var intervals = new (char Start, char End, int ClassIdx)[] {{"
    )?;
    for interval in &character_classes.elementary_intervals {
        let start = *interval.start();
        let end = *interval.end();
        let class_idx = character_classes
            .intervals
            .iter()
            .enumerate()
            .find(|(_, group)| group.contains(interval))
            .map(|(idx, _)| idx)
            .unwrap();
        writeln!(
            source,
            "                ('{}', '{}', {}),",
            escape_char(start),
            escape_char(end),
            class_idx
        )?;
    }
    writeln!(source, "            }};")?;
    writeln!(source)?;
    writeln!(
        source,
        "            int low = 0, high = intervals.Length - 1;"
    )?;
    writeln!(source, "            while (low <= high) {{")?;
    writeln!(source, "                int mid = low + (high - low) / 2;")?;
    writeln!(
        source,
        "                if (c >= intervals[mid].Start && c <= intervals[mid].End) return intervals[mid].ClassIdx;"
    )?;
    writeln!(
        source,
        "                if (c < intervals[mid].Start) high = mid - 1;"
    )?;
    writeln!(source, "                else low = mid + 1;")?;
    writeln!(source, "            }}")?;
    writeln!(source, "            return null;")?;
    writeln!(source, "        }}")?;
    Ok(())
}

fn generate_scanner_mode(
    source: &mut String,
    mode: &ScnrScannerMode,
    dfa: &Dfa,
    num_classes: usize,
) -> Result<()> {
    writeln!(source, "            new(")?;
    writeln!(source, "                \"{}\",", mode.name)?;

    // Transitions
    writeln!(source, "                [")?;
    for t in &mode.transitions {
        match t {
            TransitionToNumericMode::SetMode(token_type, target) => {
                writeln!(
                    source,
                    "                    new(TransitionType.SetMode, {}, {}),",
                    token_type, target
                )?;
            }
            TransitionToNumericMode::PushMode(token_type, target) => {
                writeln!(
                    source,
                    "                    new(TransitionType.PushMode, {}, {}),",
                    token_type, target
                )?;
            }
            TransitionToNumericMode::PopMode(token_type) => {
                writeln!(
                    source,
                    "                    new(TransitionType.PopMode, {}),",
                    token_type
                )?;
            }
        }
    }
    writeln!(source, "                ],")?;

    // DFA
    generate_dfa(source, dfa, num_classes)?;

    writeln!(source, "            ),")?;
    Ok(())
}

fn generate_dfa(source: &mut String, dfa: &Dfa, num_classes: usize) -> Result<()> {
    writeln!(source, "                new Dfa([")?;
    for state in &dfa.states {
        writeln!(source, "                    new DfaState(")?;

        // Transitions
        write!(source, "                        [ ")?;
        let mut transition_opts = vec![None; num_classes];
        for t in &state.transitions {
            transition_opts[t.elementary_interval_index.as_usize()] = Some(t.target.as_usize());
        }
        for (i, opt) in transition_opts.iter().enumerate() {
            if let Some(target) = opt {
                write!(source, "new({})", target)?;
            } else {
                write!(source, "null")?;
            }
            if i < num_classes - 1 {
                write!(source, ", ")?;
            }
        }
        writeln!(source, " ],")?;

        // Accept Data
        writeln!(source, "                        [")?;
        for ad in &state.accept_data {
            write!(
                source,
                "                            new({}, {}, ",
                ad.terminal_type.as_usize(),
                ad.priority
            )?;
            generate_lookahead(source, &ad.lookahead, num_classes)?;
            writeln!(source, "),")?;
        }
        writeln!(source, "                        ]")?;

        writeln!(source, "                    ),")?;
    }
    writeln!(source, "                ])")?;
    Ok(())
}

fn generate_lookahead(
    source: &mut String,
    lookahead: &scnr2_generate::pattern::Lookahead,
    num_classes: usize,
) -> Result<()> {
    match lookahead {
        scnr2_generate::pattern::Lookahead::None => write!(source, "new Lookahead.None()")?,
        scnr2_generate::pattern::Lookahead::Positive(
            scnr2_generate::pattern::AutomatonType::Dfa(d),
        ) => {
            write!(source, "new Lookahead.Positive(")?;
            generate_dfa(source, d, num_classes)?;
            write!(source, ")")?;
        }
        scnr2_generate::pattern::Lookahead::Negative(
            scnr2_generate::pattern::AutomatonType::Dfa(d),
        ) => {
            write!(source, "new Lookahead.Negative(")?;
            generate_dfa(source, d, num_classes)?;
            write!(source, ")")?;
        }
        _ => panic!("Unexpected lookahead type"),
    }
    Ok(())
}

fn escape_char(c: char) -> String {
    match c {
        '\'' => "\\'".to_string(),
        '\\' => "\\\\".to_string(),
        '\n' => "\\n".to_string(),
        '\r' => "\\r".to_string(),
        '\t' => "\\t".to_string(),
        '\0' => "\\0".to_string(),
        _ if c.is_ascii_graphic() || c == ' ' => c.to_string(),
        _ => {
            let u = c as u32;
            if u <= 0xFFFF {
                format!("\\u{:04x}", u)
            } else {
                // C# char is UTF-16, so we need surrogate pairs for characters above U+FFFF
                // However, our MatchFunction takes 'char' which is System.Char (UTF-16).
                // If we want to support full Unicode, we should use 'int' (32-bit).
                // For now, we'll just use the \U notation if it were a string, but for char it's not possible.
                // Wait, C# char can't hold values > 0xFFFF.
                format!("\\u{:04x}", u & 0xFFFF) // This is incorrect for full Unicode but safe for syntax
            }
        }
    }
}