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

[WIP] Support keyword async #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions ratel-transformer/src/es2015/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<'ast> Visitor<'ast> for TransformArrow<'ast> {
self.ctx.swap(*ptr, Function {
name: OptionalName::empty(),
generator: false,
is_async: false,
params: node.params,
body,
});
Expand Down
1 change: 1 addition & 0 deletions ratel/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl<'ast> From<Option<IdentifierNode<'ast>>> for OptionalName<'ast> {
pub struct Function<'ast, N: Name<'ast>> {
pub name: N,
pub generator: bool,
pub is_async: bool,
pub params: PatternList<'ast>,
pub body: BlockNode<'ast, Statement<'ast>>,
}
Expand Down
7 changes: 7 additions & 0 deletions ratel/src/astgen/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ mod test {
"value": {
"type": "FunctionExpression",
"generator": false,
"is_async": false,
"id": null,
"params": [
{
Expand Down Expand Up @@ -974,6 +975,7 @@ mod test {
{
"type": "FunctionDeclaration",
"generator": false,
"is_async": false,
"id": {
"type": "Identifier",
"name": "Handler",
Expand Down Expand Up @@ -1095,6 +1097,7 @@ mod test {
{
"type": "FunctionDeclaration",
"generator": false,
"is_async": false,
"id": {
"type": "Identifier",
"name": "foo",
Expand Down Expand Up @@ -1376,6 +1379,7 @@ mod test {
{
"type": "FunctionDeclaration",
"generator": false,
"is_async": false,
"id": {
"type": "Identifier",
"name": "foo",
Expand Down Expand Up @@ -1403,6 +1407,7 @@ mod test {
{
"type": "FunctionDeclaration",
"generator": false,
"is_async": false,
"id": {
"type": "Identifier",
"name": "foo",
Expand Down Expand Up @@ -1538,6 +1543,7 @@ mod test {
"value": {
"type": "FunctionExpression",
"generator": false,
"is_async": false,
"id": null,
"params": [],
"body": {
Expand Down Expand Up @@ -1593,6 +1599,7 @@ mod test {
"value": {
"type": "FunctionExpression",
"generator": false,
"is_async": false,
"id": null,
"params": [],
"body": {
Expand Down
1 change: 1 addition & 0 deletions ratel/src/astgen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ where
{
self.in_loc(serializer, N::IN_FUNCTION, 3, |state| {
state.serialize_field("generator", &self.generator)?;
state.serialize_field("is_async", &self.is_async)?;
state.serialize_field("id", &self.name)?;
state.serialize_field("params", &self.params)?;
state.serialize_field("body", &self.body)
Expand Down
3 changes: 3 additions & 0 deletions ratel/src/astgen/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ mod test {
{
"type": "FunctionDeclaration",
"generator": false,
"is_async": false,
"id": {
"type": "Identifier",
"name": "foo",
Expand Down Expand Up @@ -973,6 +974,7 @@ mod test {
{
"type": "FunctionDeclaration",
"generator": true,
"is_async": false,
"id": {
"type": "Identifier",
"name": "foo",
Expand Down Expand Up @@ -1000,6 +1002,7 @@ mod test {
{
"type": "FunctionDeclaration",
"generator": false,
"is_async": false,
"id": {
"type": "Identifier",
"name": "foo",
Expand Down
2 changes: 2 additions & 0 deletions ratel/src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,7 @@ mod test {
let expected = Function {
name: None.into(),
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block()
};
Expand All @@ -1048,6 +1049,7 @@ mod test {
let expected = Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block()
};
Expand Down
41 changes: 34 additions & 7 deletions ratel/src/parser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,40 @@ impl<'ast> Parse<'ast> for Pattern<'ast> {
}
}

impl<'ast, N> Parse<'ast> for Function<'ast, N> where
impl<'ast, N> Function<'ast, N> where
N: Name<'ast> + Parse<'ast, Output = N>,
{
type Output = Self;

#[inline]
fn parse(par: &mut Parser<'ast>) -> Self::Output {
let generator: bool = if par.lexer.token == OperatorMultiplication {
pub fn with_async_flag(par: &mut Parser<'ast>, is_async: bool) -> Function<'ast, N> {
let is_generator: bool = if par.lexer.token == OperatorMultiplication {
par.lexer.consume();
true
} else {
false
};

let name = N::parse(par);

Function {
name,
generator,
generator: is_generator,
is_async,
params: par.params(),
body: par.block(),
}
}
}

impl<'ast, N> Parse<'ast> for Function<'ast, N> where
N: Name<'ast> + Parse<'ast, Output = N>,
{
type Output = Self;

#[inline]
fn parse(par: &mut Parser<'ast>) -> Self::Output {
Self::with_async_flag(par, false)
}
}

impl<'ast, N> Parse<'ast> for Node<'ast, Function<'ast, N>> where
N: Name<'ast> + Parse<'ast, Output = N>,
{
Expand Down Expand Up @@ -400,6 +409,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block(),
}
Expand All @@ -418,6 +428,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: true,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block(),
}
Expand All @@ -434,6 +445,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: true,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block(),
}
Expand All @@ -450,6 +462,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: true,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block(),
}
Expand All @@ -468,6 +481,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("bar"),
Pattern::Identifier("baz"),
Expand All @@ -488,6 +502,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.block([
mock.ptr("bar"),
Expand All @@ -508,6 +523,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: mock.list([
Pattern::AssignmentPattern {
left: mock.ptr(Pattern::Identifier("a")),
Expand Down Expand Up @@ -541,6 +557,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("a"),
Pattern::Identifier("b"),
Expand Down Expand Up @@ -569,6 +586,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: mock.list([
Pattern::RestElement {
argument: mock.ptr("rest"),
Expand All @@ -589,6 +607,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("a"),
Pattern::AssignmentPattern {
Expand Down Expand Up @@ -667,6 +686,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("bar"),
Pattern::Identifier("baz")
Expand Down Expand Up @@ -716,6 +736,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("bar"),
Pattern::Identifier("baz")
Expand All @@ -732,6 +753,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("moon")
]),
Expand All @@ -747,6 +769,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block()
})
Expand All @@ -758,6 +781,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block()
})
Expand All @@ -769,6 +793,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block()
})
Expand Down Expand Up @@ -867,6 +892,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("foo")
]),
Expand All @@ -880,6 +906,7 @@ mod test {
value: mock.ptr(Function {
name: EmptyName,
generator: false,
is_async: false,
params: mock.list([
Pattern::Identifier("bar")
]),
Expand Down
5 changes: 4 additions & 1 deletion ratel/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ impl<'ast> Parser<'ast> {
self.lexer.consume();
ident
},
_ => self.error()
_ => {
println!("eeeee:.....", );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugging? :)

self.error()
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions ratel/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ create_handlers! {
const BRK = |par| par.break_statement();
const THRW = |par| par.throw_statement();
const CONT = |par| par.continue_statement();
const FUNC = |par| par.function_statement();
const FUNC = |par| par.function_statement_with_async(false);
const CLAS = |par| par.class_statement();
const IF = |par| par.if_statement();
const WHL = |par| par.while_statement();
Expand Down Expand Up @@ -190,9 +190,12 @@ impl<'ast> Parser<'ast> {
pub fn labeled_or_expression_statement(&mut self) -> StatementNode<'ast> {
let label = self.lexer.token_as_str();
let (start, end) = self.lexer.loc();

self.lexer.consume();

if label == "async" && self.lexer.token == Function {
return self.function_statement_with_async(true);
}

if self.lexer.token == Colon {
self.lexer.consume();

Expand All @@ -213,9 +216,9 @@ impl<'ast> Parser<'ast> {
}

#[inline]
pub fn function_statement(&mut self) -> StatementNode<'ast> {
pub fn function_statement_with_async(&mut self, is_async: bool) -> StatementNode<'ast> {
let start = self.lexer.start_then_consume();
let function = Function::parse(self);
let function = Function::with_async_flag(self, is_async);

self.alloc_at_loc(start, function.body.end, function)
}
Expand Down Expand Up @@ -1158,6 +1161,7 @@ mod test {
Function {
name: mock.name("foo"),
generator: false,
is_async: false,
params: NodeList::empty(),
body: mock.empty_block(),
}
Expand Down