bpo-35766: Merge typed_ast back into CPython (GH-11645) · python/cpython@dcfcd14

GitHub

@@ -7,7 +7,9 @@

77# single_input is a single interactive statement;

88# file_input is a module or sequence of commands read from an input file;

99# eval_input is the input for the eval() functions.

10+# func_type_input is a PEP 484 Python 2 function type comment

1011# NB: compound_stmt in single_input is followed by extra NEWLINE!

12+# NB: due to the way TYPE_COMMENT is tokenized it will always be followed by a NEWLINE

1113single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE

1214file_input: (NEWLINE | stmt)* ENDMARKER

1315eval_input: testlist NEWLINE* ENDMARKER

@@ -17,14 +19,14 @@ decorators: decorator+

1719decorated: decorators (classdef | funcdef | async_funcdef)

18201921async_funcdef: 'async' funcdef

20-funcdef: 'def' NAME parameters ['->' test] ':' suite

22+funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] func_body_suite

21232224parameters: '(' [typedargslist] ')'

23-typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [

24- '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]

25- | '**' tfpdef [',']]]

26- | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]

27- | '**' tfpdef [','])

25+typedargslist: (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [

26+ '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])

27+ | '**' tfpdef [','] [TYPE_COMMENT]]])

28+ | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]])

29+ | '**' tfpdef [','] [TYPE_COMMENT])

2830tfpdef: NAME [':' test]

2931varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [

3032 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]

@@ -39,7 +41,7 @@ simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE

3941small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |

4042 import_stmt | global_stmt | nonlocal_stmt | assert_stmt)

4143expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |

42- ('=' (yield_expr|testlist_star_expr))*)

44+[('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )

4345annassign: ':' test ['=' (yield_expr|testlist)]

4446testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']

4547augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |

@@ -71,13 +73,13 @@ compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef

7173async_stmt: 'async' (funcdef | with_stmt | for_stmt)

7274if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]

7375while_stmt: 'while' test ':' suite ['else' ':' suite]

74-for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]

76+for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite]

7577try_stmt: ('try' ':' suite

7678 ((except_clause ':' suite)+

7779 ['else' ':' suite]

7880 ['finally' ':' suite] |

7981 'finally' ':' suite))

80-with_stmt: 'with' with_item (',' with_item)* ':' suite

82+with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite

8183with_item: test ['as' expr]

8284# NB compile.c makes sure that the default except clause is last

8385except_clause: 'except' [test ['as' NAME]]

@@ -150,3 +152,14 @@ encoding_decl: NAME

150152151153yield_expr: 'yield' [yield_arg]

152154yield_arg: 'from' test | testlist_star_expr

155+156+# the TYPE_COMMENT in suites is only parsed for funcdefs,

157+# but can't go elsewhere due to ambiguity

158+func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT

159+160+func_type_input: func_type NEWLINE* ENDMARKER

161+func_type: '(' [typelist] ')' '->' test

162+# typelist is a modified typedargslist (see above)

163+typelist: (test (',' test)* [','

164+ ['*' [test] (',' test)* [',' '**' test] | '**' test]]

165+ | '*' [test] (',' test)* [',' '**' test] | '**' test)