diff --git a/Cargo.toml b/Cargo.toml index a74d8e04e5..a5c5125f79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,6 @@ authors = ["Jason Williams "] name = "js" path = "src/lib/lib.rs" -[[bin]] -name = "js" -path = "src/bin/bin.rs" +# [[bin]] +# name = "js" +# path = "src/bin/bin.rs" diff --git a/src/lib/syntax/ast/keyword.rs b/src/lib/syntax/ast/keyword.rs index f572a8adb5..773afb4763 100644 --- a/src/lib/syntax/ast/keyword.rs +++ b/src/lib/syntax/ast/keyword.rs @@ -72,15 +72,15 @@ pub enum Keyword { } #[derive(Debug, Clone)] -struct TokenError; -impl Display for TokenError { +pub struct KeywordError; +impl Display for KeywordError { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { write!(f, "invalid token") } } // This is important for other errors to wrap this one. -impl error::Error for TokenError { +impl error::Error for KeywordError { fn description(&self) -> &str { "invalid token" } @@ -91,7 +91,7 @@ impl error::Error for TokenError { } } impl FromStr for Keyword { - type Err = TokenError; + type Err = KeywordError; fn from_str(s: &str) -> Result { match s { "break" => Ok(KBreak), @@ -125,7 +125,7 @@ impl FromStr for Keyword { "void" => Ok(KVoid), "while" => Ok(KWhile), "with" => Ok(KWith), - _ => Err(TokenError), + _ => Err(KeywordError), } } } diff --git a/src/lib/syntax/ast/mod.rs b/src/lib/syntax/ast/mod.rs index de429baf9d..a9cf49ed81 100644 --- a/src/lib/syntax/ast/mod.rs +++ b/src/lib/syntax/ast/mod.rs @@ -1,3 +1,4 @@ pub mod keyword; pub mod pos; +pub mod punc; pub mod token; diff --git a/src/lib/syntax/ast/punc.rs b/src/lib/syntax/ast/punc.rs new file mode 100644 index 0000000000..c7ca41ac0b --- /dev/null +++ b/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 => "=>", + } + ) + } +} diff --git a/src/lib/syntax/ast/token.rs b/src/lib/syntax/ast/token.rs index d6501d0058..cd916f1ff6 100644 --- a/src/lib/syntax/ast/token.rs +++ b/src/lib/syntax/ast/token.rs @@ -1,6 +1,7 @@ use std::fmt::{Display, Formatter, Result}; use syntax::ast::keyword::Keyword; use syntax::ast::pos::Position; +use syntax::ast::punc::Punctuator; #[derive(Clone, PartialEq)] pub struct Token { @@ -37,10 +38,10 @@ impl Display for TokenData { fn fmt(&self, f: &mut Formatter) -> Result { match self.clone() { 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::TKeyword(word) => write!(f, "{}", word), - TNullLiteral => write!(f, "null"), + TokenData::TNullLiteral => write!(f, "null"), TokenData::TNumericLiteral(num) => write!(f, "{}", num), TokenData::TPunctuator(punc) => write!(f, "{}", punc), TokenData::TStringLiteral(lit) => write!(f, "{}", lit), diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 4c391509c3..0000000000 --- a/src/main.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - println!("Hello, worlddnbkjxnkjx!"); -} -