Skip to main content

antlr4_runtime/
generated.rs

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