diff --git a/Cargo.lock b/Cargo.lock index 4cb6ea307d..822f35cbcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "Boa" version = "0.1.5" diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index 7c181d950c..f4a375bcda 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -2,14 +2,14 @@ //! //! The Lexer splits its input source code into a sequence of input elements called tokens, represented by the [Token](../ast/token/struct.Token.html) structure. //! It also removes whitespace and comments and attaches them to the next token. +use crate::syntax::ast::punc::Punctuator; +use crate::syntax::ast::token::{Token, TokenData}; use std::char::from_u32; use std::error; use std::fmt; use std::iter::Peekable; use std::str::Chars; use std::str::FromStr; -use crate::syntax::ast::punc::Punctuator; -use crate::syntax::ast::token::{Token, TokenData}; #[allow(unused)] macro_rules! vop { @@ -390,8 +390,11 @@ impl<'a> Lexer<'a> { '+' => op!(self, Punctuator::AssignAdd, Punctuator::Add, { '+' => Punctuator::Inc }), - '-' => op!(self, Punctuator::AssignSub, Punctuator::AssignSub, { - '-' => Punctuator::Dec + '-' => op!(self, Punctuator::AssignSub, Punctuator::Sub, { + '-' => { + self.next()?; + Punctuator::Dec + } }), '%' => op!(self, Punctuator::AssignMod, Punctuator::Mod), '|' => op!(self, Punctuator::AssignOr, Punctuator::Or, { diff --git a/tests/lexer_test.rs b/tests/lexer_test.rs index 9e0972eddc..316d35ea10 100644 --- a/tests/lexer_test.rs +++ b/tests/lexer_test.rs @@ -52,3 +52,19 @@ fn check_positions() { assert_eq!(lexer.tokens[6].pos.column_number, 27); assert_eq!(lexer.tokens[6].pos.line_number, 1); } + +// Increment/Decrement +#[test] +fn check_decrement_advances_lexer_2_places() { + // Here we want an example of decrementing an integer + let s = &String::from("let a = b--;"); + let mut lexer = Lexer::new(s); + lexer.lex().expect("finished"); + assert_eq!(lexer.tokens[4].data, TokenData::Punctuator(Punctuator::Dec)); + // Decrementing means adding 2 characters '--', the lexer should consume it as a single token + // and move the curser forward by 2, meaning the next token should be a semicolon + assert_eq!( + lexer.tokens[5].data, + TokenData::Punctuator(Punctuator::Semicolon) + ); +}