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: <4244948c6b>
Just `src/syntax_kinds.rs`: <4244948c6b (diff-8f0d69eb4fe0148851505f787b6fd3bb)>
This commit is contained in:
bors[bot] 2018-01-28 08:07:56 +00:00
commit aff82e5ee1
7 changed files with 243 additions and 243 deletions

View file

@ -29,8 +29,10 @@ impl Grammar {
fn to_syntax_kinds(&self) -> String { fn to_syntax_kinds(&self) -> String {
let mut acc = String::new(); let mut acc = String::new();
acc.push_str("// Generated from grammar.ron\n"); acc.push_str("#![allow(bad_style, missing_docs, unreachable_pub)]\n");
acc.push_str("use tree::{SyntaxKind, SyntaxInfo};\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"); acc.push_str("\n");
let syntax_kinds: Vec<String> = self.keywords let syntax_kinds: Vec<String> = self.keywords
@ -40,41 +42,49 @@ impl Grammar {
.chain(self.nodes.iter().cloned()) .chain(self.nodes.iter().cloned())
.collect(); .collect();
for (idx, kind) in syntax_kinds.iter().enumerate() { // enum SyntaxKind
let sname = scream(kind); acc.push_str("/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.\n");
write!( acc.push_str("#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]\n");
acc, acc.push_str("#[repr(u32)]\n");
"pub const {}: SyntaxKind = SyntaxKind({});\n", acc.push_str("pub enum SyntaxKind {\n");
sname, idx for kind in syntax_kinds.iter() {
).unwrap(); write!(acc, " {},\n", scream(kind)).unwrap();
} }
acc.push_str("\n"); acc.push_str("\n");
write!( acc.push_str(" TOMBSTONE = !0 - 1,\n");
acc, acc.push_str(" EOF = !0,\n");
"static INFOS: [SyntaxInfo; {}] = [\n", acc.push_str("}\n");
syntax_kinds.len() acc.push_str("pub(crate) use self::SyntaxKind::*;\n");
).unwrap(); 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() { for kind in syntax_kinds.iter() {
let sname = scream(kind); let sname = scream(kind);
write!( write!(
acc, acc,
" SyntaxInfo {{ name: \"{sname}\" }},\n", " {sname} => &SyntaxInfo {{ name: \"{sname}\" }},\n",
sname = sname sname = sname
).unwrap(); ).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("\n");
acc.push_str("pub(crate) fn syntax_info(kind: SyntaxKind) -> &'static SyntaxInfo {\n"); // fn ident_to_keyword
acc.push_str(" &INFOS[kind.0 as usize]\n");
acc.push_str("}\n\n");
acc.push_str("pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {\n"); 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() { 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(" _ => None,\n");
acc.push_str(" }\n"); acc.push_str(" }\n");
acc.push_str("}\n"); acc.push_str("}\n");
acc acc
} }

View file

@ -22,8 +22,6 @@ mod tree;
mod lexer; mod lexer;
mod parser; mod parser;
#[cfg_attr(rustfmt, rustfmt_skip)]
#[allow(missing_docs)]
pub mod syntax_kinds; pub mod syntax_kinds;
pub use text::{TextRange, TextUnit}; pub use text::{TextRange, TextUnit};
pub use tree::{File, FileBuilder, Node, Sink, SyntaxKind, Token}; pub use tree::{File, FileBuilder, Node, Sink, SyntaxKind, Token};

View file

@ -1,6 +1,5 @@
use super::parser::{Parser, TokenSet}; use super::parser::{Parser, TokenSet};
use SyntaxKind; use SyntaxKind;
use tree::EOF;
use syntax_kinds::*; use syntax_kinds::*;
mod items; mod items;

View file

@ -1,8 +1,7 @@
use {SyntaxKind, TextUnit, Token}; use {SyntaxKind, TextUnit, Token};
use super::Event; use super::Event;
use super::super::is_insignificant; use super::super::is_insignificant;
use syntax_kinds::{ERROR, L_CURLY, R_CURLY}; use SyntaxKind::{EOF, ERROR, L_CURLY, R_CURLY, TOMBSTONE};
use tree::{EOF, TOMBSTONE};
pub(crate) struct Marker { pub(crate) struct Marker {
pos: u32, pos: u32,

View file

@ -1,7 +1,6 @@
use {File, FileBuilder, Sink, SyntaxKind, Token}; use {File, FileBuilder, Sink, SyntaxKind, Token};
use syntax_kinds::*; use syntax_kinds::*;
use tree::TOMBSTONE;
mod event_parser; mod event_parser;
use self::event_parser::Event; use self::event_parser::Event;

View file

@ -1,201 +1,215 @@
// Generated from grammar.ron #![allow(bad_style, missing_docs, unreachable_pub)]
use tree::{SyntaxKind, SyntaxInfo}; #![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated from grammar.ron
use tree::SyntaxInfo;
pub const USE_KW: SyntaxKind = SyntaxKind(0); /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.
pub const FN_KW: SyntaxKind = SyntaxKind(1); #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub const STRUCT_KW: SyntaxKind = SyntaxKind(2); #[repr(u32)]
pub const ENUM_KW: SyntaxKind = SyntaxKind(3); pub enum SyntaxKind {
pub const TRAIT_KW: SyntaxKind = SyntaxKind(4); USE_KW,
pub const IMPL_KW: SyntaxKind = SyntaxKind(5); FN_KW,
pub const TRUE_KW: SyntaxKind = SyntaxKind(6); STRUCT_KW,
pub const FALSE_KW: SyntaxKind = SyntaxKind(7); ENUM_KW,
pub const AS_KW: SyntaxKind = SyntaxKind(8); TRAIT_KW,
pub const EXTERN_KW: SyntaxKind = SyntaxKind(9); IMPL_KW,
pub const CRATE_KW: SyntaxKind = SyntaxKind(10); TRUE_KW,
pub const MOD_KW: SyntaxKind = SyntaxKind(11); FALSE_KW,
pub const PUB_KW: SyntaxKind = SyntaxKind(12); AS_KW,
pub const SELF_KW: SyntaxKind = SyntaxKind(13); EXTERN_KW,
pub const SUPER_KW: SyntaxKind = SyntaxKind(14); CRATE_KW,
pub const IN_KW: SyntaxKind = SyntaxKind(15); MOD_KW,
pub const WHERE_KW: SyntaxKind = SyntaxKind(16); PUB_KW,
pub const ERROR: SyntaxKind = SyntaxKind(17); SELF_KW,
pub const IDENT: SyntaxKind = SyntaxKind(18); SUPER_KW,
pub const UNDERSCORE: SyntaxKind = SyntaxKind(19); IN_KW,
pub const WHITESPACE: SyntaxKind = SyntaxKind(20); WHERE_KW,
pub const INT_NUMBER: SyntaxKind = SyntaxKind(21); ERROR,
pub const FLOAT_NUMBER: SyntaxKind = SyntaxKind(22); IDENT,
pub const SEMI: SyntaxKind = SyntaxKind(23); UNDERSCORE,
pub const COMMA: SyntaxKind = SyntaxKind(24); WHITESPACE,
pub const DOT: SyntaxKind = SyntaxKind(25); INT_NUMBER,
pub const DOTDOT: SyntaxKind = SyntaxKind(26); FLOAT_NUMBER,
pub const DOTDOTDOT: SyntaxKind = SyntaxKind(27); SEMI,
pub const DOTDOTEQ: SyntaxKind = SyntaxKind(28); COMMA,
pub const L_PAREN: SyntaxKind = SyntaxKind(29); DOT,
pub const R_PAREN: SyntaxKind = SyntaxKind(30); DOTDOT,
pub const L_CURLY: SyntaxKind = SyntaxKind(31); DOTDOTDOT,
pub const R_CURLY: SyntaxKind = SyntaxKind(32); DOTDOTEQ,
pub const L_BRACK: SyntaxKind = SyntaxKind(33); L_PAREN,
pub const R_BRACK: SyntaxKind = SyntaxKind(34); R_PAREN,
pub const L_ANGLE: SyntaxKind = SyntaxKind(35); L_CURLY,
pub const R_ANGLE: SyntaxKind = SyntaxKind(36); R_CURLY,
pub const AT: SyntaxKind = SyntaxKind(37); L_BRACK,
pub const POUND: SyntaxKind = SyntaxKind(38); R_BRACK,
pub const TILDE: SyntaxKind = SyntaxKind(39); L_ANGLE,
pub const QUESTION: SyntaxKind = SyntaxKind(40); R_ANGLE,
pub const COLON: SyntaxKind = SyntaxKind(41); AT,
pub const COLONCOLON: SyntaxKind = SyntaxKind(42); POUND,
pub const DOLLAR: SyntaxKind = SyntaxKind(43); TILDE,
pub const EQ: SyntaxKind = SyntaxKind(44); QUESTION,
pub const EQEQ: SyntaxKind = SyntaxKind(45); COLON,
pub const FAT_ARROW: SyntaxKind = SyntaxKind(46); COLONCOLON,
pub const NEQ: SyntaxKind = SyntaxKind(47); DOLLAR,
pub const EXCL: SyntaxKind = SyntaxKind(48); EQ,
pub const LIFETIME: SyntaxKind = SyntaxKind(49); EQEQ,
pub const CHAR: SyntaxKind = SyntaxKind(50); FAT_ARROW,
pub const BYTE: SyntaxKind = SyntaxKind(51); NEQ,
pub const STRING: SyntaxKind = SyntaxKind(52); EXCL,
pub const RAW_STRING: SyntaxKind = SyntaxKind(53); LIFETIME,
pub const BYTE_STRING: SyntaxKind = SyntaxKind(54); CHAR,
pub const RAW_BYTE_STRING: SyntaxKind = SyntaxKind(55); BYTE,
pub const PLUS: SyntaxKind = SyntaxKind(56); STRING,
pub const MINUS: SyntaxKind = SyntaxKind(57); RAW_STRING,
pub const STAR: SyntaxKind = SyntaxKind(58); BYTE_STRING,
pub const SLASH: SyntaxKind = SyntaxKind(59); RAW_BYTE_STRING,
pub const CARET: SyntaxKind = SyntaxKind(60); PLUS,
pub const PERCENT: SyntaxKind = SyntaxKind(61); MINUS,
pub const AMPERSAND: SyntaxKind = SyntaxKind(62); STAR,
pub const PIPE: SyntaxKind = SyntaxKind(63); SLASH,
pub const THIN_ARROW: SyntaxKind = SyntaxKind(64); CARET,
pub const COMMENT: SyntaxKind = SyntaxKind(65); PERCENT,
pub const DOC_COMMENT: SyntaxKind = SyntaxKind(66); AMPERSAND,
pub const SHEBANG: SyntaxKind = SyntaxKind(67); PIPE,
pub const FILE: SyntaxKind = SyntaxKind(68); THIN_ARROW,
pub const STRUCT_ITEM: SyntaxKind = SyntaxKind(69); COMMENT,
pub const NAMED_FIELD: SyntaxKind = SyntaxKind(70); DOC_COMMENT,
pub const POS_FIELD: SyntaxKind = SyntaxKind(71); SHEBANG,
pub const FN_ITEM: SyntaxKind = SyntaxKind(72); FILE,
pub const EXTERN_CRATE_ITEM: SyntaxKind = SyntaxKind(73); STRUCT_ITEM,
pub const ATTR: SyntaxKind = SyntaxKind(74); NAMED_FIELD,
pub const META_ITEM: SyntaxKind = SyntaxKind(75); POS_FIELD,
pub const MOD_ITEM: SyntaxKind = SyntaxKind(76); FN_ITEM,
pub const USE_ITEM: SyntaxKind = SyntaxKind(77); EXTERN_CRATE_ITEM,
pub const USE_TREE: SyntaxKind = SyntaxKind(78); ATTR,
pub const PATH: SyntaxKind = SyntaxKind(79); META_ITEM,
pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(80); MOD_ITEM,
pub const LITERAL: SyntaxKind = SyntaxKind(81); USE_ITEM,
pub const ALIAS: SyntaxKind = SyntaxKind(82); USE_TREE,
pub const VISIBILITY: SyntaxKind = SyntaxKind(83); PATH,
PATH_SEGMENT,
LITERAL,
ALIAS,
VISIBILITY,
static INFOS: [SyntaxInfo; 84] = [ TOMBSTONE = !0 - 1,
SyntaxInfo { name: "USE_KW" }, EOF = !0,
SyntaxInfo { name: "FN_KW" }, }
SyntaxInfo { name: "STRUCT_KW" }, pub(crate) use self::SyntaxKind::*;
SyntaxInfo { name: "ENUM_KW" },
SyntaxInfo { name: "TRAIT_KW" },
SyntaxInfo { name: "IMPL_KW" },
SyntaxInfo { name: "TRUE_KW" },
SyntaxInfo { name: "FALSE_KW" },
SyntaxInfo { name: "AS_KW" },
SyntaxInfo { name: "EXTERN_KW" },
SyntaxInfo { name: "CRATE_KW" },
SyntaxInfo { name: "MOD_KW" },
SyntaxInfo { name: "PUB_KW" },
SyntaxInfo { name: "SELF_KW" },
SyntaxInfo { name: "SUPER_KW" },
SyntaxInfo { name: "IN_KW" },
SyntaxInfo { name: "WHERE_KW" },
SyntaxInfo { name: "ERROR" },
SyntaxInfo { name: "IDENT" },
SyntaxInfo { name: "UNDERSCORE" },
SyntaxInfo { name: "WHITESPACE" },
SyntaxInfo { name: "INT_NUMBER" },
SyntaxInfo { name: "FLOAT_NUMBER" },
SyntaxInfo { name: "SEMI" },
SyntaxInfo { name: "COMMA" },
SyntaxInfo { name: "DOT" },
SyntaxInfo { name: "DOTDOT" },
SyntaxInfo { name: "DOTDOTDOT" },
SyntaxInfo { name: "DOTDOTEQ" },
SyntaxInfo { name: "L_PAREN" },
SyntaxInfo { name: "R_PAREN" },
SyntaxInfo { name: "L_CURLY" },
SyntaxInfo { name: "R_CURLY" },
SyntaxInfo { name: "L_BRACK" },
SyntaxInfo { name: "R_BRACK" },
SyntaxInfo { name: "L_ANGLE" },
SyntaxInfo { name: "R_ANGLE" },
SyntaxInfo { name: "AT" },
SyntaxInfo { name: "POUND" },
SyntaxInfo { name: "TILDE" },
SyntaxInfo { name: "QUESTION" },
SyntaxInfo { name: "COLON" },
SyntaxInfo { name: "COLONCOLON" },
SyntaxInfo { name: "DOLLAR" },
SyntaxInfo { name: "EQ" },
SyntaxInfo { name: "EQEQ" },
SyntaxInfo { name: "FAT_ARROW" },
SyntaxInfo { name: "NEQ" },
SyntaxInfo { name: "EXCL" },
SyntaxInfo { name: "LIFETIME" },
SyntaxInfo { name: "CHAR" },
SyntaxInfo { name: "BYTE" },
SyntaxInfo { name: "STRING" },
SyntaxInfo { name: "RAW_STRING" },
SyntaxInfo { name: "BYTE_STRING" },
SyntaxInfo { name: "RAW_BYTE_STRING" },
SyntaxInfo { name: "PLUS" },
SyntaxInfo { name: "MINUS" },
SyntaxInfo { name: "STAR" },
SyntaxInfo { name: "SLASH" },
SyntaxInfo { name: "CARET" },
SyntaxInfo { name: "PERCENT" },
SyntaxInfo { name: "AMPERSAND" },
SyntaxInfo { name: "PIPE" },
SyntaxInfo { name: "THIN_ARROW" },
SyntaxInfo { name: "COMMENT" },
SyntaxInfo { name: "DOC_COMMENT" },
SyntaxInfo { name: "SHEBANG" },
SyntaxInfo { name: "FILE" },
SyntaxInfo { name: "STRUCT_ITEM" },
SyntaxInfo { name: "NAMED_FIELD" },
SyntaxInfo { name: "POS_FIELD" },
SyntaxInfo { name: "FN_ITEM" },
SyntaxInfo { name: "EXTERN_CRATE_ITEM" },
SyntaxInfo { name: "ATTR" },
SyntaxInfo { name: "META_ITEM" },
SyntaxInfo { name: "MOD_ITEM" },
SyntaxInfo { name: "USE_ITEM" },
SyntaxInfo { name: "USE_TREE" },
SyntaxInfo { name: "PATH" },
SyntaxInfo { name: "PATH_SEGMENT" },
SyntaxInfo { name: "LITERAL" },
SyntaxInfo { name: "ALIAS" },
SyntaxInfo { name: "VISIBILITY" },
];
pub(crate) fn syntax_info(kind: SyntaxKind) -> &'static SyntaxInfo { impl SyntaxKind {
&INFOS[kind.0 as usize] pub(crate) fn info(self) -> &'static SyntaxInfo {
match self {
USE_KW => &SyntaxInfo { name: "USE_KW" },
FN_KW => &SyntaxInfo { name: "FN_KW" },
STRUCT_KW => &SyntaxInfo { name: "STRUCT_KW" },
ENUM_KW => &SyntaxInfo { name: "ENUM_KW" },
TRAIT_KW => &SyntaxInfo { name: "TRAIT_KW" },
IMPL_KW => &SyntaxInfo { name: "IMPL_KW" },
TRUE_KW => &SyntaxInfo { name: "TRUE_KW" },
FALSE_KW => &SyntaxInfo { name: "FALSE_KW" },
AS_KW => &SyntaxInfo { name: "AS_KW" },
EXTERN_KW => &SyntaxInfo { name: "EXTERN_KW" },
CRATE_KW => &SyntaxInfo { name: "CRATE_KW" },
MOD_KW => &SyntaxInfo { name: "MOD_KW" },
PUB_KW => &SyntaxInfo { name: "PUB_KW" },
SELF_KW => &SyntaxInfo { name: "SELF_KW" },
SUPER_KW => &SyntaxInfo { name: "SUPER_KW" },
IN_KW => &SyntaxInfo { name: "IN_KW" },
WHERE_KW => &SyntaxInfo { name: "WHERE_KW" },
ERROR => &SyntaxInfo { name: "ERROR" },
IDENT => &SyntaxInfo { name: "IDENT" },
UNDERSCORE => &SyntaxInfo { name: "UNDERSCORE" },
WHITESPACE => &SyntaxInfo { name: "WHITESPACE" },
INT_NUMBER => &SyntaxInfo { name: "INT_NUMBER" },
FLOAT_NUMBER => &SyntaxInfo { name: "FLOAT_NUMBER" },
SEMI => &SyntaxInfo { name: "SEMI" },
COMMA => &SyntaxInfo { name: "COMMA" },
DOT => &SyntaxInfo { name: "DOT" },
DOTDOT => &SyntaxInfo { name: "DOTDOT" },
DOTDOTDOT => &SyntaxInfo { name: "DOTDOTDOT" },
DOTDOTEQ => &SyntaxInfo { name: "DOTDOTEQ" },
L_PAREN => &SyntaxInfo { name: "L_PAREN" },
R_PAREN => &SyntaxInfo { name: "R_PAREN" },
L_CURLY => &SyntaxInfo { name: "L_CURLY" },
R_CURLY => &SyntaxInfo { name: "R_CURLY" },
L_BRACK => &SyntaxInfo { name: "L_BRACK" },
R_BRACK => &SyntaxInfo { name: "R_BRACK" },
L_ANGLE => &SyntaxInfo { name: "L_ANGLE" },
R_ANGLE => &SyntaxInfo { name: "R_ANGLE" },
AT => &SyntaxInfo { name: "AT" },
POUND => &SyntaxInfo { name: "POUND" },
TILDE => &SyntaxInfo { name: "TILDE" },
QUESTION => &SyntaxInfo { name: "QUESTION" },
COLON => &SyntaxInfo { name: "COLON" },
COLONCOLON => &SyntaxInfo { name: "COLONCOLON" },
DOLLAR => &SyntaxInfo { name: "DOLLAR" },
EQ => &SyntaxInfo { name: "EQ" },
EQEQ => &SyntaxInfo { name: "EQEQ" },
FAT_ARROW => &SyntaxInfo { name: "FAT_ARROW" },
NEQ => &SyntaxInfo { name: "NEQ" },
EXCL => &SyntaxInfo { name: "EXCL" },
LIFETIME => &SyntaxInfo { name: "LIFETIME" },
CHAR => &SyntaxInfo { name: "CHAR" },
BYTE => &SyntaxInfo { name: "BYTE" },
STRING => &SyntaxInfo { name: "STRING" },
RAW_STRING => &SyntaxInfo { name: "RAW_STRING" },
BYTE_STRING => &SyntaxInfo { name: "BYTE_STRING" },
RAW_BYTE_STRING => &SyntaxInfo { name: "RAW_BYTE_STRING" },
PLUS => &SyntaxInfo { name: "PLUS" },
MINUS => &SyntaxInfo { name: "MINUS" },
STAR => &SyntaxInfo { name: "STAR" },
SLASH => &SyntaxInfo { name: "SLASH" },
CARET => &SyntaxInfo { name: "CARET" },
PERCENT => &SyntaxInfo { name: "PERCENT" },
AMPERSAND => &SyntaxInfo { name: "AMPERSAND" },
PIPE => &SyntaxInfo { name: "PIPE" },
THIN_ARROW => &SyntaxInfo { name: "THIN_ARROW" },
COMMENT => &SyntaxInfo { name: "COMMENT" },
DOC_COMMENT => &SyntaxInfo { name: "DOC_COMMENT" },
SHEBANG => &SyntaxInfo { name: "SHEBANG" },
FILE => &SyntaxInfo { name: "FILE" },
STRUCT_ITEM => &SyntaxInfo { name: "STRUCT_ITEM" },
NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
POS_FIELD => &SyntaxInfo { name: "POS_FIELD" },
FN_ITEM => &SyntaxInfo { name: "FN_ITEM" },
EXTERN_CRATE_ITEM => &SyntaxInfo { name: "EXTERN_CRATE_ITEM" },
ATTR => &SyntaxInfo { name: "ATTR" },
META_ITEM => &SyntaxInfo { name: "META_ITEM" },
MOD_ITEM => &SyntaxInfo { name: "MOD_ITEM" },
USE_ITEM => &SyntaxInfo { name: "USE_ITEM" },
USE_TREE => &SyntaxInfo { name: "USE_TREE" },
PATH => &SyntaxInfo { name: "PATH" },
PATH_SEGMENT => &SyntaxInfo { name: "PATH_SEGMENT" },
LITERAL => &SyntaxInfo { name: "LITERAL" },
ALIAS => &SyntaxInfo { name: "ALIAS" },
VISIBILITY => &SyntaxInfo { name: "VISIBILITY" },
TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" },
EOF => &SyntaxInfo { name: "EOF" },
}
}
} }
pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> { pub(crate) fn ident_to_keyword(ident: &str) -> Option<SyntaxKind> {
match ident { match ident {
"use" => Some(USE_KW), "use" => Some(USE_KW),
"fn" => Some(FN_KW), "fn" => Some(FN_KW),
"struct" => Some(STRUCT_KW), "struct" => Some(STRUCT_KW),
"enum" => Some(ENUM_KW), "enum" => Some(ENUM_KW),
"trait" => Some(TRAIT_KW), "trait" => Some(TRAIT_KW),
"impl" => Some(IMPL_KW), "impl" => Some(IMPL_KW),
"true" => Some(TRUE_KW), "true" => Some(TRUE_KW),
"false" => Some(FALSE_KW), "false" => Some(FALSE_KW),
"as" => Some(AS_KW), "as" => Some(AS_KW),
"extern" => Some(EXTERN_KW), "extern" => Some(EXTERN_KW),
"crate" => Some(CRATE_KW), "crate" => Some(CRATE_KW),
"mod" => Some(MOD_KW), "mod" => Some(MOD_KW),
"pub" => Some(PUB_KW), "pub" => Some(PUB_KW),
"self" => Some(SELF_KW), "self" => Some(SELF_KW),
"super" => Some(SUPER_KW), "super" => Some(SUPER_KW),
"in" => Some(IN_KW), "in" => Some(IN_KW),
"where" => Some(WHERE_KW), "where" => Some(WHERE_KW),
_ => None, _ => None,
} }
} }

View file

@ -1,5 +1,4 @@
use text::{TextRange, TextUnit}; use text::{TextRange, TextUnit};
use syntax_kinds::syntax_info;
use std::fmt; use std::fmt;
use std::cmp; use std::cmp;
@ -7,25 +6,7 @@ use std::cmp;
mod file_builder; mod file_builder;
pub use self::file_builder::{FileBuilder, Sink}; pub use self::file_builder::{FileBuilder, Sink};
/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. pub use syntax_kinds::SyntaxKind;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SyntaxKind(pub(crate) u32);
pub(crate) const EOF: SyntaxKind = SyntaxKind(!0);
pub(crate) const EOF_INFO: SyntaxInfo = SyntaxInfo { name: "EOF" };
pub(crate) const TOMBSTONE: SyntaxKind = SyntaxKind(!0 - 1);
pub(crate) const TOMBSTONE_INFO: SyntaxInfo = SyntaxInfo { name: "TOMBSTONE" };
impl SyntaxKind {
fn info(self) -> &'static SyntaxInfo {
match self {
EOF => &EOF_INFO,
TOMBSTONE => &TOMBSTONE_INFO,
_ => syntax_info(self),
}
}
}
impl fmt::Debug for SyntaxKind { impl fmt::Debug for SyntaxKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {