Browse Source

fixing decrementation and testing

pull/7/head
Jason Williams 6 years ago
parent
commit
1f2815bea8
  1. 2
      Cargo.lock
  2. 11
      src/lib/syntax/lexer.rs
  3. 16
      tests/lexer_test.rs

2
Cargo.lock generated

@ -1,3 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]] [[package]]
name = "Boa" name = "Boa"
version = "0.1.5" version = "0.1.5"

11
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. //! 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. //! 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::char::from_u32;
use std::error; use std::error;
use std::fmt; use std::fmt;
use std::iter::Peekable; use std::iter::Peekable;
use std::str::Chars; use std::str::Chars;
use std::str::FromStr; use std::str::FromStr;
use crate::syntax::ast::punc::Punctuator;
use crate::syntax::ast::token::{Token, TokenData};
#[allow(unused)] #[allow(unused)]
macro_rules! vop { macro_rules! vop {
@ -390,8 +390,11 @@ impl<'a> Lexer<'a> {
'+' => op!(self, Punctuator::AssignAdd, Punctuator::Add, { '+' => op!(self, Punctuator::AssignAdd, Punctuator::Add, {
'+' => Punctuator::Inc '+' => Punctuator::Inc
}), }),
'-' => op!(self, Punctuator::AssignSub, Punctuator::AssignSub, { '-' => op!(self, Punctuator::AssignSub, Punctuator::Sub, {
'-' => Punctuator::Dec '-' => {
self.next()?;
Punctuator::Dec
}
}), }),
'%' => op!(self, Punctuator::AssignMod, Punctuator::Mod), '%' => op!(self, Punctuator::AssignMod, Punctuator::Mod),
'|' => op!(self, Punctuator::AssignOr, Punctuator::Or, { '|' => op!(self, Punctuator::AssignOr, Punctuator::Or, {

16
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.column_number, 27);
assert_eq!(lexer.tokens[6].pos.line_number, 1); 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)
);
}

Loading…
Cancel
Save