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

Improved parser error messages #261

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl RegularFunction {
}
}

#[derive(Finalize, Clone)]
/// Represents a native javascript function in memory
#[derive(Finalize, Clone)]
pub struct NativeFunction {
/// The fields associated with the function
pub object: Object,
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use std::{
str::FromStr,
};

#[must_use]
/// The result of a Javascript expression is represented like this so it can succeed (`Ok`) or fail (`Err`)
#[must_use]
pub type ResultValue = Result<Value, Value>;

/// A Garbage-collected Javascript value as represented in the interpreter
pub type Value = Gc<ValueData>;

Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/ast/constant.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use gc_derive::{Finalize, Trace};
use std::fmt::{Display, Formatter, Result};

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A Javascript Constant
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum Const {
/// A UTF-8 string, such as `"Hello, world"`
String(String),
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl Display for Expr {
}
}

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A Javascript Expression
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum ExprDef {
/// Run a operation between 2 expressions
BinOp(BinOp, Box<Expr>, Box<Expr>),
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/ast/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::{
str::FromStr,
};

#[derive(Clone, Copy, PartialEq, Debug)]
/// A Javascript Keyword
/// As specificed by <https://www.ecma-international.org/ecma-262/#sec-keywords>
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Keyword {
/// The `await` keyword
Await,
Expand Down
14 changes: 7 additions & 7 deletions boa/src/syntax/ast/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub trait Operator {
}
}

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

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

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

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

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

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

#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
/// A binary operation between 2 values
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum AssignOp {
/// `a += b` - Add assign
Add,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/ast/pos.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[derive(Clone, Copy, PartialEq, Debug)]
/// A position in the Javascript source code
/// Stores both the column number and the line number
///
/// ## Similar Implementations
/// [V8: Location](https://cs.chromium.org/chromium/src/v8/src/parsing/scanner.h?type=cs&q=isValid+Location&g=0&l=216)
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Position {
// Column number
pub column_number: u64,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/ast/punc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::{Display, Error, Formatter};

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

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

#[derive(Clone, PartialEq, Debug)]
/// Represents the type of Token
#[derive(Clone, PartialEq, Debug)]
pub enum TokenData {
/// A boolean literal, which is either `true` or `false`
BooleanLiteral(bool),
Expand Down
36 changes: 27 additions & 9 deletions boa/src/syntax/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::syntax::ast::{
expr::{Expr, ExprDef},
keyword::Keyword,
op::{AssignOp, BinOp, BitOp, CompOp, LogOp, NumOp, Operator, UnaryOp},
pos::Position,
punc::Punctuator,
token::{Token, TokenData},
};
Expand All @@ -17,9 +18,9 @@ pub enum ParseError {
/// When it expected a certain kind of token, but got another as part of something
Expected(Vec<TokenData>, Token, &'static str),
/// When it expected a certain expression, but got another
ExpectedExpr(&'static str, Expr),
ExpectedExpr(&'static str, Expr, Position),
/// When it didn't expect this keyword
UnexpectedKeyword(Keyword),
UnexpectedKeyword(Keyword, Position),
/// When there is an abrupt end to the parsing
AbruptEnd,
}
Expand All @@ -39,10 +40,16 @@ impl fmt::Display for ParseError {
actual.pos.line_number,
actual.pos.column_number
),
ParseError::ExpectedExpr(expected, actual) => {
write!(f, "Expected expression '{}', got '{}'", expected, actual)
}
ParseError::UnexpectedKeyword(keyword) => write!(f, "Unexpected keyword: {}", keyword),
ParseError::ExpectedExpr(expected, actual, pos) => write!(
f,
"Expected expression '{}', got '{}' at line {}, col {}",
expected, actual, pos.line_number, pos.column_number
),
ParseError::UnexpectedKeyword(keyword, pos) => write!(
f,
"Unexpected keyword '{}' at line {}, col {}",
keyword, pos.line_number, pos.column_number
),
ParseError::AbruptEnd => write!(f, "Abrupt End"),
}
}
Expand Down Expand Up @@ -230,12 +237,16 @@ impl Parser {
_ => Ok(Expr::new(ExprDef::Return(Some(Box::new(self.parse()?))))),
},
Keyword::New => {
let start_pos = self.pos;
let call = self.parse()?;
match call.def {
ExprDef::Call(ref func, ref args) => {
Ok(Expr::new(ExprDef::Construct(func.clone(), args.clone())))
}
_ => Err(ParseError::ExpectedExpr("constructor", call)),
_ => {
let token = self.get_token(start_pos)?;
Err(ParseError::ExpectedExpr("constructor", call, token.pos))
}
}
}
Keyword::TypeOf => Ok(Expr::new(ExprDef::TypeOf(Box::new(self.parse()?)))),
Expand Down Expand Up @@ -357,7 +368,10 @@ impl Parser {
Box::new(block),
)))
}
_ => Err(ParseError::UnexpectedKeyword(keyword)),
_ => {
let token = self.get_token(self.pos - 1)?; // Gets the offending token
Err(ParseError::UnexpectedKeyword(keyword, token.pos))
}
}
}

Expand Down Expand Up @@ -780,14 +794,18 @@ impl Parser {
result = self.binop(BinOp::Assign(AssignOp::Mod), expr)?
}
TokenData::Punctuator(Punctuator::Arrow) => {
let start_pos = self.pos;
self.pos += 1;
let mut args = Vec::with_capacity(1);
match result.def {
ExprDef::Local(ref name) => {
args.push(Expr::new(ExprDef::Local((*name).clone())))
}
ExprDef::UnaryOp(UnaryOp::Spread, _) => args.push(result),
_ => return Err(ParseError::ExpectedExpr("identifier", result)),
_ => {
let token = self.get_token(start_pos)?;
return Err(ParseError::ExpectedExpr("identifier", result, token.pos));
}
}
let next = self.parse()?;
result = Expr::new(ExprDef::ArrowFunctionDecl(args, Box::new(next)));
Expand Down