Skip to main content

lexigram_lib/parsergen/
mod.rs

1// Copyright (c) 2025 Redglyph (@gmail.com). All Rights Reserved.
2
3use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
4use iter_index::IndexerIterator;
5use vectree::VecTree;
6use lexigram_core::log::LogMsg;
7use lexigram_core::{CharLen, TokenId};
8use lexigram_core::alt::Alternative;
9use lexigram_core::parser::lr::{LRAction, LRStateId};
10use crate::grammar::{grtree_to_str, GrTreeExt, NTConversion, SepInfo, SepNt, SepNtInfo};
11use crate::{columns_to_str, indent_source, AltId, NameFixer, NameTransformer, SourceSpacer, StructLibs, SymbolTable, VarId};
12use crate::fixed_sym_table::{SymInfoTable};
13use crate::alt::ruleflag;
14use crate::build::{BuildError, BuildErrorSource, HasBuildErrorSource};
15use crate::CollectJoin;
16use crate::grammar::ll1::LL1ParsingTable;
17use crate::grammar::origin::{FromPRS, Origin};
18use crate::lexergen::LexigramCrate;
19use crate::log::{BufLog, LogReader, LogStatus, Logger};
20use crate::parser::{OpCode, Symbol};
21use crate::segments::Segments;
22use crate::segmap::Seg;
23
24pub mod lr;
25pub mod ll1;
26pub(crate) mod tests;
27
28pub use ll1::LLParserTables;
29use crate::adaptors::FlagLastIterator;
30// ---------------------------------------------------------------------------------------------
31
32pub(crate) fn symbol_to_code(s: &Symbol) -> String {
33    match s {
34        Symbol::Empty => "Symbol::Empty".to_string(),
35        Symbol::T(t) => format!("Symbol::T({t})"),
36        Symbol::NT(nt) => format!("Symbol::NT({nt})"),
37        Symbol::End => "Symbol::End".to_string(),
38    }
39}
40
41// ---------------------------------------------------------------------------------------------
42
43#[derive(Clone, Debug, PartialEq)]
44struct ItemInfo {
45    name: String,
46    sym: Symbol,            // NT(var) or T(token)
47    owner: VarId,           // NT owning this item; for ex. owner = `A` for `sym = b` in `A -> a b+ c`
48    index: Option<usize>    // when several identical symbols in the same alternative: `A -> id := id ( id )`
49}
50
51#[allow(unused)]
52impl ItemInfo {
53    fn to_str(&self, symbol_table: Option<&SymbolTable>) -> String {
54        format!("{} ({}{}, ◄{})",
55                self.name,
56                self.sym.to_str(symbol_table),
57                if let Some(n) = self.index { format!(", [{n}]") } else { "".to_string() },
58                Symbol::NT(self.owner).to_str(symbol_table))
59    }
60}
61
62// ---------------------------------------------------------------------------------------------
63
64/// Determines which nonterminals have a value, so a dedicated type and field in contexts:
65/// * [`None`](NTValue::None): No nonterminal has a value
66/// * [`Parents`](NTValue::Parents): Only the top nonterminal parents have a value
67/// * [`Default`](NTValue::Default): The top nonterminal parents and the children of `(<L> )+*` have a value
68/// * [`SetIds(Vec<VarId>)`](NTValue::SetIds): The nonterminals that have a value is set explicitly by ID
69/// * [`SetNames(Vec<String>)`](NTValue::SetNames): The nonterminals that have a value is set explicitly by name.
70///   The names "`<default>`" and "`<parents>`" can be used to set all the nonterminals of the corresponding class.
71///   Individual nonterminals can be preceded by a "-" to indicate they don't hold a value.
72#[derive(Clone, PartialEq, Default, Debug)]
73pub enum NTValue {
74    /// No nonterminal has a value
75    None,
76    /// Only the top nonterminal parents have a value
77    Parents,
78    /// The top nonterminal parents and the children of `(<L> )+*` have a value
79    #[default]
80    Default,
81    /// The set of nonterminals that have a value is set explicitly by ID
82    SetIds(Vec<VarId>),
83    /// The set of nonterminals that have a value is set explicitly by name. Individual names preceded by a "-" don't have a value.
84    SetNames(Vec<String>),
85}
86
87impl NTValue {
88    /// Only the top nonterminal parents have a value (can be used in [NTValue::SetNames])
89    pub const DEFAULT: &str = "<default>";
90    /// The top nonterminal parents and the children of `(<L> )+*` have a value (can be used in [NTValue::SetNames])
91    pub const PARENTS: &str = "<parents>";
92
93    pub fn is_none(&self) -> bool {
94        matches!(self, NTValue::None)
95    }
96
97    pub fn is_parents(&self) -> bool {
98        matches!(self, NTValue::Parents)
99    }
100
101    pub fn is_default(&self) -> bool {
102        matches!(self, NTValue::Default)
103    }
104
105    pub fn is_ids(&self) -> bool {
106        matches!(self, NTValue::SetIds(_))
107    }
108
109    pub fn is_names(&self) -> bool {
110        matches!(self, NTValue::SetNames(_))
111    }
112}
113
114// ---------------------------------------------------------------------------------------------
115
116pub static DEFAULT_LISTENER_NAME: &str = "Parser";
117
118pub type SpanNbr = u16;
119
120fn count_span_nbr(opcode: &[OpCode]) -> SpanNbr {
121    let count = opcode.iter().filter(|op| op.has_span()).count();
122    count.try_into().unwrap_or_else(|_| panic!("# span = {count} > {}", SpanNbr::MAX))
123}
124
125struct SourceInputContext<'a> {
126    parent_has_value        : bool,
127    parent_nt               : usize,
128    syns                    : &'a Vec<VarId>,
129    ambig_op_alts           : &'a BTreeMap<AltId, Vec<AltId>>,
130}
131
132struct SourceState<'a> {
133    init_nt_done            : &'a mut HashSet<VarId>,
134    span_init               : &'a mut HashSet<VarId>,
135    nt_contexts             : &'a mut Vec<Option<Vec<AltId>>>,
136    exit_alt_done           : &'a mut HashSet<VarId>,
137    exit_fixer              : &'a mut NameFixer,
138}
139
140struct WrapperSources {
141    src                     : Vec<String>,
142    src_listener_decl       : Vec<String>,
143    src_skel                : Vec<String>,
144    src_types               : Vec<String>,
145    src_init                : Vec<Vec<String>>,
146    src_exit                : Vec<Vec<String>>,
147    src_wrapper_impl        : Vec<String>,
148}
149
150#[derive(Clone, Copy, PartialEq, Debug)]
151pub enum ParserType {
152    /// Top-down, non-recursive LL(1) parser
153    LL1,
154    /// Bottom-up, LALR(1) parser
155    LALR,
156}
157
158impl ParserType {
159    pub fn is_ll(&self) -> bool {
160        *self == ParserType::LL1
161    }
162
163    pub fn is_lr(&self) -> bool {
164        *self == ParserType::LALR
165    }
166}
167
168#[derive(Clone, Debug)]
169pub struct ParserGenOptions {
170    pub nt_value: NTValue,
171    pub include_alts: bool,
172    pub headers: Vec<String>,
173    pub used_libs: StructLibs,
174    /// generates the wrapper source code
175    pub gen_wrapper: bool,
176    /// generates code to give the location of nonterminals and tokens as extra parameters of listener methods
177    pub gen_span_params: bool,
178    pub gen_token_enums: bool,
179    pub lib_crate: LexigramCrate,
180    /// source code indentation of the wrapper, in number of space characters
181    pub indent: usize,
182    /// source code indentation of the template for the user types
183    pub types_indent: usize,
184    /// source code indentation of the template for the listener implementation
185    pub listener_indent: usize,
186    /// parser type: LL1 or LALR
187    pub parser_type: ParserType,
188}
189
190impl ParserGenOptions {
191    fn has_lr_error_recovery(&self) -> bool {
192        // for now, error recovery enabled for LR parsers
193        self.parser_type.is_lr()
194    }
195}
196
197#[derive(Debug)]
198pub struct ParserGen {
199    // common parsing table fields:
200    num_nt: usize,
201    num_t_full: usize,               // includes the end $ symbol
202    alts: Vec<(VarId, Alternative)>,
203    flags: Vec<u32>,            // NT -> flags (+ or * normalization)
204    parent: Vec<Option<VarId>>, // NT -> parent NT
205    sep_info: SepInfo,
206    // LL1-specific parsing table fields:
207    table: Vec<AltId>,
208    // LR-specific parsing table fields:
209    #[allow(unused)]
210    num_states: usize,
211    #[allow(unused)]
212    action: Vec<LRAction>,
213    #[allow(unused)]
214    goto: Vec<LRStateId>,
215    state_symbol: Vec<Symbol>,
216    init_hook: bool,
217
218    symbol_table: SymbolTable,
219    terminal_hooks: Vec<TokenId>,
220    name: String,
221    options: ParserGenOptions,
222    nt_values: Vec<bool>,
223    /// `nt_parent[v]` is the vector of all variables having `v` has top parent (including `v` itself)
224    nt_parent: Vec<Vec<VarId>>,
225    var_alts: Vec<Vec<AltId>>,
226    origin: Origin<VarId, FromPRS>,
227    item_ops: Vec<Vec<Symbol>>,
228    opcodes: Vec<Vec<OpCode>>,
229    init_opcodes: Vec<OpCode>,
230    nt_name: Vec<(String, String, String)>,
231    alt_info: Vec<Option<(VarId, String)>>,
232    item_info: Vec<Vec<ItemInfo>>,
233    child_repeat_endpoints: HashMap<VarId, Vec<AltId>>,
234    /// generates the parser source code
235    gen_parser: bool,
236    span_nbrs: Vec<SpanNbr>,
237    /// Number of span values taken from a PARENT_REPEAT in init() to extract the first item of a SEP_LIST
238    span_nbrs_sep_list: HashMap<AltId, SpanNbr>,
239    start: VarId,
240    nt_conversion: HashMap<VarId, NTConversion>,
241    nt_type: HashMap<VarId, String>,
242    log: BufLog,
243}
244
245impl ParserGen {
246    fn post_build_from_rules(&mut self, parser_type: ParserType) {
247        self.options.parser_type = parser_type;
248        for var_id in 0..self.num_nt {
249            let top_var_id = self.get_top_parent(var_id as VarId) as usize;
250            self.nt_parent[top_var_id].push(var_id as VarId);
251        }
252    }
253
254    pub fn set_options(&mut self, options: ParserGenOptions) {
255        assert_eq!(options.parser_type, self.options.parser_type, "incompatible parser type");
256        self.options = options;
257    }
258
259    /// To call after all options are set and before generating the source code
260    fn pre_calc_data(&mut self) {
261        self.apply_options();
262        self.calc_opcodes();
263        self.add_opcode_hooks();
264        self.calc_span_nbrs();
265        self.calc_nt_value();
266    }
267
268    /// Sets internal values accordingly to the options
269    fn apply_options(&mut self) {
270        self.apply_nt_value();
271    }
272
273    pub fn set_name(&mut self, name: String) {
274        self.name = name;
275    }
276
277    pub fn get_name(&self) -> &str {
278        &self.name
279    }
280
281    #[inline]
282    pub fn get_symbol_table(&self) -> Option<&SymbolTable> {
283        Some(&self.symbol_table)
284    }
285
286    #[inline]
287    pub fn set_terminal_hooks(&mut self, terminal_hooks: Vec<TokenId>) {
288        if !terminal_hooks.is_empty() {
289            self.options.gen_token_enums = true;
290        }
291        self.terminal_hooks = terminal_hooks;
292    }
293
294    #[inline]
295    pub fn add_header<T: Into<String>>(&mut self, header: T) {
296        self.options.headers.push(header.into());
297    }
298
299    #[inline]
300    pub fn extend_headers<I: IntoIterator<Item=T>, T: Into<String>>(&mut self, headers: I) {
301        self.options.headers.extend(headers.into_iter().map(|s| s.into()));
302    }
303
304    #[inline]
305    pub fn add_lib<T: Into<String>>(&mut self, lib:T) {
306        self.options.used_libs.add(lib);
307    }
308
309    #[inline]
310    pub fn extend_libs<I: IntoIterator<Item=T>, T: Into<String>>(&mut self, libs: I) {
311        self.options.used_libs.extend(libs);
312    }
313
314    #[inline]
315    /// Declares the type of a non-terminal. The index of the NT, `org_var`, is the original index
316    /// in the ruletree set, which is the index originally assigned when parsing the grammar file.
317    pub fn add_nt_type<T: Into<String>>(&mut self, org_var: VarId, var_type: T) {
318        let var = self.conv_nt(org_var).unwrap_or_else(|| panic!("var {org_var} doesn't exist"));
319        self.nt_type.insert(var, var_type.into());
320    }
321
322    #[inline]
323    pub fn get_nt_type(&self, v: VarId) -> &str {
324        self.nt_type.get(&v).unwrap().as_str()
325    }
326
327    /// Sets which nonterminals have a value, from [nt_value](NTValue).
328    pub fn set_nt_value(&mut self, nt_value: NTValue) {
329        self.options.nt_value = nt_value;
330        self.apply_nt_value();
331    }
332
333    pub fn make_ll1_parsing_table(&self) -> LL1ParsingTable {
334        LL1ParsingTable {
335            num_nt: self.num_nt,
336            num_t_full: self.num_t_full,
337            alts: self.alts.clone(),
338            table: self.table.clone(),
339            flags: self.flags.clone(),
340            parent: self.parent.clone()
341        }
342    }
343
344    fn apply_nt_value(&mut self) {
345        let num_nt = self.get_symbol_table().unwrap().get_num_nt() as VarId;
346        let mut stack = vec![&self.options.nt_value];
347        let mut neg_stack = vec![];
348        self.nt_values.fill(false);
349        while let Some(nt_value) = stack.pop() {
350            match nt_value {
351                NTValue::None => {}
352                NTValue::Parents => {
353                    for v in 0..num_nt {
354                        if self.get_nt_parent(v).is_none() {
355                            self.nt_values[v as usize] = true;
356                        }
357                    }
358                }
359                NTValue::Default => {
360                    for v in 0..num_nt {
361                        if self.get_nt_parent(v).is_none() || self.nt_has_all_flags(v, ruleflag::CHILD_REPEAT | ruleflag::L_FORM) {
362                            self.nt_values[v as usize] = true;
363                        }
364                    }
365                }
366                NTValue::SetIds(ids) => {
367                    for v in ids {
368                        if *v < num_nt {
369                            self.nt_values[*v as usize] = true;
370                        } else {
371                            self.log.add_error(format!("setting value of NT #{v}, which doesn't exist"));
372                        }
373                    }
374                }
375                NTValue::SetNames(names) => {
376                    let name_to_id = self.symbol_table.get_nonterminals().index::<VarId>()
377                        .map(|(v, name)| (name, v))
378                        .collect::<HashMap<&str, VarId>>();
379                    for name in names {
380                        match name.as_str() {
381                            NTValue::DEFAULT => stack.push(&NTValue::Default),
382                            NTValue::PARENTS => stack.push(&NTValue::Parents),
383                            mut nt_name => {
384                                let add = if !nt_name.starts_with('-') {
385                                    true
386                                } else {
387                                    nt_name = &nt_name[1..];
388                                    false
389                                };
390                                if let Some(v) = name_to_id.get(nt_name) {
391                                    if add {
392                                        self.nt_values[*v as usize] = true;
393                                    } else {
394                                        neg_stack.push(*v);
395                                    }
396                                } else {
397                                    self.log.add_error(format!("setting value of NT '{name}', which doesn't exist"));
398                                }
399                            }
400                        }
401                    }
402                }
403            }
404        }
405        for v in neg_stack {
406            self.nt_values[v as usize] = false;
407        }
408    }
409
410    #[inline]
411    pub fn set_nt_has_value(&mut self, v: VarId, has_value: bool) {
412        self.nt_values[v as usize] = has_value;
413    }
414
415    /// Generates the parser source code if `gen_parser` is `true`. This option is `true` by default.
416    pub fn set_gen_parser(&mut self, gen_parser: bool) {
417        self.gen_parser = gen_parser;
418    }
419
420    /// Generates the wrapper source code if `gen_parser` is `true`. This option is `true` by default.
421    pub fn set_gen_wrapper(&mut self, gen_wrapper: bool) {
422        self.options.gen_wrapper = gen_wrapper;
423    }
424
425    /// Sets the source code indentation. This option is 0 by default.
426    pub fn set_indent(&mut self, indent: usize) {
427        self.options.indent = indent;
428    }
429
430    /// Sets the source code indentation of the template for the user types.
431    /// This option is 0 by default.
432    pub fn set_types_indent(&mut self, indent: usize) {
433        self.options.types_indent = indent;
434    }
435
436    /// Sets the source code indentation of the template for the listener implementation.
437    /// This option is 0 by default.
438    pub fn set_listener_indent(&mut self, indent: usize) {
439        self.options.listener_indent = indent;
440    }
441
442    /// Sets the source code indentation for the wrapper, the template for the user types and the
443    /// template for the listener implementation.
444    pub fn set_indents(&mut self, wrapper: usize, types: usize, listner: usize) {
445        self.options.indent = wrapper;
446        self.options.types_indent = types;
447        self.options.listener_indent = listner;
448    }
449
450    /// Generates code to give the location of nonterminals and tokens as extra parameters of listener methods.
451    pub fn set_gen_span_params(&mut self, gen_span_params: bool) {
452        self.options.gen_span_params = gen_span_params;
453    }
454
455    /// Generates enums for the terminal and nonterminal values. They may be helpful in the optional
456    /// listener trait methods like `hook()` and `intercept_token()` when they are used.
457    ///
458    /// # Example
459    ///
460    /// ```ignore
461    /// #[derive(Clone, Copy, PartialEq, Debug)]
462    /// #[repr(u16)]
463    /// pub enum Term {
464    ///     #[doc = "','"]        Comma = 0,
465    ///     #[doc = "';'"]        SemiColon = 1,
466    /// }
467    ///
468    /// #[derive(Clone, Copy, PartialEq, Debug)]
469    /// #[repr(u16)]
470    /// pub enum NTerm {
471    ///     #[doc = "`program`"]                   Program = 0,
472    ///     #[doc = "`stmt_i`, parent: `program`"] StmtI = 1,
473    /// }
474    /// ```
475    pub fn set_gen_token_enums(&mut self, gen_token_enums: bool) {
476        self.options.gen_token_enums = gen_token_enums;
477    }
478
479    #[inline]
480    pub fn get_nt_parent(&self, v: VarId) -> Option<VarId> {
481        self.parent[v as usize]
482    }
483
484    #[inline]
485    pub fn get_top_parent(&self, nt: VarId) -> VarId {
486        let mut var = nt;
487        while let Some(parent) = self.parent[var as usize] {
488            var = parent;
489        }
490        var
491    }
492
493    /// Include the definitions of the alternatives in the parser, for debugging purposes:
494    /// allows to print out the alternatives in VERBOSE mode.
495    pub fn set_include_alts(&mut self, include_alts: bool) {
496        self.options.include_alts = include_alts;
497    }
498
499    #[inline]
500    pub fn use_full_lib(&mut self, use_full_lib: bool) {
501        self.options.lib_crate = if use_full_lib { LexigramCrate::Full } else { LexigramCrate::Core };
502    }
503
504    #[inline]
505    pub fn set_crate(&mut self, lcrate: LexigramCrate) {
506        self.options.lib_crate = lcrate;
507    }
508
509    #[cfg(test)] // we keep it here because we'll need it later for doc comments and logs
510    fn get_original_alt_str(&self, a_id: AltId, symbol_table: Option<&SymbolTable>) -> Option<String> {
511        let (_var, f) = &self.alts[a_id as usize];
512        f.get_origin().and_then(|(o_v, o_id)| {
513            Some(format!(
514                "{} -> {}",
515                Symbol::NT(o_v).to_str(symbol_table),
516                grtree_to_str(self.origin.get_tree(o_v).unwrap(), Some(o_id), None, Some(o_v), symbol_table, false)
517            ))
518        })
519    }
520
521    /// Converts the original index of an NT to its current index.
522    ///
523    /// The original index is the NT's index of a general (non-normalized) ruletree set, as parsed from
524    /// a grammar file. The current index may differ if NTs were removed during the analysis of the
525    /// production rules or if <L> low-latency labels were declared.
526    fn conv_nt(&self, org_var: VarId) -> Option<VarId> {
527        match self.nt_conversion.get(&org_var) {
528            None => if (org_var as usize) < self.num_nt { Some(org_var) } else { None },
529            Some(NTConversion::MovedTo(new)) => Some(*new),
530            Some(NTConversion::Removed) => None
531        }
532    }
533
534    #[allow(unused)]
535    fn nt_has_all_flags(&self, var: VarId, flags: u32) -> bool {
536        self.flags[var as usize] & flags == flags
537    }
538
539    #[allow(unused)]
540    fn nt_has_any_flags(&self, var: VarId, flags: u32) -> bool {
541        self.flags[var as usize] & flags != 0
542    }
543
544    #[allow(unused)]
545    fn sym_has_flags(&self, s: &Symbol, flags: u32) -> bool {
546        if let Symbol::NT(nt) = s { self.nt_has_all_flags(*nt, flags) } else { false }
547    }
548
549    #[allow(unused)]
550    fn sym_has_value(&self, symbol: &Symbol) -> bool {
551        match symbol {
552            Symbol::T(t) => self.symbol_table.is_token_data(*t),
553            Symbol::NT(nt) => self.nt_values[*nt as usize],
554            _ => false
555        }
556    }
557
558    fn full_alt_components(&self, a_id: AltId, emphasis: Option<VarId>) -> (String, String) {
559        const VERBOSE: bool = false;
560        if VERBOSE { println!("full_alt_components(a_id = {a_id}):"); }
561        let &(mut v_a, ref alt) = &self.alts[a_id as usize];
562        while self.flags[v_a as usize] & ruleflag::CHILD_L_FACT != 0 {
563            v_a = *self.parent[v_a as usize].as_ref().unwrap();
564        }
565        let symtab = self.get_symbol_table();
566        if let Some(v_emph) = emphasis {
567            let parent_nt = self.get_top_parent(v_emph);
568            if let Some((t_emph, id_emph)) = self.origin.get(v_emph) {
569                return ((Symbol::NT(parent_nt).to_str(symtab)), grtree_to_str(t_emph, None, Some(id_emph), Some(parent_nt), symtab, true));
570            } else {
571                return (Symbol::NT(parent_nt).to_str(symtab), format!("<VAR {v_emph} NOT FOUND>"));
572            }
573        }
574        if let Some((vo, id)) = alt.get_origin() {
575            let t = self.origin.get_tree(vo).unwrap();
576            let flags = self.flags[v_a as usize];
577            if v_a != vo && flags & ruleflag::CHILD_REPEAT != 0 {
578                // iteration in parent rule
579                (
580                    String::new(),
581                    format!("`{}` {} in `{} -> {}`",
582                            grtree_to_str(t, Some(id), None, Some(vo), symtab, true),
583                            if flags & ruleflag::L_FORM != 0 { "iteration" } else { "item" },
584                            Symbol::NT(vo).to_str(symtab),
585                            grtree_to_str(t, None, Some(id), Some(vo), symtab, true))
586                )
587            } else {
588                let root = Some(id);
589                (Symbol::NT(vo).to_str(symtab), grtree_to_str(t, root, None, Some(vo), symtab, true))
590            }
591        } else {
592            (Symbol::NT(v_a).to_str(symtab), format!("<alt {a_id} NOT FOUND>"))
593        }
594    }
595
596    /// Representation of the original rule behind a context or a user variable
597    fn full_alt_str(&self, a_id: AltId, emphasis: Option<VarId>, quote: bool) -> String {
598        let (left, right) = self.full_alt_components(a_id, emphasis);
599        if left.is_empty() {
600            right
601        } else {
602            format!("{q}{left} -> {right}{q}", q = if quote { "`" } else { "" })
603        }
604    }
605
606    fn calc_opcodes(&mut self) {
607        const VERBOSE: bool = false;
608        self.log.add_note("- making opcodes...");
609        if self.options.parser_type.is_lr() {
610            self.opcodes = self.alts.iter()
611                .map(|(nt, Alternative { v, .. })| {
612                    let mut ops = v.iter().filter_map(|s| if !s.is_empty() { Some(OpCode::from(*s)) } else { None })
613                        .rev()
614                        .to_vec();
615                    if self.flags[*nt as usize] & ruleflag::CHILD_REPEAT != 0 {
616                        let op = ops.last_mut();
617                        if matches!(op, Some(OpCode::NT(nt2)) if nt == nt2) {
618                            *op.unwrap() = OpCode::Loop(*nt);
619                        }
620                    }
621                    ops
622                })
623                .to_vec();
624            return;
625        }
626        self.opcodes.clear();
627        self.init_opcodes = vec![OpCode::End, OpCode::NT(self.start)];
628        for (alt_id, (var_id, alt)) in self.alts.iter().index() {
629            if VERBOSE {
630                println!("{alt_id}: {}", alt.to_rule_str(*var_id, self.get_symbol_table(), 0));
631            }
632            let flags = self.flags[*var_id as usize];
633            let stack_sym = Symbol::NT(*var_id);
634            let mut new = self.alts[alt_id as usize].1.iter().filter(|s| !s.is_empty()).rev().cloned().to_vec();
635            if VERBOSE { println!("  - {}", new.iter().map(|s| s.to_str(self.get_symbol_table())).join(" ")); }
636            let mut opcode = Vec::<OpCode>::new();
637            let mut parent = self.parent[*var_id as usize];
638            if flags & ruleflag::CHILD_L_FACT != 0 {
639                while self.nt_has_all_flags(parent.unwrap(), ruleflag::CHILD_L_FACT) {
640                    parent = self.parent[parent.unwrap() as usize];
641                }
642                let parent = parent.unwrap();
643                // replaces Enter by Loop when going back to left-factorization parent, typically when coupled with + or *
644                // (per construction, there can't be any alternative going back to the grandparent or further up in a left factorization, so
645                //  we don't check that)
646                let parent_r_form_right_rec = self.flags[parent as usize] & ruleflag::R_RECURSION != 0 && flags & ruleflag::L_FORM == 0;
647                if VERBOSE {
648                    println!("  - child lfact, parent: {}, !parent_r_form_right_rec = !{parent_r_form_right_rec}, match = {}",
649                             Symbol::NT(parent).to_str(self.get_symbol_table()),
650                             new.first() == Some(&Symbol::NT(parent)));
651                }
652                if new.first() == Some(&Symbol::NT(parent)) && !parent_r_form_right_rec {
653                    opcode.push(OpCode::Loop(parent));
654                    new.remove(0);
655                }
656            }
657            let parent_lrec_no_lfact = flags & (ruleflag::PARENT_L_RECURSION | ruleflag::PARENT_L_FACTOR) == ruleflag::PARENT_L_RECURSION;
658            if flags & ruleflag::PARENT_L_FACTOR == 0 ||
659                parent_lrec_no_lfact ||
660                new.iter().all(|s| if let Symbol::NT(ch) = s { !self.nt_has_all_flags(*ch, ruleflag::CHILD_L_FACT) } else { true })
661            {
662                // if it's not a parent of left factorization, or
663                // if none of the NT in the alternative is a child of left factorization, or
664                // if it's the top parent of left recursion + left factorization,
665                // => adds an Exit
666                // (said otherwise: we don't want an exit in a parent or in the middle of a chain of left factorizations;
667                //  the exit should be only at the end of left factorizations, or in alternatives that aren't left-factorized -
668                //  except the special case of left recursion + left factorization, which needs the final exit)
669                opcode.push(OpCode::Exit(alt_id)); // will be popped when this NT is completed
670            }
671            opcode.extend(new.into_iter().map(OpCode::from));
672            let r_form_right_rec = flags & ruleflag::R_RECURSION != 0 && flags & ruleflag::L_FORM == 0;
673            if VERBOSE { println!("  - r_form_right_rec = {r_form_right_rec} = {} || {}",
674                                  flags & ruleflag::R_RECURSION != 0 && flags & ruleflag::L_FORM == 0,
675                                  flags & ruleflag::CHILD_L_FACT != 0 && self.flags[parent.unwrap() as usize] & ruleflag::R_RECURSION != 0 && flags & ruleflag::L_FORM == 0); }
676            if opcode.get(1).map(|op| op.matches(stack_sym)).unwrap_or(false) && !r_form_right_rec {
677                // swaps Exit(self) when it's in 2nd position (only happens in [Loop(_), Exit(self), ...],
678                // except right recursions that aren't left-form, because we let them unfold naturally (uses more stack)
679                opcode.swap(0, 1);
680                if VERBOSE { println!("  - swap 0, 1: {}", opcode.iter().map(|s| s.to_str(self.get_symbol_table())).join(" ")); }
681            } else if parent_lrec_no_lfact {
682                if let Some(OpCode::NT(x)) = opcode.get(1) {
683                    if self.nt_has_all_flags(*x, ruleflag::CHILD_L_RECURSION) {
684                        // swaps Exit(self) and call to left recursive item so that the wrapper can issue an exit_NT
685                        // with the correct context
686                        opcode.swap(0, 1);
687                        if VERBOSE { println!("  - swap 0, 1: {}", opcode.iter().map(|s| s.to_str(self.get_symbol_table())).join(" ")); }
688                    }
689                }
690            } else if flags & ruleflag::CHILD_INDEPENDENT_AMBIGUITY != 0 && opcode.len() > 1 {
691                // E_1: ◄4 ►E_2 ►E_1 abs  =>  ●E_2 ◄4 ●E_1 abs (where var_prime E_2 has child_amb flag)
692                if let Some(OpCode::NT(var_prime)) = opcode.get(1) {
693                    let vp = *var_prime; // to work around borrow checker
694                    if self.nt_has_all_flags(vp, ruleflag::CHILD_AMBIGUITY) {
695                        opcode.swap(0, 1);
696                        opcode[0] = OpCode::Loop(vp);
697                        if VERBOSE { println!("  - child indep ambig: {}", opcode.iter().map(|s| s.to_str(self.get_symbol_table())).join(" ")); }
698                    }
699                }
700            }
701            if flags & ruleflag::CHILD_L_FACT != 0 && opcode.len() >= 2 {
702                if self.nt_has_all_flags(parent.unwrap(), ruleflag::R_RECURSION | ruleflag::L_FORM)
703                    && opcode[1] == OpCode::NT(parent.unwrap())
704                {
705                    opcode.swap(0, 1);
706                    opcode[0] = OpCode::Loop(parent.unwrap());
707                }
708                let fact_top = self.get_top_parent(*var_id);
709                if VERBOSE {
710                    println!("  - check for initial exit swap: opcode = [{}], daddy = {}",
711                             opcode.iter().map(|s| s.to_str(self.get_symbol_table())).join(" "),
712                             Symbol::NT(fact_top).to_str(self.get_symbol_table()));
713                }
714                if self.flags[fact_top as usize] & ruleflag::PARENT_L_RECURSION != 0 &&
715                    matches!(opcode[0], OpCode::Exit(_)) &&
716                    matches!(opcode[1], OpCode::NT(v) if self.flags[v as usize] & ruleflag::CHILD_L_RECURSION != 0)
717                {
718                    if VERBOSE {
719                        println!("    swapping for initial exit_{}: {} <-> {}",
720                            Symbol::NT(fact_top).to_str(self.get_symbol_table()).to_lowercase(),
721                            opcode[0].to_str(self.get_symbol_table()),
722                            opcode[1].to_str(self.get_symbol_table())
723                        );
724                    }
725                    opcode.swap(0, 1);
726                }
727            }
728            opcode.iter_mut().for_each(|o| {
729                if let OpCode::NT(v) = o {
730                    // replaces Enter by Loop when back to self,
731                    // except right recursions that aren't left-form, because we let them unfold naturally (uses more stack)
732                    if v == var_id && !r_form_right_rec {
733                        *o = OpCode::Loop(*v)
734                    }
735                }
736            });
737            if VERBOSE { println!("  -> {}", opcode.iter().map(|s| s.to_str(self.get_symbol_table())).join(" ")); }
738            self.opcodes.push(opcode);
739        }
740    }
741
742    /// Adds hook opcodes that must be popped before the corresponding terminal is processed,
743    /// and possibly modified. For example, an `Id` terminal must be changed into `Type`
744    /// when that id has been previously declared as type in the parsed text.
745    ///
746    /// ```text
747    ///          | Num Id  Type  ,   ;  typedef let  =  print  -   +   $
748    /// ---------+--------------------------------------------------------
749    /// program  |  .   .   0    .   .     0     0   .    0    .   .   p
750    /// decl_i   |  .   .   1    .   .     1     2   .    2    .   .   .
751    /// inst_i   |  .   .   .    .   .     .     3   .    3    .   .   p
752    /// decl     |  .   .   4    .   .     5     p   .    p    .   .   .
753    /// […]
754    ///
755    ///  0: program -> decl_i inst_i      | ◄0 ►inst_i ►decl_i
756    ///  1: decl_i -> decl decl_i         | ●decl_i ◄1 ►decl
757    ///  2: decl_i -> ε                   | ◄2
758    ///  3: inst_i -> inst inst_i_1       | ►inst_i_1 ►inst
759    ///  4: decl -> Type Id decl_1 ";"    | ◄4 ";" ►decl_1 Id! Type!
760    ///  5: decl -> "typedef" Type Id ";" | ◄5 ";" Id! Type! "typedef"
761    /// […]
762    /// ```
763    /// - create set of NT that need hooked terminal to make a decision in the table:
764    ///
765    ///     `deps = { ►program, ►decl_i, ►decl }`
766    ///
767    /// - hook required at init if start NT in deps:
768    ///
769    ///     `init stack = [end ►program hook]`
770    ///
771    /// - hook required after each deps item that isn't in last opcode position:
772    ///     ```text
773    ///     1: decl_i -> decl decl_i         | ●decl_i hook ◄1 ►decl hook
774    ///                                                ^^^^
775    ///     ```
776    /// - hook required after each hooked terminal that isn't in last opcode position:
777    ///     ```text
778    ///     5: decl -> "typedef" Type Id ";" | ◄5 ";" Id! Type! hook "typedef"
779    ///                                                         ^^^^
780    ///     ```
781    fn add_opcode_hooks(&mut self) {
782        const VERBOSE: bool = false;
783        if !self.options.parser_type.is_ll() { return }
784        assert!(!self.opcodes.is_empty(), "opcodes haven't been calculated");
785        self.log.add_note("- adding hooks into opcodes...");
786        let hooks: HashSet<TokenId> = self.terminal_hooks.iter().cloned().collect();
787        let num_nt = self.num_nt;
788        let num_t = self.num_t_full;
789        let err = self.alts.len() as AltId;
790        if VERBOSE {
791            self.make_ll1_parsing_table().print(self.get_symbol_table(), 0);
792            println!("num_nt = {num_nt}\nnum_t = {num_t}\ntable: {}", self.table.len());
793        }
794        if VERBOSE { println!("hooks: {}", self.terminal_hooks.iter().map(|t| self.symbol_table.get_t_name(*t)).join(", ")); }
795        let deps: HashSet<VarId> = (0..num_nt as VarId)
796            .filter(|&nt| hooks.iter().any(|&t| self.table[nt as usize * num_t + t as usize] < err))
797            .collect();
798        if VERBOSE { println!("deps = {deps:?} = {}", deps.iter().map(|nt| self.symbol_table.get_nt_name(*nt)).join(", ")); }
799
800        // hook required when parser starts?
801        if deps.contains(&self.start) {
802            self.init_opcodes = vec![OpCode::End, OpCode::NT(self.start), OpCode::Hook];
803        }
804        let mut changed = false;
805        for opcodes in self.opcodes.iter_mut() {
806            let mut new = vec![];
807            let n = opcodes.len();
808            for op in &opcodes[..n - 1] {
809                new.push(*op);
810                match op {
811                    OpCode::T(t) if hooks.contains(t) => {
812                        new.push(OpCode::Hook);
813                    }
814                    OpCode::NT(nt) | OpCode::Loop(nt) if deps.contains(nt) => {
815                        new.push(OpCode::Hook);
816                    }
817                    _ => {}
818                }
819            }
820            if new.len() + 1 > n {
821                new.push(opcodes[n - 1]);
822                *opcodes = new;
823                changed = true;
824            }
825        }
826        if VERBOSE && changed {
827            println!("new opcodes:");
828            let mut cols = vec![];
829            let tbl = self.get_symbol_table();
830            for (i, (opcodes, (nt, alt))) in self.opcodes.iter().zip(&self.alts).enumerate() {
831                cols.push(vec![
832                    i.to_string(),
833                    format!("{} -> ", Symbol::NT(*nt).to_str(tbl)),
834                    alt.to_str(tbl),
835                    opcodes.iter().map(|op| op.to_str(tbl)).join(" "),
836                ]);
837            }
838            println!("{}", indent_source(vec![columns_to_str(cols, None)], 4))
839        }
840    }
841
842    fn calc_span_nbrs(&mut self) {
843        self.log.add_note("- making spans...");
844        let mut span_nbrs = vec![0 as SpanNbr; self.alts.len()];
845        for (alt_id, (var_id, _)) in self.alts.iter().enumerate() {
846            let opcode = &self.opcodes[alt_id];
847            let mut span_nbr = span_nbrs[alt_id] + count_span_nbr(opcode);
848            if self.nt_has_any_flags(*var_id, ruleflag::CHILD_REPEAT | ruleflag::CHILD_L_RECURSION) ||
849                self.nt_has_all_flags(*var_id, ruleflag::R_RECURSION | ruleflag::L_FORM) {
850                // there is a loop span
851                span_nbr += 1;
852            }
853            if matches!(opcode.first(), Some(OpCode::NT(nt)) if nt != var_id && self.flags[*nt as usize] & ruleflag::CHILD_L_RECURSION != 0) {
854                // independent lrec term: the first NT doesn't count
855                span_nbr -= 1;
856            }
857            // println!("### {} -> span = {span_nbr}: {}",
858            //          opcode.iter().map(|o| o.to_string()).join(" "), opcode.iter().filter(|o| o.has_span()).map(|o| o.to_string()).join(" "));
859            // println!("[{alt_id}]: {} + {} -> {span_nbr}", span_nbrs[alt_id], count_span_nbr(&opcode));
860            if self.nt_has_all_flags(*var_id, ruleflag::PARENT_L_FACTOR) {
861                if let Some(OpCode::NT(nt)) = opcode.first() {
862                    span_nbr -= 1;
863                    for a_id in self.var_alts[*nt as usize].iter() {
864                        span_nbrs[*a_id as usize] += span_nbr;
865                        // println!("- [{a_id}] += {span_nbr} -> {}", span_nbrs[*a_id as usize]);
866                    }
867                    // println!(" -> [{alt_id}] = 0");
868                    span_nbr = 0;
869                }
870            }
871            span_nbrs[alt_id] = span_nbr;
872        }
873        self.span_nbrs = span_nbrs;
874    }
875
876    fn get_group_alts(&self, g: &[VarId]) -> Vec<(VarId, AltId)> {
877        g.iter().flat_map(|c|
878            self.var_alts[*c as usize].iter().map(|a| (*c, *a))
879        ).collect::<Vec<_>>()
880    }
881
882    /// Gathers all the alternatives of a nonterminal, and if some of them are parent_l_fact, searches the
883    /// terminal child_l_fact instead. The result is the set of contexts that are used to
884    /// call self.listener.exit_<NT>(ctx) for a right-rec, a left-rec parent, a left-rec child, ...
885    fn gather_alts(&self, nt: VarId) -> Vec<AltId> {
886        const VERBOSE: bool = false;
887        let mut alt = vec![];
888        let mut explore = VecDeque::<VarId>::new();
889        explore.push_back(nt);
890        while !explore.is_empty() {
891            let var = explore.pop_front().unwrap();
892            if VERBOSE { println!("{var}: alt = {} | explore = {} | alts: {}",
893                                  alt.iter().join(", "), explore.iter().join(", "),
894                                  self.var_alts[var as usize].iter().join(", ")); }
895            for a in &self.var_alts[var as usize] {
896                let (_, alter) = &self.alts[*a as usize];
897                if let Some(Symbol::NT(last)) = alter.symbols().last() {
898                    if self.nt_has_all_flags(*last, ruleflag::CHILD_L_FACT) {
899                        // only one alternative calls NT(last), so we won't push it twice in explore:
900                        explore.push_back(*last);
901                        continue;
902                    }
903                }
904                alt.push(*a);
905            }
906            if VERBOSE { println!("  => alt = {} | explore = {}", alt.iter().join(", "), explore.iter().join(", ")); }
907        }
908        alt
909    }
910
911    fn calc_nt_value(&mut self) {
912        const VERBOSE: bool = false;
913        self.log.add_note("- calculating nonterminals' value...");
914        // we proceed by var parent, then all alternatives in each parent/children group
915        for g in self.nt_parent.iter().filter(|va| !va.is_empty()) {
916            // takes all the alternatives in the group (and their NT ID):
917            let group = self.get_group_alts(g);
918            let mut re_evaluate = true;
919            let g_top = g[0];
920            let is_ambig = self.nt_has_all_flags(g_top, ruleflag::PARENT_AMBIGUITY);
921            while re_evaluate {
922                re_evaluate = false;
923                let mut nt_used = HashSet::<VarId>::new();
924                if VERBOSE {
925                    let ids = group.iter().map(|(v, _)| *v).collect::<BTreeSet<VarId>>();
926                    println!("parent: {}, NT with value: {}",
927                             Symbol::NT(g[0]).to_str(self.get_symbol_table()),
928                             ids.into_iter().filter_map(|v|
929                                 if self.nt_values[v as usize] { Some(Symbol::NT(v as VarId).to_str(self.get_symbol_table())) } else { None }
930                             ).join(", "));
931                }
932                for (var_id, alt_id) in &group {
933                    // Default values are taken from opcodes.
934                    let mut has_value = false;
935                    for s in &self.opcodes[*alt_id as usize] {
936                        match s {
937                            OpCode::T(t) =>
938                                has_value |= self.symbol_table.is_token_data(*t),
939                            OpCode::NT(nt) => {
940                                let is_ambig_top = is_ambig && self.get_nt_parent(*nt) == Some(g_top)
941                                    && !self.nt_has_any_flags(*nt, ruleflag::CHILD_L_RECURSION | ruleflag::CHILD_REPEAT);
942                                let var = if is_ambig_top { g_top } else { *nt };
943                                nt_used.insert(var);
944                                has_value |= self.nt_values[var as usize]
945                            },
946                            _ => {}
947                        }
948                    }
949                    // Looks if a child_repeat has a value
950                    if has_value && self.parent[*var_id as usize].is_some() {
951                        // If it's a child of left factorization, we need to find the original nonterminal with the flags:
952                        let mut child_nt = *var_id as usize;
953                        while self.flags[child_nt] & ruleflag::CHILD_REPEAT == 0 {
954                            if let Some(parent) = self.parent[child_nt] {
955                                child_nt = parent as usize;
956                            } else {
957                                break;
958                            }
959                        }
960                        // +* non-lform children have the same value as their parent, but +* lform
961                        // children's value is independent of their parent's
962                        if self.flags[child_nt] & (ruleflag::CHILD_REPEAT | ruleflag::L_FORM) == ruleflag::CHILD_REPEAT {
963                            if VERBOSE && !self.nt_values[child_nt] {
964                                print!(" | {} is now valued {}",
965                                       Symbol::NT(child_nt as VarId).to_str(self.get_symbol_table()),
966                                       if nt_used.contains(&(child_nt as VarId)) { "and was used before" } else { "but wasn't used before" }
967                                );
968                            }
969                            re_evaluate |= !self.nt_values[child_nt] && nt_used.contains(&(child_nt as VarId));
970                            self.nt_values[child_nt] = true;
971                        }
972                    }
973                }
974            }
975        }
976    }
977
978    pub(crate) fn calc_item_ops(&mut self) {
979        const VERBOSE: bool = false;
980        self.log.add_note("- making item ops...");
981        let mut items = vec![Vec::<Symbol>::new(); self.alts.len()];
982        if VERBOSE {
983            println!("Groups:");
984            for g in self.nt_parent.iter().filter(|va| !va.is_empty()) {
985                let group = self.get_group_alts(g);
986                let ids = group.iter().map(|(v, _)| *v).collect::<BTreeSet<VarId>>();
987                println!("{}: {}, alts {}",
988                         Symbol::NT(g[0]).to_str(self.get_symbol_table()),
989                    ids.iter().map(|v| Symbol::NT(*v).to_str(self.get_symbol_table())).join(", "),
990                    group.iter().map(|(_, a)| a.to_string()).join(", ")
991                );
992            }
993        }
994        let mut alts_to_revisit = HashSet::<AltId>::new();
995        // we proceed by var parent, then all alternatives in each parent/children group
996        for g in self.nt_parent.iter().filter(|va| !va.is_empty()) {
997            // takes all the alternatives in the group (and their NT ID):
998            let group = self.get_group_alts(g);
999            let g_top = g[0];
1000            let is_ambig = self.nt_has_all_flags(g_top, ruleflag::PARENT_AMBIGUITY);
1001            if VERBOSE {
1002                let ids = group.iter().map(|(v, _)| *v).collect::<BTreeSet<VarId>>();
1003                println!("parent: {}, NT with value: {}",
1004                         Symbol::NT(g[0]).to_str(self.get_symbol_table()),
1005                         ids.into_iter().filter_map(|v|
1006                             if self.nt_values[v as usize] { Some(Symbol::NT(v as VarId).to_str(self.get_symbol_table())) } else { None }
1007                         ).join(", "));
1008            }
1009            let g_top_has_value = self.nt_values[g_top as usize];
1010            for (var_id, alt_id) in &group {
1011                let ambig_loop_value = g_top_has_value && is_ambig && self.nt_has_all_flags(*var_id, ruleflag::CHILD_L_RECURSION);
1012                items[*alt_id as usize] = if ambig_loop_value { vec![Symbol::NT(g_top)] } else { vec![] };
1013            }
1014            for (var_id, alt_id) in &group {
1015                let opcode = &self.opcodes[*alt_id as usize];
1016                let (_, alt) = &self.alts[*alt_id as usize];
1017                if VERBOSE {
1018                    print!("- {alt_id}: {} -> {}   [{}]",
1019                           Symbol::NT(*var_id).to_str(self.get_symbol_table()),
1020                           alt.to_str(self.get_symbol_table()),
1021                           opcode.iter().map(|op| op.to_str(self.get_symbol_table())).join(" "));
1022                }
1023                let flags = self.flags[*var_id as usize];
1024
1025                // Default values are taken from opcodes. Loop(nt) is only taken if the parent is l-rec;
1026                // we look at the parent's flags instead of the alternative's because left factorization could
1027                // displace the Loop(nt) to another non-l-rec child alternative.
1028                let mut has_sep_list_child_without_value = false;
1029                let mut values = self.opcodes[*alt_id as usize].iter().rev()
1030                    .filter_map(|s| {
1031                        let sym_maybe = match s {
1032                            OpCode::T(t) => Some(Symbol::T(*t)),
1033                            OpCode::NT(nt) => {
1034                                let is_ambig_top = is_ambig && self.get_nt_parent(*nt) == Some(g_top)
1035                                    && !self.nt_has_any_flags(*nt, ruleflag::CHILD_L_RECURSION | ruleflag::CHILD_REPEAT);
1036                                let var = if is_ambig_top { g_top } else { *nt };
1037                                Some(Symbol::NT(var))
1038                            },
1039                            _ => {
1040                                if VERBOSE { print!(" | {} dropped", s.to_str(self.get_symbol_table())); }
1041                                None
1042                            }
1043                        };
1044                        sym_maybe.and_then(|s| {
1045                            const REP_MASK: u32 = ruleflag::CHILD_REPEAT_PLUS_LFORM;
1046                            const CHILD_STAR: u32 = ruleflag::CHILD_REPEAT_LFORM;
1047                            let has_value = self.sym_has_value(&s);
1048                            if has_value
1049                                // for now, leaves child* nonterminals used in another nonterminal (the parent),
1050                                // because they're necessary in check_sep_list() to locate the first sep_list item:
1051                                || matches!(s, Symbol::NT(v) if v != *var_id && self.flags[v as usize] & REP_MASK == CHILD_STAR)
1052                            {
1053                                if !has_value {
1054                                    has_sep_list_child_without_value = true;
1055                                }
1056                                Some(s)
1057                            } else {
1058                                None
1059                            }
1060                        })
1061                    }).to_vec();
1062                //
1063                if has_sep_list_child_without_value {
1064                    // there's a child* nonterminal in `values` that has no value and should be removed later
1065                    alts_to_revisit.insert(*alt_id);
1066                }
1067                // Loop NTs which carry values are kept on the stack, too
1068                let parent_is_rrec_lfact = !is_ambig && self.nt_has_all_flags(g[0], ruleflag::R_RECURSION | ruleflag::PARENT_L_FACTOR);
1069                if parent_is_rrec_lfact {
1070                    if flags & ruleflag::CHILD_L_FACT != 0 && self.nt_has_all_flags(g[0], ruleflag::L_FORM) {
1071                        assert!(!self.nt_has_all_flags(*var_id, ruleflag::CHILD_L_FACT | ruleflag::L_FORM), "this was useful after all");
1072                        if VERBOSE { print!(" child_rrec_lform_lfact"); }
1073                        items[*alt_id as usize].insert(0, Symbol::NT(g[0]));
1074                    }
1075                } else {
1076                    let sym_maybe = if flags & ruleflag::CHILD_REPEAT != 0 && (self.nt_values[*var_id as usize] || flags & ruleflag::L_FORM != 0) {
1077                        Some(Symbol::NT(*var_id))
1078                    } else if !is_ambig && flags & ruleflag::CHILD_L_RECURSION != 0 {
1079                        let parent = self.parent[*var_id as usize].unwrap();
1080                        Some(Symbol::NT(parent))
1081                    } else if !is_ambig && flags & (ruleflag::R_RECURSION | ruleflag::L_FORM) == ruleflag::R_RECURSION | ruleflag::L_FORM {
1082                        Some(Symbol::NT(*var_id))
1083                    } else {
1084                        None
1085                    };
1086                    if let Some(s) = sym_maybe {
1087                        if self.sym_has_value(&s) {
1088                            if VERBOSE { print!(" | loop => {}", s.to_str(self.get_symbol_table())); }
1089                            values.insert(0, s);
1090                        }
1091                    }
1092                }
1093                if VERBOSE {
1094                    println!(" ==> [{}] + [{}]",
1095                             items[*alt_id as usize].iter().map(|s| s.to_str(self.get_symbol_table())).join(" "),
1096                             values.iter().map(|s| s.to_str(self.get_symbol_table())).join(" "));
1097                }
1098                if let Some(OpCode::NT(nt)) = opcode.first() {
1099                    // Take the values except the last NT
1100                    let backup = if matches!(values.last(), Some(Symbol::NT(x)) if x == nt) {
1101                        Some(values.pop().unwrap())
1102                    } else {
1103                        None
1104                    };
1105                    if nt != var_id && self.nt_has_all_flags(*nt, ruleflag::CHILD_L_RECURSION) {
1106                        if VERBOSE { println!("  CHILD_L_RECURSION"); }
1107                        // exit_<var_id>(context = values) before entering child loop
1108                        items[*alt_id as usize].extend(values);
1109                        continue;
1110                    }
1111                    if flags & ruleflag::PARENT_L_FACTOR != 0 {
1112                        if VERBOSE {
1113                            println!("  PARENT_L_FACTOR: moving {} to child {}",
1114                                     values.iter().map(|s| s.to_str(self.get_symbol_table())).join(" "),
1115                                     Symbol::NT(*nt).to_str(self.get_symbol_table()));
1116                        }
1117                        // factorization reports all the values to the children
1118                        let pre = &mut items[*alt_id as usize];
1119                        if !pre.is_empty() {
1120                            // pre-pends values that already exist for alt_id (and empties alt_id)
1121                            values.splice(0..0, std::mem::take(pre));
1122                        }
1123                        for a_id in self.var_alts[*nt as usize].iter() {
1124                            items[*a_id as usize].extend(values.clone());
1125                            // Can an lfact parent contain a child*? No, because child* are transformed in the
1126                            // RTS, while lfact is a late-PRS transformation. If an lfact parent included a
1127                            // child*, like in `parent -> x* A | x* B`, the x* of each lfact alt would be
1128                            // different: something like `parent -> parent_1 A | parent_2 B`.
1129                            // There wouldn't be an lfact => we don't need to add a_id to alts_to_revisit
1130                        }
1131                        continue;
1132                    }
1133                    if let Some(sym) = backup {
1134                        values.push(sym);
1135                    }
1136                }
1137                items[*alt_id as usize].extend(values);
1138            } // WARNING: loop has `continue` jumps
1139        }
1140
1141        // adds sep_list flags to * with token-separated lists, and removes the corresponding
1142        // items that will be taken in an init method or added in first position in the array
1143        self.check_sep_list(&mut items);
1144
1145        // removes the child* nonterminals with no value that had been left earlier for check_sep_list()
1146        for alt_id in alts_to_revisit {
1147            items[alt_id as usize].retain(|s| self.sym_has_value(s));
1148        }
1149        self.item_ops = items;
1150
1151        self.log.add_note(
1152            format!(
1153                "NT with value: {}",
1154                self.nt_values.iter().index()
1155                    .filter(|&(_, val)| *val)
1156                    .map(|(var, _)| Symbol::NT(var).to_str(self.get_symbol_table()))
1157                    .join(", ")));
1158    }
1159
1160    fn calc_sep_info(&mut self) {
1161        const VERBOSE: bool = false;
1162        let is_none = self.sep_info.is_none();
1163        let SepInfo::Nt(sep_nt) = self.sep_info.take() else { panic!("{}", if is_none { "no sep_info" } else { "unprocessed sep_info" }) };
1164        let mut sep_nt_alt = vec![];
1165        for SepNt { nt_parent, nt_child, item_len } in sep_nt {
1166            if VERBOSE { println!("- sep_nt: nt_parent: {nt_parent}, nt_child: {nt_child}, item_len: {item_len}"); }
1167            let nt_top = self.get_top_parent(nt_child) as usize;
1168            for &nt_group in self.nt_parent[nt_top].iter().filter(|&nt| *nt != nt_child) {
1169                for &alt_id in &self.var_alts[nt_group as usize] {
1170                    if self.alts[alt_id as usize].1.contains(&Symbol::NT(nt_child)) {
1171                        if VERBOSE { println!("  - alt {alt_id}:"); }
1172                        let mut queue = VecDeque::<AltId>::from_iter([alt_id]);
1173                        let mut timeout = 10;
1174                        while !queue.is_empty() {
1175                            let alt_id_inst = queue.pop_front().unwrap();
1176                            let mut is_fact = false;
1177                            let (nt, alt) = &self.alts[alt_id_inst as usize];
1178                            if VERBOSE { println!("    (nt {nt}, alt {alt_id_inst}) {}", alt.to_rule_str(*nt, self.get_symbol_table(), 0)); }
1179                            queue.extend(
1180                                alt.iter()
1181                                    .filter_map(|s|
1182                                        if let &Symbol::NT(nt_child) = s && nt_child != *nt && self.flags[nt_child as usize] & ruleflag::CHILD_L_FACT != 0 {
1183                                            is_fact = true;
1184                                            Some(nt_child)
1185                                        } else {
1186                                            None
1187                                        })
1188                                    .flat_map(|nt_child| {
1189                                        let alts_child = &self.var_alts[nt_child as usize];
1190                                        if VERBOSE { println!("    => child lfact alts {}", alts_child.iter().map(AltId::to_string).join(", ")); }
1191                                        alts_child
1192                                    })
1193                                    .copied()
1194                            );
1195                            if !is_fact {
1196                                if VERBOSE { println!("    => [alt_id_inst {alt_id_inst}, nt_child {nt_child}, item_len {item_len}]"); }
1197                                sep_nt_alt.push(SepNtInfo { alt_id_inst, nt_child, item_len });
1198                            } else {
1199                                if VERBOSE { println!("    queue: {queue:?}"); }
1200                            }
1201                            timeout -= 1;
1202                            if timeout == 0 { panic!() }
1203                        }
1204                    }
1205                }
1206            }
1207        }
1208        let check = sep_nt_alt.iter().map(|SepNtInfo { alt_id_inst, nt_child, .. }| (alt_id_inst, nt_child)).collect::<HashSet::<_>>();
1209        assert_eq!(check.len(), sep_nt_alt.len());
1210        self.sep_info = SepInfo::NtInfo(sep_nt_alt);
1211    }
1212
1213    fn check_sep_list(&mut self, items: &mut [Vec<Symbol>]) {
1214        //                                             span# item ops (actual)  (desired)
1215        // - 109                                       ----------------------- --------------------
1216        //   a -> Id "(" (Id ":" type / ",")+ ")"
1217        //   - LL1: 0: a -> Id "(" Id ":" type a_1 ")" | 7 | Id Id type a_1    | 4    | Id a_1
1218        //          2: . a_1 -> "," Id ":" type a_1    | 5 | a_1 Id type       | 5, 3 | a_1 Id type     + sep_list flag on a_1
1219        //          3: . a_1 -> ε                      | 1 | a_1               | 1    | Id
1220        //          1: type -> Id                      | 1 | Id                | 1    | a_1
1221        //
1222        //   - LR:  0: a -> Id "(" a_1 ")"             | 4 | Id a_1            | 4    | Id a_1
1223        //          2: . a_1 -> a_1 "," Id ":" type    | 5 | a_1 Id type       | 5, 3 | a_1 Id type     + sep_list flag on a_1
1224        //          3: . a_1 -> Id ":" type            | 4 | a_1 Id type       | 4    | a_1 Id type     (item_ops not used)
1225        //          1: type -> Id                      | 1 | Id                | 1    | Id
1226        // - 110
1227        //   a -> Id "(" ((Id ":" type / ",")+)? ")"
1228        //   - LL1: 0: a -> Id "(" a_2                 | 0 |                   | 0    |
1229        //          1: type -> Id                      | 1 | Id                | 1    | Id
1230        //          2: a_1 -> "," Id ":" type a_1      | 5 | a_1 Id type       | 5, 3 | a_1 Id type     + sep_list flag on a_1
1231        //          3: a_1 -> ε                        | 1 | a_1               | 1    | a_1
1232        //          4: a_2 -> Id ":" type a_1 ")"      | 7 | Id Id type a_1    | 4    | Id a_1
1233        //          5: a_2 -> ")"                      | 3 | Id                | 3    | Id
1234        //
1235        // - 220
1236        //   a -> "var" (<L=i> Id / ",")+ ";"
1237        //   - LL1:    NT    name  val   flags
1238        //           +--------------------------------------------------+
1239        //           |   0 | a    | y  | parent_+_or_*                  |
1240        //           |   1 | . i  |    | child_+_or_*, L-form, sep_list |
1241        //           +--------------------------------------------------+
1242        //          0: a -> "var" Id i ";"             | 4 | Id                | 3    |
1243        //          1: i -> "," Id i                   | 3 | Id                | 3, 1 | Id              + sep_list flag on i
1244        //          2: i -> ε                          | 1 |                   | 1    |
1245        const VERBOSE: bool = false;
1246        self.calc_sep_info();
1247        let is_lr = self.options.parser_type.is_lr();
1248        let SepInfo::NtInfo(sep_nt_info) = self.sep_info.take() else { panic!("unprocessed sep_info") };
1249        for SepNtInfo { alt_id_inst, nt_child, item_len } in sep_nt_info {
1250            if VERBOSE { println!("- sep_nt_info: alt_id_inst = {alt_id_inst}, nt_child = {nt_child}, item_len = {item_len} "); }
1251            // verifies the separator has no value
1252            let item_len_span = SpanNbr::try_from(item_len).unwrap_or_else(|_| panic!("item_len = {item_len}"));
1253            let c0_alt_id = self.var_alts[nt_child as usize][0];                                            // 2
1254            let alt_child0 = &self.alts[c0_alt_id as usize].1;
1255            let sep_len = alt_child0.len() - item_len - 1;                                                  // 5 - 3 - 1 = 1: [","]
1256            let sep_range = if is_lr { 1 .. sep_len + 1 } else { 0 .. sep_len };
1257            let valuables = sep_range.map(|i| alt_child0[i]).filter(|s| self.sym_has_value(s)).to_vec();
1258            if !valuables.is_empty() {
1259                let alt_str = self.full_alt_str(c0_alt_id, None, false);
1260                self.log.add_error(format!(
1261                    "separator used in {alt_str} contains valuable items: {}",
1262                    valuables.into_iter().map(|s| s.to_str(self.get_symbol_table())).join(", ")));
1263                continue
1264            }
1265            // span of initial item; note that in LR, it's stored in the 1st alt but used in the 2nd
1266            self.span_nbrs_sep_list.insert(c0_alt_id, item_len_span);                                       // , 3
1267            if !is_lr {
1268                self.span_nbrs[alt_id_inst as usize] -= item_len_span;                                      // 7 -> 4
1269                let c_len = if self.nt_values[nt_child as usize] { 1 } else { 0 };
1270                let item_ops_len = items[c0_alt_id as usize].len() - c_len;                                 // 2: [Id, type]
1271                if item_ops_len > 0 {
1272                    let p_alt = &mut items[alt_id_inst as usize];
1273                    if let Some(c_pos) = p_alt.iter().position(|s| *s == Symbol::NT(nt_child)) {
1274                        p_alt.drain(c_pos - item_ops_len..c_pos);
1275                    }
1276                }
1277            }
1278            self.flags[nt_child as usize] |= ruleflag::SEP_LIST;
1279
1280        }
1281    }
1282
1283    /// Sorts the alternatives to keep them in the same order as in the original rule.
1284    /// The ID in the original tree are used to see where each alt is coming from.
1285    ///
1286    /// CAUTION! If two alts come from the same ID, like the children of a *, only the last one is kept.
1287    fn sort_alt_ids(&self, top_nt: VarId, alts: &[AltId]) -> Vec<AltId> {
1288        const VERBOSE: bool = false;
1289        if VERBOSE {
1290            println!("  sorting {} alts {alts:?}", Symbol::NT(top_nt).to_str(self.get_symbol_table()));
1291            for &a_id in alts {
1292                let &(_nt, ref alt) = &self.alts[a_id as usize];
1293                if let Some((v, id)) = alt.origin {
1294                    let tree = &self.origin.trees[v as usize];
1295                    println!("    [{a_id}] id = {},{id} -> {}  <->  {}",
1296                             Symbol::NT(v).to_str(self.get_symbol_table()),
1297                             crate::grammar::grtree_to_str_ansi(tree, None, Some(id), Some(v), self.get_symbol_table(), false),
1298                             tree.to_str_index(None, self.get_symbol_table())
1299                    );
1300                    assert_eq!(v, top_nt, "v = {}, top_nt = {}", Symbol::NT(v).to_str(self.get_symbol_table()), Symbol::NT(top_nt).to_str(self.get_symbol_table()));
1301                }
1302            }
1303            println!("    top_nt tree: {top_nt}: {}", self.origin.trees[top_nt as usize].to_str_index(None, self.get_symbol_table()));
1304        }
1305        let mut sorted = vec![];
1306        let mut ids = alts.iter().filter_map(|&alt_id| self.alts[alt_id as usize].1.origin.map(|(_var, id)| (id, alt_id)))
1307            .collect::<HashMap<_, _>>();
1308        assert_eq!(alts.len(), ids.len(), "several alts come from the same subtree of the original rule");
1309        let tree = &self.origin.trees[top_nt as usize];
1310        for node in tree.iter_post_depth() {
1311            if let Some((_, alt_id)) = ids.remove_entry(&node.index) {
1312                sorted.push(alt_id);
1313            }
1314        }
1315        if VERBOSE { println!("    -> {sorted:?}"); }
1316        sorted
1317    }
1318
1319    /// Calculates nt_name, nt_info, item_info
1320    ///
1321    /// * `nt_name[var]: (String, String, String)` contains (upper, lower, lower)-case unique identifiers for each parent NT (the first two
1322    ///   are changed if they're Rust identifiers)
1323    /// * `alt_info[alt]: Vec<Option<(VarId, String)>>` contains the enum variant names for each context (must be regrouped by VarId)
1324    /// * `item_info[alt]: Vec<ItemInfo>` contains the data available on the stacks when exiting the alternative
1325    /// * `child_repeat_endpoints[var]: HashMap<VarId, Vec<AltId>>` list of alts (several when the repeat child has several outcomes,
1326    ///   as in `a -> (A | B)+`), where each alt corresponds to the item_ops with the values on the stack.
1327    ///
1328    /// For example:
1329    ///
1330    /// ```text
1331    /// // a -> (b C | A D | A E E)* D | A; b -> B;
1332    /// //
1333    /// //  0: a -> a_1 D     | ◄0 D! ►a_1    | a_1 D
1334    /// //  1: a -> A         | ◄1 A!         | A
1335    /// //  2: b -> B         | ◄2 B!         | B
1336    /// //  3: a_1 -> A a_2   | ►a_2 A!       |
1337    /// //  4: a_1 -> b C a_1 | ●a_1 ◄4 C! ►b | a_1 b C
1338    /// //  5: a_1 -> ε       | ◄5            | a_1
1339    /// //  6: a_2 -> D a_1   | ●a_1 ◄6 D!    | a_1 A D
1340    /// //  7: a_2 -> E E a_1 | ●a_1 ◄7 E! E! | a_1 A E E
1341    ///
1342    /// pub enum CtxA {
1343    ///     A1 { star: SynA1, d: String },  // `a -> (b C | A D | A E E)* D`
1344    ///     A2 { a: String },               // `a -> A`
1345    /// }
1346    /// pub enum CtxB {
1347    ///     B { b: String },                // `b -> B`
1348    /// }
1349    /// pub struct SynA1(pub Vec<SynA1Item>);
1350    /// pub enum SynA1Item {
1351    ///     Ch1 { b: SynB, c: String },         // `b C` item in `a -> ( ►► b C ◄◄  | A D | A E E)* D | A`
1352    ///     Ch2 { a: String, d: String },       // `A D` item in `a -> (b C |  ►► A D ◄◄  | A E E)* D | A`
1353    ///     Ch3 { a: String, e: [String; 2] },  // `A E E` item in `a -> (b C | A D |  ►► A E E ◄◄ )* D | A`
1354    /// }
1355    /// // User-defined: SynA, SynB
1356    ///
1357    /// nt_name: [("A", "a", "a"), ("B", "b", "b"), ("A1", "a1", "a1"), ("A2", "a2", "a2")]
1358    /// alt_info: [Some((0, "A1")), Some((0, "A2")), Some((1, "B")), None, None, None, None, None]
1359    /// item_info:
1360    /// 0:  [ItemInfo { name: "star", sym: NT(2), owner: 0, index: None },
1361    ///      ItemInfo { name: "d", sym: T(2), owner: 0, index: None }],
1362    /// 1:  [ItemInfo { name: "a", sym: T(1), owner: 0, index: None }],
1363    /// 2:  [ItemInfo { name: "b", sym: T(4), owner: 1, index: None }],
1364    /// 3:  [],
1365    /// 4:  [ItemInfo { name: "b", sym: NT(1), owner: 2, index: None },
1366    ///      ItemInfo { name: "c", sym: T(0), owner: 2, index: None }],
1367    /// 5:  [],
1368    /// 6:  [ItemInfo { name: "a", sym: T(1), owner: 2, index: None },
1369    ///      ItemInfo { name: "d", sym: T(2), owner: 2, index: None }],
1370    /// 7:  [ItemInfo { name: "a", sym: T(1), owner: 2, index: None },
1371    ///      ItemInfo { name: "e", sym: T(3), owner: 2, index: Some(0) },
1372    ///      ItemInfo { name: "e", sym: T(3), owner: 2, index: Some(1) }]
1373    /// child_repeat_endpoints: {2: [4, 6, 7]}
1374    /// ```
1375    fn calc_type_info(&mut self) {
1376        const VERBOSE: bool = false;
1377
1378        self.log.add_note("- determining item_info...");
1379        let is_lr = self.options.parser_type.is_lr();
1380        let mut nt_upper_fixer = NameFixer::new();
1381        let mut nt_lower_fixer = NameFixer::new();
1382        let mut nt_plower_fixer = NameFixer::new_empty(); // prefixed lowercase: don't worry about reserved words
1383        let nt_name: Vec<(String, String, String)> = (0..self.num_nt).map(|v| {
1384            let name = self.symbol_table.get_nt_name(v as VarId);
1385            let nu = nt_upper_fixer.get_unique_name(name.to_camelcase());
1386            let nl = nt_lower_fixer.get_unique_name(nu.to_underscore_lowercase());
1387            let npl = nt_plower_fixer.get_unique_name(nu.to_underscore_lowercase());
1388            (nu, nl, npl)
1389        }).to_vec();
1390
1391        let mut alt_info: Vec<Option<(VarId, String)>> = vec![None; self.alts.len()];
1392        let mut nt_repeat = HashMap::<VarId, Vec<ItemInfo>>::new();
1393        let mut item_info: Vec<Vec<ItemInfo>> = vec![vec![]; self.alts.len()];
1394        let mut child_repeat_endpoints = HashMap::<VarId, Vec<AltId>>::new();
1395        for group in self.nt_parent.iter().filter(|vf| !vf.is_empty()) {
1396            let is_ambig = self.nt_has_any_flags(group[0], ruleflag::PARENT_AMBIGUITY);
1397            let mut is_ambig_1st_child = is_ambig;
1398            let mut alt_info_to_sort = HashMap::<VarId, Vec<AltId>>::new();
1399            for var in group {
1400                let nt = *var as usize;
1401                let nt_flags = self.flags[nt];
1402                if is_ambig && (nt_flags & ruleflag::PARENT_L_RECURSION != 0 || (nt_flags & ruleflag::CHILD_L_RECURSION != 0 && !is_ambig_1st_child)) {
1403                    continue;
1404                }
1405                if nt_flags & (ruleflag::CHILD_REPEAT | ruleflag::L_FORM) == ruleflag::CHILD_REPEAT {
1406                    // collects the alt endpoints that correspond to each choice (one or several choices if | is used inside the repeat),
1407                    // each alt endpoint corresponding to the data in item_info
1408                    let is_plus = nt_flags & ruleflag::CHILD_PLUS != 0;
1409                    let mut endpoints = self.gather_alts(*var);
1410                    if VERBOSE { println!("** {} endpoints: {endpoints:?} ", Symbol::NT(*var).to_str(self.get_symbol_table())); }
1411                    if is_plus {
1412                        // with +, alt endpoints come in couples: the first loops, the other exits, both having the same data
1413                        // (in each (id1, id2) couple, id2 == id1 + 1)
1414                        endpoints = endpoints.chunks(2).map(|slice| slice[0]).to_vec();
1415                    } else {
1416                        // with *: in LL, the alts with no data are those with ε; there can be multiple of them because of lfact
1417                        //         in LR, the alts with no data don't have ε in sep_list, but there's no lfact, so we can just discard the last
1418                        if is_lr {
1419                            endpoints.pop();
1420                        } else {
1421                            endpoints.retain(|e| !self.alts[*e as usize].1.is_sym_empty());
1422                        }
1423                    }
1424                    assert!(!endpoints.is_empty());
1425                    let endpoints = self.sort_alt_ids(group[0], &endpoints);
1426                    child_repeat_endpoints.insert(*var, endpoints);
1427                }
1428                let last_alt_id = *self.var_alts[nt].last().unwrap();
1429                for &alt_id in &self.var_alts[nt] {
1430                    let i = alt_id as usize;
1431                    if is_ambig_1st_child && self.alts[i].1.is_sym_empty() {
1432                        continue;
1433                    }
1434                    let item_ops = &self.item_ops[alt_id as usize];
1435                    // Adds a suffix to the names of different symbols that would otherwise collide in the same context option:
1436                    // - identical symbols are put in a vector (e.g. `id: [String; 2]`)
1437                    // - different symbols, which means T vs NT, must have different names (e.g. `NT(A)` becomes "a",
1438                    //   `T(a)` becomes "a", too => one is renamed to "a1" to avoid the collision: `{ a: SynA, a1: String }`)
1439                    let mut indices = HashMap::<Symbol, (String, Option<usize>)>::new();
1440                    let mut fixer = NameFixer::new();
1441                    let mut owner = self.alts[i].0;
1442                    while let Some(parent) = self.parent[owner as usize] {
1443                        if self.flags[owner as usize] & ruleflag::CHILD_REPEAT != 0 {
1444                            // a child + * is owner
1445                            // - if <L>, it has its own public context and a user-defined return type
1446                            // - if not <L>, it has no context and a generator-defined return type (like Vec<String>)
1447                            // (we keep the loop for +, which has a left factorization, too)
1448                            break;
1449                        }
1450                        owner = parent;
1451                    }
1452                    let owner_flags = self.flags[owner as usize];
1453                    let is_nt_child_repeat = owner_flags & ruleflag::CHILD_REPEAT != 0;
1454                    // let is_nt_child_repeat = self.flags[owner as usize] & ruleflag::CHILD_REPEAT != 0;
1455                    for s in item_ops {
1456                        if let Some((_, c)) = indices.get_mut(s) {
1457                            *c = Some(0);
1458                        } else {
1459                            let name = if let Symbol::NT(vs) = s {
1460                                let flag = self.flags[*vs as usize];
1461                                if flag & ruleflag::CHILD_REPEAT != 0 {
1462                                    if is_nt_child_repeat && indices.is_empty() {
1463                                        // iterator variable in a + * loop (visible with <L>, for ex)
1464                                        if flag & (ruleflag::CHILD_PLUS | ruleflag::SEP_LIST) != 0 { "plus_acc".to_string() } else { "star_acc".to_string() }
1465                                    } else {
1466                                        // reference to a + * result
1467                                        if flag & (ruleflag::CHILD_PLUS | ruleflag::SEP_LIST) != 0 { "plus".to_string() } else { "star".to_string() }
1468                                    }
1469                                } else {
1470                                    nt_name[*vs as usize].clone().1
1471                                }
1472                            } else {
1473                                s.to_str(self.get_symbol_table()).to_lowercase()
1474                            };
1475                            indices.insert(*s, (fixer.get_unique_name(name), None));
1476                        }
1477                    }
1478
1479                    // A parent of left factorization has no context, but we must check the alternatives that are the actual parents.
1480                    // The flag test is optional, but it serves to gate the more complex parental test.
1481                    let has_lfact_child = nt_flags & ruleflag::PARENT_L_FACTOR != 0 &&
1482                        self.alts[i].1.symbols().iter().any(|s| matches!(s, &Symbol::NT(c) if self.flags[c as usize] & ruleflag::CHILD_L_FACT != 0));
1483
1484                    // (α)* doesn't call the listener for each α, unless it's l-form. We say it's a hidden child_repeat, and it doesn't need a context.
1485                    // The only children a child_repeat can have is due to left factorization in (α)+, so we check `owner` rather than `nt`.
1486                    let is_hidden_repeat_child = owner_flags & ruleflag::CHILD_REPEAT_LFORM == ruleflag::CHILD_REPEAT;
1487
1488                    // TODO: simplify and change names:
1489
1490                    // last * child is not in the endpoints. We don't simply check that the alt is empty because, in LR sep_list, the last
1491                    // alt isn't empty (it's used as first loop iteration to collect the first item).
1492                    let is_last_alt = if owner_flags & ruleflag::CHILD_PLUS != 0 {
1493                        if is_lr {
1494                            self.alts[alt_id as usize].1.first() != Some(&Symbol::NT(owner))
1495                        } else {
1496                            alt_id == last_alt_id
1497                        }
1498                    } else {
1499                        alt_id == last_alt_id
1500                    };
1501
1502                    // (α <L>)+ have two similar alternatives with the same data on the stack, one that loops and the last iteration. We only
1503                    // keep one context because we use a flag to tell the listener when it's the last iteration (more convenient).
1504                    let is_duplicate = i > 0 && owner_flags & ruleflag::CHILD_REPEAT_PLUS_LFORM == ruleflag::CHILD_REPEAT_PLUS_LFORM &&
1505                        is_last_alt;
1506
1507                    // is it the independent loop iteration, so the last one in LL or the first one in LR?
1508                    let is_independant_iteration = (nt_flags & ruleflag::CHILD_L_RECURSION != 0
1509                        || self.nt_has_all_flags(*var, ruleflag::CHILD_REPEAT_LFORM)) && is_last_alt;
1510
1511                    let has_context = !has_lfact_child && !is_hidden_repeat_child && !is_duplicate && !is_independant_iteration;
1512                    if VERBOSE {
1513                        println!("NT {nt}, alt {alt_id}: has_lfact_child = {has_lfact_child}, is_hidden_repeat_child = {is_hidden_repeat_child}, \
1514                            is_duplicate = {is_duplicate}, is_independant_iteration = {is_independant_iteration} => has_context = {has_context}");
1515                        println!("has_context: has_lfact_child = {has_lfact_child}, is_hidden_repeat_child = {is_hidden_repeat_child}, is_duplicate = {is_duplicate}, is_independant_iteration = {is_independant_iteration},");
1516                    }
1517                    if has_context {
1518                        alt_info_to_sort.entry(owner)
1519                            .and_modify(|v| v.push(alt_id))
1520                            .or_insert_with(|| vec![alt_id]);
1521                    }
1522                    let has_owner_value = self.nt_values[owner as usize];
1523                    item_info[i] = if item_ops.is_empty() && nt_flags & ruleflag::CHILD_L_RECURSION != 0 {
1524                        // we put here the return context for the final exit of left recursive rule
1525                        if has_owner_value {
1526                            vec![ItemInfo {
1527                                name: nt_name[owner as usize].1.clone(),
1528                                sym: Symbol::NT(owner),
1529                                owner,
1530                                index: None,
1531                            }]
1532                        } else {
1533                            vec![]
1534                        }
1535                    } else {
1536                        let is_rrec_lform = self.nt_has_all_flags(owner, ruleflag::R_RECURSION | ruleflag::L_FORM);
1537                        let skip = if (is_nt_child_repeat || is_rrec_lform) && has_owner_value { 1 } else { 0 };
1538                        let mut infos = item_ops.iter()
1539                            .skip(skip)
1540                            .map(|s| {
1541                                let index = if let Some((_, Some(index))) = indices.get_mut(s) {
1542                                    let idx = *index;
1543                                    *index += 1;
1544                                    Some(idx)
1545                                } else {
1546                                    None
1547                                };
1548                                ItemInfo {
1549                                    name: indices[s].0.clone(),
1550                                    sym: *s,
1551                                    owner,
1552                                    index,
1553                                }
1554                            }).to_vec();
1555                        if self.nt_has_all_flags(owner, ruleflag::CHILD_REPEAT_PLUS_LFORM)
1556                            && self.options.parser_type.is_ll()
1557                        {
1558                            // we add the flag telling the listener whether it's the last iteration or not
1559                            let last_name = fixer.get_unique_name("last_iteration".to_string());
1560                            infos.push(ItemInfo {
1561                                name: last_name,
1562                                sym: Symbol::Empty, // this marks the special flag variable
1563                                owner,
1564                                index: None,
1565                            });
1566                        };
1567                        if is_nt_child_repeat && !infos.is_empty() && !nt_repeat.contains_key(&owner) {
1568                            nt_repeat.insert(owner, infos.clone());
1569                        }
1570                        infos
1571                    }
1572                } // alt_id in var
1573                if is_ambig && nt_flags & ruleflag::CHILD_L_RECURSION != 0 {
1574                    is_ambig_1st_child = false;
1575                }
1576            } // var in group
1577            if VERBOSE { println!("alt_info_to_sort = {alt_info_to_sort:?}"); }
1578            for (owner, alts) in alt_info_to_sort {
1579                for (num, alt) in self.sort_alt_ids(group[0], &alts).into_iter().index_start(1) {
1580                    alt_info[alt as usize] = Some((owner, format!("V{num}")));
1581                }
1582            }
1583        } // group
1584
1585        if VERBOSE {
1586            println!("NT names: {}", nt_name.iter()
1587                .map(|(u, l, pl)| format!("{u}/{l}/{pl}"))
1588                .join(", "));
1589            println!("alt info:");
1590            for (alt_id, alt_names) in alt_info.iter().enumerate() {
1591                if let Some((v, name)) = alt_names {
1592                    println!("- alt {alt_id}, NT {v} {}, Ctx name: {name}", Symbol::NT(*v).to_str(self.get_symbol_table()));
1593                }
1594            }
1595            println!();
1596            println!("nt_name: {nt_name:?}");
1597            println!("alt_info: {alt_info:?}");
1598            println!("item_info:");
1599            for (i, item) in item_info.iter().enumerate().filter(|(_, item)| !item.is_empty()) {
1600                println!("- {i}: {{ {} }}", item.iter()
1601                    .map(|ii| format!("{}{} ({})", ii.name, ii.index.map(|i| format!("[{i}]")).unwrap_or(String::new()), ii.sym.to_str(self.get_symbol_table())))
1602                    .join(", "));
1603            }
1604            println!("item_info: {item_info:?}");
1605            println!("child_repeat_endpoints: {child_repeat_endpoints:?}");
1606        }
1607        self.nt_name = nt_name;
1608        self.alt_info = alt_info;
1609        self.item_info = item_info;
1610        self.child_repeat_endpoints = child_repeat_endpoints;
1611    }
1612
1613    /// Generates the source code of the wrapper and the accompanying templates. Returns
1614    /// * the indented source of the wrapper
1615    /// * the indented source of the template for the user types
1616    /// * the indented source of the template for the listener implementation
1617    pub fn gen_source_code(&mut self) -> (String, String, String) {
1618        self.pre_calc_data();
1619        self.log.add_note("generating source code...");
1620        if !self.log.has_no_errors() {
1621            return (String::new(), String::new(), String::new());
1622        }
1623        // Building the source code as we do below is not the most efficient, but it's done that way to
1624        // - be able to build only a part of the parser, and
1625        // - get the sources for the validation tests or print them / write them into a file.
1626        // The whole code isn't that big, so it's not a major issue.
1627        let mut parts = vec![];
1628        if !self.options.headers.is_empty() {
1629            parts.push(self.options.headers.clone());
1630        }
1631        let mut tmp_parts = if self.gen_parser || self.options.has_lr_error_recovery() {
1632            vec![self.source_common_parser_wrapper()]
1633        } else {
1634            vec![]
1635        };
1636        if self.gen_parser {
1637            tmp_parts.push(self.source_build_parser());
1638        }
1639        let (src_types, src_listener) = if self.options.gen_wrapper {
1640            self.calc_item_ops();
1641            let (src_wrapper, src_types, src_listener) = self.source_wrapper();
1642            tmp_parts.push(src_wrapper);
1643            (
1644                indent_source(vec![src_types], self.options.types_indent),
1645                indent_source(vec![src_listener], self.options.listener_indent)
1646            )
1647        } else {
1648            (String::new(), String::new())
1649        };
1650        self.log_nt_info();
1651        self.log_alt_info();
1652        parts.push(self.source_use());
1653        parts.extend(tmp_parts);
1654        // Create source code:
1655        (indent_source(parts, self.options.indent), src_types, src_listener)
1656    }
1657
1658    pub fn try_gen_source_code(mut self) -> Result<(BufLog, String, String, String), BuildError> {
1659        let (src, src_types, src_listener) = self.gen_source_code();
1660        if self.log.has_no_errors() {
1661            Ok((self.give_log(), src, src_types, src_listener))
1662        } else {
1663            Err(BuildError::new(self.give_log(), BuildErrorSource::ParserGen))
1664        }
1665    }
1666
1667    fn source_use(&self) -> Vec<String> {
1668        self.options.used_libs.gen_source_code()
1669    }
1670
1671    fn source_token_enums(&self) -> Vec<String> {
1672        let mut src = vec![];
1673        if self.options.gen_token_enums || self.options.has_lr_error_recovery() {
1674            let mut enum_fixer = NameFixer::new();
1675            let mut term_names = vec![];
1676            src.push("#[derive(Clone, Copy, PartialEq, Debug)]".to_string());
1677            src.push("#[repr(u16)]".to_string());
1678            src.push("pub enum Term {".to_string());
1679            let cols = self.symbol_table.get_terminals().enumerate()
1680                .map(|(t, (s, s_opt))| {
1681                    let su = enum_fixer.get_unique_name(s.to_camelcase());
1682                    term_names.push(su);
1683                    vec![
1684                        // format!("    #[doc=\"{:?}\"]", if let Some(so) = s_opt { format!("{so:?}") } else { String::new() }),
1685                        // if let Some(so) = s_opt { format!("    #[doc = \"'{so}'\"]") } else { String::new() },
1686                        format!("    #[doc = \"{}\"]", if let Some(so) = s_opt { format!("'{so}'") } else { "(variable)".to_string() }),
1687                        format!("{} = {t},", term_names.last().unwrap())]
1688                })
1689                .to_vec();
1690            src.extend(columns_to_str(cols, Some(vec![16, 0])));
1691            src.push("}".to_string());
1692            src.add_space();
1693            src.push("// Unfortunately, Rust has no way to safely convert to enum constants...".to_string());
1694            src.push("impl From<TokenId> for Term {".to_string());
1695            src.push("    fn from(value: TokenId) -> Self {".to_string());
1696            src.push("        match value {".to_string());
1697            src.extend(term_names.iter()
1698                .map(|s| format!("            _ if value == Term::{s} as TokenId => Term::{s},"))
1699            );
1700            src.push(r#"            _ => panic!("cannot convert terminal index #{value} to Term"),"#.to_string());
1701            src.push("        }".to_string());
1702            src.push("    }".to_string());
1703            src.push("}".to_string());
1704            src.add_space();
1705            src.push("#[derive(Clone, Copy, PartialEq, Debug)]".to_string());
1706            src.push("#[repr(u16)]".to_string());
1707            src.push("pub enum NTerm {".to_string());
1708            let cols = self.symbol_table.get_nonterminals().take(self.num_nt).index()
1709                .map(|(t, s)| vec![
1710                    format!(
1711                        "    #[doc = \"`{s}`{}\"]",
1712                        if let Some(p) = self.get_nt_parent(t) {
1713                            format!(", parent: `{}`", Symbol::NT(p).to_str(self.get_symbol_table()))
1714                        } else {
1715                            String::new()
1716                        }),
1717                    format!("{} = {t},", s.to_camelcase())])
1718                .to_vec();
1719            src.extend(columns_to_str(cols, Some(vec![16, 0])));
1720            src.push("}".to_string());
1721            src.add_space();
1722            src.push("impl TryFrom<TokenId> for NTerm {".to_string());
1723            src.push("    type Error = String;".to_string());
1724            src.push("    fn try_from(value: VarId) -> Result<Self, Self::Error> {".to_string());
1725            src.push("        match value {".to_string());
1726            src.extend(self.symbol_table.get_nonterminals().take(self.num_nt)
1727                .map(|s| format!("            _ if value == NTerm::{str} as VarId => Ok(NTerm::{str}),", str = s.to_camelcase()))
1728            );
1729            src.push(r#"            _ => Err(format!("cannot convert nonterminal index #{value} to NTerm")),"#.to_string());
1730            src.push("        }".to_string());
1731            src.push("    }".to_string());
1732            src.push("}".to_string());
1733            src.add_space();
1734            src.push("pub fn get_term_name(t: TokenId) -> (&'static str, Option<&'static str>) {".to_string());
1735            src.push("    SYMBOLS_T[t as usize]".to_string());
1736            src.push("}".to_string());
1737        }
1738        src
1739    }
1740
1741    fn source_common_parser_wrapper(&mut self) -> Vec<String> {
1742        let mut src = vec![];
1743        let num_t_table = self.symbol_table.get_num_t(); // includes <$> and <empty>
1744        assert!(num_t_table > 0, "terminal table is empty");
1745        const T_CHUNK: usize = 10;
1746        src.push(format!("static SYMBOLS_T: [(&str, Option<&str>); {num_t_table}] = ["));
1747        let mut it = self.symbol_table.get_terminals();
1748        src.extend(
1749            (0..self.symbol_table.get_num_t().div_ceil(T_CHUNK))
1750                .flag_first_last()
1751                .map(|(_, is_last, _)|
1752                    format!("    {}{}", (0..T_CHUNK).filter_map(|_| it.next()).map(|v| format!("{v:?}")).join(","), if is_last { "];" } else { "," })));
1753        src
1754    }
1755
1756    fn source_build_parser(&mut self) -> Vec<String> {
1757        match self.options.parser_type {
1758            ParserType::LL1 => self.source_build_parser_ll1(),
1759            ParserType::LALR => self.source_build_parser_lalr(),
1760        }
1761    }
1762
1763    fn get_info_type(&self, infos: &[ItemInfo], info: &ItemInfo) -> String {
1764        let type_name_base = match info.sym {
1765            Symbol::T(_) => "String".to_string(),
1766            Symbol::NT(vs) => self.get_nt_type(vs).to_string(),
1767            Symbol::Empty => "bool".to_string(),
1768            _ => panic!("unexpected symbol {}", info.sym)
1769        };
1770        if info.index.is_some() {
1771            let nbr = infos.iter()
1772                .map(|nfo| if nfo.sym == info.sym { nfo.index.unwrap() } else { 0 })
1773                .max().unwrap() + 1;
1774            format!("[{type_name_base}; {nbr}]")
1775        } else {
1776            type_name_base
1777        }
1778    }
1779
1780    /// Structure elements used in a context or in a +* child type
1781    fn source_infos(&self, infos: &[ItemInfo], add_pub: bool, add_type: bool) -> String {
1782        let pub_str = if add_pub { "pub " } else { "" };
1783        infos.iter()
1784            .filter_map(|info| {
1785                if info.index.is_none() || info.index == Some(0) {
1786                    let type_name = if add_type {
1787                        format!(": {}", self.get_info_type(infos, info))
1788                    } else {
1789                        String::new()
1790                    };
1791                    Some(format!("{pub_str}{}{type_name}", info.name))
1792                } else {
1793                    None
1794                }
1795        }).join(", ")
1796    }
1797
1798    fn is_alt_sym_empty(&self, a_id: AltId) -> bool {
1799        self.alts[a_id as usize].1.is_sym_empty()
1800    }
1801
1802    /// Generates the match cases for the "Call::Exit" in the `switch` method.
1803    fn make_match_choices(
1804        &self, alts: &[AltId], name: &str, init_name: &str, flags: u32, no_method: bool, force_id: Option<AltId>, init_discarded: bool
1805    ) -> (bool, Vec<String>)
1806    {
1807        assert!(!alts.is_empty(), "alts cannot be empty");
1808        let is_lr = self.options.parser_type.is_lr();
1809
1810        // If + <L> child, the two alts are identical, except for LR. We keep the two alts for LL because it's more coherent
1811        // for the rest of the flow. At the end, when we generate the wrapper method, we'll discard the 2nd alternative and use
1812        // the `alt_id` parameter to determine whether it's the last iteration or not.
1813        // In LL, we discard the 2nd, empty alternative immediately for a non-<L> * child because there's no associated context;
1814        // in LR, we call the init method. We also remove the last choice in <L> sep because it's init (no need for alt_id in exit with 2 alternatives).
1815        // no_method is true for non-<L> repeat children with no value (nothing to accumulate in a vector)
1816        let is_repeat_with_l_or_value = !no_method && flags & ruleflag::CHILD_REPEAT_PLUS_LFORM == ruleflag::CHILD_REPEAT;
1817        let is_lr_repeat_with_l_and_sep = is_lr && flags & (ruleflag::CHILD_REPEAT_LFORM | ruleflag::SEP_LIST) == ruleflag::CHILD_REPEAT_LFORM | ruleflag::SEP_LIST;
1818        let discarded = usize::from(is_repeat_with_l_or_value || is_lr_repeat_with_l_and_sep);
1819
1820        // + children always have 2*n left-factorized children, each couple with identical item_ops (one for the loop, one for the last iteration).
1821        // So in non-<L> +, we need more than 2 alts to need the alt_id parameter. In other cases, we need more than one
1822        // alt (after removing the possible discarded one) to require the alt_id parameter. On top of all that, in LR we don't need alt_id if
1823        // there's no value and no spans in child+, except if there are several alternatives, hence the additional `init_discarded`
1824        // to boost the threshold (only used in LR).
1825        let theshold_init = usize::from(init_discarded);
1826        let is_plus_no_lform = flags & ruleflag::CHILD_REPEAT_PLUS_LFORM == ruleflag::CHILD_REPEAT_PLUS;
1827        let is_alt_id_threshold = if is_plus_no_lform && !is_lr { 2 } else { 1 + theshold_init };
1828        let is_alt_id = force_id.is_none() && alts.len() - discarded > is_alt_id_threshold;
1829
1830        let mut choices = Vec::<String>::new();
1831        let force_id_str = force_id.map(|f| f.to_string()).unwrap_or_default();
1832        if alts.len() - discarded == 1 {
1833            if no_method {
1834                choices.push(format!("                    {} => {{}}", alts[0]));
1835            } else {
1836                choices.push(format!("                    {} => self.{name}({force_id_str}),", alts[0]));
1837            }
1838        } else {
1839            let last = alts.len() - 1 - discarded;
1840            choices.extend((0..last).map(|i| format!("                    {} |", alts[i])));
1841            if no_method {
1842                choices.push(format!("                    {} => {{}}", alts[last]));
1843            } else {
1844                choices.push(format!(
1845                    "                    {} => self.{name}({}{force_id_str}),",
1846                    alts[last],
1847                    if is_alt_id { "alt_id" } else { "" }));
1848            }
1849        }
1850        if discarded == 1 {
1851            if is_lr {
1852                choices.push(format!("                    {} => self.{init_name}(),", alts.last().unwrap()));
1853            } else {
1854                choices.push(format!("                    {} => {{}}", alts.last().unwrap()));
1855            }
1856        }
1857        (is_alt_id, choices)
1858    }
1859
1860    /// Generates a string with either `"{common}"` or `"({span_code}, {common})"`, where `span_code` is
1861    /// created by the closure. We use a closure because it's executed only if necessary, which
1862    /// avoids accessing data that might not be available when the span code is not generated.
1863    fn gen_match_item<F: FnOnce() -> String>(&self, common: String, span_only: F, has_value: bool) -> String {
1864        if self.options.gen_span_params {
1865            let span_code = span_only();
1866            if has_value {
1867                format!("({span_code}, {common})")
1868            } else {
1869                span_code
1870            }
1871        } else {
1872            common
1873        }
1874    }
1875
1876    fn get_var_param(item: &ItemInfo, indices: &HashMap<Symbol, Vec<String>>, non_indices: &mut Vec<String>) -> Option<String> {
1877        if let Some(index) = item.index {
1878            if index == 0 {
1879                Some(format!("{}: [{}]", item.name, indices[&item.sym].iter().rev().join(", ")))
1880            } else {
1881                None
1882            }
1883        } else {
1884            let name = non_indices.pop().unwrap();
1885            if name == item.name {
1886                Some(name)
1887            } else {
1888                Some(format!("{}: {name}", item.name))
1889            }
1890        }
1891    }
1892
1893    fn get_var_params(item_info: &[ItemInfo], skip: usize, indices: &HashMap<Symbol, Vec<String>>, non_indices: &mut Vec<String>) -> String {
1894        item_info.iter().skip(skip).filter_map(|item| {
1895            Self::get_var_param(item, indices, non_indices)
1896        }).join(", ")
1897    }
1898
1899    fn source_lets(infos: &[ItemInfo], nt_name: &[(String, String, String)], indent: &str, last_alt_id_maybe: Option<AltId>, alt_id_name: &str) -> (Vec<String>, String) {
1900        let mut src_let = vec![];
1901        let mut var_fixer = NameFixer::new();
1902        let mut indices = HashMap::<Symbol, Vec<String>>::new();
1903        let mut non_indices = Vec::<String>::new();
1904        for item in infos.iter().rev() {
1905            let varname = if let Some(index) = item.index {
1906                let name = var_fixer.get_unique_name(format!("{}_{}", item.name, index + 1));
1907                indices.entry(item.sym).and_modify(|v| v.push(name.clone())).or_insert(vec![name.clone()]);
1908                name
1909            } else {
1910                let name = item.name.clone();
1911                non_indices.push(name.clone());
1912                name
1913            };
1914            if item.sym.is_empty() {
1915                src_let.push(format!("{indent}let {varname} = {alt_id_name} == {};", last_alt_id_maybe.unwrap()));
1916            } else if let Symbol::NT(v) = item.sym {
1917                src_let.push(format!("{indent}let {varname} = self.stack.pop().unwrap().get_{}();", nt_name[v as usize].2));
1918            } else {
1919                src_let.push(format!("{indent}let {varname} = self.stack_t.pop().unwrap();"));
1920            }
1921        }
1922        let src_struct = Self::get_var_params(infos, 0, &indices, &mut non_indices);
1923        (src_let, src_struct)
1924    }
1925
1926    fn source_update_span(n: &str, is_mut: bool) -> Vec<String> {
1927        vec![
1928            format!("        let {}spans = self.stack_span.drain(self.stack_span.len() - {n} ..).collect::<Vec<_>>();", if is_mut { "mut " } else { "" }),
1929            "        self.stack_span.push(spans.iter().fold(PosSpan::empty(), |acc, sp| acc + sp));".to_string(),
1930        ]
1931    }
1932
1933    /// Source code of the first part of exit_* method for children of *+,
1934    /// which takes the values of the NT and T items from the stack at each iteration.
1935    /// The same code is generated for init_* methods in the case of token-separated items,
1936    /// when the accumulator is initialized with the first items.
1937    fn source_child_repeat_lets(
1938        &self,
1939        endpoints: &[AltId],
1940        item_info: &[Vec<ItemInfo>],
1941        is_plus: bool,
1942        nt_name: &[(String, String, String)],
1943        fn_name: &str,
1944        nu: &str,
1945        is_init: bool,
1946        has_value: bool,
1947        alt_id_name: &str
1948    ) -> (Vec<String>, String, Vec<String>)
1949    {
1950        // ex:
1951        //     let type1 = self.stack.pop().unwrap().get_type();
1952        //     let id = self.stack_t.pop().unwrap();
1953        //     let val = SynA1Item { id, type1 };
1954        //     let spans = self.stack_span.drain(self.stack_span.len() - 5 ..).collect::<Vec<_>>();
1955        //     self.stack_span.push(spans.iter().fold(PosSpan::empty(), |acc, sp| acc + sp));
1956        //
1957        let mut src_val = vec![];
1958        let mut src_span = vec![];
1959        let val_name = if endpoints.len() > 1 {
1960            // several possibilities; for ex. a -> (A | B)+  => Vec of enum type
1961            src_val.push(format!("        let {} = match {alt_id_name} {{", self.gen_match_item("val".to_string(), || "n".to_string(), has_value)));
1962            for (i, &a_id) in endpoints.iter().index_start(1) {
1963                let infos = &item_info[a_id as usize];
1964                src_val.push(format!("            {a_id}{} => {{", if is_plus { format!(" | {}", a_id + 1) } else { String::new() }));
1965                let (src_let, src_struct) = Self::source_lets(infos, nt_name, "                ", None, alt_id_name);
1966                if has_value {
1967                    src_val.extend(src_let);
1968                }
1969                let return_value = self.gen_match_item(
1970                    format!("Syn{nu}Item::V{i} {{ {} }}", src_struct),
1971                    || self.span_nbrs[a_id as usize].to_string(),
1972                    has_value
1973                );
1974                src_val.push(format!("                {return_value}"));
1975                src_val.push("            }".to_string());
1976            }
1977            src_val.push(format!("            _ => panic!(\"unexpected alt id {{{alt_id_name}}} in method {fn_name}\"),"));
1978            src_val.push("        };".to_string());
1979            if self.options.gen_span_params {
1980                src_span.extend(Self::source_update_span("n", false));
1981            }
1982            "val".to_string()
1983        } else {
1984            // single possibility; for ex. a -> (A B)+  => struct
1985            let a_id = endpoints[0];
1986            if self.options.gen_span_params {
1987                let span_nbr = if is_init {
1988                    *self.span_nbrs_sep_list.get(&a_id).unwrap()
1989                } else {
1990                    self.span_nbrs[a_id as usize]
1991                };
1992                src_span.extend(Self::source_update_span(&span_nbr.to_string(), false));
1993            }
1994            if has_value {
1995                let infos = &item_info[a_id as usize];
1996                let (src_let, src_struct) = Self::source_lets(infos, nt_name, "        ", None, alt_id_name);
1997                src_val.extend(src_let);
1998                if infos.len() == 1 {
1999                    // single repeat item; for ex. A -> B+  => type directly as Vec<type>
2000                    infos[0].name.clone()
2001                } else {
2002                    // several repeat items; for ex. A -> (B b)+  => intermediate struct type for Vec
2003                    src_val.push(format!("        let val = Syn{nu}Item {{ {} }};", src_struct));
2004                    "val".to_string()
2005                }
2006            } else {
2007                String::new() // don't care
2008            }
2009        };
2010        (src_val, val_name, src_span)
2011    }
2012
2013    /// Generates the wrapper source code and returns
2014    /// * the wrapper source code
2015    /// * the template for the user-defined types (Syn*)
2016    /// * the template for the listener implementation
2017    fn source_wrapper(&mut self) -> (Vec<String>, Vec<String>, Vec<String>) {
2018        const VERBOSE: bool = false;
2019        const MATCH_COMMENTS_SHOW_DESCRIPTIVE_ALTS: bool = false;
2020
2021        static PARSER_LIBS: [&str; 9] = [
2022            "::VarId", "::parser::Call", "::parser::ListenerWrapper",
2023            "::AltId", "::log::Logger", "::TokenId", "::lexer::PosSpan",
2024            "::parser::Terminate", "::log::LogMsg"
2025        ];
2026        static LR_ERROR_RECOVERY_LIB: &[&str] = &[
2027            "::parser::RecoveryNt",
2028            "::parser::lr::WrapperLRErrorRecovery",
2029        ];
2030
2031        self.log.add_note("generating wrapper source...");
2032        self.options.used_libs.extend(PARSER_LIBS.into_iter().map(|s| format!("{}{s}", self.options.lib_crate)));
2033        if self.options.has_lr_error_recovery() {
2034            self.options.used_libs.extend(LR_ERROR_RECOVERY_LIB.iter().map(|s| format!("{}{s}", self.options.lib_crate)));
2035        }
2036
2037        self.calc_type_info();
2038
2039        // defines missing type names
2040        for (v, name) in self.nt_name.iter().enumerate().filter(|(v, _)| self.nt_values[*v]) {
2041            let v = v as VarId;
2042            self.nt_type.entry(v).or_insert_with(|| format!("Syn{}", name.0));
2043        }
2044
2045        let mut src = vec![];
2046
2047        // writes contexts
2048        let mut nt_contexts = self.source_wrapper_ctx::<VERBOSE>(&mut src);
2049
2050        // writes intermediate Syn types
2051        let (src_types, syns) = self.source_wrapper_types::<VERBOSE>(&mut src);
2052
2053        // prepares the data for the following sections
2054        let mut exit_fixer = NameFixer::new();
2055        let mut span_init = HashSet::<VarId>::new();
2056        let mut src_skel = vec![
2057            format!("// {:-<80}", ""),
2058            format!("// Template for the user implementation of {}Listener", self.name),
2059            String::new(),
2060            "struct Listener {".to_string(),
2061            "    log: BufLog,".to_string(),
2062            "}".to_string(),
2063            String::new(),
2064            "#[allow(unused)]".to_string(),
2065            format!("impl {}Listener for Listener {{", self.name),
2066            "    fn get_log_mut(&mut self) -> &mut impl Logger {".to_string(),
2067            "        &mut self.log".to_string(),
2068            "    }".to_string(),
2069            String::new(),
2070        ];
2071        if self.options.has_lr_error_recovery() {
2072            src_skel.push("    fn get_recovery_value(&mut self, nt: VarId, last_dropped: Option<EnumSynValue>, _err_span: &PosSpan) -> RecoveryNtValue {".to_string());
2073            src_skel.push("        let nterm = NTerm::from(nt);".to_string());
2074            src_skel.push("        last_dropped".to_string());
2075            src_skel.push("            .and_then(|value| if value.nt() == nt { Some(value) } else { None })".to_string());
2076            src_skel.push("            .or_else(||".to_string());
2077            src_skel.push("                Some(match nterm {".to_string());
2078            src_skel.extend(syns.iter()
2079                .map(|&v| format!("                    NTerm::{0} => EnumSynValue::{0}({1}()),", self.nt_name[v as usize].0, self.get_nt_type(v)))
2080            );
2081            src_skel.push("                    _ => panic!()".to_string());
2082            src_skel.push("                }))".to_string());
2083            src_skel.push("            .map(|value| RecoveryNtValue::Value(value))".to_string());
2084            src_skel.push("            .unwrap_or_else(|| RecoveryNtValue::Abort)".to_string());
2085            src_skel.push("    }".to_string());
2086            src_skel.push(String::new());
2087        }
2088        let mut sources = WrapperSources {
2089            src,
2090            src_listener_decl: vec![],
2091            src_skel,
2092            src_types,
2093            src_init: vec![],
2094            src_exit: vec![],
2095            src_wrapper_impl: vec![],
2096        };
2097
2098        // we proceed by var parent, then all alts in each parent/children group
2099        for group in self.nt_parent.iter().filter(|vf| !vf.is_empty()) {
2100            let parent_nt = group[0] as usize;
2101            let parent_flags = self.flags[parent_nt];
2102            let parent_has_value = self.nt_values[parent_nt];
2103            let mut exit_alt_done = HashSet::<VarId>::new();
2104            let mut init_nt_done = HashSet::<VarId>::new();
2105            if VERBOSE { println!("- GROUP {}, parent has {}value, parent flags: {}",
2106                                  group.iter().map(|v| Symbol::NT(*v).to_str(self.get_symbol_table())).join(", "),
2107                                  if parent_has_value { "" } else { "no " },
2108                                  ruleflag::to_string(parent_flags).join(" | ")); }
2109            let is_ambig = parent_flags & ruleflag::PARENT_AMBIGUITY != 0;
2110            let ambig_children = if is_ambig {
2111                group.iter().filter(|&v| self.nt_has_any_flags(*v, ruleflag::CHILD_L_RECURSION)).cloned().to_vec()
2112            } else {
2113                Vec::new()
2114            };
2115            let mut ambig_op_alts = BTreeMap::<AltId, Vec<AltId>>::new();
2116            for (id, f) in ambig_children.iter()        // id = operator priority/ID in ambig rule
2117                .flat_map(|v| self.gather_alts(*v))
2118                .filter_map(|f| self.alts[f as usize].1.get_ambig_alt_id().map(|id| (id, f)))
2119            {
2120                ambig_op_alts.entry(id).or_default().push(f);
2121            }
2122            if VERBOSE && is_ambig {
2123                println!("- ambig children vars: {}", ambig_children.iter().map(|v| Symbol::NT(*v).to_str(self.get_symbol_table())).join(", "));
2124                println!("  ambig op alts: {ambig_op_alts:?}");
2125            }
2126
2127            // contexts used in source_wrapper_init and source_wrapper_exit methods:
2128            let in_ctx = SourceInputContext {
2129                parent_has_value,
2130                parent_nt,
2131                syns: &syns,
2132                ambig_op_alts: &ambig_op_alts,
2133            };
2134            let mut state = SourceState {
2135                init_nt_done: &mut init_nt_done,
2136                span_init: &mut span_init,
2137                nt_contexts: &mut nt_contexts,
2138                exit_alt_done: &mut exit_alt_done,
2139                exit_fixer: &mut exit_fixer,
2140            };
2141
2142            for var in group {
2143                let nt = *var as usize;
2144                let flags = self.flags[nt];
2145                // the parents of left recursion are not useful in ambiguous rules (they just push / pop the same value):
2146                let is_ambig_1st_child =  is_ambig && flags & ruleflag::CHILD_L_RECURSION != 0 && ambig_children.first() == Some(var);
2147                // we only process the first variable of the left recursion; below we gather the alts of
2148                // the other variables of the same type (in ambiguous rules, they repeat the same operators)
2149                let is_ambig_redundant = is_ambig && flags & ruleflag::L_RECURSION != 0 && !is_ambig_1st_child;
2150                let has_value = self.nt_values[nt];
2151                let mut init_needs_param = false;
2152
2153                // Call::Enter
2154                self.source_wrapper_init::<VERBOSE>(
2155                    &in_ctx,
2156                    *var,
2157                    flags,
2158                    has_value,
2159                    is_ambig_1st_child,
2160                    &mut init_needs_param,
2161                    &mut state,
2162                    &mut sources
2163                );
2164
2165                // Call::Exit
2166                self.source_wrapper_exit::<VERBOSE>(
2167                    &in_ctx,
2168                    *var,
2169                    flags,
2170                    has_value,
2171                    is_ambig_1st_child,
2172                    is_ambig_redundant,
2173                    init_needs_param,
2174                    &mut state,
2175                    &mut sources
2176                );
2177            }
2178
2179            // completes the unused nt entries in the match's Call::Exit => { ... }
2180            for a in group.iter().flat_map(|v| &self.var_alts[*v as usize]).filter(|a| !exit_alt_done.contains(a)) {
2181                let is_called = self.opcodes[*a as usize].contains(&OpCode::Exit(*a));
2182                let (v, alt) = &self.alts[*a as usize];
2183                let alt_str = if MATCH_COMMENTS_SHOW_DESCRIPTIVE_ALTS {
2184                    self.full_alt_str(*a, None, false)
2185                } else {
2186                    alt.to_rule_str(*v, self.get_symbol_table(), self.flags[*v as usize])
2187                };
2188                let comment = format!("// {alt_str} ({})", if is_called { "not used" } else { "never called" });
2189                if is_called {
2190                    sources.src_exit.push(vec![format!("                    {a} => {{}}"), comment]);
2191                } else {
2192                    sources.src_exit.push(vec![format!("                 /* {a} */"), comment]);
2193                }
2194            }
2195            // adds unused init calls, using Segments to regroup them
2196            let mut seg_init = Segments::from_iter(
2197                group.iter()
2198                    .filter_map(|&v| if !init_nt_done.contains(&v) { Some(Seg(v as u32, v as u32)) } else { None })
2199            );
2200            seg_init.normalize();
2201            for seg in seg_init {
2202                let Seg(a, b) = seg;
2203                if a == b {
2204                    sources.src_init.push(vec![format!("                    {a} => {{}}"), format!("// {}", Symbol::NT(a as VarId).to_str(self.get_symbol_table()))]);
2205                } else {
2206                    sources.src_init.push(vec![
2207                        format!("                    {a}{}{b} => {{}}", if b == a + 1 { " | " } else { " ..= " }),
2208                        format!("// {}", (a..=b).map(|v| Symbol::NT(v as VarId).to_str(self.get_symbol_table())).join(", "))
2209                    ]);
2210                }
2211            }
2212        }
2213
2214        self.source_wrapper_finalize(span_init, sources)
2215    }
2216
2217    /// Adds the wrapper source code related to the Ctx types
2218    fn source_wrapper_ctx<const VERBOSE: bool>(&self, src: &mut Vec<String>) -> Vec<Option<Vec<AltId>>> {
2219        let mut nt_contexts: Vec<Option<Vec<AltId>>> = vec![None; self.num_nt];
2220        for group in self.nt_parent.iter().filter(|vf| !vf.is_empty()) {
2221            let mut group_names = HashMap::<VarId, Vec<AltId>>::new();
2222            // fetches the NT that have alt data
2223            for nt in group {
2224                for &alt_id in &self.var_alts[*nt as usize] {
2225                    if let Some((owner, _name)) = &self.alt_info[alt_id as usize] {
2226                        group_names.entry(*owner)
2227                            .and_modify(|v| v.push(alt_id))
2228                            .or_insert_with(|| vec![alt_id]);
2229                    }
2230                }
2231            }
2232            if VERBOSE {
2233                println!("group {}", group.iter().map(|nt| Symbol::NT(*nt).to_str(self.get_symbol_table())).join(" "));
2234            }
2235            for &nt in group {
2236                if let Some(alts) = group_names.get(&nt) {
2237                    let flags = self.flags[nt as usize];
2238                    if VERBOSE {
2239                        print!("- {}: flags {}", Symbol::NT(nt).to_str(self.get_symbol_table()), ruleflag::to_string(flags).join(" "));
2240                        if let Some(gn) = group_names.get(&nt) {
2241                            println!(", alts = {}", gn.iter().map(|a| a.to_string()).join(", "));
2242                            let sorted = self.sort_alt_ids(group[0], gn);
2243                            println!("     sorted alts: {sorted:?}");
2244                        } else {
2245                            println!();
2246                        }
2247                    }
2248                    // if flags & (ruleflag::SEP_LIST | ruleflag::L_FORM) == ruleflag::SEP_LIST | ruleflag::L_FORM {
2249                    //     src.push("#[derive(Debug)]".to_string());
2250                    //     src.push(format!("pub enum InitCtx{} {{", self.nt_name[nt as usize].0));
2251                    //     let a_id = self.var_alts[nt as usize][0];
2252                    //     let comment = format!("first {}", self.full_alt_components(a_id, None).1);
2253                    //     let ctx_content = self.source_infos(&self.item_info[a_id as usize], false, true);
2254                    //     src.push(format!("    /// {comment}"));
2255                    //     let a_name = &self.alt_info[a_id as usize].as_ref().unwrap().1;
2256                    //     let ctx_item = if ctx_content.is_empty() {
2257                    //         if VERBOSE { println!("      {a_name},"); }
2258                    //         format!("    {a_name},", )
2259                    //     } else {
2260                    //         if VERBOSE { println!("      {a_name} {{ {ctx_content} }},"); }
2261                    //         format!("    {a_name} {{ {ctx_content} }},", )
2262                    //     };
2263                    //     src.push(ctx_item);
2264                    //     src.push("}".to_string());
2265                    // }
2266                    src.push("#[derive(Debug)]".to_string());
2267                    src.push(format!("pub enum Ctx{} {{", self.nt_name[nt as usize].0));
2268                    if VERBOSE { println!("  context Ctx{}:", self.nt_name[nt as usize].0); }
2269                    let alts = self.sort_alt_ids(group[0], alts);
2270                    nt_contexts[nt as usize] = Some(alts.clone());
2271                    for a_id in alts {
2272                        let comment = self.full_alt_str(a_id, None, true);
2273                        src.push(format!("    /// {comment}"));
2274                        if VERBOSE { println!("      /// {comment}"); }
2275                        let ctx_content = self.source_infos(&self.item_info[a_id as usize], false, true);
2276                        let a_name = &self.alt_info[a_id as usize].as_ref().unwrap().1;
2277                        let ctx_item = if ctx_content.is_empty() {
2278                            if VERBOSE { println!("      {a_name},"); }
2279                            format!("    {a_name},", )
2280                        } else {
2281                            if VERBOSE { println!("      {a_name} {{ {ctx_content} }},"); }
2282                            format!("    {a_name} {{ {ctx_content} }},", )
2283                        };
2284                        src.push(ctx_item);
2285                    }
2286                    src.push("}".to_string());
2287                }
2288            }
2289        }
2290        nt_contexts
2291    }
2292
2293    /// Adds the wrapper source code related to the user types
2294    fn source_wrapper_types<const VERBOSE: bool>(&self, src: &mut Vec<String>) -> (Vec<String>, Vec<VarId>) {
2295        static TYPE_DERIVE: &str = "#[derive(Debug, PartialEq)]";
2296
2297        let mut src_types = vec![
2298            format!("// {:-<80}", ""),
2299            "// Template for the user-defined types:".to_string(),
2300        ];
2301        src.add_space();
2302        let mut syns = Vec::<VarId>::new(); // list of valuable NTs
2303        for (v, names) in self.nt_name.iter().enumerate().filter(|(v, _)| self.nt_values[*v]) {
2304            let v = v as VarId;
2305            let (nu, _nl, _npl) = names;
2306            let nt_type = self.get_nt_type(v);
2307            if self.nt_has_all_flags(v, ruleflag::CHILD_REPEAT) {
2308                let is_lform = self.nt_has_all_flags(v, ruleflag::L_FORM);
2309                let first_alt = self.var_alts[v as usize][0];
2310                let (t, var_oid) = self.origin.get(v).unwrap();
2311                if is_lform {
2312                    let astr = format!("/// User-defined type for {}", self.full_alt_str(first_alt, None, true));
2313                    src_types.push(String::new());
2314                    src_types.push(astr.clone());
2315                    src_types.push(TYPE_DERIVE.to_string());
2316                    src_types.push(format!("pub struct {}();", self.get_nt_type(v)));
2317                } else {
2318                    let top_parent = self.get_top_parent(v);
2319                    src.push(format!("/// Computed `{}` array in `{} -> {}`",
2320                                     grtree_to_str(t, Some(var_oid), None, Some(top_parent), self.get_symbol_table(), true),
2321                                     Symbol::NT(top_parent).to_str(self.get_symbol_table()),
2322                                     grtree_to_str(t, None, Some(var_oid), Some(top_parent), self.get_symbol_table(), true),
2323                    ));
2324                    let endpoints = self.child_repeat_endpoints.get(&v).unwrap();
2325                    if endpoints.len() > 1 {
2326                        // several possibilities; for ex. a -> (A | B)+  => Vec of enum type
2327                        src.push("#[derive(Debug, PartialEq)]".to_string());
2328                        src.push(format!("pub struct {nt_type}(pub Vec<Syn{nu}Item>);"));
2329                        src.push("#[derive(Debug, PartialEq)]".to_string());
2330                        src.push(format!("pub enum Syn{nu}Item {{"));
2331                        for (i, &a_id) in endpoints.iter().index_start(1) {
2332                            src.push(format!("    /// {}", self.full_alt_str(a_id, None, true)));
2333                            src.push(format!("    V{i} {{ {} }},", self.source_infos(&self.item_info[a_id as usize], false, true)));
2334                        }
2335                        src.push("}".to_string());
2336                    } else {
2337                        // single possibility; for ex. a -> (A B)+  => struct
2338                        let a_id = endpoints[0];
2339                        let infos = &self.item_info[a_id as usize];
2340                        if infos.len() == 1 {
2341                            // single repeat item; for ex. A -> B+  => type directly as Vec<type>
2342                            let type_name = self.get_info_type(infos, &infos[0]);
2343                            src.push("#[derive(Debug, PartialEq)]".to_string());
2344                            src.push(format!("pub struct {nt_type}(pub Vec<{type_name}>);", ));
2345                        } else {
2346                            // several repeat items; for ex. A -> (B b)+  => intermediate struct type for Vec
2347                            src.push("#[derive(Debug, PartialEq)]".to_string());
2348                            src.push(format!("pub struct {nt_type}(pub Vec<Syn{nu}Item>);"));
2349                            src.push(format!("/// {}", self.full_alt_str(first_alt, None, false)));
2350                            src.push("#[derive(Debug, PartialEq)]".to_string());
2351                            src.push(format!("pub struct Syn{nu}Item {{ {} }}", self.source_infos(infos, true, true)));
2352                        }
2353                    }
2354                }
2355            } else {
2356                src_types.push(String::new());
2357                src_types.push(format!("/// User-defined type for `{}`", Symbol::NT(v).to_str(self.get_symbol_table())));
2358                src_types.push(TYPE_DERIVE.to_string());
2359                src_types.push(format!("pub struct {}();", self.get_nt_type(v)));
2360            }
2361            syns.push(v);
2362        }
2363        if !self.nt_values[self.start as usize] {
2364            let nu = &self.nt_name[self.start as usize].0;
2365            src.push(format!("/// Top non-terminal {nu} (has no value)"));
2366            src.push("#[derive(Debug, PartialEq)]".to_string());
2367            src.push(format!("pub struct Syn{nu}();"))
2368        }
2369
2370        // Writes EnumSynValue type and implementation
2371        if VERBOSE { println!("syns = {syns:?}"); }
2372        src.add_space();
2373        // EnumSynValue type
2374        src.push("#[derive(Debug)]".to_string());
2375        src.push(format!(
2376            "{}enum EnumSynValue {{ {} }}",
2377            if self.options.has_lr_error_recovery() { "pub " } else { "" },
2378            syns.iter().map(|v| format!("{}({})", self.nt_name[*v as usize].0, self.get_nt_type(*v))).join(", ")));
2379        src.add_space();
2380        src.push("impl EnumSynValue {".to_string());
2381        if !syns.is_empty() {
2382            // EnumSynValue getters
2383            for v in &syns {
2384                let (nu, _, npl) = &self.nt_name[*v as usize];
2385                let nt_type = self.get_nt_type(*v);
2386                src.push(format!("    fn get_{npl}(self) -> {nt_type} {{"));
2387                if syns.len() == 1 {
2388                    src.push(format!("        let EnumSynValue::{nu}(val) = self;"));
2389                    src.push("        val".to_string());
2390                } else {
2391                    src.push(format!("        if let EnumSynValue::{nu}(val) = self {{ val }} else {{ panic!() }}"));
2392                }
2393                src.push("    }".to_string());
2394            }
2395            src.push("    #[allow(unused)]".to_string());
2396            src.push("    fn nt(&self) -> VarId {".to_string());
2397            src.push("        match &self {".to_string());
2398            src.extend(syns.iter().map(|v| {
2399                format!("            EnumSynValue::{}(_) => {v},", self.nt_name[*v as usize].0)
2400            }));
2401            src.push("        }".to_string());
2402            src.push("    }".to_string());
2403        } else {
2404            src.push("    #[allow(unused)]".to_string());
2405            src.push(r#"    fn nt(&self) -> VarId {{ panic!("EnumSynValue holds no value") }}"#.to_string());
2406        }
2407        src.push("}".to_string());
2408        (src_types, syns)
2409    }
2410
2411    /// Adds the wrapper source code related to Call::Enter
2412    fn source_wrapper_init<const VERBOSE: bool>(
2413        &self,
2414        ctx                 : &SourceInputContext,
2415        var                 : VarId,
2416        flags               : u32,
2417        has_value           : bool,
2418        is_ambig_1st_child  : bool,
2419        init_needs_param    : &mut bool,
2420        state               : &mut SourceState,
2421        sources             : &mut WrapperSources
2422    ) {
2423        let is_ll = self.options.parser_type.is_ll();
2424        let &SourceInputContext { ambig_op_alts, .. } = ctx;
2425        let SourceState { init_nt_done, span_init, .. } = state;
2426        let WrapperSources { src_listener_decl, src_skel, src_init, src_wrapper_impl, .. } = sources;
2427        let nt = var as usize;
2428        let sym_nt = Symbol::NT(var);
2429        let nt_comment = format!("// {}", sym_nt.to_str(self.get_symbol_table()));
2430        let is_sep_list = flags & ruleflag::SEP_LIST != 0;
2431        let is_lform = flags & ruleflag::L_FORM != 0;
2432        let is_rrec_lform = is_lform && flags & ruleflag::R_RECURSION != 0;
2433        let is_plus = flags & ruleflag::CHILD_PLUS != 0;
2434        let has_span = self.options.gen_span_params;
2435        let (nu, nl, npl) = &self.nt_name[nt];
2436        if VERBOSE { println!("  - VAR {}, has {}value, flags: {}",
2437                              sym_nt.to_str(self.get_symbol_table()),
2438                              if has_value { "" } else { "no " },
2439                              ruleflag::to_string(flags).join(" | ")); }
2440
2441        let mut has_skel_init = false;
2442        let init_fn_name = format!("init_{npl}");
2443        if self.parent[nt].is_none() {
2444            if is_ll {
2445                init_nt_done.insert(var);
2446                if is_rrec_lform && is_ll {
2447                    span_init.insert(var);
2448                }
2449                if is_rrec_lform && has_value {
2450                    src_wrapper_impl.push(String::new());
2451                    src_listener_decl.push(format!("    fn {init_fn_name}(&mut self) -> {};", self.get_nt_type(nt as VarId)));
2452                    src_skel.push(format!("    fn {init_fn_name}(&mut self) -> {} {{", self.get_nt_type(nt as VarId)));
2453                    has_skel_init = true;
2454                    src_init.push(vec![format!("                    {nt} => self.init_{nl}(),"), nt_comment]);
2455                    src_wrapper_impl.push(format!("    fn {init_fn_name}(&mut self) {{"));
2456                    src_wrapper_impl.push(format!("        let val = self.listener.init_{nl}();"));
2457                    src_wrapper_impl.push(format!("        self.stack.push(EnumSynValue::{nu}(val));"));
2458                    src_wrapper_impl.push("    }".to_string());
2459                } else {
2460                    src_listener_decl.push(format!("    fn {init_fn_name}(&mut self) {{}}"));
2461                    src_init.push(vec![format!("                    {nt} => self.listener.{init_fn_name}(),"), nt_comment]);
2462                }
2463            }
2464        } else if flags & ruleflag::CHILD_REPEAT != 0 {
2465            if !is_sep_list && is_ll {
2466                span_init.insert(var);
2467            }
2468            // with LR code, we need the init() if there's a value or if there are span parameters
2469            if has_value || is_sep_list || (!is_ll && has_span) {
2470                let mut trailing_init = vec![];
2471                if has_span && !is_ll && !is_sep_list {
2472                    if is_plus {
2473                        let alts = self.var_alts[nt].iter().filter_map(|a| {
2474                            let alt_a = &self.alts[*a as usize].1;
2475                            if alt_a.first() != Some(&sym_nt) {
2476                                Some((*a, self.span_nbrs[*a as usize] - 1))
2477                            } else {
2478                                None
2479                            }
2480                        }).to_vec();
2481                        let n0 = &alts[0].1;
2482                        *init_needs_param = alts.iter().any(|(_, n)| n != n0);
2483                        if *init_needs_param {
2484                            trailing_init.push("        let n = match alt_id {".to_string());
2485                            trailing_init.extend(alts.iter().map(|(a, n)| format!("            {a} => {n},")));
2486                            trailing_init.push(format!(r#"            _ => panic!("alt_id = {{alt_id}} unexpected in method {init_fn_name}")"#));
2487                            trailing_init.push("        };".to_string());
2488                            trailing_init.push("        self.stack_span.insert(self.stack_span.len() - n, PosSpan::empty());".to_string());
2489                        } else {
2490                            trailing_init.push(format!("        self.stack_span.insert(self.stack_span.len() - {n0}, PosSpan::empty());"));
2491                        }
2492                    } else {
2493                        trailing_init.push("        self.stack_span.push(PosSpan::empty());".to_string());
2494                    }
2495                }
2496                let mut body_init = vec![];
2497                if is_lform {
2498                    if is_sep_list {
2499                        let all_exit_alts = if is_ambig_1st_child {
2500                            ambig_op_alts.values().rev().map(|v| v[0]).to_vec()
2501                        } else {
2502                            self.gather_alts(nt as VarId)
2503                        };
2504                        let exit_alts = all_exit_alts.into_iter()
2505                            .filter(|f|
2506                                (flags & ruleflag::CHILD_L_RECURSION == 0
2507                                    && flags & (ruleflag::CHILD_REPEAT_PLUS | ruleflag::L_FORM) != ruleflag::CHILD_REPEAT_LFORM)
2508                                || !self.is_alt_sym_empty(*f)
2509                            );
2510                        let (mut last_alt_ids, exit_info_alts): (Vec<AltId>, Vec<AltId>) = exit_alts.into_iter()
2511                            .partition(|i| self.alt_info[*i as usize].is_none());
2512                        let last_alt_id_maybe = if last_alt_ids.is_empty() { None } else { Some(last_alt_ids.remove(0)) };
2513                        let a = exit_info_alts[0];
2514                        let indent = "        ";
2515                        let (src_let, ctx_params) = Self::source_lets(&self.item_info[a as usize], &self.nt_name, indent, last_alt_id_maybe, "alt_id");
2516                        body_init.extend(src_let);
2517                        let ctx = if ctx_params.is_empty() {
2518                            format!("Ctx{nu}::{}", self.alt_info[a as usize].as_ref().unwrap().1)
2519                        } else {
2520                            format!("Ctx{nu}::{} {{ {ctx_params} }}", self.alt_info[a as usize].as_ref().unwrap().1)
2521                        };
2522                        body_init.push(format!("        let ctx = {ctx};"));
2523                        if has_span {
2524                            body_init.extend(Self::source_update_span(&self.span_nbrs_sep_list[&a].to_string(), false));
2525                        }
2526                        body_init.push(format!(
2527                            "        {}self.listener.{init_fn_name}();",
2528                            if has_value { "let mut val = " } else { "" }));
2529                        body_init.push(format!(
2530                            "        self.listener.exit_{npl}({}ctx{});",
2531                            if has_value { "&mut val, " } else { "" },
2532                            if has_span { ", spans" } else { "" }));
2533                        let ret = if has_value {
2534                            format!("-> {};", self.get_nt_type(nt as VarId))
2535                        } else {
2536                            "{}".to_string()
2537                        };
2538                        src_listener_decl.push(format!("    fn {init_fn_name}(&mut self) {ret}"));
2539
2540                        // skeleton (listener template)
2541                        let ret = if has_value { format!(" -> {}", self.get_nt_type(nt as VarId)) } else { String::new() };
2542                        src_skel.push(format!(
2543                            "    fn {init_fn_name}(&mut self){ret} {{"));
2544                        has_skel_init = true;
2545                    } else {
2546                        if has_value {
2547                            body_init.push(format!("        let val = self.listener.{init_fn_name}();"));
2548                            src_listener_decl.push(format!("    fn {init_fn_name}(&mut self) -> {};", self.get_nt_type(nt as VarId)));
2549                            src_skel.push(format!("    fn {init_fn_name}(&mut self) -> {} {{", self.get_nt_type(nt as VarId)));
2550                            has_skel_init = true;
2551                        } else {
2552                            src_listener_decl.push(format!("    fn {init_fn_name}(&mut self) {{}}"));
2553                            if has_span {
2554                                body_init.push(format!("        self.listener.{init_fn_name}();"));
2555                            }
2556                        }
2557                    }
2558                    if has_value {
2559                        body_init.push(format!("        self.stack.push(EnumSynValue::{nu}(val));"));
2560                    }
2561                } else if is_sep_list {
2562                    // fetch values from stack to init the list with the first value that was outside the repetition:
2563                    // first α in α (β α)*
2564                    let endpoints = self.child_repeat_endpoints.get(&var).unwrap();
2565                    let (src_val, val_name, src_span) = self.source_child_repeat_lets(
2566                        endpoints, &self.item_info, is_plus, &self.nt_name, &init_fn_name, nu, true, has_value, "alt_id");
2567                    if has_value {
2568                        body_init.extend(src_val);
2569                        body_init.push(format!("        self.stack.push(EnumSynValue::{nu}(Syn{nu}(vec![{val_name}])));"));
2570                    }
2571                    body_init.extend(src_span);
2572                } else {
2573                    if has_value {
2574                        body_init.push(format!("        let val = Syn{nu}(Vec::new());"));
2575                        body_init.push(format!("        self.stack.push(EnumSynValue::{nu}(val));"));
2576                    }
2577                }
2578                if !trailing_init.is_empty() || !body_init.is_empty() {
2579                    init_nt_done.insert(var);
2580                    src_init.push(vec![format!("                    {nt} => self.{init_fn_name}(),"), nt_comment]);
2581                    src_wrapper_impl.push(String::new());
2582                    src_wrapper_impl.push(format!("    fn {init_fn_name}(&mut self{}) {{", if *init_needs_param { ", alt_id: AltId" } else { "" }));
2583                    src_wrapper_impl.extend(body_init);
2584                    src_wrapper_impl.extend(trailing_init);
2585                    src_wrapper_impl.push("    }".to_string());
2586                }
2587            } else if is_lform {
2588                // !has_value, !is_sep_list, !(is_lr && has_span)
2589                if is_ll {
2590                    init_nt_done.insert(var);
2591                    src_init.push(vec![format!("                    {nt} => self.listener.{init_fn_name}(),"), nt_comment]);
2592                    src_listener_decl.push(format!("    fn {init_fn_name}(&mut self) {{}}"));
2593                } else {
2594                    src_listener_decl.push(format!("    fn {init_fn_name}(&mut self) {{}}"));
2595                }
2596            } else {
2597                // src_init.push(vec![format!("                    {nt} => {{}}"), nt_comment]);
2598            }
2599        } else {
2600            // src_init.push(vec![format!("                    {nt} => {{}}"), nt_comment]);
2601        }
2602        if has_skel_init {
2603            if has_value {
2604                src_skel.push(format!("        {}()", self.get_nt_type(nt as VarId)));
2605            }
2606            src_skel.push("    }".to_string());
2607            src_skel.push(String::new());
2608        }
2609    }
2610
2611    /// Adds the wrapper source code related to Call::Exit
2612    fn source_wrapper_exit<const VERBOSE: bool>(
2613        &self,
2614        ctx                 : &SourceInputContext,
2615        var                 : VarId,
2616        flags               : u32,
2617        has_value           : bool,
2618        is_ambig_1st_child  : bool,
2619        is_ambig_redundant  : bool,
2620        init_needs_param    : bool,
2621        state               : &mut SourceState,
2622        sources             : &mut WrapperSources
2623    ) {
2624        const MATCH_COMMENTS_SHOW_DESCRIPTIVE_ALTS: bool = false;
2625        const OPTIMIZE_IF_MATCH_ALT_ID: bool = true;
2626        let is_lr = self.options.parser_type.is_lr();
2627        let &SourceInputContext {
2628            parent_has_value, parent_nt, syns, ambig_op_alts
2629        } = ctx;
2630        let SourceState { nt_contexts, exit_alt_done, exit_fixer, .. } = state;
2631        let WrapperSources { src_listener_decl, src_skel, src_exit, src_wrapper_impl, .. } = sources;
2632        let nt = var as usize;
2633        let is_plus = flags & ruleflag::CHILD_PLUS != 0;
2634        let is_parent = nt == parent_nt;
2635        let is_child_repeat_lform = flags & ruleflag::CHILD_REPEAT_LFORM == ruleflag::CHILD_REPEAT_LFORM;
2636        let has_span = self.options.gen_span_params;
2637        let (nu, _nl, npl) = &self.nt_name[nt];
2638
2639        // handles most rules except children of left factorization (already taken by self.gather_alts)
2640        if !is_ambig_redundant && flags & ruleflag::CHILD_L_FACT == 0 {
2641            let mut has_skel_exit = false;
2642            let mut has_skel_exit_return = false;
2643            let is_child_plus = flags & ruleflag::CHILD_REPEAT_PLUS == ruleflag::CHILD_REPEAT_PLUS;
2644            let (pnu, _pnl, pnpl) = &self.nt_name[parent_nt];
2645            if VERBOSE { println!("    {nu} (parent {pnu})"); }
2646            let no_method = !has_value && !has_span && flags & ruleflag::CHILD_REPEAT_LFORM == ruleflag::CHILD_REPEAT;
2647            let is_rrec_lform = self.nt_has_all_flags(var, ruleflag::R_RECURSION | ruleflag::L_FORM);
2648            let (fnpl, fnu, fnt, f_valued) = if is_ambig_1st_child {
2649                (pnpl, pnu, parent_nt, parent_has_value)    // parent_nt doesn't come through this code, so we must do it now
2650            } else {
2651                (npl, nu, nt, has_value)
2652            };
2653            if is_parent || (is_child_repeat_lform && !no_method) || is_ambig_1st_child {
2654                let extra_param = if has_span { ", spans: Vec<PosSpan>" } else { "" };
2655                if f_valued {
2656                    let nt_type = self.get_nt_type(fnt as VarId);
2657                    if is_rrec_lform || (is_child_repeat_lform) {
2658                        src_listener_decl.push(format!("    fn exit_{fnpl}(&mut self, acc: &mut {nt_type}, ctx: Ctx{fnu}{extra_param});"));
2659                        src_skel.push(format!("    fn exit_{fnpl}(&mut self, acc: &mut {nt_type}, ctx: Ctx{fnu}{extra_param}) {{"));
2660                    } else {
2661                        src_listener_decl.push(format!("    fn exit_{fnpl}(&mut self, ctx: Ctx{fnu}{extra_param}) -> {nt_type};"));
2662                        src_skel.push(format!("    fn exit_{fnpl}(&mut self, ctx: Ctx{fnu}{extra_param}) -> {nt_type} {{"));
2663                        has_skel_exit_return = true;
2664                    }
2665                } else {
2666                    src_listener_decl.push("    #[allow(unused_variables)]".to_string());
2667                    src_listener_decl.push(format!("    fn exit_{fnpl}(&mut self, ctx: Ctx{fnu}{extra_param}) {{}}"));
2668                    src_skel.push(format!("    fn exit_{fnpl}(&mut self, ctx: Ctx{fnu}{extra_param}) {{"));
2669                }
2670                has_skel_exit = true;
2671            }
2672            let gathered_alt_ids = self.gather_alts(nt as VarId);
2673            let mut alt_id_name = "alt_id".to_string();
2674            let lr_init_alt_ids_maybe = if is_lr && is_child_plus {
2675                let mut var_fixer = NameFixer::new();
2676                var_fixer.extend(gathered_alt_ids.iter().flat_map(|a| self.item_info[*a as usize].iter().map(|ii| &ii.name)));
2677                alt_id_name = var_fixer.get_unique_name(alt_id_name);
2678                if has_value || has_span {
2679                    let match_ids = gathered_alt_ids.iter()
2680                        .filter(|&id| self.alts[*id as usize].1.first() != Some(&Symbol::NT(nt as VarId)))
2681                        .to_vec();
2682                    if OPTIMIZE_IF_MATCH_ALT_ID && match_ids.len() > 1 {
2683                        let is_even = match_ids[0].is_multiple_of(2);
2684                        Some(format!(
2685                            "        if {}{alt_id_name}.is_multiple_of(2) {{ self.init_{npl}({}); }} // {}",
2686                            if is_even { "" } else { "!" },
2687                            if init_needs_param { &alt_id_name } else { "" },
2688                            match_ids.into_iter().map(AltId::to_string).join(" | ")))
2689                    } else {
2690                        let pattern = match_ids.into_iter().map(|id| id.to_string()).join(" | ");
2691                        Some(format!(
2692                            "        if matches!({alt_id_name}, {pattern}) {{ self.init_{npl}({}); }}",
2693                            if init_needs_param { &alt_id_name } else { "" }))
2694                    }
2695                } else {
2696                    None
2697                }
2698            } else {
2699                None
2700            };
2701            let all_exit_alts = if is_ambig_1st_child {
2702                ambig_op_alts.values().rev().map(|v| v[0]).to_vec()
2703            } else {
2704                gathered_alt_ids
2705            };
2706            // last_it_alts: ε child lrec or ε child *<L>
2707            let (last_it_alts, exit_alts) = all_exit_alts.into_iter()
2708                .partition::<Vec<_>, _>(|f|
2709                    (flags & ruleflag::CHILD_L_RECURSION != 0
2710                        || flags & ruleflag::CHILD_REPEAT_PLUS_LFORM == ruleflag::CHILD_REPEAT_LFORM)
2711                    && self.is_alt_sym_empty(*f));
2712            if VERBOSE {
2713                println!("    no_method: {no_method}, exit alts: {}", exit_alts.iter().join(", "));
2714                if !last_it_alts.is_empty() {
2715                    println!("    last_it_alts: {}", last_it_alts.iter().join(", "));
2716                }
2717            }
2718
2719            // skeleton (listener template)
2720            if has_skel_exit {
2721                if let Some(alts) = &nt_contexts[fnt] {
2722                    let mut skel_ctx = vec![];
2723                    for &a_id in alts {
2724                        if let Some((_, variant)) = self.alt_info[a_id as usize].as_ref() {
2725                            let comment = self.full_alt_str(a_id, None, false);
2726                            let fields = self.source_infos(&self.item_info[a_id as usize], false, false);
2727                            let ctx_content = if fields.is_empty() {
2728                                String::new()
2729                            } else {
2730                                format!(" {{ {fields} }}")
2731                            };
2732                            skel_ctx.push((comment, variant, ctx_content));
2733                        }
2734                    }
2735                    match skel_ctx.len() {
2736                        0 => {}
2737                        1 => {
2738                            let (comment, variant, ctx_content) = skel_ctx.pop().unwrap();
2739                            src_skel.push(format!("        // {comment}"));
2740                            src_skel.push(format!("        let Ctx{fnu}::{variant}{ctx_content} = ctx;"));
2741                        }
2742                        _ => {
2743                            src_skel.push("        match ctx {".to_string());
2744                            for (comment, variant, ctx_content) in skel_ctx {
2745                                src_skel.push(format!("            // {comment}"));
2746                                src_skel.push(format!("            Ctx{fnu}::{variant}{ctx_content} => {{}}"));
2747                            }
2748                            src_skel.push("        }".to_string());
2749                        }
2750                    }
2751                    if has_skel_exit_return {
2752                        src_skel.push(format!("        {}()", self.get_nt_type(fnt as VarId)));
2753                    }
2754                    src_skel.push("    }".to_string());
2755                    src_skel.push(String::new());
2756                } else {
2757                    panic!("no alts for NT {fnpl} [{fnt}]");
2758                }
2759            }
2760
2761            for f in &exit_alts {
2762                exit_alt_done.insert(*f);
2763            }
2764            let inter_or_exit_name = if flags & ruleflag::PARENT_L_RECURSION != 0 { format!("inter_{npl}") } else { format!("exit_{npl}") };
2765            let fn_name = exit_fixer.get_unique_name(inter_or_exit_name.clone());
2766            let fn_init_name = format!("init_{npl}");
2767            let init_discarded = is_lr && is_plus && !has_value && !has_span;
2768            let (is_alt_id, choices) = self.make_match_choices(&exit_alts, &fn_name, &fn_init_name, flags, no_method, None, init_discarded);
2769            if VERBOSE { println!("    choices: {}", choices.iter().map(|s| s.trim()).join(" ")); }
2770            let comments = exit_alts.iter().map(|f| {
2771                let (v, pf) = &self.alts[*f as usize];
2772                if MATCH_COMMENTS_SHOW_DESCRIPTIVE_ALTS {
2773                    format!("// {}", self.full_alt_str(*f, None, false))
2774                } else {
2775                    format!("// {}", pf.to_rule_str(*v, self.get_symbol_table(), self.flags[*v as usize]))
2776                }
2777            }).to_vec();
2778            src_exit.extend(choices.into_iter().zip(comments).map(|(a, b)| vec![a, b]));
2779            if is_ambig_1st_child {
2780                for (a_id, dup_alts) in ambig_op_alts.values().rev().filter_map(|v| if v.len() > 1 { v.split_first() } else { None }) {
2781                    // note: is_alt_id must be true because we wouldn't get duplicate alternatives otherwise in an ambiguous rule
2782                    //       (it's duplicated to manage the priority between several alternatives, which are all in the first NT)
2783                    let (_, choices) = self.make_match_choices(dup_alts, &fn_name, &fn_init_name, 0, no_method, Some(*a_id), true);
2784                    let comments = dup_alts.iter()
2785                        .map(|a| {
2786                            let (v, alt) = &self.alts[*a as usize];
2787                            format!("// {} (duplicate of {a_id})", alt.to_rule_str(*v, self.get_symbol_table(), 0))
2788                        }).to_vec();
2789                    src_exit.extend(choices.into_iter().zip(comments).map(|(a, b)| vec![a, b]));
2790                    for a in dup_alts {
2791                        exit_alt_done.insert(*a);
2792                    }
2793                }
2794            }
2795            if !no_method {
2796                src_wrapper_impl.push(String::new());
2797                src_wrapper_impl.push(format!("    fn {fn_name}(&mut self{}) {{", if is_alt_id { format!(", {alt_id_name}: AltId") } else { String::new() }));
2798            }
2799            if flags & ruleflag::CHILD_REPEAT_LFORM == ruleflag::CHILD_REPEAT {
2800                if has_value || has_span {
2801                    let endpoints = self.child_repeat_endpoints.get(&var).unwrap();
2802                    let (src_val, val_name, src_span) = self.source_child_repeat_lets(
2803                        endpoints, &self.item_info, is_plus, &self.nt_name, &fn_name, nu, false, has_value, &alt_id_name);
2804                    src_wrapper_impl.extend(src_val);
2805                    let vec_name = if flags & ruleflag::SEP_LIST != 0 { "sep_list_acc" } else if is_plus { "plus_acc" } else { "star_acc" };
2806                    if let Some(lr_init_alt_ids) = lr_init_alt_ids_maybe { // has content only when has_value || has_span
2807                        src_wrapper_impl.push(lr_init_alt_ids);
2808                    }
2809                    if has_value {
2810                        src_wrapper_impl.push(format!("        let Some(EnumSynValue::{nu}(Syn{nu}({vec_name}))) = self.stack.last_mut() else {{"));
2811                        src_wrapper_impl.push(format!("            panic!(\"expected Syn{nu} item on wrapper stack\");"));
2812                        src_wrapper_impl.push("        };".to_string());
2813                        src_wrapper_impl.push(format!("        {vec_name}.push({val_name});"));
2814                    }
2815                    if has_span {
2816                        src_wrapper_impl.extend(src_span);
2817                    }
2818                }
2819            } else {
2820                assert!(!no_method, "no_method is not expected here (only used in +* with no lform)");
2821                let (mut last_alt_ids, exit_info_alts): (Vec<AltId>, Vec<AltId>) = exit_alts.into_iter()
2822                    .partition(|i| self.alt_info[*i as usize].is_none());
2823                let fnu = if is_child_repeat_lform { nu } else { pnu }; // +* <L> use the loop variable, the other alternatives use the parent
2824                let fnpl = if is_child_repeat_lform { npl } else { pnpl }; // +* <L> use the loop variable, the other alternatives use the parent
2825                let a_has_value = if is_child_repeat_lform { has_value } else { parent_has_value };
2826                let is_single = exit_info_alts.len() == 1;
2827                let indent = if is_single { "        " } else { "                " };
2828                if !is_single {
2829                    if has_span {
2830                        src_wrapper_impl.push(format!("        let (n, ctx) = match {alt_id_name} {{"));
2831                    } else {
2832                        src_wrapper_impl.push(format!("        let ctx = match {alt_id_name} {{"));
2833                    }
2834                }
2835                if VERBOSE { println!("    exit_alts -> {exit_info_alts:?}, last_alt_id -> {last_alt_ids:?}"); }
2836                let spans_param = if has_span { ", spans" } else { "" };
2837                for a in exit_info_alts {
2838                    if VERBOSE {
2839                        println!("    - ALTERNATIVE {a}: {} -> {}",
2840                                 Symbol::NT(var).to_str(self.get_symbol_table()),
2841                                 self.alts[a as usize].1.to_str(self.get_symbol_table()));
2842                    }
2843                    let last_alt_id_maybe = if last_alt_ids.is_empty() { None } else { Some(last_alt_ids.remove(0)) };
2844                    if !is_single {
2845                        let last_alt_choice = if let Some(last_alt_id) = last_alt_id_maybe { format!(" | {last_alt_id}") } else { String::new() };
2846                        src_wrapper_impl.push(format!("            {a}{last_alt_choice} => {{", ));
2847                    }
2848                    let (src_let, ctx_params) = Self::source_lets(&self.item_info[a as usize], &self.nt_name, indent, last_alt_id_maybe, &alt_id_name);
2849                    src_wrapper_impl.extend(src_let);
2850                    let ctx = if ctx_params.is_empty() {
2851                        format!("Ctx{fnu}::{}", self.alt_info[a as usize].as_ref().unwrap().1)
2852                    } else {
2853                        format!("Ctx{fnu}::{} {{ {ctx_params} }}", self.alt_info[a as usize].as_ref().unwrap().1)
2854                    };
2855                    if is_single {
2856                        src_wrapper_impl.push(format!("        let ctx = {ctx};"));
2857                        if let Some(lr_init_alt_ids) = &lr_init_alt_ids_maybe {
2858                            src_wrapper_impl.push(lr_init_alt_ids.to_string());
2859                        }
2860                        if has_span {
2861                            let is_drained = is_child_repeat_lform && flags & ruleflag::SEP_LIST != 0;
2862                            src_wrapper_impl.extend(Self::source_update_span(&self.span_nbrs[a as usize].to_string(), is_drained));
2863                            if is_drained {
2864                                src_wrapper_impl.push(format!("        spans.drain(..{});", self.span_nbrs[a as usize] - self.span_nbrs_sep_list[&a]));
2865                            }
2866                        }
2867                    } else {
2868                        let ctx_value = self.gen_match_item(ctx, || self.span_nbrs[a as usize].to_string(), true);
2869                        src_wrapper_impl.push(format!("{indent}{ctx_value}"));
2870                        src_wrapper_impl.push("            }".to_string());
2871                    }
2872                }
2873                if !is_single {
2874                    src_wrapper_impl.push(format!("            _ => panic!(\"unexpected alt id {{{alt_id_name}}} in method {fn_name}\")"));
2875                    src_wrapper_impl.push("        };".to_string());
2876                    if let Some(lr_init_alt_ids) = &lr_init_alt_ids_maybe {
2877                        src_wrapper_impl.push(lr_init_alt_ids.to_string());
2878                    }
2879                    if has_span {
2880                        src_wrapper_impl.extend(Self::source_update_span("n", false));
2881                    }
2882                }
2883                if (is_rrec_lform | is_child_repeat_lform) && f_valued {
2884                    src_wrapper_impl.push(
2885                        format!("        let Some(EnumSynValue::{fnu}(acc)) = self.stack.last_mut() else {{ panic!() }};"));
2886                    src_wrapper_impl.push(
2887                        format!("        self.listener.exit_{fnpl}(acc, ctx{spans_param});"));
2888                } else {
2889                    src_wrapper_impl.push(format!(
2890                        "        {}self.listener.exit_{fnpl}(ctx{spans_param});",
2891                        if a_has_value { "let val = " } else { "" }));
2892                    if a_has_value {
2893                        src_wrapper_impl.push(format!("        self.stack.push(EnumSynValue::{fnu}(val));"));
2894                    }
2895                }
2896            }
2897            if !no_method {
2898                src_wrapper_impl.push("    }".to_string());
2899            }
2900            for a in last_it_alts {
2901                assert_eq!(flags, self.flags[nt]);
2902                let (v, pf) = &self.alts[a as usize];
2903                let alt_str = if MATCH_COMMENTS_SHOW_DESCRIPTIVE_ALTS {
2904                    self.full_alt_str(a, None, false)
2905                } else {
2906                    pf.to_rule_str(*v, self.get_symbol_table(), self.flags[*v as usize])
2907                };
2908                if is_lr && is_child_repeat_lform {
2909                    if has_value | has_span {
2910                        src_exit.push(vec![format!("                    {a} => self.{fn_init_name}(),"), format!("// {alt_str}")]);
2911                    } else {
2912                        src_exit.push(vec![format!("                    {a} => self.listener.{fn_init_name}(),"), format!("// {alt_str}")]);
2913                    }
2914                    exit_alt_done.insert(a);
2915                } else {
2916                    // optional exitloop_<NT> used by lrec and child_* <L> to post-process the accumulator
2917                    // (rrec <L> and child_+ <L> don't need it because the context indicates the end alternative)
2918                    let owner_maybe = if flags & ruleflag::CHILD_REPEAT_LFORM == ruleflag::CHILD_REPEAT_LFORM {
2919                        Some(var)
2920                    } else if flags & ruleflag::CHILD_L_RECURSION != 0 {
2921                        self.parent[nt]
2922                    } else {
2923                        None
2924                    };
2925                    if let Some(owner) = owner_maybe {
2926                        if self.nt_values[owner as usize] {
2927                            let (variant, _, fnname) = &self.nt_name[owner as usize];
2928                            let typ = self.get_nt_type(owner);
2929                            let varname = if is_child_repeat_lform { "acc" } else { fnname };
2930                            if VERBOSE { println!("    exitloop{fnname}({varname}) owner = {}", Symbol::NT(owner).to_str(self.get_symbol_table())); }
2931                            src_listener_decl.push("    #[allow(unused_variables)]".to_string());
2932                            src_listener_decl.push(format!("    fn exitloop_{fnname}(&mut self, {varname}: &mut {typ}) {{}}"));
2933                            src_exit.push(vec![format!("                    {a} => self.exitloop_{fnpl}(),"), format!("// {alt_str}")]);
2934                            exit_alt_done.insert(a);
2935                            src_wrapper_impl.push(String::new());
2936                            src_wrapper_impl.push(format!("    fn exitloop_{fnpl}(&mut self) {{"));
2937                            src_wrapper_impl.push(format!("        let EnumSynValue::{variant}({varname}) = self.stack.last_mut().unwrap(){};",
2938                                                          if syns.len() > 1 { " else { panic!() }" } else { "" }));
2939                            src_wrapper_impl.push(format!("        self.listener.exitloop_{fnname}({varname});"));
2940                            src_wrapper_impl.push("    }".to_string());
2941                        } else {
2942                            src_listener_decl.push("    #[allow(unused_variables)]".to_string());
2943                            src_listener_decl.push(format!("    fn exitloop_{fnpl}(&mut self) {{}}"));
2944                            src_exit.push(vec![format!("                    {a} => self.listener.exitloop_{fnpl}(),"), format!("// {alt_str}")]);
2945                            exit_alt_done.insert(a);
2946                        }
2947                    }
2948                }
2949            }
2950        }
2951    }
2952
2953    fn source_wrapper_finalize(&mut self, span_init: HashSet<VarId>, sources: WrapperSources) -> (Vec<String>, Vec<String>, Vec<String>) {
2954        let WrapperSources { mut src, src_listener_decl, mut src_skel, mut src_types, src_init, src_exit, src_wrapper_impl } = sources;
2955
2956        // Writes the listener trait declaration
2957        src.add_space();
2958        if self.options.has_lr_error_recovery() {
2959            src.push("/// Result returned by [TestListener::get_recovery_value].".to_string());
2960            src.push("///".to_string());
2961            src.push("/// * [Abort](RecoveryNtValue::Abort): stops using the wrapper/listener".to_string());
2962            src.push("/// * [Skip](RecoveryNtValue::Skip): skips this nonterminal and tries to recover from a more global nonterminal".to_string());
2963            src.push("/// * [Value](RecoveryNtValue::Value): recovery nonterminal has been pushed, parsing resumes normally".to_string());
2964            src.push("pub enum RecoveryNtValue {".to_string());
2965            src.push("    /// Aborts the wrapper/listener. Tries to recover the parser and continue to parse without calling the wrapper/listener any more.".to_string());
2966            src.push("    Abort,".to_string());
2967            src.push("    /// Skips the recovery at this level. Tries to recover from another nonterminal.".to_string());
2968            src.push("    Skip,".to_string());
2969            src.push("    /// The recovery nonterminal has been pushed. The parser can continue to parse the stream normally.".to_string());
2970            src.push("    Value(EnumSynValue),".to_string());
2971            src.push("}".to_string());
2972            src.push(String::new());
2973        }
2974        src.push(format!("pub trait {}Listener {{", self.name));
2975        src.push("    /// Checks if the listener requests an abort. This happens if an error is too difficult to recover from".to_string());
2976        src.push("    /// and may corrupt the stack content. In that case, the parser immediately stops and returns `ParserError::AbortRequest`.".to_string());
2977        src.push("    fn check_abort_request(&self) -> Terminate { Terminate::None }".to_string());
2978        src.push("    fn get_log_mut(&mut self) -> &mut impl Logger;".to_string());
2979        src.push("    #[allow(unused_variables)]".to_string());
2980        src.push("    fn handle_msg(&mut self, span_opt: Option<&PosSpan>, msg: LogMsg) {".to_string());
2981        src.push("        self.get_log_mut().add(msg);".to_string());
2982        src.push("    }".to_string());
2983        // if self.options.gen_span_params {
2984        //     // for debug purpose
2985        //     src.push("    #[allow(unused_variables)]".to_string());
2986        //     src.push("    fn push_span(&mut self, span: &PosSpan) {}".to_string());
2987        // }
2988        if self.options.has_lr_error_recovery() {
2989            src.push("    #[allow(unused_variables)]".to_string());
2990            src.push("    fn drop_nt_value(&mut self, value: &EnumSynValue) {}".to_string());
2991            src.push("    #[allow(unused_variables)]".to_string());
2992            src.push("    fn get_recovery_value(&mut self, nt: VarId, last_dropped: Option<EnumSynValue>, err_span: &PosSpan) -> RecoveryNtValue { RecoveryNtValue::Abort }".to_string());
2993            src.push("    fn syntax_error_recovered(&mut self) {}".to_string());
2994        }
2995        let extra_span = if self.options.gen_span_params { ", span: PosSpan" } else { "" };
2996        let extra_ref_span = if self.options.gen_span_params { ", span: &PosSpan" } else { "" };
2997        if !self.terminal_hooks.is_empty() {
2998            src.push("    #[allow(unused_variables)]".to_string());
2999            src.push(format!("    fn hook(&mut self, token: TokenId, text: &str{extra_ref_span}) -> TokenId {{ token }}"));
3000        }
3001        src.push("    #[allow(unused_variables)]".to_string());
3002        src.push(format!("    fn intercept_token(&mut self, token: TokenId, text: &str{extra_ref_span}) -> TokenId {{ token }}"));
3003        if self.nt_values[self.start as usize] || self.options.gen_span_params {
3004            src.push("    #[allow(unused_variables)]".to_string());
3005        }
3006        if self.nt_values[self.start as usize] {
3007            src.push(format!("    fn exit(&mut self, {}: {}{extra_span}) {{}}", self.nt_name[self.start as usize].2, self.get_nt_type(self.start)));
3008        } else {
3009            src.push(format!("    fn exit(&mut self{extra_span}) {{}}"));
3010        }
3011        src.push("    #[allow(unused_variables)]".to_string());
3012        src.push("    fn abort(&mut self, terminate: Terminate) {}".to_string());
3013        /*
3014                              fn init_a(&mut self) {}
3015                              fn exit_a(&mut self, ctx: CtxA, spans: Vec<PosSpan>) -> SynA;
3016                              fn init_a_iter(&mut self) -> SynAIter;
3017                              #[allow(unused_variables)]
3018                              fn exit_a_iter(&mut self, ctx: CtxAIter) -> SynAIter {};
3019        */
3020        src.extend(src_listener_decl);
3021        src.push("}".to_string());
3022
3023        // Writes the switch() function
3024        src.push(String::new());
3025        src.push("pub struct Wrapper<T> {".to_string());
3026        src.push("    verbose: bool,".to_string());
3027        src.push("    listener: T,".to_string());
3028        src.push("    stack: Vec<EnumSynValue>,".to_string());
3029        src.push("    max_stack: usize,".to_string());
3030        src.push("    stack_t: Vec<String>,".to_string());
3031        if self.options.gen_span_params {
3032            src.push("    stack_span: Vec<PosSpan>,".to_string());
3033        }
3034        if self.options.has_lr_error_recovery() {
3035            src.push("    last_dropped_nt_value: Option<EnumSynValue>,".to_string());
3036        }
3037        src.push("}".to_string());
3038        src.push(String::new());
3039        src.push(format!("impl<T: {}Listener> ListenerWrapper for Wrapper<T> {{", self.name));
3040        src.push("    fn switch(&mut self, call: Call, nt: VarId, alt_id: AltId, t_data: Option<Vec<String>>) {".to_string());
3041        src.push("        if self.verbose {".to_string());
3042        src.push("            println!(\"switch: call={call:?}, nt={nt}, alt={alt_id}, t_data={t_data:?}\");".to_string());
3043        src.push("        }".to_string());
3044        src.push("        if let Some(mut t_data) = t_data {".to_string());
3045        src.push("            self.stack_t.append(&mut t_data);".to_string());
3046        src.push("        }".to_string());
3047        src.push("        match call {".to_string());
3048        if self.options.parser_type.is_ll() {
3049            src.push("            Call::Enter => {".to_string());
3050            if self.options.gen_span_params {
3051                // adds span accumulator inits, using Segments to regroup them
3052                let mut seg_span = Segments::from_iter(span_init.into_iter().map(|v| Seg(v as u32, v as u32)));
3053                seg_span.normalize();
3054                let pattern = seg_span.into_iter().map(|Seg(a, b)| {
3055                    if a == b {
3056                        a.to_string()
3057                    } else if b == a + 1 {
3058                        format!("{a} | {b}")
3059                    } else {
3060                        format!("{a} ..= {b}")
3061                    }
3062                }).join(" | ");
3063                if !pattern.is_empty() {
3064                    src.push(format!("                if matches!(nt, {pattern}) {{"));
3065                    src.push("                    self.stack_span.push(PosSpan::empty());".to_string());
3066                    src.push("                }".to_string());
3067                }
3068            }
3069            src.push("                match nt {".to_string());
3070            /*
3071                                                  0 => self.listener.init_a(),                // A
3072                                                  1 => self.init_a_iter(),                    // AIter1
3073                                                  2 => {}                                     // A_1
3074            */
3075            src.extend(columns_to_str(src_init, Some(vec![64, 0])));
3076            src.push("                    _ => panic!(\"unexpected enter nonterminal id: {nt}\")".to_string());
3077            src.push("                }".to_string());
3078            src.push("            }".to_string());
3079            src.push("            Call::Loop => {}".to_string());
3080        }
3081        src.push("            Call::Exit => {".to_string());
3082        src.push("                match alt_id {".to_string());
3083        /*
3084                                              3 |                                         // A -> a a (b <L>)* c
3085                                              4 => self.exit_a(alt_id),                   // A -> a c (b <L>)* c
3086                                              1 => self.exit_a_iter(),                    // (b <L>)* iteration in A -> a a  ► (b <L>)* ◄  c | ...
3087                                              2 => {}                                     // end of (b <L>)* iterations in A -> a a  ► (b <L>)* ◄  c | ...
3088                                           /* 0 */                                        // A -> a a (b <L>)* c | a c (b <L>)* c (never called)
3089        */
3090        src.extend(columns_to_str(src_exit, Some(vec![64, 0])));
3091        src.push("                    _ => panic!(\"unexpected exit alternative id: {alt_id}\")".to_string());
3092        src.push("                }".to_string());
3093        src.push("            }".to_string());
3094        src.push("            Call::End(terminate) => {".to_string());
3095        src.push("                match terminate {".to_string());
3096        src.push("                    Terminate::None => {".to_string());
3097        let mut args = vec![];
3098        let (_nu, _nl, npl) = &self.nt_name[self.start as usize];
3099        if self.nt_values[self.start as usize] {
3100            src.push(format!("                        let val = self.stack.pop().unwrap().get_{npl}();"));
3101            args.push("val");
3102        }
3103        if self.options.gen_span_params {
3104            src.push("                        let span = self.stack_span.pop().unwrap();".to_string());
3105            args.push("span");
3106        }
3107        src.push(format!("                        self.listener.exit({});", args.join(", ")));
3108        src.push("                    }".to_string());
3109        src.push("                    Terminate::Abort | Terminate::Conclude => self.listener.abort(terminate),".to_string());
3110        src.push("                }".to_string());
3111        src.push("            }".to_string());
3112        if !self.options.parser_type.is_ll() {
3113            src.push(r#"            _ => panic!("unexpected call {call:?}, nt {nt}, alt_id {alt_id}")"#.to_string());
3114        }
3115        src.push("        }".to_string());
3116        src.push("        self.max_stack = std::cmp::max(self.max_stack, self.stack.len());".to_string());
3117        src.push("        if self.verbose {".to_string());
3118        src.push(r#"            println!("{}", self.get_status().join("\n"));"#.to_string());
3119        src.push("        }".to_string());
3120        src.push("    }".to_string());
3121        src.push(String::new());
3122        src.push("    fn check_abort_request(&self) -> Terminate {".to_string());
3123        src.push("        self.listener.check_abort_request()".to_string());
3124        src.push("    }".to_string());
3125        src.push(String::new());
3126        src.push("    fn abort(&mut self) {".to_string());
3127        src.push("        self.stack.clear();".to_string());
3128        if self.options.gen_span_params {
3129            src.push("        self.stack_span.clear();".to_string());
3130        }
3131        src.push("        self.stack_t.clear();".to_string());
3132        src.push("    }".to_string());
3133        src.push(String::new());
3134        src.push("    fn get_log_mut(&mut self) -> &mut impl Logger {".to_string());
3135        src.push("        self.listener.get_log_mut()".to_string());
3136        src.push("    }".to_string());
3137        src.push(String::new());
3138        src.push("    fn report(&mut self, span_opt: Option<&PosSpan>, msg: LogMsg) {".to_string());
3139        src.push("        self.listener.handle_msg(span_opt, msg);".to_string());
3140        src.push("    }".to_string());
3141        src.push(String::new());
3142        src.push("    fn is_stack_empty(&self) -> bool {".to_string());
3143        src.push("        self.stack.is_empty()".to_string());
3144        src.push("    }".to_string());
3145        src.push(String::new());
3146        src.push("    fn is_stack_t_empty(&self) -> bool {".to_string());
3147        src.push("        self.stack_t.is_empty()".to_string());
3148        src.push("    }".to_string());
3149        if self.options.gen_span_params {
3150            src.add_space();
3151            src.push("    fn is_stack_span_empty(&self) -> bool {".to_string());
3152            src.push("        self.stack_span.is_empty()".to_string());
3153            src.push("    }".to_string());
3154        }
3155        let unused_span = if self.options.gen_span_params { "" } else { "_" };
3156        let extra_span_arg = if self.options.gen_span_params { ", span" } else { "" };
3157        if !self.terminal_hooks.is_empty() {
3158            src.add_space();
3159            src.push(format!("    fn hook(&mut self, token: TokenId, text: &str, {unused_span}span: &PosSpan) -> TokenId {{"));
3160            src.push(format!("        self.listener.hook(token, text{extra_span_arg})"));
3161            src.push("    }".to_string());
3162        }
3163        src.add_space();
3164        src.push(format!("    fn intercept_token(&mut self, token: TokenId, text: &str, {unused_span}span: &PosSpan) -> TokenId {{"));
3165        src.push(format!("        self.listener.intercept_token(token, text{extra_span_arg})"));
3166        src.push("    }".to_string());
3167        src.push(String::new());
3168        src.push(r#"    fn get_status(&self) -> Vec<String> {"#.to_string());
3169        src.push(r#"        vec!["#.to_string());
3170        src.push(r#"            format!("> stack_t:    [{}]", self.stack_t.join(", ")),"#.to_string());
3171        src.push(r#"            format!("> stack:      [{}]", self.stack.iter().map(|it| format!("{it:?}")).collect::<Vec<_>>().join(", ")),"#.to_string());
3172        if self.options.gen_span_params {
3173            src.push(r#"            format!("> stack_span: [{}]", self.stack_span.iter().map(PosSpan::to_string).collect::<Vec<_>>().join(", ")),"#.to_string());
3174        }
3175        src.push(r#"        ]"#.to_string());
3176        src.push(r#"    }"#.to_string());
3177        if self.options.gen_span_params {
3178            src.push(String::new());
3179            src.push("    fn push_span(&mut self, span: PosSpan) {".to_string());
3180            // src.push("        self.listener.push_span(&span);".to_string()); // for debug purpose
3181            src.push("        self.stack_span.push(span);".to_string());
3182            src.push("    }".to_string());
3183            src.push(String::new());
3184            src.push("    fn pop_span(&mut self) -> PosSpan {".to_string());
3185            src.push("        self.stack_span.pop().unwrap()".to_string());
3186            src.push("    }".to_string());
3187        }
3188        src.push("}".to_string());
3189        if self.options.has_lr_error_recovery() {
3190            src.push(String::new());
3191            src.push(format!("impl<T: {}Listener> WrapperLRErrorRecovery for Wrapper<T> {{", self.name));
3192            src.push(r"    fn pop_nt_value(&mut self) {".to_string());
3193            src.push(r"        self.last_dropped_nt_value = self.stack.pop();".to_string());
3194            src.push(r#"        if self.verbose { println!("dropped {:?} value", self.last_dropped_nt_value.as_ref().unwrap()); }"#.to_string());
3195            src.push(r"        self.listener.drop_nt_value(self.last_dropped_nt_value.as_ref().unwrap());".to_string());
3196            src.push(r"    }".to_string());
3197            src.push(String::new());
3198            src.push(r"    fn push_nt_recovery_value(&mut self, nt: VarId, err_span: &PosSpan) -> RecoveryNt {".to_string());
3199            src.push(r"        match self.listener.get_recovery_value(nt, self.last_dropped_nt_value.take(), err_span) {".to_string());
3200            src.push(r"            RecoveryNtValue::Abort => RecoveryNt::Abort,".to_string());
3201            src.push(r"            RecoveryNtValue::Skip => RecoveryNt::Skip,".to_string());
3202            src.push(r"            RecoveryNtValue::Value(val) => {".to_string());
3203            src.push(r"                self.stack.push(val);".to_string());
3204            src.push(r"                RecoveryNt::Done".to_string());
3205            src.push(r"            }".to_string());
3206            src.push(r"        }".to_string());
3207            src.push(r"    }".to_string());
3208            src.push(String::new());
3209            src.push(r"    fn syntax_error_recovered(&mut self) {".to_string());
3210            src.push(r"        self.listener.syntax_error_recovered();".to_string());
3211            src.push(r"    }".to_string());
3212            src.push("}".to_string());
3213        }
3214        src.add_space();
3215        src.push(format!("impl<T: {}Listener> Wrapper<T> {{", self.name));
3216        src.push("    pub fn new(listener: T, verbose: bool) -> Self {".to_string());
3217        src.push(format!(
3218            "        Wrapper {{ verbose, listener, stack: Vec::new(), max_stack: 0, stack_t: Vec::new(){}{} }}",
3219            if self.options.gen_span_params { ", stack_span: Vec::new()" } else { "" },
3220            if self.options.has_lr_error_recovery() { ", last_dropped_nt_value: None" } else { "" }
3221        ));
3222        src.push("    }".to_string());
3223        src.push(String::new());
3224        src.push("    pub fn get_listener(&self) -> &T {".to_string());
3225        src.push("        &self.listener".to_string());
3226        src.push("    }".to_string());
3227        src.push(String::new());
3228        src.push("    pub fn get_listener_mut(&mut self) -> &mut T {".to_string());
3229        src.push("        &mut self.listener".to_string());
3230        src.push("    }".to_string());
3231        src.push(String::new());
3232        src.push("    pub fn give_listener(self) -> T {".to_string());
3233        src.push("        self.listener".to_string());
3234        src.push("    }".to_string());
3235        src.push(String::new());
3236        src.push("    pub fn set_verbose(&mut self, verbose: bool) {".to_string());
3237        src.push("        self.verbose = verbose;".to_string());
3238        src.push("    }".to_string());
3239/*
3240                              impl<T: TestListener> ListenerWrapper<T> {
3241                                  fn exit(&mut self) {
3242                                      let a = self.stack.pop().unwrap().get_a();
3243                                      self.listener.exit(Ctx::A { a });
3244                                  }
3245                                  fn init_a_iter(&mut self) {
3246                                      let val = self.listener.init_a_iter();
3247                                      self.stack.push(EnumSynValue::AIter(val));
3248                                  }
3249                                  fn exit_a_iter(&mut self) {
3250                                      let b = self.stack_t.pop().unwrap();
3251                                      let star_acc = self.stack.pop().unwrap().get_a_iter();
3252                                      let val = self.listener.exit_a_iter(CtxAIter::Aiter1 { star_acc, b });
3253                                      self.stack.push(EnumSynValue::AIter(val));
3254                                  }
3255                                  // ...
3256                              }
3257*/
3258        src.extend(src_wrapper_impl);
3259        src.push("}".to_string());
3260
3261        // templates
3262        src_types.extend(vec![
3263            String::new(),
3264            format!("// {:-<80}", ""),
3265        ]);
3266        if let Some(line) = src_skel.last() {
3267            if line.is_empty() {
3268                src_skel.pop();
3269            }
3270        }
3271        src_skel.extend(vec![
3272            "}".to_string(),
3273            String::new(),
3274            format!("// {:-<80}", ""),
3275        ]);
3276        self.log.add_info(format!("Template for the user types:\n\n{}\n", src_types.join("\n")));
3277        self.log.add_info(format!("Template for the listener implementation:\n\n{}\n", src_skel.join("\n")));
3278
3279        (src, src_types, src_skel)
3280    }
3281}
3282
3283impl LogReader for ParserGen {
3284    type Item = BufLog;
3285
3286    fn get_log(&self) -> &Self::Item {
3287        &self.log
3288    }
3289
3290    fn give_log(self) -> Self::Item {
3291        self.log
3292    }
3293}
3294
3295impl HasBuildErrorSource for ParserGen {
3296    const SOURCE: BuildErrorSource = BuildErrorSource::ParserGen;
3297}
3298
3299impl Default for ParserGenOptions {
3300    fn default() -> Self {
3301        ParserGenOptions {
3302            nt_value: NTValue::Default,
3303            include_alts: false,
3304            headers: vec![],
3305            used_libs: StructLibs::new(),
3306            gen_wrapper: true,
3307            gen_span_params: false,
3308            gen_token_enums: false,
3309            lib_crate: LexigramCrate::Core,
3310            indent: 0,
3311            types_indent: 0,
3312            listener_indent: 0,
3313            parser_type: ParserType::LL1,
3314        }
3315    }
3316}
3317
3318// ---------------------------------------------------------------------------------------------
3319// Supporting functions
3320
3321impl ParserGen {
3322
3323    pub fn get_nt_tree(&self) -> VecTree<VarId> {
3324        let mut tree = VecTree::new();
3325        let root = tree.add_root(0);
3326        let mut idx = HashMap::new();
3327        for group in self.nt_parent.iter().filter(|vf| !vf.is_empty()) {
3328            idx.clear();
3329            // some parents are later in the group, so we can't do this:
3330            // for &c in group {
3331            //     let idx_parent = self.parsing_table
3332            //         .parent[c as usize]
3333            //         .map(|v| idx.get(&v).unwrap())
3334            //         .unwrap_or(&root);
3335            //     let idx_c = tree.add(Some(*idx_parent), c);
3336            //     idx.insert(c, idx_c);
3337            // }
3338            let tree_ids = tree.add_iter(None, group.iter().cloned()).to_vec();
3339            idx.extend(group.iter().zip(tree_ids));
3340            for &child in group.iter() {
3341                tree.attach_child(
3342                    self.parent[child as usize]
3343                        .map(|p| idx[&p])
3344                        .unwrap_or(root),
3345                    idx[&child]);
3346            }
3347        }
3348        tree
3349    }
3350
3351    pub fn get_indented_nt(&self) -> Vec<(VarId, String)>{
3352        let tree = self.get_nt_tree();
3353        let mut indented = vec![];
3354        let mut indent = vec![];
3355        for node in tree.iter_pre_depth_simple().skip(1) {
3356            let depth = node.depth as usize;
3357            if indent.len() < depth {
3358                indent.push((1..depth).map(|i| if i & 1 == 0 { "  " } else { ". " }).join(""));
3359            }
3360            indented.push((*node, format!("{}{}", indent[depth - 1], Symbol::NT(*node).to_str(self.get_symbol_table()))));
3361        }
3362        indented
3363    }
3364
3365    pub fn nt_info_str(&self) -> Vec<String> {
3366        let indented = self.get_indented_nt();
3367        let mut cols = vec![
3368            vec!["  NT".to_string(), "  name".to_string(), " val".to_string(), "  flags".to_string(), String::new()]];
3369        for (v, line) in indented {
3370            let nt = v as usize;
3371            cols.push(vec![
3372                format!("| {v:3}"),
3373                format!("| {line}"),
3374                if self.nt_values[nt] { "| y".to_string() } else { "|".to_string() },
3375                format!("| {}", ruleflag::to_string(self.flags[nt]).join(", ")),
3376                "|".to_string(),
3377            ]);
3378        }
3379        let mut txt = columns_to_str(cols, Some(vec![3, 5, 0, /*0,*/ 0, 0]));
3380        if let Some(max) = txt.get(1).map(|s| s.charlen()) {
3381            let sep = format!("+{:-<1$}+", "", max - 2);
3382            txt.insert(1, sep.clone());
3383            txt.push(sep);
3384        }
3385        txt
3386    }
3387
3388    pub fn log_nt_info(&mut self) {
3389        let mut txt = self.nt_info_str();
3390        txt.push(String::new());
3391        self.log.add_info("nonterminal information:");
3392        self.log.extend_messages(txt.into_iter().map(LogMsg::Info));
3393    }
3394
3395    pub fn alt_info_str(&self) -> Vec<String> {
3396        let indented = self.get_indented_nt();
3397        let mut cols = vec![
3398            vec!["  NT".to_string(), "  alt".to_string(), "  opcodes".to_string(), " spans".to_string(), "  item_ops".to_string(), String::new()]];
3399        for (v, line) in indented {
3400            let nt = v as usize;
3401            for &alt_id in &self.var_alts[nt] {
3402                let a_id = alt_id as usize;
3403                let alt = &self.alts[a_id].1;
3404                let opcodes = self.opcodes[a_id].iter().map(|o| o.to_str_quote(self.get_symbol_table())).join(" ");
3405                let item_ops = self.item_ops.get(a_id)
3406                    .map(|ops| ops.iter().map(|s| s.to_str(self.get_symbol_table())).join(" "))
3407                    .unwrap_or_else(|| "-".to_string());
3408                cols.push(vec![
3409                    format!("| {v:3}"),
3410                    format!("| {alt_id:4}: {line} -> {}", alt.to_str(self.get_symbol_table())),
3411                    format!("| {opcodes}"),
3412                    format!("| {}{}",
3413                        &self.span_nbrs[a_id],
3414                        if let Some(ispan) = self.span_nbrs_sep_list.get(&alt_id) { format!(", {ispan}") } else { String::new() }),
3415                    format!("| {item_ops}"),
3416                    "|".to_string(),
3417                ]);
3418            }
3419        }
3420        let mut txt = columns_to_str(cols, Some(vec![3, 5, 0, 0, 0, 0]));
3421        if let Some(max) = txt.get(1).map(|s| s.charlen()) {
3422            let sep = format!("+{:-<1$}+", "", max - 2);
3423            txt.insert(1, sep.clone());
3424            txt.push(sep);
3425        }
3426        txt
3427    }
3428
3429    pub fn log_alt_info(&mut self) {
3430        let mut txt = self.alt_info_str();
3431        txt.push("legend: ►nt = enter nonterminal nt, ◄0 = exit alt, ●nt = loop nonterminal, Xyz! = variable terminal, \"…\" = fixed terminal, ▲ = hook".to_string());
3432        txt.push(String::new());
3433        self.log.add_note("rule alternatives:");
3434        self.log.extend_messages(txt.into_iter().map(LogMsg::Info));
3435    }
3436
3437    pub fn print_items(&self, indent: usize, show_symbols: bool) {
3438        let tbl = self.get_symbol_table();
3439        let fields = (0..self.alts.len())
3440            .map(|a| {
3441                let a_id = a as AltId;
3442                let (v, alt) = &self.alts[a];
3443                let ops = &self.opcodes[a];
3444                let it = &self.item_ops[a_id as usize];
3445                let mut cols = vec![];
3446                if show_symbols {
3447                    cols.push(format!("(strip![{}],", ops.iter().map(|o| o.to_macro_item()).join(", ")));
3448                    let symbols = format!("symbols![{}]", it.iter().map(|s| s.to_macro_item()).join(", "));
3449                    // assert!(self.options.gen_span_params, "ParserGen is not configured for spans");
3450                    cols.push(format!("{}, {symbols}),", self.span_nbrs[a_id as usize]));
3451                }
3452                cols.extend([
3453                    format!("// {a_id:2}: {} -> {}", Symbol::NT(*v).to_str(tbl), alt.iter().map(|s| s.to_str_quote(tbl)).join(" ")),
3454                    format!("| {}", ops.iter().map(|s| s.to_str_quote(tbl)).join(" ")),
3455                    format!(
3456                        "| {}{}",
3457                        self.span_nbrs[a_id as usize],
3458                        if let Some(ispan) = self.span_nbrs_sep_list.get(&a_id) { format!(", {ispan}") } else { String::new() }),
3459                    format!("| {}", it.iter().map(|s| s.to_str(tbl)).join(" ")),
3460                ]);
3461                cols
3462            }).to_vec();
3463        let widths = if show_symbols { vec![40, 0, 0, 0, 0, 0] } else { vec![16, 0, 0, 0, 0, 0] };
3464        for l in columns_to_str(fields, Some(widths)) {
3465            println!("{:indent$}{l}", "", indent = indent)
3466        }
3467    }
3468
3469    pub fn print_flags(&self, indent: usize) {
3470        let tbl: Option<&SymbolTable> = self.get_symbol_table();
3471        let prefix = format!("{:width$}//", "", width = indent);
3472        let nt_flags = self.flags.iter().index().filter_map(|(nt, &f)|
3473            if f != 0 { Some(format!("{prefix}  - {}: {} ({})", Symbol::NT(nt).to_str(tbl), ruleflag::to_string(f).join(" | "), f)) } else { None }
3474        ).join("\n");
3475        let parents = self.parent.iter().index().filter_map(|(c, &par)|
3476            par.map(|p| format!("{prefix}  - {} -> {}", Symbol::NT(c).to_str(tbl), Symbol::NT(p).to_str(tbl)))
3477        ).join("\n");
3478        if !nt_flags.is_empty() {
3479            println!("{prefix} NT flags:\n{nt_flags}");
3480        }
3481        if !parents.is_empty() {
3482            println!("{prefix} parents:\n{parents}");
3483        }
3484    }
3485}