Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reserve some keywords that might be needed in the future #314

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ The Koto project adheres to

## [0.15.0] Unreleased

### Changed

#### Language

- `await`, `const`, and `let` have been reserved as keywords for future use.

## [0.14.0] 2024.04.17

Expand Down
10 changes: 9 additions & 1 deletion crates/lexer/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum Token {
Less,
LessOrEqual,

// Pipe tokens are created at the parsing time tokens
// Pipe is detected by the parser instead of the lexer
Pipe,

// Keywords
Expand Down Expand Up @@ -93,6 +93,11 @@ pub enum Token {
Until,
While,
Yield,

// Reserved keywords
Await,
Const,
Let,
}

impl Token {
Expand Down Expand Up @@ -639,8 +644,10 @@ impl<'a> TokenLexer<'a> {
if !matches!(self.previous_token, Some(Token::Dot)) {
check_keyword!("as", As);
check_keyword!("and", And);
check_keyword!("await", Await);
check_keyword!("break", Break);
check_keyword!("catch", Catch);
check_keyword!("const", Const);
check_keyword!("continue", Continue);
check_keyword!("debug", Debug);
check_keyword!("export", Export);
Expand All @@ -651,6 +658,7 @@ impl<'a> TokenLexer<'a> {
check_keyword!("if", If);
check_keyword!("import", Import);
check_keyword!("in", In);
check_keyword!("let", Let);
check_keyword!("loop", Loop);
check_keyword!("match", Match);
check_keyword!("not", Not);
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ pub enum SyntaxError {
MatchEllipsisOutsideOfNestedPatterns,
#[error("'else' can only be used in the last arm in a match expression")]
MatchElseNotInLastArm,
#[error("Keyword reserved for future use")]
ReservedKeyword,
#[error("'self' doesn't need to be declared as an argument")]
SelfArg,
#[error("'else' can only be used in the last arm in a switch expression")]
Expand Down
5 changes: 5 additions & 0 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,11 @@ impl<'source> Parser<'source> {
Token::From | Token::Import => self.consume_import(context),
Token::Export => self.consume_export(context),
Token::Try => self.consume_try_expression(context),
// Reserved keywords
Token::Await => self.consume_token_and_error(SyntaxError::ReservedKeyword),
Token::Const => self.consume_token_and_error(SyntaxError::ReservedKeyword),
Token::Let => self.consume_token_and_error(SyntaxError::ReservedKeyword),
// An error occurred in the lexer
Token::Error => self.consume_token_and_error(SyntaxError::LexerError),
_ => return Ok(None),
};
Expand Down
19 changes: 19 additions & 0 deletions crates/parser/tests/parsing_failures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,24 @@ switch
check_parsing_fails("from foo import bar as");
}
}

mod reserved_keywords {
use super::*;

#[test]
fn r#await() {
check_parsing_fails("await = 99");
}

#[test]
fn r#const() {
check_parsing_fails("const = 99");
}

#[test]
fn r#let() {
check_parsing_fails("let = 99");
}
}
}
}
Loading