Allow keywords as fields

This commit is contained in:
Laurenz 2023-03-14 22:31:05 +01:00
parent 2bacbaf2bd
commit e50189cfa7
2 changed files with 14 additions and 8 deletions

View File

@ -479,15 +479,18 @@ impl Lexer<'_> {
fn ident(&mut self, start: usize) -> SyntaxKind { fn ident(&mut self, start: usize) -> SyntaxKind {
self.s.eat_while(is_id_continue); self.s.eat_while(is_id_continue);
match self.s.from(start) { let ident = self.s.from(start);
"none" => SyntaxKind::None,
"auto" => SyntaxKind::Auto, let prev = self.s.get(0..start);
"true" => SyntaxKind::Bool, if !prev.ends_with(['.', '@']) || prev.ends_with("..") {
"false" => SyntaxKind::Bool, if let Some(keyword) = keyword(ident) {
id => keyword(id).unwrap_or(SyntaxKind::Ident), return keyword;
} }
} }
SyntaxKind::Ident
}
fn number(&mut self, start: usize, c: char) -> SyntaxKind { fn number(&mut self, start: usize, c: char) -> SyntaxKind {
// Read the first part (integer or fractional depending on `first`). // Read the first part (integer or fractional depending on `first`).
self.s.eat_while(char::is_ascii_digit); self.s.eat_while(char::is_ascii_digit);
@ -556,6 +559,10 @@ impl Lexer<'_> {
/// Try to parse an identifier into a keyword. /// Try to parse an identifier into a keyword.
fn keyword(ident: &str) -> Option<SyntaxKind> { fn keyword(ident: &str) -> Option<SyntaxKind> {
Some(match ident { Some(match ident {
"none" => SyntaxKind::None,
"auto" => SyntaxKind::Auto,
"true" => SyntaxKind::Bool,
"false" => SyntaxKind::Bool,
"not" => SyntaxKind::Not, "not" => SyntaxKind::Not,
"and" => SyntaxKind::And, "and" => SyntaxKind::And,
"or" => SyntaxKind::Or, "or" => SyntaxKind::Or,

View File

@ -36,6 +36,5 @@
= A = A
--- ---
// Error: 9 expected identifier // Error: 9-13 cannot access fields on type boolean
// Error: 9 expected semicolon or line break
#{false.true} #{false.true}