bpo-35766: Merge typed_ast back into CPython (GH-11645) · python/cpython@dcfcd14 (original) (raw)
`@@ -7,7 +7,9 @@
`
7
7
`# single_input is a single interactive statement;
`
8
8
`# file_input is a module or sequence of commands read from an input file;
`
9
9
`# eval_input is the input for the eval() functions.
`
``
10
`+
func_type_input is a PEP 484 Python 2 function type comment
`
10
11
`# 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
`
11
13
`single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
`
12
14
`file_input: (NEWLINE | stmt)* ENDMARKER
`
13
15
`eval_input: testlist NEWLINE* ENDMARKER
`
`@@ -17,14 +19,14 @@ decorators: decorator+
`
17
19
`decorated: decorators (classdef | funcdef | async_funcdef)
`
18
20
``
19
21
`async_funcdef: 'async' funcdef
`
20
``
`-
funcdef: 'def' NAME parameters ['->' test] ':' suite
`
``
22
`+
funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] func_body_suite
`
21
23
``
22
24
`parameters: '(' [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])
`
28
30
`tfpdef: NAME [':' test]
`
29
31
`varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
`
30
32
` '' [vfpdef] (',' vfpdef ['=' test]) [',' ['**' vfpdef [',']]]
`
`@@ -39,7 +41,7 @@ simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
`
39
41
`small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
`
40
42
` import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
`
41
43
`expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
`
42
``
`-
('=' (yield_expr|testlist_star_expr))*)
`
``
44
`+
[('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
`
43
45
`annassign: ':' test ['=' (yield_expr|testlist)]
`
44
46
`testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
`
45
47
`augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
`
`@@ -71,13 +73,13 @@ compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef
`
71
73
`async_stmt: 'async' (funcdef | with_stmt | for_stmt)
`
72
74
`if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
`
73
75
`while_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]
`
75
77
`try_stmt: ('try' ':' suite
`
76
78
` ((except_clause ':' suite)+
`
77
79
` ['else' ':' suite]
`
78
80
` ['finally' ':' suite] |
`
79
81
` 'finally' ':' suite))
`
80
``
`-
with_stmt: 'with' with_item (',' with_item)* ':' suite
`
``
82
`+
with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite
`
81
83
`with_item: test ['as' expr]
`
82
84
`# NB compile.c makes sure that the default except clause is last
`
83
85
`except_clause: 'except' [test ['as' NAME]]
`
`@@ -150,3 +152,14 @@ encoding_decl: NAME
`
150
152
``
151
153
`yield_expr: 'yield' [yield_arg]
`
152
154
`yield_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)
`