From c21f6818bf78df35aea918f0409cb648f6ff6507 Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Thu, 9 Aug 2018 19:23:02 +0100 Subject: [PATCH] updates --- src/lib/syntax/ast/keyword.rs | 151 ++++++++++++++++++++++++++++++++++ src/lib/syntax/ast/mod.rs | 2 + src/lib/syntax/ast/pos.rs | 18 ++++ src/lib/syntax/ast/token.rs | 3 + 4 files changed, 174 insertions(+) create mode 100644 src/lib/syntax/ast/keyword.rs create mode 100644 src/lib/syntax/ast/pos.rs diff --git a/src/lib/syntax/ast/keyword.rs b/src/lib/syntax/ast/keyword.rs new file mode 100644 index 0000000000..c990f7551a --- /dev/null +++ b/src/lib/syntax/ast/keyword.rs @@ -0,0 +1,151 @@ +use std::fmt::{Display, Formatter}; +use std::str::FromStr; +use syntax::ast::keyword::Keyword::*; + +#[derive(Clone, PartialEq)] +/// A Javascript Keyword +pub enum Keyword { + /// The `break` keyword + KBreak, + /// The `case` keyword + KCase, + /// The `catch` keyword + KCatch, + /// The `class` keyword, which is reserved for future use + KClass, + /// The `continue` keyword + KContinue, + /// The `debugger` keyword + KDebugger, + /// The `default` keyword + KDefault, + /// The `delete` keyword + KDelete, + /// The `do` keyword + KDo, + /// The `else` keyword + KElse, + /// The `enum` keyword + KEnum, + /// The `extends` keyword + KExtends, + /// The `finally` keyword + KFinally, + /// The `for` keyword + KFor, + /// The `function` keyword + KFunction, + /// The `if` keyword + KIf, + /// The `in` keyword + KIn, + /// The `instanceof` keyword + KInstanceOf, + /// The `import` keyword + KImport, + /// The `new` keyword + KNew, + /// The `return` keyword + KReturn, + /// The `super` keyword + KSuper, + /// The `switch` keyword + KSwitch, + /// The `this` keyword + KThis, + /// The `throw` keyword + KThrow, + /// The `try` keyword + KTry, + /// The `typeof` keyword + KTypeOf, + /// The `var` keyword + KVar, + /// The `void` keyword + KVoid, + /// The `while` keyword + KWhile, + /// The `with` keyword + KWith, +} + +impl FromStr for Keyword { + 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), + _ => Err("Oh dear!"), + } + } +} +impl Display for Keyword { + fn fmt(&self, f: &mut Formatter) -> Result { + write!( + 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", + _ => Err("ahgh"), + } + ) + } +} diff --git a/src/lib/syntax/ast/mod.rs b/src/lib/syntax/ast/mod.rs index 79c66ba635..de429baf9d 100644 --- a/src/lib/syntax/ast/mod.rs +++ b/src/lib/syntax/ast/mod.rs @@ -1 +1,3 @@ +pub mod keyword; +pub mod pos; pub mod token; diff --git a/src/lib/syntax/ast/pos.rs b/src/lib/syntax/ast/pos.rs new file mode 100644 index 0000000000..b68c6b6196 --- /dev/null +++ b/src/lib/syntax/ast/pos.rs @@ -0,0 +1,18 @@ +#[derive(Clone, PartialEq)] +// A position in Javascript source code +pub struct Position { + // Column number + pub column_number: u64, + // Line number + pub line_number: u64, +} + +impl Position { + // Create a new position + pub fn new(line_number: u64, column_number: u64) -> Position { + Position { + line_number: line_number, + column_number: column_number, + } + } +} diff --git a/src/lib/syntax/ast/token.rs b/src/lib/syntax/ast/token.rs index f1af432737..d6501d0058 100644 --- a/src/lib/syntax/ast/token.rs +++ b/src/lib/syntax/ast/token.rs @@ -1,4 +1,6 @@ use std::fmt::{Display, Formatter, Result}; +use syntax::ast::keyword::Keyword; +use syntax::ast::pos::Position; #[derive(Clone, PartialEq)] pub struct Token { @@ -7,6 +9,7 @@ pub struct Token { pub pos: Position, } +#[derive(Clone, PartialEq)] pub enum TokenData { /// A boolean literal, which is either `true` or `false` TBooleanLiteral(bool),