Skip to main content

veryl_parser/
finder.rs

1use crate::veryl_grammar_trait::*;
2use crate::veryl_token::{Token, VerylToken};
3use crate::veryl_walker::VerylWalker;
4
5#[derive(Default)]
6pub struct Finder {
7    pub line: u32,
8    pub column: u32,
9    pub token: Option<Token>,
10    pub token_group: Vec<Token>,
11    hit: bool,
12    group_hit: bool,
13    in_group: bool,
14    lock_group: bool,
15}
16
17impl Finder {
18    pub fn new() -> Self {
19        Default::default()
20    }
21}
22
23impl VerylWalker for Finder {
24    /// Semantic action for non-terminal 'VerylToken'
25    fn veryl_token(&mut self, arg: &VerylToken) {
26        if arg.token.line == self.line
27            && arg.token.column <= self.column
28            && self.column < arg.token.column + arg.token.length
29        {
30            self.token = Some(arg.token);
31            self.hit = true;
32            self.group_hit = true;
33        }
34        if self.in_group && !self.lock_group {
35            self.token_group.push(arg.token);
36        }
37    }
38
39    /// Semantic action for non-terminal 'HierarchicalIdentifier'
40    fn hierarchical_identifier(&mut self, arg: &HierarchicalIdentifier) {
41        self.group_hit = false;
42        self.in_group = true;
43        self.identifier(&arg.identifier);
44        self.in_group = false;
45        for x in &arg.hierarchical_identifier_list {
46            self.select(&x.select);
47        }
48        for x in &arg.hierarchical_identifier_list0 {
49            self.dot(&x.dot);
50            self.in_group = true;
51            self.identifier(&x.identifier);
52            self.in_group = false;
53            for x in &x.hierarchical_identifier_list0_list {
54                self.select(&x.select);
55            }
56        }
57        if self.group_hit {
58            self.lock_group = true;
59        } else if !self.lock_group {
60            self.token_group.clear();
61        }
62    }
63
64    /// Semantic action for non-terminal 'ScopedIdentifier'
65    fn scoped_identifier(&mut self, arg: &ScopedIdentifier) {
66        self.group_hit = false;
67        self.in_group = true;
68        match &*arg.scoped_identifier_group {
69            ScopedIdentifierGroup::IdentifierScopedIdentifierOpt(x) => {
70                self.identifier(&x.identifier);
71                if let Some(ref x) = x.scoped_identifier_opt {
72                    self.with_generic_argument(&x.with_generic_argument);
73                }
74            }
75            ScopedIdentifierGroup::DollarIdentifier(x) => {
76                self.dollar_identifier(&x.dollar_identifier)
77            }
78        }
79        self.in_group = false;
80        for x in &arg.scoped_identifier_list {
81            self.colon_colon(&x.colon_colon);
82            self.in_group = true;
83            self.identifier(&x.identifier);
84            self.in_group = false;
85            if let Some(ref x) = x.scoped_identifier_opt0 {
86                self.with_generic_argument(&x.with_generic_argument);
87            }
88        }
89        if self.group_hit {
90            self.lock_group = true;
91        } else if !self.lock_group {
92            self.token_group.clear();
93        }
94    }
95
96    /// Semantic action for non-terminal 'ExpressionIdentifier'
97    fn expression_identifier(&mut self, arg: &ExpressionIdentifier) {
98        let x = &arg.scoped_identifier;
99        self.group_hit = false;
100        self.in_group = true;
101        match &*x.scoped_identifier_group {
102            ScopedIdentifierGroup::IdentifierScopedIdentifierOpt(x) => {
103                self.identifier(&x.identifier);
104                if let Some(ref x) = x.scoped_identifier_opt {
105                    self.with_generic_argument(&x.with_generic_argument);
106                }
107            }
108            ScopedIdentifierGroup::DollarIdentifier(x) => {
109                self.dollar_identifier(&x.dollar_identifier)
110            }
111        }
112        self.in_group = false;
113        for x in &x.scoped_identifier_list {
114            self.colon_colon(&x.colon_colon);
115            self.in_group = true;
116            self.identifier(&x.identifier);
117            self.in_group = false;
118            if let Some(ref x) = x.scoped_identifier_opt0 {
119                self.with_generic_argument(&x.with_generic_argument);
120            }
121        }
122        if let Some(ref x) = arg.expression_identifier_opt {
123            self.width(&x.width);
124        }
125        for x in &arg.expression_identifier_list {
126            self.select(&x.select);
127        }
128        for x in &arg.expression_identifier_list0 {
129            self.dot(&x.dot);
130            self.in_group = true;
131            self.identifier(&x.identifier);
132            self.in_group = false;
133            for x in &x.expression_identifier_list0_list {
134                self.select(&x.select);
135            }
136        }
137        if self.group_hit {
138            self.lock_group = true;
139        } else if !self.lock_group {
140            self.token_group.clear();
141        }
142    }
143
144    /// Semantic action for non-terminal 'Veryl'
145    fn veryl(&mut self, arg: &Veryl) {
146        self.hit = false;
147        self.lock_group = false;
148        self.token_group.clear();
149
150        self.start(&arg.start);
151        for x in &arg.veryl_list {
152            self.description_group(&x.description_group);
153        }
154    }
155}