Skip to content

Commit 7e8eed6

Browse files
committed
Update rules according to PEP 492 + fix await.
ac317700ce7439e38a8b420218d9a5035bba92ed
1 parent 1f00721 commit 7e8eed6

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ pub enum Expression {
226226
SetComp(Box<SetItem>, Vec<ComprehensionChunk>),
227227
ListComp(Box<SetItem>, Vec<ComprehensionChunk>),
228228
Generator(Box<SetItem>, Vec<ComprehensionChunk>),
229+
Await(Box<Expression>),
229230

230231
Call(Box<Expression>, Vec<Argument>),
231232
Subscript(Box<Expression>, Vec<Subscript>),

src/expressions.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,12 @@ named!(power<StrSpan, Box<Expression>>,
212212
enum Trailer { Call(Vec<Argument>), Subscript(Vec<Subscript>), Attribute(Name) }
213213
impl<ANS: AreNewlinesSpaces> ExpressionParser<ANS> {
214214

215-
// atom_expr: [AWAIT] atom trailer*
215+
// atom_expr: ['await'] atom trailer*
216216
// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
217217
// subscriptlist: subscript (',' subscript)* [',']
218218
named!(atom_expr<StrSpan, Box<Expression>>,
219219
do_parse!(
220+
async: map!(opt!(terminated!(tag!("await"), space_sep!())), |o| o.is_some()) >>
220221
lhs: call!(Self::atom) >>
221222
trailers: fold_many0!(
222223
ws_auto!(alt!(
@@ -231,7 +232,7 @@ named!(atom_expr<StrSpan, Box<Expression>>,
231232
Trailer::Attribute(name) => Expression::Attribute(acc, name),
232233
})
233234
) >> (
234-
trailers
235+
if async { Box::new(Expression::Await(trailers)) } else { trailers }
235236
)
236237
)
237238
);
@@ -530,7 +531,8 @@ named_args!(opt_comp_iter(acc: Vec<ComprehensionChunk>) <StrSpan, Vec<Comprehens
530531
return_error!(map!(opt!(call!(Self::comp_iter, acc.clone())), |r| r.unwrap_or(acc))) // FIXME: do not clone
531532
);
532533

533-
// comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
534+
// sync_comp_for: 'for' exprlist 'in' or_test [comp_iter]
535+
// comp_for: ['async'] sync_comp_for
534536
named!(comp_for<StrSpan, Vec<ComprehensionChunk>>,
535537
call!(Self::comp_for2, Vec::new())
536538
);

src/functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ named_args!(pub decorated(indent: usize) <StrSpan, CompoundStatement>,
4545
* Function definition
4646
*********************************************************************/
4747

48-
// async_funcdef: ASYNC funcdef
48+
// async_funcdef: 'async' funcdef
4949
// funcdef: 'def' NAME parameters ['->' test] ':' suite
5050
named_args!(funcdef(indent: usize, decorators: Vec<Decorator>) <StrSpan, CompoundStatement>,
5151
do_parse!(

src/statements.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ named_args!(pub block(indent: usize) <StrSpan, Vec<Statement>>,
338338
preceded!(
339339
newline,
340340
return_error!(
341-
ErrorKind::Custom(PyParseError::ExpectedIndent.into()),
341+
::nom::ErrorKind::Custom(PyParseError::ExpectedIndent.into()),
342342
do_parse!(
343343
count!(char!(' '), indent) >>
344344
new_spaces: many1!(char!(' ')) >> ({
@@ -386,15 +386,15 @@ named_args!(compound_stmt(indent: usize) <StrSpan, CompoundStatement>,
386386
| "def" => return_error!(call!(decorated, indent))
387387
| "class" => return_error!(call!(decorated, indent))
388388
| "async" => return_error!(alt!(
389-
call!(decorated, indent) // ASYNC funcdef
389+
call!(decorated, indent) // 'async' funcdef
390390
| call!(for_stmt, indent)
391391
))
392392
)
393393
| call!(decorated, indent)
394394
)
395395
);
396396

397-
// async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
397+
// async_stmt: 'async' (funcdef | with_stmt | for_stmt)
398398
// taken care of in other parsers
399399

400400
named_args!(else_block(indent: usize) <StrSpan, Option<Vec<Statement>>>,

src/visitors/printer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ fn format_expr(e: &Expression) -> String {
535535
format!("[{} {}]", format_setitem(e), space_join(comp.iter().map(format_comp))),
536536
Expression::Generator(ref e, ref comp) =>
537537
format!("({} {})", format_setitem(e), space_join(comp.iter().map(format_comp))),
538+
Expression::Await(ref e) =>
539+
format!("await {}", format_expr(e)),
538540

539541
Expression::Call(ref e, ref args) => {
540542
match **e {

0 commit comments

Comments
 (0)