Browse Source

Merge pull request #7 from jasonwilliams/decrementFix

fixing decrementation and testing
pull/8/head
Jason Williams 6 years ago committed by GitHub
parent
commit
af28263ea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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]]
name = "Boa"
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.
//! 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, {

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.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