Browse Source

added punc and cleaned up errors

pull/1/head
Jason Williams 6 years ago
parent
commit
fd2999f857
  1. 6
      Cargo.toml
  2. 10
      src/lib/syntax/ast/keyword.rs
  3. 1
      src/lib/syntax/ast/mod.rs
  4. 162
      src/lib/syntax/ast/punc.rs
  5. 5
      src/lib/syntax/ast/token.rs
  6. 4
      src/main.rs

6
Cargo.toml

@ -9,6 +9,6 @@ authors = ["Jason Williams <jase.williams@gmail.com>"]
name = "js" name = "js"
path = "src/lib/lib.rs" path = "src/lib/lib.rs"
[[bin]] # [[bin]]
name = "js" # name = "js"
path = "src/bin/bin.rs" # path = "src/bin/bin.rs"

10
src/lib/syntax/ast/keyword.rs

@ -72,15 +72,15 @@ pub enum Keyword {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct TokenError; pub struct KeywordError;
impl Display for TokenError { impl Display for KeywordError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "invalid token") write!(f, "invalid token")
} }
} }
// This is important for other errors to wrap this one. // This is important for other errors to wrap this one.
impl error::Error for TokenError { impl error::Error for KeywordError {
fn description(&self) -> &str { fn description(&self) -> &str {
"invalid token" "invalid token"
} }
@ -91,7 +91,7 @@ impl error::Error for TokenError {
} }
} }
impl FromStr for Keyword { impl FromStr for Keyword {
type Err = TokenError; type Err = KeywordError;
fn from_str(s: &str) -> Result<Keyword, Self::Err> { fn from_str(s: &str) -> Result<Keyword, Self::Err> {
match s { match s {
"break" => Ok(KBreak), "break" => Ok(KBreak),
@ -125,7 +125,7 @@ impl FromStr for Keyword {
"void" => Ok(KVoid), "void" => Ok(KVoid),
"while" => Ok(KWhile), "while" => Ok(KWhile),
"with" => Ok(KWith), "with" => Ok(KWith),
_ => Err(TokenError), _ => Err(KeywordError),
} }
} }
} }

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

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

162
src/lib/syntax/ast/punc.rs

@ -0,0 +1,162 @@
use std::fmt::{Display, Error, Formatter};
#[derive(PartialEq, Clone)]
/// Punctuation
pub enum Punctuator {
/// `{`
POpenBlock,
/// `}`
PCloseBlock,
/// `(`
POpenParen,
/// `)`
PCloseParen,
/// `[`
POpenBracket,
/// `]`
PCloseBracket,
/// `.`
PDot,
/// `;`
PSemicolon,
/// `,`
PComma,
/// `<`
PLessThan,
/// `>`
PGreaterThan,
/// `<=`
PLessThanOrEq,
/// `>=`
PGreaterThanOrEq,
/// `==`
PEq,
/// `!=`
PNotEq,
/// `===`
PStrictEq,
/// `!==`
PStrictNotEq,
/// `+`
PAdd,
/// `-`
PSub,
/// `*`
PMul,
/// `/`
PDiv,
/// `%`
PMod,
/// `++`
PInc,
/// `--`
PDec,
/// `<<`
PLeftSh,
/// `>>`
PRightSh,
/// `>>>`
PURightSh,
/// `&`
PAnd,
/// `|`
POr,
/// `^`
PXor,
/// `!`
PNot,
/// `~`
PNeg,
/// `&&`
PBoolAnd,
/// `||`
PBoolOr,
/// `?`
PQuestion,
/// `:`
PColon,
/// `=`
PAssign,
/// `+=`
PAssignAdd,
/// `-=`
PAssignSub,
/// `*=`
PAssignMul,
/// `/=`
PAssignDiv,
/// `%=`
PAssignMod,
/// `<<=`
PAssignLeftSh,
/// `>>=`
PAssignRightSh,
/// `>>>=`
PAssignURightSh,
/// `&=`
PAssignAnd,
/// `|=`
PAssignOr,
/// `^=`
PAssignXor,
/// `=>`
PArrow,
}
impl Display for Punctuator {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(
f,
"{}",
match self {
Punctuator::POpenBlock => "{",
Punctuator::PCloseBlock => "}",
Punctuator::POpenParen => "(",
Punctuator::PCloseParen => ")",
Punctuator::POpenBracket => "[",
Punctuator::PCloseBracket => "]",
Punctuator::PDot => ".",
Punctuator::PSemicolon => ";",
Punctuator::PComma => ",",
Punctuator::PLessThan => "<",
Punctuator::PGreaterThan => ">",
Punctuator::PLessThanOrEq => "<=",
Punctuator::PGreaterThanOrEq => ">=",
Punctuator::PEq => "==",
Punctuator::PNotEq => "!=",
Punctuator::PStrictEq => "===",
Punctuator::PStrictNotEq => "!==",
Punctuator::PAdd => "+",
Punctuator::PSub => "-",
Punctuator::PMul => "*",
Punctuator::PDiv => "/",
Punctuator::PMod => "%",
Punctuator::PInc => "++",
Punctuator::PDec => "--",
Punctuator::PLeftSh => "<<",
Punctuator::PRightSh => ">>",
Punctuator::PURightSh => ">>>",
Punctuator::PAnd => "&",
Punctuator::POr => "|",
Punctuator::PXor => "^",
Punctuator::PNot => "!",
Punctuator::PNeg => "~",
Punctuator::PBoolAnd => "&&",
Punctuator::PBoolOr => "||",
Punctuator::PQuestion => "?",
Punctuator::PColon => ":",
Punctuator::PAssign => "=",
Punctuator::PAssignAdd => "+=",
Punctuator::PAssignSub => "-=",
Punctuator::PAssignMul => "*=",
Punctuator::PAssignDiv => "/=",
Punctuator::PAssignMod => "%=",
Punctuator::PAssignLeftSh => "<<=",
Punctuator::PAssignRightSh => ">>=",
Punctuator::PAssignURightSh => ">>>=",
Punctuator::PAssignAnd => "&=",
Punctuator::PAssignOr => "|=",
Punctuator::PAssignXor => "^=",
Punctuator::PArrow => "=>",
}
)
}
}

5
src/lib/syntax/ast/token.rs

@ -1,6 +1,7 @@
use std::fmt::{Display, Formatter, Result}; use std::fmt::{Display, Formatter, Result};
use syntax::ast::keyword::Keyword; use syntax::ast::keyword::Keyword;
use syntax::ast::pos::Position; use syntax::ast::pos::Position;
use syntax::ast::punc::Punctuator;
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct Token { pub struct Token {
@ -37,10 +38,10 @@ impl Display for TokenData {
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
match self.clone() { match self.clone() {
TokenData::TBooleanLiteral(val) => write!(f, "{}", val), TokenData::TBooleanLiteral(val) => write!(f, "{}", val),
TEOF => write!(f, "end of file"), TokenData::TEOF => write!(f, "end of file"),
TokenData::TIdentifier(ident) => write!(f, "{}", ident), TokenData::TIdentifier(ident) => write!(f, "{}", ident),
TokenData::TKeyword(word) => write!(f, "{}", word), TokenData::TKeyword(word) => write!(f, "{}", word),
TNullLiteral => write!(f, "null"), TokenData::TNullLiteral => write!(f, "null"),
TokenData::TNumericLiteral(num) => write!(f, "{}", num), TokenData::TNumericLiteral(num) => write!(f, "{}", num),
TokenData::TPunctuator(punc) => write!(f, "{}", punc), TokenData::TPunctuator(punc) => write!(f, "{}", punc),
TokenData::TStringLiteral(lit) => write!(f, "{}", lit), TokenData::TStringLiteral(lit) => write!(f, "{}", lit),

4
src/main.rs

@ -1,4 +0,0 @@
fn main() {
println!("Hello, worlddnbkjxnkjx!");
}
Loading…
Cancel
Save