Skip to main content

antlr4_runtime/
recognizer.rs

1use crate::vocabulary::Vocabulary;
2
3#[derive(Clone, Debug)]
4pub struct RecognizerData {
5    grammar_file_name: String,
6    rule_names: Vec<String>,
7    channel_names: Vec<String>,
8    mode_names: Vec<String>,
9    vocabulary: Vocabulary,
10    state: isize,
11}
12
13impl RecognizerData {
14    pub fn new(grammar_file_name: impl Into<String>, vocabulary: Vocabulary) -> Self {
15        Self {
16            grammar_file_name: grammar_file_name.into(),
17            rule_names: Vec::new(),
18            channel_names: Vec::new(),
19            mode_names: Vec::new(),
20            vocabulary,
21            state: -1,
22        }
23    }
24
25    #[must_use]
26    pub fn with_rule_names(
27        mut self,
28        rule_names: impl IntoIterator<Item = impl Into<String>>,
29    ) -> Self {
30        self.rule_names = rule_names.into_iter().map(Into::into).collect();
31        self
32    }
33
34    #[must_use]
35    pub fn with_channel_names(
36        mut self,
37        channel_names: impl IntoIterator<Item = impl Into<String>>,
38    ) -> Self {
39        self.channel_names = channel_names.into_iter().map(Into::into).collect();
40        self
41    }
42
43    #[must_use]
44    pub fn with_mode_names(
45        mut self,
46        mode_names: impl IntoIterator<Item = impl Into<String>>,
47    ) -> Self {
48        self.mode_names = mode_names.into_iter().map(Into::into).collect();
49        self
50    }
51
52    pub const fn state(&self) -> isize {
53        self.state
54    }
55
56    pub const fn set_state(&mut self, state: isize) {
57        self.state = state;
58    }
59}
60
61pub trait Recognizer {
62    fn data(&self) -> &RecognizerData;
63    fn data_mut(&mut self) -> &mut RecognizerData;
64
65    fn grammar_file_name(&self) -> &str {
66        &self.data().grammar_file_name
67    }
68
69    fn rule_names(&self) -> &[String] {
70        &self.data().rule_names
71    }
72
73    fn channel_names(&self) -> &[String] {
74        &self.data().channel_names
75    }
76
77    fn mode_names(&self) -> &[String] {
78        &self.data().mode_names
79    }
80
81    fn vocabulary(&self) -> &Vocabulary {
82        &self.data().vocabulary
83    }
84
85    fn state(&self) -> isize {
86        self.data().state()
87    }
88
89    fn set_state(&mut self, state: isize) {
90        self.data_mut().set_state(state);
91    }
92
93    fn sempred(&mut self, _rule_index: usize, _pred_index: usize) -> bool {
94        true
95    }
96
97    fn action(&mut self, _rule_index: usize, _action_index: usize) {}
98}