1902: simplify r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-09-24 06:28:40 +00:00 committed by GitHub
commit 4c293c0a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 38 deletions

View File

@ -148,15 +148,7 @@ fn convert_ident(ident: &tt::Ident) -> TtToken {
} }
fn convert_punct(p: tt::Punct) -> TtToken { fn convert_punct(p: tt::Punct) -> TtToken {
let kind = match p.char { let kind = SyntaxKind::from_char(p.char).unwrap();
// lexer may produce compound tokens for these ones
'.' => T![.],
':' => T![:],
'=' => T![=],
'!' => T![!],
'-' => T![-],
c => SyntaxKind::from_char(c).unwrap(),
};
let text = { let text = {
let mut buf = [0u8; 4]; let mut buf = [0u8; 4];
let s: &str = p.char.encode_utf8(&mut buf); let s: &str = p.char.encode_utf8(&mut buf);

View File

@ -342,6 +342,11 @@ impl SyntaxKind {
'^' => CARET, '^' => CARET,
'%' => PERCENT, '%' => PERCENT,
'_' => UNDERSCORE, '_' => UNDERSCORE,
'.' => DOT,
':' => COLON,
'=' => EQ,
'!' => EXCL,
'-' => MINUS,
_ => return None, _ => return None,
}; };
Some(tok) Some(tok)

View File

@ -1,7 +1,7 @@
// Stores definitions which must be used in multiple places // Stores definitions which must be used in multiple places
// See `cargo gen-syntax` (defined in crates/tools/src/main.rs) // See `cargo gen-syntax` (defined in crates/tools/src/main.rs)
Grammar( Grammar(
single_byte_tokens: [ punct: [
(";", "SEMI"), (";", "SEMI"),
(",", "COMMA"), (",", "COMMA"),
("(", "L_PAREN"), ("(", "L_PAREN"),
@ -25,9 +25,6 @@ Grammar(
("^", "CARET"), ("^", "CARET"),
("%", "PERCENT"), ("%", "PERCENT"),
("_", "UNDERSCORE"), ("_", "UNDERSCORE"),
],
// Tokens for which the longest match must be chosen (e.g. `..` is a DOTDOT, but `.` is a DOT)
multi_byte_tokens: [
(".", "DOT"), (".", "DOT"),
("..", "DOTDOT"), ("..", "DOTDOT"),
("...", "DOTDOTDOT"), ("...", "DOTDOTDOT"),

View File

@ -160,31 +160,24 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
} }
fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> { fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> {
let single_byte_tokens_values = let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar
grammar.single_byte_tokens.iter().map(|(token, _name)| token.chars().next().unwrap()); .punct
let single_byte_tokens = grammar
.single_byte_tokens
.iter() .iter()
.map(|(_token, name)| format_ident!("{}", name)) .filter(|(token, _name)| token.len() == 1)
.collect::<Vec<_>>(); .map(|(token, name)| (token.chars().next().unwrap(), format_ident!("{}", name)))
.unzip();
let punctuation_values = let punctuation_values = grammar.punct.iter().map(|(token, _name)| {
grammar.single_byte_tokens.iter().chain(grammar.multi_byte_tokens.iter()).map( if "{}[]()".contains(token) {
|(token, _name)| { let c = token.chars().next().unwrap();
if "{}[]()".contains(token) { quote! { #c }
let c = token.chars().next().unwrap(); } else {
quote! { #c } let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint));
} else { quote! { #(#cs)* }
let cs = token.chars().map(|c| Punct::new(c, Spacing::Joint)); }
quote! { #(#cs)* } });
} let punctuation =
}, grammar.punct.iter().map(|(_token, name)| format_ident!("{}", name)).collect::<Vec<_>>();
);
let punctuation = single_byte_tokens
.clone()
.into_iter()
.chain(grammar.multi_byte_tokens.iter().map(|(_token, name)| format_ident!("{}", name)))
.collect::<Vec<_>>();
let full_keywords_values = &grammar.keywords; let full_keywords_values = &grammar.keywords;
let full_keywords = let full_keywords =
@ -294,8 +287,7 @@ fn reformat(text: impl std::fmt::Display) -> Result<String> {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct Grammar { struct Grammar {
single_byte_tokens: Vec<(String, String)>, punct: Vec<(String, String)>,
multi_byte_tokens: Vec<(String, String)>,
keywords: Vec<String>, keywords: Vec<String>,
contextual_keywords: Vec<String>, contextual_keywords: Vec<String>,
literals: Vec<String>, literals: Vec<String>,