Skip to main content

antlr4_runtime/
generated.rs

1use crate::atn::serialized::SerializedAtn;
2use crate::vocabulary::Vocabulary;
3
4#[derive(Clone, Debug)]
5pub struct GrammarMetadata {
6    grammar_file_name: &'static str,
7    rule_names: &'static [&'static str],
8    literal_names: &'static [Option<&'static str>],
9    symbolic_names: &'static [Option<&'static str>],
10    display_names: &'static [Option<&'static str>],
11    channel_names: &'static [&'static str],
12    mode_names: &'static [&'static str],
13    serialized_atn: &'static [i32],
14}
15
16impl GrammarMetadata {
17    /// Creates static grammar metadata emitted by the Rust target generator.
18    #[allow(clippy::too_many_arguments)]
19    pub const fn new(
20        grammar_file_name: &'static str,
21        rule_names: &'static [&'static str],
22        literal_names: &'static [Option<&'static str>],
23        symbolic_names: &'static [Option<&'static str>],
24        display_names: &'static [Option<&'static str>],
25        channel_names: &'static [&'static str],
26        mode_names: &'static [&'static str],
27        serialized_atn: &'static [i32],
28    ) -> Self {
29        Self {
30            grammar_file_name,
31            rule_names,
32            literal_names,
33            symbolic_names,
34            display_names,
35            channel_names,
36            mode_names,
37            serialized_atn,
38        }
39    }
40
41    pub const fn grammar_file_name(&self) -> &'static str {
42        self.grammar_file_name
43    }
44
45    pub const fn rule_names(&self) -> &'static [&'static str] {
46        self.rule_names
47    }
48
49    pub const fn channel_names(&self) -> &'static [&'static str] {
50        self.channel_names
51    }
52
53    pub const fn mode_names(&self) -> &'static [&'static str] {
54        self.mode_names
55    }
56
57    pub fn vocabulary(&self) -> Vocabulary {
58        Vocabulary::new(
59            self.literal_names.iter().copied(),
60            self.symbolic_names.iter().copied(),
61            self.display_names.iter().copied(),
62        )
63    }
64
65    /// Borrows the serialized ATN values for deserialization by the runtime
66    /// simulators without copying generated static data.
67    pub const fn serialized_atn(&self) -> SerializedAtn<'_> {
68        SerializedAtn::from_i32(self.serialized_atn)
69    }
70}
71
72pub trait GeneratedLexer {
73    fn metadata() -> &'static GrammarMetadata;
74}
75
76pub trait GeneratedParser {
77    fn metadata() -> &'static GrammarMetadata;
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    static META: GrammarMetadata = GrammarMetadata::new(
85        "Mini.g4",
86        &["file"],
87        &[None, Some("'x'")],
88        &[None, Some("X")],
89        &[None, None],
90        &["DEFAULT_TOKEN_CHANNEL", "HIDDEN"],
91        &["DEFAULT_MODE"],
92        &[4, 1, 1, 0, 0, 0],
93    );
94
95    #[test]
96    fn metadata_builds_vocabulary() {
97        assert_eq!(META.grammar_file_name(), "Mini.g4");
98        assert_eq!(META.vocabulary().display_name(1), "'x'");
99    }
100}