regen/
grammar.rs

1#![allow(dead_code)]
2#![cfg_attr(rustfmt, rustfmt_skip)]
3/*
4  Generated with regen-lang
5*/
6//// /* src/grammar.rs.pp */
7//! Generated static implementation of the Regen language, generated by Regen itself
8//! 
9//! This module should only depend on the sdk module.
10
11/* 
12 This is the preprocessor file for generated.rs.
13
14 In regen grammar files, you can specify a literal string appended with ";" as a statement to include another file
15 The path is relative to the grammar file. This is useful for including import for hook and others
16
17 Included files will always be at the beginning of the generated file.
18*/
19
20// Hooks
21// Since hooks are defined in the grammar file as literal strings, we need to include the implementation here.
22use crate::core::{RuleValue, Param, Hook, LangBuilder};
23use crate::core::rule::{parse_rule, parse_rule_value};
24use crate::core::param::parse_param;
25use crate::core::hook::parse_hook;
26use crate::core::token::{parse_token_def, parse_token_ignore_rule, parse_token_rule};
27use crate::core::{parse_context, parse_semantic, parse_include};
28
29//// /* src/grammar.rs.pp */
30crate::sdk!(
31  context: LangBuilder;
32  target: TopLevelStatement;
33  tokens: [
34    TKeyword,
35    TIdentifier,
36    TRegExp,
37    TLiteral,
38    TSymbol,
39    TComment,
40  ];
41  rules: [
42    lex::Rule::Regex(Regex::new(r"^\s+").unwrap(),lex::Target::Ignore),
43    lex::Rule::Regex(Regex::new(r"^//[^\n]*\n?").unwrap(), lex::Target::Extract(Tok::TComment)),
44    lex::Rule::Regex(Regex::new(r"^/\*([^\*]|(\*[^/]))*\*/").unwrap(), lex::Target::Extract(Tok::TComment)),
45    lex::Rule::Regex(Regex::new(r#"^"((\\.)|[^\\"])*""#).unwrap(), lex::Target::Keep(Tok::TLiteral)),
46    lex::Rule::Regex(Regex::new(r"^/((\\.)|[^\\/])*/").unwrap(), lex::Target::Keep(Tok::TRegExp)),
47    lex::Rule::Literal("ignore".to_owned(), lex::Target::Keep(Tok::TKeyword)), 
48    lex::Rule::Literal("extract".to_owned(), lex::Target::Keep(Tok::TKeyword)), 
49    lex::Rule::Literal("token".to_owned(), lex::Target::Keep(Tok::TKeyword)), 
50    lex::Rule::Literal("semantic".to_owned(), lex::Target::Keep(Tok::TKeyword)), 
51    lex::Rule::Literal("rule".to_owned(), lex::Target::Keep(Tok::TKeyword)), 
52    lex::Rule::Literal("optional".to_owned(), lex::Target::Keep(Tok::TKeyword)), 
53    lex::Rule::Literal("context".to_owned(), lex::Target::Keep(Tok::TKeyword)), 
54    lex::Rule::Regex(Regex::new(r"^[{};|()=,:\.\[\]\+]").unwrap(), lex::Target::Keep(Tok::TSymbol)),
55    lex::Rule::Regex(Regex::new(r"^[_a-zA-Z]\w*").unwrap(), lex::Target::Keep(Tok::TIdentifier)),
56  ];
57  semantics: [
58    SVariable,
59    SToken,
60    SSemantic,
61    SRule,
62    SHookName,
63    SHookType,
64    SContextType,
65  ];
66);
67pub mod ast {
68  use super::*;
69  #[derive(Debug)] pub struct DefineContextStatement {
70    pub m_0: Token,
71    pub m_context_type: Token,
72  }
73  #[derive(Debug)] pub struct DefineIgnoreTokenRuleStatement {
74    pub m_0: Token,
75    pub m_value: Box<LiteralOrRegExp>,
76  }
77  #[derive(Debug)] pub struct DefineIncludeStatement {
78    pub m_path: Token,
79  }
80  #[derive(Debug)] pub struct DefineRuleStatement {
81    pub m_0: Token,
82    pub m_hook_attr: Option<Box<HookAttribute>>,
83    pub m_rule_name: Token,
84    pub m_body: Box<RuleDefineBody>,
85  }
86  #[derive(Debug)] pub struct DefineSemanticStatement {
87    pub m_0: Token,
88    pub m_id: Token,
89  }
90  #[derive(Debug)] pub struct DefineTokenRuleStatement {
91    pub m_token_type: Token,
92    pub m_value: Box<LiteralOrRegExp>,
93  }
94  #[derive(Debug)] pub struct DefineTokenTypeStatement {
95    pub m_kw_extract: Option<Token>,
96    pub m_1: Token,
97    pub m_token_type: Token,
98  }
99  #[derive(Debug)] pub struct FunctionalRuleBody {
100    pub m_0: Token,
101    pub m_first_param: Option<Box<Parameter>>,
102    pub m_rest_params: Vec<ParamListTail>,
103    pub m_3: Token,
104  }
105  #[derive(Debug)] pub struct HookAttribute {
106    pub m_0: Token,
107    pub m_hook_name: Token,
108    pub m_2: Token,
109    pub m_hook_type: Token,
110    pub m_4: Token,
111  }
112  #[derive(Debug)] pub enum LiteralOrRegExp {
113    TokenLiteral(Box<TokenLiteral>),
114    TokenRegExp(Box<TokenRegExp>),
115  }
116  #[derive(Debug)] pub struct ParamListTail {
117    pub m_0: Token,
118    pub m_p: Box<Parameter>,
119  }
120  #[derive(Debug)] pub struct ParamSemantic {
121    pub m_0: Token,
122    pub m_semantic_name: Option<Token>,
123    pub m_2: Token,
124  }
125  #[derive(Debug)] pub struct Parameter {
126    pub m_sem_attr: Option<Box<ParamSemantic>>,
127    pub m_variable: Token,
128    pub m_2: Token,
129    pub m_type: Option<Box<RuleType>>,
130  }
131  #[derive(Debug)] pub enum RuleDefineBody {
132    UnionRuleBody(Box<UnionRuleBody>),
133    FunctionalRuleBody(Box<FunctionalRuleBody>),
134  }
135  #[derive(Debug)] pub struct RuleType {
136    pub m_kw_optional: Option<Token>,
137    pub m_kw_token: Option<Token>,
138    pub m_id: Token,
139    pub m_token_content: Option<Token>,
140    pub m_is_list: Option<Token>,
141  }
142  #[derive(Debug)] pub struct TokenLiteral {
143    pub m_t: Token,
144  }
145  #[derive(Debug)] pub struct TokenRegExp {
146    pub m_t: Token,
147  }
148  #[derive(Debug)] pub enum TopLevelDefine {
149    DefineIncludeStatement(Box<DefineIncludeStatement>),
150    DefineContextStatement(Box<DefineContextStatement>),
151    DefineRuleStatement(Box<DefineRuleStatement>),
152    DefineTokenTypeStatement(Box<DefineTokenTypeStatement>),
153    DefineIgnoreTokenRuleStatement(Box<DefineIgnoreTokenRuleStatement>),
154    DefineTokenRuleStatement(Box<DefineTokenRuleStatement>),
155    DefineSemanticStatement(Box<DefineSemanticStatement>),
156  }
157  #[derive(Debug)] pub struct TopLevelStatement {
158    pub m_body: Box<TopLevelDefine>,
159    pub m_1: Token,
160  }
161  #[derive(Debug)] pub struct UnionRuleBody {
162    pub m_0: Token,
163    pub m_first: Option<Token>,
164    pub m_rest: Vec<UnionRuleListTail>,
165  }
166  #[derive(Debug)] pub struct UnionRuleListTail {
167    pub m_0: Token,
168    pub m_r: Token,
169  }
170}
171pub mod pt {
172  use super::*;
173  #[derive(Debug)] pub struct DefineContextStatement<'p> {
174    pub ast: &'p ast::DefineContextStatement,
175    pub m_context_type: String,
176  }
177  #[derive(Debug)] pub struct DefineIgnoreTokenRuleStatement<'p> {
178    pub ast: &'p ast::DefineIgnoreTokenRuleStatement,
179    pub m_value: Box<pt::LiteralOrRegExp<'p>>,
180  }
181  #[derive(Debug)] pub struct DefineIncludeStatement<'p> {
182    pub ast: &'p ast::DefineIncludeStatement,
183    pub m_path: String,
184  }
185  #[derive(Debug)] pub struct DefineRuleStatement<'p> {
186    pub ast: &'p ast::DefineRuleStatement,
187    pub m_hook_attr: Option<Box<ParseHook<Hook, pt::HookAttribute<'p>>>>,
188    pub m_rule_name: String,
189    pub m_body: Box<ParseHook<RuleValue, pt::RuleDefineBody<'p>>>,
190  }
191  #[derive(Debug)] pub struct DefineSemanticStatement<'p> {
192    pub ast: &'p ast::DefineSemanticStatement,
193    pub m_id: String,
194  }
195  #[derive(Debug)] pub struct DefineTokenRuleStatement<'p> {
196    pub ast: &'p ast::DefineTokenRuleStatement,
197    pub m_token_type: String,
198    pub m_value: Box<pt::LiteralOrRegExp<'p>>,
199  }
200  #[derive(Debug)] pub struct DefineTokenTypeStatement<'p> {
201    pub ast: &'p ast::DefineTokenTypeStatement,
202    pub m_kw_extract: bool,
203    pub m_token_type: String,
204  }
205  #[derive(Debug)] pub struct FunctionalRuleBody<'p> {
206    pub ast: &'p ast::FunctionalRuleBody,
207    pub m_first_param: Option<Box<ParseHook<Param, pt::Parameter<'p>>>>,
208    pub m_rest_params: Vec<pt::ParamListTail<'p>>,
209  }
210  #[derive(Debug)] pub struct HookAttribute<'p> {
211    pub ast: &'p ast::HookAttribute,
212    pub m_hook_name: String,
213    pub m_hook_type: String,
214  }
215  #[derive(Debug)] pub enum LiteralOrRegExp<'p> { 
216    TokenLiteral(Box<pt::TokenLiteral<'p>>),
217    TokenRegExp(Box<pt::TokenRegExp<'p>>),
218  }
219  #[derive(Debug)] pub struct ParamListTail<'p> {
220    pub ast: &'p ast::ParamListTail,
221    pub m_p: Box<ParseHook<Param, pt::Parameter<'p>>>,
222  }
223  #[derive(Debug)] pub struct ParamSemantic<'p> {
224    pub ast: &'p ast::ParamSemantic,
225    pub m_semantic_name: Option<String>,
226  }
227  #[derive(Debug)] pub struct Parameter<'p> {
228    pub ast: &'p ast::Parameter,
229    pub m_sem_attr: Option<Box<pt::ParamSemantic<'p>>>,
230    pub m_variable: String,
231    pub m_type: Option<Box<pt::RuleType<'p>>>,
232  }
233  #[derive(Debug)] pub enum RuleDefineBody<'p> { 
234    UnionRuleBody(Box<pt::UnionRuleBody<'p>>),
235    FunctionalRuleBody(Box<pt::FunctionalRuleBody<'p>>),
236  }
237  #[derive(Debug)] pub struct RuleType<'p> {
238    pub ast: &'p ast::RuleType,
239    pub m_kw_optional: bool,
240    pub m_kw_token: bool,
241    pub m_id: String,
242    pub m_token_content: Option<String>,
243    pub m_is_list: bool,
244  }
245  #[derive(Debug)] pub struct TokenLiteral<'p> {
246    pub ast: &'p ast::TokenLiteral,
247    pub m_t: String,
248  }
249  #[derive(Debug)] pub struct TokenRegExp<'p> {
250    pub ast: &'p ast::TokenRegExp,
251    pub m_t: String,
252  }
253  #[derive(Debug)] pub enum TopLevelDefine<'p> { 
254    DefineIncludeStatement(Box<ParseHook<(), pt::DefineIncludeStatement<'p>>>),
255    DefineContextStatement(Box<ParseHook<(), pt::DefineContextStatement<'p>>>),
256    DefineRuleStatement(Box<ParseHook<(), pt::DefineRuleStatement<'p>>>),
257    DefineTokenTypeStatement(Box<ParseHook<(), pt::DefineTokenTypeStatement<'p>>>),
258    DefineIgnoreTokenRuleStatement(Box<ParseHook<(), pt::DefineIgnoreTokenRuleStatement<'p>>>),
259    DefineTokenRuleStatement(Box<ParseHook<(), pt::DefineTokenRuleStatement<'p>>>),
260    DefineSemanticStatement(Box<ParseHook<(), pt::DefineSemanticStatement<'p>>>),
261  }
262  #[derive(Debug)] pub struct TopLevelStatement<'p> {
263    pub ast: &'p ast::TopLevelStatement,
264    pub m_body: Box<pt::TopLevelDefine<'p>>,
265  }
266  #[derive(Debug)] pub struct UnionRuleBody<'p> {
267    pub ast: &'p ast::UnionRuleBody,
268    pub m_first: Option<String>,
269    pub m_rest: Vec<pt::UnionRuleListTail<'p>>,
270  }
271  #[derive(Debug)] pub struct UnionRuleListTail<'p> {
272    pub ast: &'p ast::UnionRuleListTail,
273    pub m_r: String,
274  }
275}
276impl ast::DefineContextStatement {
277  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
278    Some(Self {
279      m_0: required!(ts, token!(TKeyword::"context"(ts)))?,
280      m_context_type: required!(ts, token!(TLiteral::parse(ts)))?,
281    })
282  }
283  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
284    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
285    si.set(&self.m_context_type, _ovr.as_ref().cloned().unwrap_or(Tok::SContextType));
286  }
287}
288impl<'p> pt::DefineContextStatement<'p> {
289  fn from_ast_internal(ast: &'p ast::DefineContextStatement, _ctx: &mut Ctx) -> Self {
290    Self {
291      ast,
292      m_context_type: ast.m_context_type.value.clone(),
293    }
294  }
295  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::DefineContextStatement, ctx: &mut Ctx) -> ParseHook<(), pt::DefineContextStatement<'p>> {
296    let mut pt = Self::from_ast_internal(ast, ctx);
297    ParseHook { val: parse_context(&mut pt, ctx), pt }
298  }
299}
300impl ast::DefineIgnoreTokenRuleStatement {
301  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
302    Some(Self {
303      m_0: required!(ts, token!(TKeyword::"ignore"(ts)))?,
304      m_value: Box::new(required!(ts, ast::LiteralOrRegExp::parse(ts))?),
305    })
306  }
307  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
308    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
309    self.m_value.apply_semantic(si, _ovr);
310  }
311}
312impl<'p> pt::DefineIgnoreTokenRuleStatement<'p> {
313  fn from_ast_internal(ast: &'p ast::DefineIgnoreTokenRuleStatement, _ctx: &mut Ctx) -> Self {
314    Self {
315      ast,
316      m_value: Box::new(pt::LiteralOrRegExp::from_ast(ast.m_value.as_ref(), _ctx)),
317    }
318  }
319  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::DefineIgnoreTokenRuleStatement, ctx: &mut Ctx) -> ParseHook<(), pt::DefineIgnoreTokenRuleStatement<'p>> {
320    let mut pt = Self::from_ast_internal(ast, ctx);
321    ParseHook { val: parse_token_ignore_rule(&mut pt, ctx), pt }
322  }
323}
324impl ast::DefineIncludeStatement {
325  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
326    Some(Self {
327      m_path: required!(ts, token!(TLiteral::parse(ts)))?,
328    })
329  }
330  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
331    if let Some(o) = _ovr { si.set(&self.m_path, o.clone()); }
332  }
333}
334impl<'p> pt::DefineIncludeStatement<'p> {
335  fn from_ast_internal(ast: &'p ast::DefineIncludeStatement, _ctx: &mut Ctx) -> Self {
336    Self {
337      ast,
338      m_path: ast.m_path.value.clone(),
339    }
340  }
341  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::DefineIncludeStatement, ctx: &mut Ctx) -> ParseHook<(), pt::DefineIncludeStatement<'p>> {
342    let mut pt = Self::from_ast_internal(ast, ctx);
343    ParseHook { val: parse_include(&mut pt, ctx), pt }
344  }
345}
346impl ast::DefineRuleStatement {
347  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
348    Some(Self {
349      m_0: required!(ts, token!(TKeyword::"rule"(ts)))?,
350      m_hook_attr: (optional!(ts, ast::HookAttribute::parse(ts))).map(Box::new),
351      m_rule_name: required!(ts, token!(TIdentifier::parse(ts)))?,
352      m_body: Box::new(required!(ts, ast::RuleDefineBody::parse(ts))?),
353    })
354  }
355  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
356    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
357    if let Some(m) = &self.m_hook_attr { m.apply_semantic(si, _ovr); }
358    si.set(&self.m_rule_name, _ovr.as_ref().cloned().unwrap_or(Tok::SRule));
359    self.m_body.apply_semantic(si, _ovr);
360  }
361}
362impl<'p> pt::DefineRuleStatement<'p> {
363  fn from_ast_internal(ast: &'p ast::DefineRuleStatement, _ctx: &mut Ctx) -> Self {
364    Self {
365      ast,
366      m_hook_attr: ast.m_hook_attr.as_ref().map(|x| Box::new(pt::HookAttribute::from_ast(x, _ctx))),
367      m_rule_name: ast.m_rule_name.value.clone(),
368      m_body: Box::new(pt::RuleDefineBody::from_ast(ast.m_body.as_ref(), _ctx)),
369    }
370  }
371  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::DefineRuleStatement, ctx: &mut Ctx) -> ParseHook<(), pt::DefineRuleStatement<'p>> {
372    let mut pt = Self::from_ast_internal(ast, ctx);
373    ParseHook { val: parse_rule(&mut pt, ctx), pt }
374  }
375}
376impl ast::DefineSemanticStatement {
377  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
378    Some(Self {
379      m_0: required!(ts, token!(TKeyword::"semantic"(ts)))?,
380      m_id: required!(ts, token!(TIdentifier::parse(ts)))?,
381    })
382  }
383  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
384    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
385    si.set(&self.m_id, _ovr.as_ref().cloned().unwrap_or(Tok::SSemantic));
386  }
387}
388impl<'p> pt::DefineSemanticStatement<'p> {
389  fn from_ast_internal(ast: &'p ast::DefineSemanticStatement, _ctx: &mut Ctx) -> Self {
390    Self {
391      ast,
392      m_id: ast.m_id.value.clone(),
393    }
394  }
395  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::DefineSemanticStatement, ctx: &mut Ctx) -> ParseHook<(), pt::DefineSemanticStatement<'p>> {
396    let mut pt = Self::from_ast_internal(ast, ctx);
397    ParseHook { val: parse_semantic(&mut pt, ctx), pt }
398  }
399}
400impl ast::DefineTokenRuleStatement {
401  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
402    Some(Self {
403      m_token_type: required!(ts, token!(TIdentifier::parse(ts)))?,
404      m_value: Box::new(required!(ts, ast::LiteralOrRegExp::parse(ts))?),
405    })
406  }
407  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
408    si.set(&self.m_token_type, _ovr.as_ref().cloned().unwrap_or(Tok::SToken));
409    self.m_value.apply_semantic(si, _ovr);
410  }
411}
412impl<'p> pt::DefineTokenRuleStatement<'p> {
413  fn from_ast_internal(ast: &'p ast::DefineTokenRuleStatement, _ctx: &mut Ctx) -> Self {
414    Self {
415      ast,
416      m_token_type: ast.m_token_type.value.clone(),
417      m_value: Box::new(pt::LiteralOrRegExp::from_ast(ast.m_value.as_ref(), _ctx)),
418    }
419  }
420  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::DefineTokenRuleStatement, ctx: &mut Ctx) -> ParseHook<(), pt::DefineTokenRuleStatement<'p>> {
421    let mut pt = Self::from_ast_internal(ast, ctx);
422    ParseHook { val: parse_token_rule(&mut pt, ctx), pt }
423  }
424}
425impl ast::DefineTokenTypeStatement {
426  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
427    Some(Self {
428      m_kw_extract: optional!(ts, token!(TKeyword::"extract"(ts))),
429      m_1: required!(ts, token!(TKeyword::"token"(ts)))?,
430      m_token_type: required!(ts, token!(TIdentifier::parse(ts)))?,
431    })
432  }
433  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
434    if let Some(o) = _ovr { if let Some(m) = &self.m_kw_extract { si.set(m, o.clone()); } }
435    if let Some(o) = _ovr { si.set(&self.m_1, o.clone()); }
436    si.set(&self.m_token_type, _ovr.as_ref().cloned().unwrap_or(Tok::SToken));
437  }
438}
439impl<'p> pt::DefineTokenTypeStatement<'p> {
440  fn from_ast_internal(ast: &'p ast::DefineTokenTypeStatement, _ctx: &mut Ctx) -> Self {
441    Self {
442      ast,
443      m_kw_extract: ast.m_kw_extract.is_some(),
444      m_token_type: ast.m_token_type.value.clone(),
445    }
446  }
447  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::DefineTokenTypeStatement, ctx: &mut Ctx) -> ParseHook<(), pt::DefineTokenTypeStatement<'p>> {
448    let mut pt = Self::from_ast_internal(ast, ctx);
449    ParseHook { val: parse_token_def(&mut pt, ctx), pt }
450  }
451}
452impl ast::FunctionalRuleBody {
453  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
454    Some(Self {
455      m_0: required!(ts, token!(TSymbol::"("(ts)))?,
456      m_first_param: (optional!(ts, ast::Parameter::parse(ts))).map(Box::new),
457      m_rest_params: { let mut v = vec![]; list!(ts, v, ast::ParamListTail::parse(ts)) },
458      m_3: required!(ts, token!(TSymbol::")"(ts)))?,
459    })
460  }
461  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
462    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
463    if let Some(m) = &self.m_first_param { m.apply_semantic(si, _ovr); }
464    for m in &self.m_rest_params { m.apply_semantic(si, _ovr); }
465    if let Some(o) = _ovr { si.set(&self.m_3, o.clone()); }
466  }
467}
468impl<'p> pt::FunctionalRuleBody<'p> {
469  fn from_ast(ast: &'p ast::FunctionalRuleBody, _ctx: &mut Ctx) -> Self {
470    Self {
471      ast,
472      m_first_param: ast.m_first_param.as_ref().map(|x| Box::new(pt::Parameter::from_ast(x, _ctx))),
473      m_rest_params: ast.m_rest_params.iter().map(|x| pt::ParamListTail::from_ast(x, _ctx)).collect::<Vec<_>>(),
474    }
475  }
476}
477impl ast::HookAttribute {
478  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
479    Some(Self {
480      m_0: required!(ts, token!(TSymbol::"("(ts)))?,
481      m_hook_name: required!(ts, token!(TLiteral::parse(ts)))?,
482      m_2: required!(ts, token!(TSymbol::":"(ts)))?,
483      m_hook_type: required!(ts, token!(TLiteral::parse(ts)))?,
484      m_4: required!(ts, token!(TSymbol::")"(ts)))?,
485    })
486  }
487  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
488    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
489    si.set(&self.m_hook_name, _ovr.as_ref().cloned().unwrap_or(Tok::SHookName));
490    if let Some(o) = _ovr { si.set(&self.m_2, o.clone()); }
491    si.set(&self.m_hook_type, _ovr.as_ref().cloned().unwrap_or(Tok::SHookType));
492    if let Some(o) = _ovr { si.set(&self.m_4, o.clone()); }
493  }
494}
495impl<'p> pt::HookAttribute<'p> {
496  fn from_ast_internal(ast: &'p ast::HookAttribute, _ctx: &mut Ctx) -> Self {
497    Self {
498      ast,
499      m_hook_name: ast.m_hook_name.value.clone(),
500      m_hook_type: ast.m_hook_type.value.clone(),
501    }
502  }
503  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::HookAttribute, ctx: &mut Ctx) -> ParseHook<Hook, pt::HookAttribute<'p>> {
504    let mut pt = Self::from_ast_internal(ast, ctx);
505    ParseHook { val: parse_hook(&mut pt, ctx), pt }
506  }
507}
508crate::impl_union!(from_ast, LiteralOrRegExp, {
509  TokenLiteral,
510  TokenRegExp,
511});
512impl ast::ParamListTail {
513  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
514    Some(Self {
515      m_0: required!(ts, token!(TSymbol::","(ts)))?,
516      m_p: Box::new(required!(ts, ast::Parameter::parse(ts))?),
517    })
518  }
519  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
520    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
521    self.m_p.apply_semantic(si, _ovr);
522  }
523}
524impl<'p> pt::ParamListTail<'p> {
525  fn from_ast(ast: &'p ast::ParamListTail, _ctx: &mut Ctx) -> Self {
526    Self {
527      ast,
528      m_p: Box::new(pt::Parameter::from_ast(ast.m_p.as_ref(), _ctx)),
529    }
530  }
531}
532impl ast::ParamSemantic {
533  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
534    Some(Self {
535      m_0: required!(ts, token!(TSymbol::"("(ts)))?,
536      m_semantic_name: optional!(ts, token!(TIdentifier::parse(ts))),
537      m_2: required!(ts, token!(TSymbol::")"(ts)))?,
538    })
539  }
540  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
541    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
542    if let Some(m) = &self.m_semantic_name { si.set(m, _ovr.as_ref().cloned().unwrap_or(Tok::SSemantic)); }
543    if let Some(o) = _ovr { si.set(&self.m_2, o.clone()); }
544  }
545}
546impl<'p> pt::ParamSemantic<'p> {
547  fn from_ast(ast: &'p ast::ParamSemantic, _ctx: &mut Ctx) -> Self {
548    Self {
549      ast,
550      m_semantic_name: ast.m_semantic_name.as_ref().map(|t| t.value.clone()),
551    }
552  }
553}
554impl ast::Parameter {
555  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
556    Some(Self {
557      m_sem_attr: (optional!(ts, ast::ParamSemantic::parse(ts))).map(Box::new),
558      m_variable: required!(ts, token!(TIdentifier::parse(ts)))?,
559      m_2: required!(ts, token!(TSymbol::":"(ts)))?,
560      m_type: (optional!(ts, ast::RuleType::parse(ts))).map(Box::new),
561    })
562  }
563  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
564    if let Some(m) = &self.m_sem_attr { m.apply_semantic(si, _ovr); }
565    si.set(&self.m_variable, _ovr.as_ref().cloned().unwrap_or(Tok::SVariable));
566    if let Some(o) = _ovr { si.set(&self.m_2, o.clone()); }
567    if let Some(m) = &self.m_type { m.apply_semantic(si, _ovr); }
568  }
569}
570impl<'p> pt::Parameter<'p> {
571  fn from_ast_internal(ast: &'p ast::Parameter, _ctx: &mut Ctx) -> Self {
572    Self {
573      ast,
574      m_sem_attr: ast.m_sem_attr.as_ref().map(|x| Box::new(pt::ParamSemantic::from_ast(x, _ctx))),
575      m_variable: ast.m_variable.value.clone(),
576      m_type: ast.m_type.as_ref().map(|x| Box::new(pt::RuleType::from_ast(x, _ctx))),
577    }
578  }
579  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::Parameter, ctx: &mut Ctx) -> ParseHook<Param, pt::Parameter<'p>> {
580    let mut pt = Self::from_ast_internal(ast, ctx);
581    ParseHook { val: parse_param(&mut pt, ctx), pt }
582  }
583}
584crate::impl_union!(from_ast_internal, RuleDefineBody, {
585  UnionRuleBody,
586  FunctionalRuleBody,
587});
588impl<'p> pt::RuleDefineBody<'p> {
589  #[inline] #[allow(clippy::unnecessary_mut_passed)] fn from_ast(ast: &'p ast::RuleDefineBody, ctx: &mut Ctx) -> ParseHook<RuleValue, pt::RuleDefineBody<'p>> {
590    let mut pt = Self::from_ast_internal(ast, ctx);
591    ParseHook { val: parse_rule_value(&mut pt, ctx), pt }
592  }
593}
594impl ast::RuleType {
595  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
596    Some(Self {
597      m_kw_optional: optional!(ts, token!(TKeyword::"optional"(ts))),
598      m_kw_token: optional!(ts, token!(TKeyword::"token"(ts))),
599      m_id: required!(ts, token!(TIdentifier::parse(ts)))?,
600      m_token_content: optional!(ts, token!(TLiteral::parse(ts))),
601      m_is_list: optional!(ts, token!(TSymbol::"+"(ts))),
602    })
603  }
604  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
605    if let Some(o) = _ovr { if let Some(m) = &self.m_kw_optional { si.set(m, o.clone()); } }
606    if let Some(o) = _ovr { if let Some(m) = &self.m_kw_token { si.set(m, o.clone()); } }
607    if let Some(o) = _ovr { si.set(&self.m_id, o.clone()); }
608    if let Some(o) = _ovr { if let Some(m) = &self.m_token_content { si.set(m, o.clone()); } }
609    if let Some(o) = _ovr { if let Some(m) = &self.m_is_list { si.set(m, o.clone()); } }
610  }
611}
612impl<'p> pt::RuleType<'p> {
613  fn from_ast(ast: &'p ast::RuleType, _ctx: &mut Ctx) -> Self {
614    Self {
615      ast,
616      m_kw_optional: ast.m_kw_optional.is_some(),
617      m_kw_token: ast.m_kw_token.is_some(),
618      m_id: ast.m_id.value.clone(),
619      m_token_content: ast.m_token_content.as_ref().map(|t| t.value.clone()),
620      m_is_list: ast.m_is_list.is_some(),
621    }
622  }
623}
624impl ast::TokenLiteral {
625  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
626    Some(Self {
627      m_t: required!(ts, token!(TLiteral::parse(ts)))?,
628    })
629  }
630  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
631    if let Some(o) = _ovr { si.set(&self.m_t, o.clone()); }
632  }
633}
634impl<'p> pt::TokenLiteral<'p> {
635  fn from_ast(ast: &'p ast::TokenLiteral, _ctx: &mut Ctx) -> Self {
636    Self {
637      ast,
638      m_t: ast.m_t.value.clone(),
639    }
640  }
641}
642impl ast::TokenRegExp {
643  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
644    Some(Self {
645      m_t: required!(ts, token!(TRegExp::parse(ts)))?,
646    })
647  }
648  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
649    if let Some(o) = _ovr { si.set(&self.m_t, o.clone()); }
650  }
651}
652impl<'p> pt::TokenRegExp<'p> {
653  fn from_ast(ast: &'p ast::TokenRegExp, _ctx: &mut Ctx) -> Self {
654    Self {
655      ast,
656      m_t: ast.m_t.value.clone(),
657    }
658  }
659}
660crate::impl_union!(from_ast, TopLevelDefine, {
661  DefineIncludeStatement,
662  DefineContextStatement,
663  DefineRuleStatement,
664  DefineTokenTypeStatement,
665  DefineIgnoreTokenRuleStatement,
666  DefineTokenRuleStatement,
667  DefineSemanticStatement,
668});
669impl ast::TopLevelStatement {
670  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
671    Some(Self {
672      m_body: Box::new(required!(ts, ast::TopLevelDefine::parse(ts))?),
673      m_1: required!(ts, token!(TSymbol::";"(ts)))?,
674    })
675  }
676  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
677    self.m_body.apply_semantic(si, _ovr);
678    if let Some(o) = _ovr { si.set(&self.m_1, o.clone()); }
679  }
680}
681impl<'p> pt::TopLevelStatement<'p> {
682  fn from_ast(ast: &'p ast::TopLevelStatement, _ctx: &mut Ctx) -> Self {
683    Self {
684      ast,
685      m_body: Box::new(pt::TopLevelDefine::from_ast(ast.m_body.as_ref(), _ctx)),
686    }
687  }
688}
689impl ast::UnionRuleBody {
690  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
691    Some(Self {
692      m_0: required!(ts, token!(TSymbol::"="(ts)))?,
693      m_first: optional!(ts, token!(TIdentifier::parse(ts))),
694      m_rest: { let mut v = vec![]; list!(ts, v, ast::UnionRuleListTail::parse(ts)) },
695    })
696  }
697  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
698    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
699    if let Some(m) = &self.m_first { si.set(m, _ovr.as_ref().cloned().unwrap_or(Tok::SRule)); }
700    for m in &self.m_rest { m.apply_semantic(si, _ovr); }
701  }
702}
703impl<'p> pt::UnionRuleBody<'p> {
704  fn from_ast(ast: &'p ast::UnionRuleBody, _ctx: &mut Ctx) -> Self {
705    Self {
706      ast,
707      m_first: ast.m_first.as_ref().map(|t| t.value.clone()),
708      m_rest: ast.m_rest.iter().map(|x| pt::UnionRuleListTail::from_ast(x, _ctx)).collect::<Vec<_>>(),
709    }
710  }
711}
712impl ast::UnionRuleListTail {
713  pub fn parse(ts: &mut TokenStream<Tok>) -> Option<Self> {
714    Some(Self {
715      m_0: required!(ts, token!(TSymbol::"|"(ts)))?,
716      m_r: required!(ts, token!(TIdentifier::parse(ts)))?,
717    })
718  }
719  pub fn apply_semantic(&self, si: &mut TokenBlocks<Tok>, _ovr: &Option<Tok>) {
720    if let Some(o) = _ovr { si.set(&self.m_0, o.clone()); }
721    si.set(&self.m_r, _ovr.as_ref().cloned().unwrap_or(Tok::SRule));
722  }
723}
724impl<'p> pt::UnionRuleListTail<'p> {
725  fn from_ast(ast: &'p ast::UnionRuleListTail, _ctx: &mut Ctx) -> Self {
726    Self {
727      ast,
728      m_r: ast.m_r.value.clone(),
729    }
730  }
731}