Skip to content

Commit

Permalink
Added reserved word checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Nov 5, 2022
1 parent fea4aa9 commit caeab76
Show file tree
Hide file tree
Showing 28 changed files with 612 additions and 429 deletions.
5 changes: 4 additions & 1 deletion boa_ast/src/declaration/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use boa_interner::Sym;
///
/// [spec]: https://tc39.es/ecma262/#prod-ExportDeclaration
#[derive(Debug, Clone, Copy)]
pub struct ExportDeclaration;
pub enum ExportDeclaration {
/// List of exports.
ExportList,
}

/// Export specifier
///
Expand Down
4 changes: 4 additions & 0 deletions boa_engine/src/syntax/lexer/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ pub enum Error {
}

impl From<io::Error> for Error {
#[inline]
fn from(err: io::Error) -> Self {
Self::IO(err)
}
}

impl Error {
/// Creates a new syntax error.
#[inline]
pub(super) fn syntax<M, P>(err: M, pos: P) -> Self
where
M: Into<Box<str>>,
Expand All @@ -41,6 +43,7 @@ impl Error {
}

impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IO(e) => write!(f, "I/O error: {e}"),
Expand All @@ -50,6 +53,7 @@ impl fmt::Display for Error {
}

impl StdError for Error {
#[inline]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::IO(err) => Some(err),
Expand Down
53 changes: 49 additions & 4 deletions boa_engine/src/syntax/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Token {
}

/// Converts the token to a `String`.
#[inline]
pub(crate) fn to_string(&self, interner: &Interner) -> String {
self.kind.to_string(interner)
}
Expand Down Expand Up @@ -95,16 +96,33 @@ pub enum TokenKind {
/// The end of the file.
EOF,

/// An identifier.
Identifier(Sym),
/// An identifier name.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-IdentifierName
IdentifierName(Sym),

/// A private identifier.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-PrivateIdentifier
PrivateIdentifier(Sym),

/// A keyword and a flag if the keyword contains unicode escaped chars.
///
/// For more information, see [`Keyword`].
Keyword((Keyword, bool)),

/// A `null` literal.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-NullLiteral
NullLiteral,

/// A numeric literal.
Expand All @@ -114,6 +132,11 @@ pub enum TokenKind {
Punctuator(Punctuator),

/// A string literal.
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-StringLiteral
StringLiteral(Sym),

/// A part of a template literal without substitution.
Expand All @@ -126,53 +149,66 @@ pub enum TokenKind {
RegularExpressionLiteral(Sym, Sym),

/// Indicates the end of a line (`\n`).
///
/// More information:
/// - [ECMAScript specification][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-LineTerminator
LineTerminator,

/// Indicates a comment, the content isn't stored.
Comment,
}

impl From<bool> for TokenKind {
#[inline]
fn from(oth: bool) -> Self {
Self::BooleanLiteral(oth)
}
}

impl From<(Keyword, bool)> for TokenKind {
#[inline]
fn from(kw: (Keyword, bool)) -> Self {
Self::Keyword(kw)
}
}

impl From<Punctuator> for TokenKind {
#[inline]
fn from(punc: Punctuator) -> Self {
Self::Punctuator(punc)
}
}

impl From<Numeric> for TokenKind {
#[inline]
fn from(num: Numeric) -> Self {
Self::NumericLiteral(num)
}
}

impl TokenKind {
/// Creates a `BooleanLiteral` token kind.
#[inline]
pub fn boolean_literal(lit: bool) -> Self {
Self::BooleanLiteral(lit)
}

/// Creates an `EOF` token kind.
#[inline]
pub fn eof() -> Self {
Self::EOF
}

/// Creates an `Identifier` token type.
#[inline]
pub fn identifier(ident: Sym) -> Self {
Self::Identifier(ident)
Self::IdentifierName(ident)
}

/// Creates a `NumericLiteral` token kind.
#[inline]
pub fn numeric_literal<L>(lit: L) -> Self
where
L: Into<Numeric>,
Expand All @@ -181,34 +217,43 @@ impl TokenKind {
}

/// Creates a `Punctuator` token type.
#[inline]
pub fn punctuator(punc: Punctuator) -> Self {
Self::Punctuator(punc)
}

/// Creates a `StringLiteral` token type.
#[inline]
pub fn string_literal(lit: Sym) -> Self {
Self::StringLiteral(lit)
}

/// Creates a `TemplateMiddle` token type.
#[inline]
pub fn template_middle(template_string: TemplateString) -> Self {
Self::TemplateMiddle(template_string)
}

/// Creates a `TemplateNoSubstitution` token type.
#[inline]
pub fn template_no_substitution(template_string: TemplateString) -> Self {
Self::TemplateNoSubstitution(template_string)
}

/// Creates a `RegularExpressionLiteral` token kind.
#[inline]
pub fn regular_expression_literal(body: Sym, flags: Sym) -> Self {
Self::RegularExpressionLiteral(body, flags)
}

/// Creates a `LineTerminator` token kind.
#[inline]
pub fn line_terminator() -> Self {
Self::LineTerminator
}

/// Creates a 'Comment' token kind.
#[inline]
pub fn comment() -> Self {
Self::Comment
}
Expand All @@ -218,7 +263,7 @@ impl TokenKind {
match *self {
Self::BooleanLiteral(val) => val.to_string(),
Self::EOF => "end of file".to_owned(),
Self::Identifier(ident) => interner.resolve_expect(ident).to_string(),
Self::IdentifierName(ident) => interner.resolve_expect(ident).to_string(),
Self::PrivateIdentifier(ident) => format!("#{}", interner.resolve_expect(ident)),
Self::Keyword((word, _)) => word.to_string(),
Self::NullLiteral => "null".to_owned(),
Expand Down
5 changes: 3 additions & 2 deletions boa_engine/src/syntax/parser/expression/assignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ where
.parse(cursor, interner)
}
// ArrowFunction[?In, ?Yield, ?Await] -> ArrowParameters[?Yield, ?Await] -> BindingIdentifier[?Yield, ?Await]
TokenKind::Identifier(_) | TokenKind::Keyword((Keyword::Yield | Keyword::Await, _)) => {
TokenKind::IdentifierName(_)
| TokenKind::Keyword((Keyword::Yield | Keyword::Await, _)) => {
cursor.set_goal(InputElement::Div);

// Because we already peeked the identifier token, there may be a line terminator before the identifier token.
Expand Down Expand Up @@ -160,7 +161,7 @@ where
.peek(1, interner)?
.ok_or(ParseError::AbruptEnd)?
.kind(),
TokenKind::Identifier(_)
TokenKind::IdentifierName(_)
| TokenKind::Keyword((Keyword::Yield | Keyword::Await, _))
| TokenKind::Punctuator(Punctuator::OpenParen)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ where
.parse(cursor, interner)?;
Ok(Yield::new(Some(expr), true).into())
}
TokenKind::Identifier(_)
TokenKind::IdentifierName(_)
| TokenKind::Punctuator(
Punctuator::OpenParen
| Punctuator::Add
Expand Down
Loading

0 comments on commit caeab76

Please sign in to comment.