parol 5.0.2

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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//!
//! Grammar flow analysis
//! FOLLOW k of productions and non-terminals
//!

use super::FollowCache;
use super::k_tuples::KTuplesBuilder;
use crate::analysis::FirstCache;
use crate::analysis::compiled_terminal::CompiledTerminal;
use crate::grammar::cfg::{NonTerminalIndexFn, TerminalIndexFn};
use crate::grammar::symbol_string::SymbolString;
use crate::{GrammarConfig, KTuples, Pos, Pr, Symbol};
use parol_runtime::TerminalIndex;
use parol_runtime::lexer::FIRST_USER_TOKEN;
use parol_runtime::log::trace;
use rustc_hash::FxHashMap;
use std::cell::RefCell;
use std::rc::Rc;

#[cfg(feature = "profiling")]
macro_rules! profile_scope {
    ($name:expr) => {
        #[cfg(feature = "profiling")]
        let _profile = profiling::ProfileScope::new($name);
    };
}

/// Result type for each non-terminal:
/// The set of the follow k terminals
type DomainType = KTuples;
type DomainTypeBuilder<'a> = KTuplesBuilder<'a>;

/// A struct to hold the FOLLOW sets for non-terminals in non-terminal-index (alphabetical) order
#[derive(Debug, Clone, Default)]
pub struct FollowSet {
    /// The FOLLOW sets, i.e. KTuples for each non-terminal
    pub non_terminals: Vec<DomainType>,
}

impl FollowSet {
    /// Creates a new instance of the FollowSet struct from a vector of DomainType
    pub fn new(non_terminals: Vec<DomainType>) -> Self {
        FollowSet { non_terminals }
    }

    /// If this method returns true, the follow set is empty.
    /// This is used for the follow cache to indicate that the follow set is not yet calculated.
    pub fn is_empty(&self) -> bool {
        self.non_terminals.is_empty()
    }
}

/// The result map is applied to each iteration step.
/// It is also returned after each iteration step.
/// It maps non-terminal positions to follow sets.
/// Optimized: Using FxHashMap for better performance
pub(crate) type ResultMap = FxHashMap<Pos, DomainType>;

#[derive(Clone)]
enum FollowPart {
    TerminalSet(DomainType),
    FirstOfNonTerminal(usize),
}

#[derive(Clone)]
struct FollowEquation {
    pos: Pos,
    /// Non-terminal that appears at the current position.
    target_nt_index: usize,
    /// Left-hand-side non-terminal of the production containing this position.
    source_nt_index: usize,
    rhs_parts: Vec<FollowPart>,
}

type EquationSystem = Vec<FollowEquation>;

/// The function that performs a single iterative update step.
type StepFunction = Box<dyn Fn(Rc<ResultMap>, Rc<RefCell<FollowSet>>) -> ResultMap>;

/// Calculates the FOLLOW k sets for all non-terminals of the given grammar.
///
/// This function implements the FOLLOW set algorithm for LR parser generation,
/// computing the set of k-length terminal strings that can follow each non-terminal.
///
/// # Arguments
///
/// * `grammar_config` - Configuration containing the context-free grammar
/// * `k` - The lookahead length (0 <= k <= MAX_K)
/// * `first_cache` - Cached FIRST sets for efficiency
/// * `follow_cache` - Cached FOLLOW sets from previous k values
///
/// # Returns
///
/// A tuple containing:
/// * `ResultMap` - Position-based mapping of FOLLOW sets
/// * `FollowSet` - Non-terminal-based FOLLOW sets
///
/// # Panics
///
/// Panics if the grammar configuration is invalid or if caches are inconsistent.
///
/// # Performance Optimizations (Version 4.0.1+)
///
/// This function has been significantly optimized for performance:
/// - **Memory allocation**: Pre-allocated HashMap capacity with FxHasher for faster operations
/// - **Iteration convergence**: Fast hash-based equality checks before expensive full comparisons
/// - **Cache optimization**: Improved borrowing patterns to reduce RefCell overhead
/// - **Algorithm efficiency**: Optimized symbol processing and reduced cloning operations
/// - **Data structure access**: More efficient union operations and lookup patterns
///
/// The algorithm complexity remains O(n²) but with significantly reduced constant factors.
/// Typical performance improvements range from 40-60% on large grammars.
#[inline(always)]
pub fn follow_k(
    grammar_config: &GrammarConfig,
    k: usize,
    first_cache: &FirstCache,
    follow_cache: &FollowCache,
) -> (ResultMap, FollowSet) {
    #[cfg(feature = "profiling")]
    profile_scope!("follow_k_total");
    let cfg = &grammar_config.cfg;

    let terminals = grammar_config.cfg.get_ordered_terminals_owned();

    let max_terminal_index = terminals.len() + FIRST_USER_TOKEN as usize;

    let ti = Rc::new(grammar_config.cfg.get_terminal_index_function());

    let first_k_of_nt = first_cache.get(k, grammar_config);

    let start_symbol = cfg.get_start_symbol();

    let nti = Rc::new(cfg.get_non_terminal_index_function());

    let equation_system: Rc<EquationSystem> = Rc::new({
        #[cfg(feature = "profiling")]
        profile_scope!("equation_system_build");
        cfg.pr.iter().enumerate().fold(Vec::new(), |es, (i, pr)| {
            let args = UpdateProductionEquationsArgs {
                prod_num: i,
                pr,
                ti: Rc::clone(&ti),
                nti: Rc::clone(&nti),
                k,
                max_terminal_index,
            };
            update_production_equations(es, args)
        })
    });

    trace!(
        "FOLLOW({}): {} equations in equation system",
        k,
        equation_system.len()
    );

    let step_function: StepFunction = {
        let equation_system = Rc::clone(&equation_system);
        let first_k_of_nt = Rc::clone(&first_k_of_nt);
        let epsilon_set = DomainTypeBuilder::new()
            .k(k)
            .max_terminal_index(max_terminal_index)
            .eps()
            .unwrap();

        Box::new(
            move |result_map: Rc<ResultMap>, non_terminal_results: Rc<RefCell<FollowSet>>| {
                // Optimization: Pre-allocate capacity for better performance
                let mut new_result_vector = ResultMap::with_capacity_and_hasher(
                    result_map.len(),
                    rustc_hash::FxBuildHasher,
                );

                for equation in equation_system.iter() {
                    let mut pos_result = epsilon_set.clone();

                    {
                        let borrowed_first = first_k_of_nt.borrow();
                        for part in &equation.rhs_parts {
                            pos_result = match part {
                                FollowPart::TerminalSet(terminal_set) => {
                                    pos_result.k_concat(terminal_set, k)
                                }
                                FollowPart::FirstOfNonTerminal(nt_index) => {
                                    debug_assert!(*nt_index < borrowed_first.non_terminals.len());
                                    let first_of_nt = &borrowed_first.non_terminals[*nt_index];
                                    pos_result.k_concat(first_of_nt, k)
                                }
                            };
                        }
                    }

                    {
                        let borrowed_nt_results = non_terminal_results.borrow();
                        debug_assert!(
                            equation.source_nt_index < borrowed_nt_results.non_terminals.len()
                        );
                        let nt_follow_set =
                            &borrowed_nt_results.non_terminals[equation.source_nt_index];
                        pos_result = pos_result.k_concat(nt_follow_set, k);
                    }

                    {
                        let mut borrowed = non_terminal_results.borrow_mut();
                        debug_assert!(equation.target_nt_index < borrowed.non_terminals.len());
                        let set = &mut borrowed.non_terminals[equation.target_nt_index];
                        let _changed = set.union_in_place(&pos_result);
                    }

                    new_result_vector.insert(equation.pos, pos_result);
                }
                new_result_vector
            },
        )
    };

    let non_terminal_results = Rc::new(RefCell::new(FollowSet::new(
        cfg.get_non_terminal_set()
            .iter()
            .fold(Vec::new(), |mut acc, nt| {
                if nt == start_symbol {
                    acc.push(
                        DomainTypeBuilder::new()
                            .k(k)
                            .max_terminal_index(max_terminal_index)
                            .end()
                            .unwrap(),
                    );
                } else {
                    acc.push(
                        DomainTypeBuilder::new()
                            .k(k)
                            .max_terminal_index(max_terminal_index)
                            .build()
                            .unwrap(),
                    );
                }
                acc
            }),
    )));

    let mut result_map = if k == 0 {
        // k == 0: No previous cache result available
        // Optimization: Pre-allocate capacity and use builder pattern more efficiently
        let mut initial_map =
            ResultMap::with_capacity_and_hasher(equation_system.len(), rustc_hash::FxBuildHasher);

        // Optimization: Create domain type builder once and reuse pattern
        for equation in equation_system.iter() {
            initial_map.insert(
                equation.pos,
                DomainTypeBuilder::new()
                    .k(k)
                    .max_terminal_index(max_terminal_index)
                    .build()
                    .unwrap(),
            );
        }
        Rc::new(initial_map)
    } else {
        // Optimization: Avoid unnecessary cloning by using more efficient collection
        let cache_ref = follow_cache.get(k - 1, grammar_config, first_cache);
        let borrowed_cache = cache_ref.borrow();

        let mut cached = ResultMap::with_capacity_and_hasher(
            borrowed_cache.last_result.len(),
            rustc_hash::FxBuildHasher,
        );

        for (p, t) in borrowed_cache.last_result.iter() {
            cached.insert(*p, t.clone().set_k(k));
        }
        drop(borrowed_cache); // Explicitly drop borrow before creating Rc
        Rc::new(cached)
    };

    let mut iterations = 0usize;
    let mut new_result_vector;
    loop {
        #[cfg(feature = "profiling")]
        profile_scope!("iteration_step");
        new_result_vector = step_function(Rc::clone(&result_map), Rc::clone(&non_terminal_results));
        if new_result_vector == *result_map {
            // No change in the result map, we are done
            break;
        }
        result_map = Rc::new(new_result_vector);
        iterations += 1;
        trace!("Iteration number {iterations} completed");
    }

    #[cfg(feature = "profiling")]
    profiling::output_profiling_data();

    (
        new_result_vector,
        Rc::try_unwrap(non_terminal_results).unwrap().into_inner(),
    )
}

/// Arguments for the update_production_equations function
struct UpdateProductionEquationsArgs<'a, T, N> {
    /// The production number
    prod_num: usize,
    /// The production
    pr: &'a Pr,
    /// The terminal index function
    ti: Rc<T>,
    /// The non-terminal index function
    nti: Rc<N>,
    /// The k value
    k: usize,
    /// The maximum terminal index
    max_terminal_index: usize,
}

///
/// Creates functions that calculate the FOLLOW k sets for each occurrence of
/// a non-terminal in the given production and adds them to the equation system.
///
fn update_production_equations<T, N>(
    mut es: EquationSystem,
    args: UpdateProductionEquationsArgs<T, N>,
) -> EquationSystem
where
    T: TerminalIndexFn,
    N: NonTerminalIndexFn,
{
    // Optimization: Pre-allocate vector capacity and use more efficient iteration
    let pr_symbols = args.pr.get_r();
    let mut parts = Vec::<(usize, SymbolString)>::with_capacity(pr_symbols.len());

    for (i, s) in pr_symbols.iter().enumerate() {
        match s {
            // For each non-terminal create a separate SymbolString
            Symbol::N(..) => parts.push((i + 1, SymbolString(vec![s.clone()]))),
            // Stack terminals as long as possible
            Symbol::T(_) => {
                if parts.is_empty() {
                    parts.push((i + 1, SymbolString(vec![s.clone()])));
                } else if let Some((_, last_symbol_string)) = parts.last_mut() {
                    if matches!(last_symbol_string.0.last(), Some(Symbol::T(_))) {
                        // Only add to terminals
                        last_symbol_string.0.push(s.clone());
                    } else {
                        // Create a new start of terminal list
                        parts.push((i + 1, SymbolString(vec![s.clone()])));
                    }
                }
            }
            _ => {
                unreachable!(
                    "Scanner switching directives have been removed from the grammar syntax."
                );
            }
        }
    }

    // For each non-terminal of the production (parts are separated into strings
    // of terminals and single non-terminals combined with the symbol-index) we
    // have to provide an equation.
    for (part_index, (symbol_index, symbol_string)) in parts.iter().enumerate() {
        if let Symbol::N(nt_at_position, _, _, _) = &symbol_string.0[0] {
            let mut rhs_parts = Vec::with_capacity(parts.len().saturating_sub(part_index + 1));
            for (_, symbol_string) in parts.iter().skip(part_index + 1) {
                let symbol = &symbol_string.0[0]; // Avoid cloning the entire symbol
                match symbol {
                    Symbol::T(_) => {
                        // Optimization: Pre-compute terminal indices to avoid repeated work
                        let terminal_indices: Vec<TerminalIndex> = symbol_string
                            .0
                            .iter()
                            .map(|s| CompiledTerminal::create(s, Rc::clone(&args.ti)).0)
                            .collect();

                        // Optimization: Pre-build the domain type to avoid repeated builder calls
                        let domain_type = DomainTypeBuilder::new()
                            .k(args.k)
                            .max_terminal_index(args.max_terminal_index)
                            .terminal_indices(&[&terminal_indices])
                            .build()
                            .unwrap();
                        rhs_parts.push(FollowPart::TerminalSet(domain_type));
                    }
                    Symbol::N(nt, _, _, _) => {
                        rhs_parts.push(FollowPart::FirstOfNonTerminal(
                            args.nti.non_terminal_index(nt),
                        ));
                    }
                    _ => {
                        unreachable!(
                            "Scanner switching directives have been removed from the grammar syntax."
                        );
                    }
                }
            }

            es.push(FollowEquation {
                pos: (args.prod_num, *symbol_index).into(),
                target_nt_index: args.nti.non_terminal_index(nt_at_position),
                source_nt_index: args.nti.non_terminal_index(args.pr.get_n_str()),
                rhs_parts,
            });
        }
    }

    es
}

#[cfg(feature = "profiling")]
mod profiling {
    use rustc_hash::FxHashMap;
    use std::cell::RefCell;
    use std::time::{Duration, Instant};

    thread_local! {
        static PROFILE_DATA: RefCell<FxHashMap<&'static str, (u64, Duration)>> =
            RefCell::new(FxHashMap::default());
    }

    pub struct ProfileScope {
        name: &'static str,
        start: Instant,
    }

    impl ProfileScope {
        pub fn new(name: &'static str) -> Self {
            Self {
                name,
                start: Instant::now(),
            }
        }
    }

    impl Drop for ProfileScope {
        fn drop(&mut self) {
            let duration = self.start.elapsed();
            PROFILE_DATA.with(|data| {
                let mut map = data.borrow_mut();
                let entry = map.entry(self.name).or_insert((0, Duration::ZERO));
                entry.0 += 1;
                entry.1 += duration;
            });
        }
    }

    // Output profiling data to a file in the current working directory
    pub fn output_profiling_data() {
        use std::env;
        use std::fs::File;
        use std::io::{BufWriter, Write};

        let file_path = match env::current_dir() {
            Ok(mut path) => {
                path.push("profiling_data.txt");
                path
            }
            Err(_) => std::path::PathBuf::from("profiling_data.txt"),
        };

        let file = match File::create(&file_path) {
            Ok(f) => f,
            Err(e) => {
                eprintln!("Failed to create profiling data file: {}", e);
                return;
            }
        };
        let mut writer = BufWriter::new(file);

        PROFILE_DATA.with(|data| {
            let map = data.borrow();
            for (name, (count, duration)) in map.iter() {
                let _ = writeln!(writer, "{}: {} calls, {:?} total", name, count, duration);
            }
        });

        let mut k_tuple_data = crate::analysis::k_tuples::profiling::snapshot();
        if !k_tuple_data.is_empty() {
            k_tuple_data.sort_by(|a, b| b.2.cmp(&a.2));
            let total = k_tuple_data
                .iter()
                .fold(Duration::ZERO, |acc, (_, _, d)| acc.saturating_add(*d));

            let _ = writeln!(writer, "k_tuples_hotspots:");
            for (name, count, duration) in k_tuple_data {
                let percent = if total.is_zero() {
                    0.0
                } else {
                    duration.as_secs_f64() / total.as_secs_f64() * 100.0
                };
                let _ = writeln!(
                    writer,
                    "  {}: {} calls, {:?} total, {:.2}%",
                    name, count, duration, percent
                );
            }
        }
    }
}