Skip to content

Commit aff82e5

Browse files
committed
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]`](rust-lang/rust#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: <4244948> Just `src/syntax_kinds.rs`: <4244948#diff-8f0d69eb4fe0148851505f787b6fd3bb>
2 parents efadcf7 + 4244948 commit aff82e5

File tree

7 files changed

+243
-243
lines changed

7 files changed

+243
-243
lines changed

src/bin/gen.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ impl Grammar {
2929

3030
fn to_syntax_kinds(&self) -> String {
3131
let mut acc = String::new();
32-
acc.push_str("// Generated from grammar.ron\n");
33-
acc.push_str("use tree::{SyntaxKind, SyntaxInfo};\n");
32+
acc.push_str("#![allow(bad_style, missing_docs, unreachable_pub)]\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");
3436
acc.push_str("\n");
3537

3638
let syntax_kinds: Vec<String> = self.keywords
@@ -40,41 +42,49 @@ impl Grammar {
4042
.chain(self.nodes.iter().cloned())
4143
.collect();
4244

43-
for (idx, kind) in syntax_kinds.iter().enumerate() {
44-
let sname = scream(kind);
45-
write!(
46-
acc,
47-
"pub const {}: SyntaxKind = SyntaxKind({});\n",
48-
sname, idx
49-
).unwrap();
45+
// enum SyntaxKind
46+
acc.push_str("/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.\n");
47+
acc.push_str("#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]\n");
48+
acc.push_str("#[repr(u32)]\n");
49+
acc.push_str("pub enum SyntaxKind {\n");
50+
for kind in syntax_kinds.iter() {
51+
write!(acc, " {},\n", scream(kind)).unwrap();
5052
}
5153
acc.push_str("\n");
52-
write!(
53-
acc,
54-
"static INFOS: [SyntaxInfo; {}] = [\n",
55-
syntax_kinds.len()
56-
).unwrap();
54+
acc.push_str(" TOMBSTONE = !0 - 1,\n");
55+
acc.push_str(" EOF = !0,\n");
56+
acc.push_str("}\n");
57+
acc.push_str("pub(crate) use self::SyntaxKind::*;\n");
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");
5764
for kind in syntax_kinds.iter() {
5865
let sname = scream(kind);
5966
write!(
6067
acc,
61-
" SyntaxInfo {{ name: \"{sname}\" }},\n",
68+
" {sname} => &SyntaxInfo {{ name: \"{sname}\" }},\n",
6269
sname = sname
6370
).unwrap();
6471
}
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");
6678
acc.push_str("\n");
6779

68-
acc.push_str("pub(crate) fn syntax_info(kind: SyntaxKind) -> &'static SyntaxInfo {\n");
69-
acc.push_str(" &INFOS[kind.0 as usize]\n");
70-
acc.push_str("}\n\n");
80+
// fn ident_to_keyword
7181
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");
7383
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();
7585
}
76-
acc.push_str(" _ => None,\n");
77-
acc.push_str(" }\n");
86+
acc.push_str(" _ => None,\n");
87+
acc.push_str(" }\n");
7888
acc.push_str("}\n");
7989
acc
8090
}

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ mod tree;
2222
mod lexer;
2323
mod parser;
2424

25-
#[cfg_attr(rustfmt, rustfmt_skip)]
26-
#[allow(missing_docs)]
2725
pub mod syntax_kinds;
2826
pub use text::{TextRange, TextUnit};
2927
pub use tree::{File, FileBuilder, Node, Sink, SyntaxKind, Token};

src/parser/event_parser/grammar/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::parser::{Parser, TokenSet};
22
use SyntaxKind;
3-
use tree::EOF;
43
use syntax_kinds::*;
54

65
mod items;

src/parser/event_parser/parser.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use {SyntaxKind, TextUnit, Token};
22
use super::Event;
33
use super::super::is_insignificant;
4-
use syntax_kinds::{ERROR, L_CURLY, R_CURLY};
5-
use tree::{EOF, TOMBSTONE};
4+
use SyntaxKind::{EOF, ERROR, L_CURLY, R_CURLY, TOMBSTONE};
65

76
pub(crate) struct Marker {
87
pos: u32,

src/parser/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use {File, FileBuilder, Sink, SyntaxKind, Token};
22

33
use syntax_kinds::*;
4-
use tree::TOMBSTONE;
54

65
mod event_parser;
76
use self::event_parser::Event;

0 commit comments

Comments
 (0)