Skip to content

Commit 49f0d46

Browse files
committed
Support async keyword
1 parent 6372264 commit 49f0d46

File tree

9 files changed

+62
-13
lines changed

9 files changed

+62
-13
lines changed

ratel-transformer/src/es2015/arrow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl<'ast> Visitor<'ast> for TransformArrow<'ast> {
3535
self.ctx.swap(*ptr, Function {
3636
name: OptionalName::empty(),
3737
generator: false,
38+
is_async: false,
3839
params: node.params,
3940
body,
4041
});

ratel/src/ast/function.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl<'ast> From<Option<IdentifierNode<'ast>>> for OptionalName<'ast> {
6666
pub struct Function<'ast, N: Name<'ast>> {
6767
pub name: N,
6868
pub generator: bool,
69+
pub is_async: bool,
6970
pub params: PatternList<'ast>,
7071
pub body: BlockNode<'ast, Statement<'ast>>,
7172
}

ratel/src/astgen/expression.rs

+7
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ mod test {
927927
"value": {
928928
"type": "FunctionExpression",
929929
"generator": false,
930+
"is_async": false,
930931
"id": null,
931932
"params": [
932933
{
@@ -974,6 +975,7 @@ mod test {
974975
{
975976
"type": "FunctionDeclaration",
976977
"generator": false,
978+
"is_async": false,
977979
"id": {
978980
"type": "Identifier",
979981
"name": "Handler",
@@ -1095,6 +1097,7 @@ mod test {
10951097
{
10961098
"type": "FunctionDeclaration",
10971099
"generator": false,
1100+
"is_async": false,
10981101
"id": {
10991102
"type": "Identifier",
11001103
"name": "foo",
@@ -1376,6 +1379,7 @@ mod test {
13761379
{
13771380
"type": "FunctionDeclaration",
13781381
"generator": false,
1382+
"is_async": false,
13791383
"id": {
13801384
"type": "Identifier",
13811385
"name": "foo",
@@ -1403,6 +1407,7 @@ mod test {
14031407
{
14041408
"type": "FunctionDeclaration",
14051409
"generator": false,
1410+
"is_async": false,
14061411
"id": {
14071412
"type": "Identifier",
14081413
"name": "foo",
@@ -1538,6 +1543,7 @@ mod test {
15381543
"value": {
15391544
"type": "FunctionExpression",
15401545
"generator": false,
1546+
"is_async": false,
15411547
"id": null,
15421548
"params": [],
15431549
"body": {
@@ -1593,6 +1599,7 @@ mod test {
15931599
"value": {
15941600
"type": "FunctionExpression",
15951601
"generator": false,
1602+
"is_async": false,
15961603
"id": null,
15971604
"params": [],
15981605
"body": {

ratel/src/astgen/function.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ where
122122
{
123123
self.in_loc(serializer, N::IN_FUNCTION, 3, |state| {
124124
state.serialize_field("generator", &self.generator)?;
125+
state.serialize_field("is_async", &self.is_async)?;
125126
state.serialize_field("id", &self.name)?;
126127
state.serialize_field("params", &self.params)?;
127128
state.serialize_field("body", &self.body)

ratel/src/astgen/statement.rs

+3
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ mod test {
946946
{
947947
"type": "FunctionDeclaration",
948948
"generator": false,
949+
"is_async": false,
949950
"id": {
950951
"type": "Identifier",
951952
"name": "foo",
@@ -973,6 +974,7 @@ mod test {
973974
{
974975
"type": "FunctionDeclaration",
975976
"generator": true,
977+
"is_async": false,
976978
"id": {
977979
"type": "Identifier",
978980
"name": "foo",
@@ -1000,6 +1002,7 @@ mod test {
10001002
{
10011003
"type": "FunctionDeclaration",
10021004
"generator": false,
1005+
"is_async": false,
10031006
"id": {
10041007
"type": "Identifier",
10051008
"name": "foo",

ratel/src/parser/expression.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ mod test {
10331033
let expected = Function {
10341034
name: None.into(),
10351035
generator: false,
1036+
is_async: false,
10361037
params: NodeList::empty(),
10371038
body: mock.empty_block()
10381039
};
@@ -1048,6 +1049,7 @@ mod test {
10481049
let expected = Function {
10491050
name: mock.name("foo"),
10501051
generator: false,
1052+
is_async: false,
10511053
params: NodeList::empty(),
10521054
body: mock.empty_block()
10531055
};

ratel/src/parser/function.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,40 @@ impl<'ast> Parse<'ast> for Pattern<'ast> {
5959
}
6060
}
6161

62-
impl<'ast, N> Parse<'ast> for Function<'ast, N> where
62+
impl<'ast, N> Function<'ast, N> where
6363
N: Name<'ast> + Parse<'ast, Output = N>,
6464
{
65-
type Output = Self;
66-
6765
#[inline]
68-
fn parse(par: &mut Parser<'ast>) -> Self::Output {
69-
let generator: bool = if par.lexer.token == OperatorMultiplication {
66+
pub fn with_async_flag(par: &mut Parser<'ast>, is_async: bool) -> Function<'ast, N> {
67+
let is_generator: bool = if par.lexer.token == OperatorMultiplication {
7068
par.lexer.consume();
7169
true
7270
} else {
7371
false
7472
};
75-
7673
let name = N::parse(par);
7774

7875
Function {
7976
name,
80-
generator,
77+
generator: is_generator,
78+
is_async,
8179
params: par.params(),
8280
body: par.block(),
8381
}
8482
}
8583
}
8684

85+
impl<'ast, N> Parse<'ast> for Function<'ast, N> where
86+
N: Name<'ast> + Parse<'ast, Output = N>,
87+
{
88+
type Output = Self;
89+
90+
#[inline]
91+
fn parse(par: &mut Parser<'ast>) -> Self::Output {
92+
Self::with_async_flag(par, false)
93+
}
94+
}
95+
8796
impl<'ast, N> Parse<'ast> for Node<'ast, Function<'ast, N>> where
8897
N: Name<'ast> + Parse<'ast, Output = N>,
8998
{
@@ -400,6 +409,7 @@ mod test {
400409
Function {
401410
name: mock.name("foo"),
402411
generator: false,
412+
is_async: false,
403413
params: NodeList::empty(),
404414
body: mock.empty_block(),
405415
}
@@ -418,6 +428,7 @@ mod test {
418428
Function {
419429
name: mock.name("foo"),
420430
generator: true,
431+
is_async: false,
421432
params: NodeList::empty(),
422433
body: mock.empty_block(),
423434
}
@@ -434,6 +445,7 @@ mod test {
434445
Function {
435446
name: mock.name("foo"),
436447
generator: true,
448+
is_async: false,
437449
params: NodeList::empty(),
438450
body: mock.empty_block(),
439451
}
@@ -450,6 +462,7 @@ mod test {
450462
Function {
451463
name: mock.name("foo"),
452464
generator: true,
465+
is_async: false,
453466
params: NodeList::empty(),
454467
body: mock.empty_block(),
455468
}
@@ -468,6 +481,7 @@ mod test {
468481
Function {
469482
name: mock.name("foo"),
470483
generator: false,
484+
is_async: false,
471485
params: mock.list([
472486
Pattern::Identifier("bar"),
473487
Pattern::Identifier("baz"),
@@ -488,6 +502,7 @@ mod test {
488502
Function {
489503
name: mock.name("foo"),
490504
generator: false,
505+
is_async: false,
491506
params: NodeList::empty(),
492507
body: mock.block([
493508
mock.ptr("bar"),
@@ -508,6 +523,7 @@ mod test {
508523
Function {
509524
name: mock.name("foo"),
510525
generator: false,
526+
is_async: false,
511527
params: mock.list([
512528
Pattern::AssignmentPattern {
513529
left: mock.ptr(Pattern::Identifier("a")),
@@ -541,6 +557,7 @@ mod test {
541557
Function {
542558
name: mock.name("foo"),
543559
generator: false,
560+
is_async: false,
544561
params: mock.list([
545562
Pattern::Identifier("a"),
546563
Pattern::Identifier("b"),
@@ -569,6 +586,7 @@ mod test {
569586
Function {
570587
name: mock.name("foo"),
571588
generator: false,
589+
is_async: false,
572590
params: mock.list([
573591
Pattern::RestElement {
574592
argument: mock.ptr("rest"),
@@ -589,6 +607,7 @@ mod test {
589607
Function {
590608
name: mock.name("foo"),
591609
generator: false,
610+
is_async: false,
592611
params: mock.list([
593612
Pattern::Identifier("a"),
594613
Pattern::AssignmentPattern {
@@ -667,6 +686,7 @@ mod test {
667686
value: mock.ptr(Function {
668687
name: EmptyName,
669688
generator: false,
689+
is_async: false,
670690
params: mock.list([
671691
Pattern::Identifier("bar"),
672692
Pattern::Identifier("baz")
@@ -716,6 +736,7 @@ mod test {
716736
value: mock.ptr(Function {
717737
name: EmptyName,
718738
generator: false,
739+
is_async: false,
719740
params: mock.list([
720741
Pattern::Identifier("bar"),
721742
Pattern::Identifier("baz")
@@ -732,6 +753,7 @@ mod test {
732753
value: mock.ptr(Function {
733754
name: EmptyName,
734755
generator: false,
756+
is_async: false,
735757
params: mock.list([
736758
Pattern::Identifier("moon")
737759
]),
@@ -747,6 +769,7 @@ mod test {
747769
value: mock.ptr(Function {
748770
name: EmptyName,
749771
generator: false,
772+
is_async: false,
750773
params: NodeList::empty(),
751774
body: mock.empty_block()
752775
})
@@ -758,6 +781,7 @@ mod test {
758781
value: mock.ptr(Function {
759782
name: EmptyName,
760783
generator: false,
784+
is_async: false,
761785
params: NodeList::empty(),
762786
body: mock.empty_block()
763787
})
@@ -769,6 +793,7 @@ mod test {
769793
value: mock.ptr(Function {
770794
name: EmptyName,
771795
generator: false,
796+
is_async: false,
772797
params: NodeList::empty(),
773798
body: mock.empty_block()
774799
})
@@ -867,6 +892,7 @@ mod test {
867892
value: mock.ptr(Function {
868893
name: EmptyName,
869894
generator: false,
895+
is_async: false,
870896
params: mock.list([
871897
Pattern::Identifier("foo")
872898
]),
@@ -880,6 +906,7 @@ mod test {
880906
value: mock.ptr(Function {
881907
name: EmptyName,
882908
generator: false,
909+
is_async: false,
883910
params: mock.list([
884911
Pattern::Identifier("bar")
885912
]),

ratel/src/parser/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ impl<'ast> Parser<'ast> {
171171
self.lexer.consume();
172172
ident
173173
},
174-
_ => self.error()
174+
_ => {
175+
println!("eeeee:.....", );
176+
self.error()
177+
}
175178
}
176179
}
177180

ratel/src/parser/statement.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ create_handlers! {
8080
const BRK = |par| par.break_statement();
8181
const THRW = |par| par.throw_statement();
8282
const CONT = |par| par.continue_statement();
83-
const FUNC = |par| par.function_statement();
83+
const FUNC = |par| par.function_statement_with_async(false);
8484
const CLAS = |par| par.class_statement();
8585
const IF = |par| par.if_statement();
8686
const WHL = |par| par.while_statement();
@@ -190,9 +190,12 @@ impl<'ast> Parser<'ast> {
190190
pub fn labeled_or_expression_statement(&mut self) -> StatementNode<'ast> {
191191
let label = self.lexer.token_as_str();
192192
let (start, end) = self.lexer.loc();
193-
193+
194194
self.lexer.consume();
195-
195+
if label == "async" && self.lexer.token == Function {
196+
return self.function_statement_with_async(true);
197+
}
198+
196199
if self.lexer.token == Colon {
197200
self.lexer.consume();
198201

@@ -213,9 +216,9 @@ impl<'ast> Parser<'ast> {
213216
}
214217

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

220223
self.alloc_at_loc(start, function.body.end, function)
221224
}
@@ -1158,6 +1161,7 @@ mod test {
11581161
Function {
11591162
name: mock.name("foo"),
11601163
generator: false,
1164+
is_async: false,
11611165
params: NodeList::empty(),
11621166
body: mock.empty_block(),
11631167
}

0 commit comments

Comments
 (0)