diff options
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/gen.rs | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/src/bin/gen.rs b/src/bin/gen.rs index 8f6402f5c..4b8a5afec 100644 --- a/src/bin/gen.rs +++ b/src/bin/gen.rs | |||
@@ -29,8 +29,10 @@ impl Grammar { | |||
29 | 29 | ||
30 | fn to_syntax_kinds(&self) -> String { | 30 | fn to_syntax_kinds(&self) -> String { |
31 | let mut acc = String::new(); | 31 | let mut acc = String::new(); |
32 | acc.push_str("// Generated from grammar.ron\n"); | 32 | acc.push_str("#![allow(bad_style, missing_docs, unreachable_pub)]\n"); |
33 | acc.push_str("use tree::{SyntaxKind, SyntaxInfo};\n"); | 33 | acc.push_str("#![cfg_attr(rustfmt, rustfmt_skip)]\n"); |
34 | acc.push_str("//! Generated from grammar.ron\n"); | ||
35 | acc.push_str("use tree::SyntaxInfo;\n"); | ||
34 | acc.push_str("\n"); | 36 | acc.push_str("\n"); |
35 | 37 | ||
36 | let syntax_kinds: Vec<String> = self.keywords | 38 | let syntax_kinds: Vec<String> = self.keywords |
@@ -40,41 +42,49 @@ impl Grammar { | |||
40 | .chain(self.nodes.iter().cloned()) | 42 | .chain(self.nodes.iter().cloned()) |
41 | .collect(); | 43 | .collect(); |
42 | 44 | ||
43 | for (idx, kind) in syntax_kinds.iter().enumerate() { | 45 | // enum SyntaxKind |
44 | let sname = scream(kind); | 46 | acc.push_str("/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.\n"); |
45 | write!( | 47 | acc.push_str("#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]\n"); |
46 | acc, | 48 | acc.push_str("#[repr(u32)]\n"); |
47 | "pub const {}: SyntaxKind = SyntaxKind({});\n", | 49 | acc.push_str("pub enum SyntaxKind {\n"); |
48 | sname, idx | 50 | for kind in syntax_kinds.iter() { |
49 | ).unwrap(); | 51 | write!(acc, " {},\n", scream(kind)).unwrap(); |
50 | } | 52 | } |
51 | acc.push_str("\n"); | 53 | acc.push_str("\n"); |
52 | write!( | 54 | acc.push_str(" TOMBSTONE = !0 - 1,\n"); |
53 | acc, | 55 | acc.push_str(" EOF = !0,\n"); |
54 | "static INFOS: [SyntaxInfo; {}] = [\n", | 56 | acc.push_str("}\n"); |
55 | syntax_kinds.len() | 57 | acc.push_str("pub(crate) use self::SyntaxKind::*;\n"); |
56 | ).unwrap(); | 58 | acc.push_str("\n"); |
59 | |||
60 | // fn info | ||
61 | acc.push_str("impl SyntaxKind {\n"); | ||
62 | acc.push_str(" pub(crate) fn info(self) -> &'static SyntaxInfo {\n"); | ||
63 | acc.push_str(" match self {\n"); | ||
57 | for kind in syntax_kinds.iter() { | 64 | for kind in syntax_kinds.iter() { |
58 | let sname = scream(kind); | 65 | let sname = scream(kind); |
59 | write!( | 66 | write!( |
60 | acc, | 67 | acc, |
61 | " SyntaxInfo {{ name: \"{sname}\" }},\n", | 68 | " {sname} => &SyntaxInfo {{ name: \"{sname}\" }},\n", |
62 | sname = sname | 69 | sname = sname |
63 | ).unwrap(); | 70 | ).unwrap(); |
64 | } | 71 | } |
65 | acc.push_str("];\n"); | 72 | acc.push_str("\n"); |
73 | acc.push_str(" TOMBSTONE => &SyntaxInfo { name: \"TOMBSTONE\" },\n"); | ||
74 | acc.push_str(" EOF => &SyntaxInfo { name: \"EOF\" },\n"); | ||
75 | acc.push_str(" }\n"); | ||
76 | acc.push_str(" }\n"); | ||
77 | acc.push_str("}\n"); | ||
66 | acc.push_str("\n"); | 78 | acc.push_str("\n"); |
67 | 79 | ||
68 | acc.push_str("pub(crate) fn syntax_info(kind: SyntaxKind) -> &'static SyntaxInfo {\n"); | 80 | // fn ident_to_keyword |
69 | acc.push_str(" &INFOS[kind.0 as usize]\n"); | ||
70 | acc.push_str("}\n\n"); | ||
71 | acc.push_str("pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {\n"); | 81 | acc.push_str("pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {\n"); |
72 | acc.push_str(" match ident {\n"); | 82 | acc.push_str(" match ident {\n"); |
73 | for kw in self.keywords.iter() { | 83 | for kw in self.keywords.iter() { |
74 | write!(acc, " {:?} => Some({}),\n", kw, kw_token(kw)).unwrap(); | 84 | write!(acc, " {:?} => Some({}),\n", kw, kw_token(kw)).unwrap(); |
75 | } | 85 | } |
76 | acc.push_str(" _ => None,\n"); | 86 | acc.push_str(" _ => None,\n"); |
77 | acc.push_str(" }\n"); | 87 | acc.push_str(" }\n"); |
78 | acc.push_str("}\n"); | 88 | acc.push_str("}\n"); |
79 | acc | 89 | acc |
80 | } | 90 | } |