Skip to content

Commit ae3eada

Browse files
committedSep 9, 2020
Add TYPE_COMMENT in the grammar excerpt in comments.
Completely ignored for now as I don't want to prioritize this, but needed in order to keep up with other evolutions of the grammar. cpython commit: dcfcd146f8e6fc5c2fc16a4c192a0c5f5ca8c53c
1 parent 3fb429c commit ae3eada

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed
 

‎src/functions.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use nom::IResult;
55
use ast::*;
66
use expressions::ExpressionParser;
77
use helpers::*;
8-
use statements::{block, ImportParser};
8+
use statements::{block, func_body_suite, ImportParser};
99

1010
/*********************************************************************
1111
* Decorators
@@ -46,7 +46,7 @@ named_args!(pub decorated(indent: usize) <StrSpan, CompoundStatement>,
4646
*********************************************************************/
4747

4848
// async_funcdef: 'async' funcdef
49-
// funcdef: 'def' NAME parameters ['->' test] ':' suite
49+
// funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] func_body_suite
5050
named_args!(funcdef(indent: usize, decorators: Vec<Decorator>) <StrSpan, CompoundStatement>,
5151
do_parse!(
5252
indent!(indent) >>
@@ -57,7 +57,7 @@ named_args!(funcdef(indent: usize, decorators: Vec<Decorator>) <StrSpan, Compoun
5757
parameters: ws_nonl!(parameters) >>
5858
return_type: opt!(ws_nonl!(preceded!(tag!("->"), call!(ExpressionParser::<NewlinesAreNotSpaces>::test)))) >>
5959
ws_nonl!(char!(':')) >>
60-
code: call!(block, indent) >> (
60+
code: call!(func_body_suite, indent) >> (
6161
CompoundStatement::Funcdef(Funcdef {
6262
async: async.is_some(), decorators, name, parameters, return_type: return_type.map(|t| *t), code
6363
})
@@ -195,11 +195,11 @@ named!(parameters<StrSpan, TypedArgsList>,
195195
map!(delimited!(char!('('), opt!(ws_comm!(typedargslist)), char!(')')), |o| o.unwrap_or_default())
196196
);
197197

198-
// typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
199-
// '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
200-
// | '**' tfpdef [',']]]
201-
// | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
202-
// | '**' tfpdef [','])
198+
// typedargslist: (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [
199+
// '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])
200+
// | '**' tfpdef [','] [TYPE_COMMENT]]])
201+
// | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])
202+
// | '**' tfpdef [','] [TYPE_COMMENT])
203203
//
204204
// tfpdef: NAME [':' test]
205205
//

‎src/statements.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ named!(small_stmt<StrSpan, Statement>,
5757
*********************************************************************/
5858

5959
// expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
60-
// ('=' (yield_expr|testlist_star_expr))*)
60+
// [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
6161
// annassign: ':' test ['=' (yield_expr|testlist)]
6262
named!(expr_stmt<StrSpan, Statement>,
6363
do_parse!(
@@ -354,6 +354,12 @@ named_args!(cond_and_block(indent: usize) <StrSpan, (Expression, Vec<Statement>)
354354
))
355355
);
356356

357+
// func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT
358+
// for now, we ignore type comments, so it's exactly the same as 'suite'
359+
named_args!(pub func_body_suite(indent: usize) <StrSpan, Vec<Statement>>,
360+
call!(block, indent)
361+
);
362+
357363
// compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
358364
named_args!(compound_stmt(indent: usize) <StrSpan, CompoundStatement>,
359365
alt!(
@@ -419,7 +425,7 @@ named_args!(while_stmt(indent: usize) <StrSpan, CompoundStatement>,
419425
)
420426
);
421427

422-
// for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
428+
// for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite]
423429
named_args!(for_stmt(indent: usize) <StrSpan, CompoundStatement>,
424430
do_parse!(
425431
indent!(indent) >>
@@ -500,7 +506,7 @@ named_args!(try_stmt(indent: usize) <StrSpan, CompoundStatement>,
500506
)
501507
);
502508

503-
// with_stmt: 'with' with_item (',' with_item)* ':' suite
509+
// with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite
504510
// with_item: test ['as' expr]
505511
named_args!(with_stmt(indent: usize) <StrSpan, CompoundStatement>,
506512
do_parse!(

0 commit comments

Comments
 (0)