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

[Merged by Bors] - Allow some keywords as identifiers #2269

Closed
wants to merge 6 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ fn class_declaration_elements() {
}
set e(value) {}
get e() {}
set(a, b) {}
get(a, b) {}
};
"#,
);
Expand Down
4 changes: 4 additions & 0 deletions boa_engine/src/syntax/parser/expression/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ where
TokenKind::Keyword((Keyword::Await, _)) if !self.allow_await.0 => {
Ok(Identifier::new(Sym::AWAIT))
}
TokenKind::Keyword((Keyword::Async, _)) => Ok(Identifier::new(Sym::ASYNC)),
TokenKind::Keyword((Keyword::Of, _)) => Ok(Identifier::new(Sym::OF)),
_ => Err(ParseError::unexpected(
token.to_string(interner),
token.span(),
Expand Down Expand Up @@ -217,6 +219,8 @@ where
))
}
TokenKind::Keyword((Keyword::Await, _)) if !self.allow_await.0 => Ok(Sym::AWAIT),
TokenKind::Keyword((Keyword::Async, _)) => Ok(Sym::ASYNC),
TokenKind::Keyword((Keyword::Of, _)) => Ok(Sym::OF),
_ => Err(ParseError::expected(
["identifier".to_owned()],
next_token.to_string(interner),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ where
.ok_or(ParseError::AbruptEnd)?
.kind()
{
TokenKind::Identifier(_) | TokenKind::Keyword((Keyword::Yield | Keyword::Await, _)) => {
Some(BindingIdentifier::new(false, false).parse(cursor, interner)?)
}
TokenKind::Identifier(_)
| TokenKind::Keyword((
Keyword::Yield | Keyword::Await | Keyword::Async | Keyword::Of,
_,
)) => Some(BindingIdentifier::new(false, false).parse(cursor, interner)?),
_ => self.name,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,60 @@ fn check_nested_function_expression() {
interner,
);
}

#[test]
fn check_function_non_reserved_keyword() {
let genast = |keyword, interner: &mut Interner| {
vec![DeclarationList::Const(
vec![Declaration::new_with_identifier(
interner.get_or_intern_static("add"),
Some(
FunctionExpr::new::<_, _, StatementList>(
Some(interner.get_or_intern_static(keyword)),
FormalParameterList::default(),
vec![Return::new::<_, _, Option<Sym>>(Const::from(1), None).into()].into(),
)
.into(),
),
)]
.into(),
)
.into()]
};

let mut interner = Interner::default();
let ast = genast("as", &mut interner);
check_parser("const add = function as() { return 1; };", ast, interner);

let mut interner = Interner::default();
let ast = genast("async", &mut interner);
check_parser("const add = function async() { return 1; };", ast, interner);

let mut interner = Interner::default();
let ast = genast("from", &mut interner);
check_parser("const add = function from() { return 1; };", ast, interner);

let mut interner = Interner::default();
let ast = genast("get", &mut interner);
check_parser("const add = function get() { return 1; };", ast, interner);

let mut interner = Interner::default();
let ast = genast("meta", &mut interner);
check_parser("const add = function meta() { return 1; };", ast, interner);

let mut interner = Interner::default();
let ast = genast("of", &mut interner);
check_parser("const add = function of() { return 1; };", ast, interner);

let mut interner = Interner::default();
let ast = genast("set", &mut interner);
check_parser("const add = function set() { return 1; };", ast, interner);

let mut interner = Interner::default();
let ast = genast("target", &mut interner);
check_parser(
"const add = function target() { return 1; };",
ast,
interner,
);
}
50 changes: 33 additions & 17 deletions boa_engine/src/syntax/parser/expression/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ where
// TODO: tok currently consumes the token instead of peeking, so the token
// isn't passed and consumed by parsers according to spec (EX: GeneratorExpression)
let tok = cursor.peek(0, interner)?.ok_or(ParseError::AbruptEnd)?;
let tok_position = tok.span().start();

match tok.kind() {
TokenKind::Keyword((Keyword::This | Keyword::Async, true)) => Err(ParseError::general(
TokenKind::Keyword((Keyword::This, true)) => Err(ParseError::general(
"Keyword must not contain escaped characters",
tok.span().start(),
tok_position,
)),
TokenKind::Keyword((Keyword::This, false)) => {
cursor.next(interner).expect("token disappeared");
Expand All @@ -126,17 +127,31 @@ where
ClassExpression::new(self.name, self.allow_yield, self.allow_await)
.parse(cursor, interner)
}
TokenKind::Keyword((Keyword::Async, false)) => {
cursor.next(interner).expect("token disappeared");
let mul_peek = cursor.peek(1, interner)?.ok_or(ParseError::AbruptEnd)?;
if mul_peek.kind() == &TokenKind::Punctuator(Punctuator::Mul) {
AsyncGeneratorExpression::new(self.name)
.parse(cursor, interner)
.map(Node::from)
} else {
AsyncFunctionExpression::new(self.name, self.allow_yield)
TokenKind::Keyword((Keyword::Async, contain_escaped_char)) => {
let contain_escaped_char = *contain_escaped_char;
match cursor.peek(1, interner)?.map(Token::kind) {
Some(TokenKind::Keyword((Keyword::Function, _))) if contain_escaped_char => {
Err(ParseError::general(
"Keyword must not contain escaped characters",
tok_position,
))
}
Some(TokenKind::Keyword((Keyword::Function, _))) => {
cursor.next(interner).expect("token disappeared");
match cursor.peek(1, interner)?.map(Token::kind) {
Some(TokenKind::Punctuator(Punctuator::Mul)) => {
AsyncGeneratorExpression::new(self.name)
.parse(cursor, interner)
.map(Node::from)
}
_ => AsyncFunctionExpression::new(self.name, self.allow_yield)
.parse(cursor, interner)
.map(Node::from),
}
}
_ => IdentifierReference::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)
.map(Node::from)
.map(Node::from),
}
}
TokenKind::Punctuator(Punctuator::OpenParen) => {
Expand Down Expand Up @@ -174,11 +189,12 @@ where
Ok(Const::Null.into())
}
TokenKind::Identifier(_)
| TokenKind::Keyword((Keyword::Let | Keyword::Yield | Keyword::Await, _)) => {
IdentifierReference::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)
.map(Node::from)
}
| TokenKind::Keyword((
Keyword::Let | Keyword::Yield | Keyword::Await | Keyword::Of,
_,
)) => IdentifierReference::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)
.map(Node::from),
TokenKind::StringLiteral(lit) => {
let node = Const::from(*lit).into();
cursor.next(interner).expect("token disappeared");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,22 @@ where
}

//Async [AsyncMethod, AsyncGeneratorMethod] object methods
let is_keyword = !matches!(
cursor
.peek(1, interner)?
.ok_or(ParseError::AbruptEnd)?
.kind(),
TokenKind::Punctuator(Punctuator::OpenParen | Punctuator::Colon)
);
let token = cursor.peek(0, interner)?.ok_or(ParseError::AbruptEnd)?;
match token.kind() {
TokenKind::Keyword((Keyword::Async, true)) => {
TokenKind::Keyword((Keyword::Async, true)) if is_keyword => {
return Err(ParseError::general(
"Keyword must not contain escaped characters",
token.span().start(),
));
}
TokenKind::Keyword((Keyword::Async, false)) => {
TokenKind::Keyword((Keyword::Async, false)) if is_keyword => {
cursor.next(interner)?.expect("token disappeared");
cursor.peek_expect_no_lineterminator(0, "Async object methods", interner)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::syntax::{
node::{
object::{MethodDefinition, PropertyDefinition},
AsyncFunctionExpr, AsyncGeneratorExpr, Declaration, DeclarationList, FormalParameter,
FormalParameterList, FormalParameterListFlags, FunctionExpr, Identifier, Object,
FormalParameterList, FormalParameterListFlags, FunctionExpr, Identifier, Node, Object,
},
Const,
},
Expand Down Expand Up @@ -447,3 +447,59 @@ fn check_async_gen_method_lineterminator() {
",
);
}

#[test]
fn check_async_ordinary_method() {
let mut interner = Interner::default();

let object_properties = vec![PropertyDefinition::method_definition(
MethodDefinition::Ordinary(FunctionExpr::new(
None,
FormalParameterList::default(),
vec![],
)),
Node::Const(Const::from(interner.get_or_intern_static("async"))),
)];

check_parser(
"const x = {
async() {}
};
",
vec![DeclarationList::Const(
vec![Declaration::new_with_identifier(
interner.get_or_intern_static("x"),
Some(Object::from(object_properties).into()),
)]
.into(),
)
.into()],
interner,
);
}

#[test]
fn check_async_property() {
let mut interner = Interner::default();

let object_properties = vec![PropertyDefinition::property(
Node::Const(Const::from(interner.get_or_intern_static("async"))),
Const::from(true),
)];

check_parser(
"const x = {
async: true
};
",
vec![DeclarationList::Const(
vec![Declaration::new_with_identifier(
interner.get_or_intern_static("x"),
Some(Object::from(object_properties).into()),
)]
.into(),
)
.into()],
interner,
);
}
28 changes: 28 additions & 0 deletions boa_engine/src/syntax/parser/expression/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,31 @@ fn check_logical_expressions() {
check_invalid("a ?? b || c");
check_invalid("a || b ?? c");
}

#[track_caller]
fn check_non_reserved_identifier(keyword: &'static str) {
let mut interner = Interner::default();
check_parser(
format!("({})", keyword).as_str(),
vec![Identifier::new(interner.get_or_intern_static(keyword)).into()],
interner,
);
}

#[test]
fn check_non_reserved_identifiers() {
// https://tc39.es/ecma262/#sec-keywords-and-reserved-words
// Those that are always allowed as identifiers, but also appear as
// keywords within certain syntactic productions, at places where
// Identifier is not allowed: as, async, from, get, meta, of, set,
// and target.

check_non_reserved_identifier("as");
check_non_reserved_identifier("async");
check_non_reserved_identifier("from");
check_non_reserved_identifier("get");
check_non_reserved_identifier("meta");
check_non_reserved_identifier("of");
check_non_reserved_identifier("set");
check_non_reserved_identifier("target");
}
Loading