Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to dump the token stream or ast in bin. #278

Merged
merged 8 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added serde serialization and deserialization to token and the ast.
  • Loading branch information
HalidOdat committed Mar 17, 2020
commit 1ca6c267683b5e39fd855a951eff1fb16a71acc0
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ default = ["wasm-bindgen"]
gc = "0.3.3"
gc_derive = "0.3.2"
serde_json = "1.0.46"
serde = { version = "1.0", features = ["derive"] }
rand = "0.7.3"
regex = "1.3.4"

Expand Down
2 changes: 2 additions & 0 deletions boa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::{
syntax::{ast::expr::Expr, lexer::Lexer, parser::Parser},
};

pub use serde_json;

fn parser_expr(src: &str) -> Result<Expr, String> {
let mut lexer = Lexer::new(src);
lexer.lex().map_err(|e| format!("SyntaxError: {}", e))?;
Expand Down
3 changes: 2 additions & 1 deletion boa/src/syntax/ast/constant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use gc_derive::{Finalize, Trace};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result};

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A Javascript Constant
pub enum Const {
/// A UTF-8 string, such as `"Hello, world"`
Expand Down
5 changes: 3 additions & 2 deletions boa/src/syntax/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use crate::syntax::ast::{
op::{BinOp, Operator, UnaryOp},
};
use gc_derive::{Finalize, Trace};
use serde::{Deserialize, Serialize};
use std::{
collections::btree_map::BTreeMap,
fmt::{Display, Formatter, Result},
};

#[derive(Clone, Trace, Finalize, Debug, PartialEq)]
#[derive(Clone, Trace, Finalize, Debug, PartialEq, Serialize, Deserialize)]
pub struct Expr {
/// The expression definition
pub def: ExprDef,
Expand All @@ -27,7 +28,7 @@ impl Display for Expr {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A Javascript Expression
pub enum ExprDef {
/// Run a operation between 2 expressions
Expand Down
3 changes: 2 additions & 1 deletion boa/src/syntax/ast/keyword.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use serde::{Deserialize, Serialize};
use std::{
error,
fmt::{Display, Error, Formatter},
str::FromStr,
};

#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
/// A Javascript Keyword
/// As specificed by <https://www.ecma-international.org/ecma-262/#sec-keywords>
pub enum Keyword {
Expand Down
15 changes: 8 additions & 7 deletions boa/src/syntax/ast/op.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use gc_derive::{Finalize, Trace};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result};

/// Represents an operator
Expand All @@ -13,7 +14,7 @@ pub trait Operator {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A numeric operation between 2 values
pub enum NumOp {
/// `a + b` - Addition
Expand Down Expand Up @@ -47,7 +48,7 @@ impl Display for NumOp {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A unary operation on a single value
pub enum UnaryOp {
/// `a++` - increment the value
Expand Down Expand Up @@ -88,7 +89,7 @@ impl Display for UnaryOp {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A bitwise operation between 2 values
pub enum BitOp {
/// `a & b` - Bitwise and
Expand Down Expand Up @@ -119,7 +120,7 @@ impl Display for BitOp {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A comparitive operation between 2 values
pub enum CompOp {
/// `a == b` - Equality
Expand Down Expand Up @@ -159,7 +160,7 @@ impl Display for CompOp {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A logical operation between 2 boolean values
pub enum LogOp {
/// `a && b` - Logical and
Expand All @@ -181,7 +182,7 @@ impl Display for LogOp {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A binary operation between 2 values
pub enum BinOp {
/// Numeric operation
Expand Down Expand Up @@ -240,7 +241,7 @@ impl Display for BinOp {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Trace, Finalize, PartialEq, Serialize, Deserialize)]
/// A binary operation between 2 values
pub enum AssignOp {
/// `a += b` - Add assign
Expand Down
4 changes: 3 additions & 1 deletion boa/src/syntax/ast/pos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Clone, Copy, PartialEq, Debug)]
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
/// A position in the Javascript source code
/// Stores both the column number and the line number
///
Expand Down
3 changes: 2 additions & 1 deletion boa/src/syntax/ast/punc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Error, Formatter};

#[derive(PartialEq, Clone, Copy, Debug)]
#[derive(PartialEq, Clone, Copy, Debug, Serialize, Deserialize)]
/// Punctuation
pub enum Punctuator {
/// `+`
Expand Down
5 changes: 3 additions & 2 deletions boa/src/syntax/ast/token.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::syntax::ast::{keyword::Keyword, pos::Position, punc::Punctuator};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter, Result};

#[derive(Clone, PartialEq)]
/// Represents a token
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Token {
/// The token Data
pub data: TokenData,
Expand Down Expand Up @@ -39,7 +40,7 @@ impl Debug for VecToken {
}
}

#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
/// Represents the type of Token
pub enum TokenData {
/// A boolean literal, which is either `true` or `false`
Expand Down