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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ see [CHANGELOG](./CHANGELOG.md)

```
USAGE:
boa_cli [FILE]...
boa_cli [OPTIONS] [FILE]...

FLAGS:
-h, --help Prints help information
-V, --version Prints version information

OPTIONS:
-a, --dump-ast <FORMAT> Dump the ast to stdout with the given format [possible values: Debug, Json,
JsonPretty]
-t, --dump-tokens <FORMAT> Dump the token stream to stdout with the given format [possible values: Debug, Json,
JsonPretty]

ARGS:
<FILE>... The JavaScript file(s) to be evaluated
```
Expand Down
2 changes: 2 additions & 0 deletions boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exclude = ["../.vscode/*", "../Dockerfile", "../Makefile", "../.editorConfig"]
edition = "2018"

[features]
serde-ast = ["serde"]
default = ["wasm-bindgen"]

[dependencies]
Expand All @@ -22,6 +23,7 @@ regex = "1.3.4"

# Optional Dependencies
wasm-bindgen = { version = "0.2.58", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]
criterion = "0.3.1"
Expand Down
3 changes: 3 additions & 0 deletions boa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use crate::{
syntax::{ast::expr::Expr, lexer::Lexer, parser::Parser},
};

#[cfg(feature = "serde-ast")]
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
4 changes: 4 additions & 0 deletions boa/src/syntax/ast/constant.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use gc_derive::{Finalize, Trace};
use std::fmt::{Display, Formatter, Result};

#[cfg(feature = "serde-ast")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A Javascript Constant
pub enum Const {
Expand Down
5 changes: 5 additions & 0 deletions boa/src/syntax/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use std::{
fmt::{Display, Formatter, Result},
};

#[cfg(feature = "serde-ast")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Trace, Finalize, Debug, PartialEq)]
pub struct Expr {
/// The expression definition
Expand All @@ -27,6 +31,7 @@ impl Display for Expr {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A Javascript Expression
pub enum ExprDef {
Expand Down
4 changes: 4 additions & 0 deletions boa/src/syntax/ast/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::{
str::FromStr,
};

#[cfg(feature = "serde-ast")]
use serde::{Deserialize, Serialize};

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

#[cfg(feature = "serde-ast")]
use serde::{Deserialize, Serialize};

/// Represents an operator
pub trait Operator {
/// Get the associativity as a boolean that is true if it goes rightwards
Expand All @@ -13,6 +16,7 @@ pub trait Operator {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A numeric operation between 2 values
pub enum NumOp {
Expand Down Expand Up @@ -47,6 +51,7 @@ impl Display for NumOp {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A unary operation on a single value
pub enum UnaryOp {
Expand Down Expand Up @@ -88,6 +93,7 @@ impl Display for UnaryOp {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A bitwise operation between 2 values
pub enum BitOp {
Expand Down Expand Up @@ -119,6 +125,7 @@ impl Display for BitOp {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A comparitive operation between 2 values
pub enum CompOp {
Expand Down Expand Up @@ -159,6 +166,7 @@ impl Display for CompOp {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A logical operation between 2 boolean values
pub enum LogOp {
Expand All @@ -181,6 +189,7 @@ impl Display for LogOp {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A binary operation between 2 values
pub enum BinOp {
Expand Down Expand Up @@ -240,6 +249,7 @@ impl Display for BinOp {
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A binary operation between 2 values
pub enum AssignOp {
Expand Down
4 changes: 4 additions & 0 deletions boa/src/syntax/ast/pos.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(feature = "serde-ast")]
use serde::{Deserialize, Serialize};

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

#[cfg(feature = "serde-ast")]
use serde::{Deserialize, Serialize};

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

#[derive(Clone, PartialEq)]
#[cfg(feature = "serde-ast")]
use serde::{Deserialize, Serialize};

/// Represents a token
#[derive(Debug)]
#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
/// The token Data
pub data: TokenData,
Expand Down Expand Up @@ -38,7 +41,7 @@ impl Debug for VecToken {
write!(f, "{}", buffer)
}
}

#[cfg_attr(feature = "serde-ast", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Debug)]
/// Represents the type of Token
pub enum TokenData {
Expand Down
2 changes: 1 addition & 1 deletion boa_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ exclude = ["../.vscode/*", "../Dockerfile", "../Makefile", "../.editorConfig"]
edition = "2018"

[dependencies]
Boa = { path = "../boa", default-features = false }
Boa = { path = "../boa", features = ["serde-ast"], default-features = false }
structopt = "0.3.9"