Browse Source

updates

pull/1/head
jasonwilliams 6 years ago
parent
commit
c21f6818bf
  1. 151
      src/lib/syntax/ast/keyword.rs
  2. 2
      src/lib/syntax/ast/mod.rs
  3. 18
      src/lib/syntax/ast/pos.rs
  4. 3
      src/lib/syntax/ast/token.rs

151
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<Self, Self::Err> {
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<String, String> {
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"),
}
)
}
}

2
src/lib/syntax/ast/mod.rs

@ -1 +1,3 @@
pub mod keyword;
pub mod pos;
pub mod token;

18
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,
}
}
}

3
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),

Loading…
Cancel
Save