Skip to content

Commit

Permalink
Merge #16
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] committed Jan 28, 2018
2 parents efadcf7 + 4244948 commit aff82e5
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 243 deletions.
56 changes: 33 additions & 23 deletions src/bin/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ impl Grammar {

fn to_syntax_kinds(&self) -> String {
let mut acc = String::new();
acc.push_str("// Generated from grammar.ron\n");
acc.push_str("use tree::{SyntaxKind, SyntaxInfo};\n");
acc.push_str("#![allow(bad_style, missing_docs, unreachable_pub)]\n");
acc.push_str("#![cfg_attr(rustfmt, rustfmt_skip)]\n");
acc.push_str("//! Generated from grammar.ron\n");
acc.push_str("use tree::SyntaxInfo;\n");
acc.push_str("\n");

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

for (idx, kind) in syntax_kinds.iter().enumerate() {
let sname = scream(kind);
write!(
acc,
"pub const {}: SyntaxKind = SyntaxKind({});\n",
sname, idx
).unwrap();
// enum SyntaxKind
acc.push_str("/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.\n");
acc.push_str("#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]\n");
acc.push_str("#[repr(u32)]\n");
acc.push_str("pub enum SyntaxKind {\n");
for kind in syntax_kinds.iter() {
write!(acc, " {},\n", scream(kind)).unwrap();
}
acc.push_str("\n");
write!(
acc,
"static INFOS: [SyntaxInfo; {}] = [\n",
syntax_kinds.len()
).unwrap();
acc.push_str(" TOMBSTONE = !0 - 1,\n");
acc.push_str(" EOF = !0,\n");
acc.push_str("}\n");
acc.push_str("pub(crate) use self::SyntaxKind::*;\n");
acc.push_str("\n");

// fn info
acc.push_str("impl SyntaxKind {\n");
acc.push_str(" pub(crate) fn info(self) -> &'static SyntaxInfo {\n");
acc.push_str(" match self {\n");
for kind in syntax_kinds.iter() {
let sname = scream(kind);
write!(
acc,
" SyntaxInfo {{ name: \"{sname}\" }},\n",
" {sname} => &SyntaxInfo {{ name: \"{sname}\" }},\n",
sname = sname
).unwrap();
}
acc.push_str("];\n");
acc.push_str("\n");
acc.push_str(" TOMBSTONE => &SyntaxInfo { name: \"TOMBSTONE\" },\n");
acc.push_str(" EOF => &SyntaxInfo { name: \"EOF\" },\n");
acc.push_str(" }\n");
acc.push_str(" }\n");
acc.push_str("}\n");
acc.push_str("\n");

acc.push_str("pub(crate) fn syntax_info(kind: SyntaxKind) -> &'static SyntaxInfo {\n");
acc.push_str(" &INFOS[kind.0 as usize]\n");
acc.push_str("}\n\n");
// fn ident_to_keyword
acc.push_str("pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {\n");
acc.push_str(" match ident {\n");
acc.push_str(" match ident {\n");
for kw in self.keywords.iter() {
write!(acc, " {:?} => Some({}),\n", kw, kw_token(kw)).unwrap();
write!(acc, " {:?} => Some({}),\n", kw, kw_token(kw)).unwrap();
}
acc.push_str(" _ => None,\n");
acc.push_str(" }\n");
acc.push_str(" _ => None,\n");
acc.push_str(" }\n");
acc.push_str("}\n");
acc
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ mod tree;
mod lexer;
mod parser;

#[cfg_attr(rustfmt, rustfmt_skip)]
#[allow(missing_docs)]
pub mod syntax_kinds;
pub use text::{TextRange, TextUnit};
pub use tree::{File, FileBuilder, Node, Sink, SyntaxKind, Token};
Expand Down
1 change: 0 additions & 1 deletion src/parser/event_parser/grammar/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::parser::{Parser, TokenSet};
use SyntaxKind;
use tree::EOF;
use syntax_kinds::*;

mod items;
Expand Down
3 changes: 1 addition & 2 deletions src/parser/event_parser/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use {SyntaxKind, TextUnit, Token};
use super::Event;
use super::super::is_insignificant;
use syntax_kinds::{ERROR, L_CURLY, R_CURLY};
use tree::{EOF, TOMBSTONE};
use SyntaxKind::{EOF, ERROR, L_CURLY, R_CURLY, TOMBSTONE};

pub(crate) struct Marker {
pos: u32,
Expand Down
1 change: 0 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use {File, FileBuilder, Sink, SyntaxKind, Token};

use syntax_kinds::*;
use tree::TOMBSTONE;

mod event_parser;
use self::event_parser::Event;
Expand Down
Loading

0 comments on commit aff82e5

Please sign in to comment.