diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-01-28 08:07:56 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-01-28 08:07:56 +0000 |
commit | aff82e5ee1d587b858e7237511e611bb8cc61cf3 (patch) | |
tree | 83d1a4bbaa4be96f76c2d48c00ad479173f61274 /src/bin | |
parent | efadcf715862a2d96af0f57d2b53bfa325390779 (diff) | |
parent | 4244948c6b1a62bd0e1ef276d1f0cc22c36f6f66 (diff) |
Merge #16
16: Proof-of-concept SyntaxKind as enum r=matklad a=CAD97
This was the one bit about the original RFC I was most confused about. Why isn't `SyntaxKind` a normal `enum`?
If it's to signify that it's non-exhaustive [`#[non_exhaustive]`](https://github.com/rust-lang/rust/issues/44109) should be used. (Or `#[doc(hidden)] __Nonexhaustive` on stable.)
If it's so that more variants can be added externally, why? There's no need for that, that I can foresee.
If it's to expose the `SyntaxKind` type but not any of its instances, why? This is the only actual benefit I can see of this pseudo-enum style.
This diff is meant to be as non-invasive as possible, and as such reexports all symbols as they existed prior to this. It's diffed on top of the assumed-good-to-merge #15 to avoid the conflict between them.
Diff without #15: <https://github.com/matklad/libsyntax2/pull/16/commits/4244948c6b1a62bd0e1ef276d1f0cc22c36f6f66>
Just `src/syntax_kinds.rs`: <https://github.com/matklad/libsyntax2/pull/16/commits/4244948c6b1a62bd0e1ef276d1f0cc22c36f6f66#diff-8f0d69eb4fe0148851505f787b6fd3bb>
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 | } |