Skip to content

Commit

Permalink
Convert SyntaxKind to an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Jan 28, 2018
1 parent 50b9012 commit 4244948
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 4244948

Please sign in to comment.