From 495aa5de29795d121e7ac2da1ab9b4c5f97aecd5 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Fri, 17 Aug 2018 14:29:46 +0100 Subject: [PATCH] adding Let --- src/bin/bin.rs | 3 +- src/lib/syntax/ast/keyword.rs | 192 +++++++++++++++++----------------- src/lib/syntax/ast/pos.rs | 2 +- src/lib/syntax/ast/punc.rs | 2 +- src/lib/syntax/ast/token.rs | 26 ++++- src/lib/syntax/lexer.rs | 6 +- test.js | 2 +- 7 files changed, 130 insertions(+), 103 deletions(-) diff --git a/src/bin/bin.rs b/src/bin/bin.rs index dfed6e9f78..fb24e0e8a1 100644 --- a/src/bin/bin.rs +++ b/src/bin/bin.rs @@ -5,5 +5,6 @@ use std::fs::read_to_string; pub fn main() { let buffer = read_to_string("test.js").unwrap(); let mut lexer = Lexer::new(&buffer); - lexer.lex().expect("finished") + lexer.lex().expect("finished"); + println!("{:?}", lexer.tokens); } diff --git a/src/lib/syntax/ast/keyword.rs b/src/lib/syntax/ast/keyword.rs index 773afb4763..69ed5f628d 100644 --- a/src/lib/syntax/ast/keyword.rs +++ b/src/lib/syntax/ast/keyword.rs @@ -4,71 +4,73 @@ use std::fmt::{Display, Formatter}; use std::str::FromStr; use syntax::ast::keyword::Keyword::*; -#[derive(Clone, PartialEq)] +#[derive(Clone, PartialEq, Debug)] /// A Javascript Keyword pub enum Keyword { /// The `break` keyword - KBreak, + Break, /// The `case` keyword - KCase, + Case, /// The `catch` keyword - KCatch, + Catch, /// The `class` keyword, which is reserved for future use - KClass, + Class, /// The `continue` keyword - KContinue, + Continue, /// The `debugger` keyword - KDebugger, + Debugger, /// The `default` keyword - KDefault, + Default, /// The `delete` keyword - KDelete, + Delete, /// The `do` keyword - KDo, + Do, /// The `else` keyword - KElse, + Else, /// The `enum` keyword - KEnum, + Enum, /// The `extends` keyword - KExtends, + Extends, /// The `finally` keyword - KFinally, + Finally, /// The `for` keyword - KFor, + For, /// The `function` keyword - KFunction, + Function, /// The `if` keyword - KIf, + If, /// The `in` keyword - KIn, + In, /// The `instanceof` keyword - KInstanceOf, + InstanceOf, /// The `import` keyword - KImport, + Import, + /// The `let` keyword + Let, /// The `new` keyword - KNew, + New, /// The `return` keyword - KReturn, + Return, /// The `super` keyword - KSuper, + Super, /// The `switch` keyword - KSwitch, + Switch, /// The `this` keyword - KThis, + This, /// The `throw` keyword - KThrow, + Throw, /// The `try` keyword - KTry, + Try, /// The `typeof` keyword - KTypeOf, + TypeOf, /// The `var` keyword - KVar, + Var, /// The `void` keyword - KVoid, + Void, /// The `while` keyword - KWhile, + While, /// The `with` keyword - KWith, + With, } #[derive(Debug, Clone)] @@ -94,37 +96,38 @@ impl FromStr for Keyword { type Err = KeywordError; fn from_str(s: &str) -> Result { match s { - "break" => Ok(KBreak), - "case" => Ok(KCase), - "catch" => Ok(KCatch), - "class" => Ok(KClass), - "continue" => Ok(KContinue), - "debugger" => Ok(KDebugger), - "default" => Ok(KDefault), - "delete" => Ok(KDelete), - "do" => Ok(KDo), - "else" => Ok(KElse), - "enum" => Ok(KEnum), - "extends" => Ok(KExtends), - "finally" => Ok(KFinally), - "for" => Ok(KFor), - "function" => Ok(KFunction), - "if" => Ok(KIf), - "in" => Ok(KIn), - "instanceof" => Ok(KInstanceOf), - "import" => Ok(KImport), - "new" => Ok(KNew), - "return" => Ok(KReturn), - "super" => Ok(KSuper), - "switch" => Ok(KSwitch), - "this" => Ok(KThis), - "throw" => Ok(KThrow), - "try" => Ok(KTry), - "typeof" => Ok(KTypeOf), - "var" => Ok(KVar), - "void" => Ok(KVoid), - "while" => Ok(KWhile), - "with" => Ok(KWith), + "break" => Ok(Break), + "case" => Ok(Case), + "catch" => Ok(Catch), + "class" => Ok(Class), + "continue" => Ok(Continue), + "debugger" => Ok(Debugger), + "default" => Ok(Default), + "delete" => Ok(Delete), + "do" => Ok(Do), + "else" => Ok(Else), + "enum" => Ok(Enum), + "extends" => Ok(Extends), + "finally" => Ok(Finally), + "for" => Ok(For), + "function" => Ok(Function), + "if" => Ok(If), + "in" => Ok(In), + "instanceof" => Ok(InstanceOf), + "import" => Ok(Import), + "let" => Ok(Let), + "new" => Ok(New), + "return" => Ok(Return), + "super" => Ok(Super), + "switch" => Ok(Switch), + "this" => Ok(This), + "throw" => Ok(Throw), + "try" => Ok(Try), + "typeof" => Ok(TypeOf), + "var" => Ok(Var), + "void" => Ok(Void), + "while" => Ok(While), + "with" => Ok(With), _ => Err(KeywordError), } } @@ -135,37 +138,38 @@ impl Display for Keyword { f, "{}", match *self { - KBreak => "break", - KCase => "case", - KCatch => "catch", - KClass => "class", - KContinue => "continue", - KDebugger => "debugger", - KDefault => "default", - KDelete => "delete", - KDo => "do", - KElse => "else", - KEnum => "enum", - KExtends => "extends", - KFinally => "finally", - KFor => "for", - KFunction => "function", - KIf => "if", - KIn => "in", - KInstanceOf => "instanceof", - KImport => "import", - KNew => "new", - KReturn => "return", - KSuper => "super", - KSwitch => "switch", - KThis => "this", - KThrow => "throw", - KTry => "try", - KTypeOf => "typeof", - KVar => "var", - KVoid => "void", - KWhile => "while", - KWith => "with", + Break => "break", + Case => "case", + Catch => "catch", + Class => "class", + Continue => "continue", + Debugger => "debugger", + Default => "default", + Delete => "delete", + Do => "do", + Else => "else", + Enum => "enum", + Extends => "extends", + Finally => "finally", + For => "for", + Function => "function", + If => "if", + In => "in", + InstanceOf => "instanceof", + Import => "import", + Let => "let", + New => "new", + Return => "return", + Super => "super", + Switch => "switch", + This => "this", + Throw => "throw", + Try => "try", + TypeOf => "typeof", + Var => "var", + Void => "void", + While => "while", + With => "with", } ) } diff --git a/src/lib/syntax/ast/pos.rs b/src/lib/syntax/ast/pos.rs index b68c6b6196..af89ebc2d5 100644 --- a/src/lib/syntax/ast/pos.rs +++ b/src/lib/syntax/ast/pos.rs @@ -1,4 +1,4 @@ -#[derive(Clone, PartialEq)] +#[derive(Clone, PartialEq, Debug)] // A position in Javascript source code pub struct Position { // Column number diff --git a/src/lib/syntax/ast/punc.rs b/src/lib/syntax/ast/punc.rs index d48d6bdb51..0210380dbd 100644 --- a/src/lib/syntax/ast/punc.rs +++ b/src/lib/syntax/ast/punc.rs @@ -1,5 +1,5 @@ use std::fmt::{Display, Error, Formatter}; -#[derive(PartialEq, Clone)] +#[derive(PartialEq, Clone, Debug)] /// Punctuation pub enum Punctuator { /// `{` diff --git a/src/lib/syntax/ast/token.rs b/src/lib/syntax/ast/token.rs index a5693a3f2e..f1abfc7a5c 100644 --- a/src/lib/syntax/ast/token.rs +++ b/src/lib/syntax/ast/token.rs @@ -1,13 +1,15 @@ -use std::fmt::{Display, Formatter, Result}; +use std::fmt::{Debug, Display, Formatter, Result}; use syntax::ast::keyword::Keyword; use syntax::ast::pos::Position; use syntax::ast::punc::Punctuator; #[derive(Clone, PartialEq)] /// Represents a token +#[derive(Debug)] pub struct Token { - // // The token + /// The token Data pub data: TokenData, + /// Token position from original source code pub pos: Position, } @@ -21,7 +23,25 @@ impl Token { } } -#[derive(Clone, PartialEq)] +impl Display for Token { + fn fmt(&self, f: &mut Formatter) -> Result { + write!(f, "{}", self.data) + } +} + +pub struct VecToken(Vec); + +impl Debug for VecToken { + fn fmt(&self, f: &mut Formatter) -> Result { + let mut buffer = String::new(); + for token in &self.0 { + buffer.push_str(&token.to_string()); + } + write!(f, "{}", buffer) + } +} + +#[derive(Clone, PartialEq, Debug)] /// Represents the type of Token pub enum TokenData { /// A boolean literal, which is either `true` or `false` diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index 59ea7b3b7e..0a300de52e 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -167,10 +167,12 @@ impl<'a> Lexer<'a> { // Check if we've reached the end match self.preview_next() { Ok(_) => (), // If there are still characters, carry on - Err(LexerError { details }) => { - if details == "finished" { + Err(e) => { + if e.details == "finished" { // If there are no more characters left in the Chars iterator, we should just return return Ok(()); + } else { + return Err(e); } } } diff --git a/test.js b/test.js index e0686e9e23..48ce80e0c0 100644 --- a/test.js +++ b/test.js @@ -1 +1 @@ -console.log('hello world from js'); \ No newline at end of file +let a = (2 * 2) \ No newline at end of file