Warn against === and !==.

This commit is contained in:
Stephen Chung 2020-04-22 17:36:51 +08:00
parent 9a1c715aad
commit 7df36033c4
2 changed files with 32 additions and 6 deletions

View File

@ -19,6 +19,8 @@ pub enum LexError {
MalformedChar(String),
/// An identifier is in an invalid format.
MalformedIdentifier(String),
/// Bad keyword encountered when tokenizing the script text.
ImproperKeyword(String),
}
impl Error for LexError {}
@ -32,6 +34,7 @@ impl fmt::Display for LexError {
Self::MalformedChar(s) => write!(f, "Invalid character: '{}'", s),
Self::MalformedIdentifier(s) => write!(f, "Variable name is not proper: '{}'", s),
Self::UnterminatedString => write!(f, "Open string is not terminated"),
Self::ImproperKeyword(s) => write!(f, "{}", s),
}
}
}

View File

@ -361,10 +361,9 @@ impl Token {
use Token::*;
match self {
Equals | PlusAssign | MinusAssign | MultiplyAssign | DivideAssign | LeftShiftAssign
| RightShiftAssign | AndAssign | OrAssign | XOrAssign | ModuloAssign
| PowerOfAssign => 10,
// Equals | PlusAssign | MinusAssign | MultiplyAssign | DivideAssign | LeftShiftAssign
// | RightShiftAssign | AndAssign | OrAssign | XOrAssign | ModuloAssign
// | PowerOfAssign => 10,
Or | XOr | Pipe => 40,
And | Ampersand => 50,
@ -670,7 +669,7 @@ impl<'a> TokenIterator<'a> {
.map(Token::IntegerConstant)
.unwrap_or_else(|_| {
Token::LexError(Box::new(LERR::MalformedNumber(
result.iter().collect(),
result.into_iter().collect(),
)))
}),
pos,
@ -686,7 +685,7 @@ impl<'a> TokenIterator<'a> {
return Some((
num.unwrap_or_else(|_| {
Token::LexError(Box::new(LERR::MalformedNumber(
result.iter().collect(),
result.into_iter().collect(),
)))
}),
pos,
@ -880,6 +879,18 @@ impl<'a> TokenIterator<'a> {
('=', '=') => {
self.eat_next();
// Warn against `===`
if self.peek_next() == Some('=') {
return Some((
Token::LexError(Box::new(LERR::ImproperKeyword(
"'===' is not a valid operator. This is not JavaScript! Should it be '=='?"
.to_string(),
))),
pos,
));
}
return Some((Token::EqualsTo, pos));
}
('=', _) => return Some((Token::Equals, pos)),
@ -924,6 +935,18 @@ impl<'a> TokenIterator<'a> {
('!', '=') => {
self.eat_next();
// Warn against `!==`
if self.peek_next() == Some('=') {
return Some((
Token::LexError(Box::new(LERR::ImproperKeyword(
"'!==' is not a valid operator. This is not JavaScript! Should it be '!='?"
.to_string(),
))),
pos,
));
}
return Some((Token::NotEqualsTo, pos));
}
('!', _) => return Some((Token::Bang, pos)),