]> git.madduck.net Git - etc/vim.git/blob - blib2to3/Grammar.txt

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

1061ac81e24645618f5abb23fc2ba02e3b914200
[etc/vim.git] / blib2to3 / Grammar.txt
1 # Grammar for 2to3. This grammar supports Python 2.x and 3.x.
2
3 # NOTE WELL: You should also follow all the steps listed at
4 # https://devguide.python.org/grammar/
5
6 # Start symbols for the grammar:
7 #       file_input is a module or sequence of commands read from an input file;
8 #       single_input is a single interactive statement;
9 #       eval_input is the input for the eval() and input() functions.
10 # NB: compound_stmt in single_input is followed by extra NEWLINE!
11 file_input: (NEWLINE | stmt)* ENDMARKER
12 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
13 eval_input: testlist NEWLINE* ENDMARKER
14
15 decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
16 decorators: decorator+
17 decorated: decorators (classdef | funcdef | async_funcdef)
18 async_funcdef: ASYNC funcdef
19 funcdef: 'def' NAME parameters ['->' test] ':' suite
20 parameters: '(' [typedargslist] ')'
21 typedargslist: ((tfpdef ['=' test] ',')*
22                 ('*' [tname] (',' tname ['=' test])* [',' ['**' tname [',']]] | '**' tname [','])
23                 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
24 tname: NAME [':' test]
25 tfpdef: tname | '(' tfplist ')'
26 tfplist: tfpdef (',' tfpdef)* [',']
27 varargslist: ((vfpdef ['=' test] ',')*
28               ('*' [vname] (',' vname ['=' test])*  [',' ['**' vname [',']]] | '**' vname [','])
29               | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
30 vname: NAME
31 vfpdef: vname | '(' vfplist ')'
32 vfplist: vfpdef (',' vfpdef)* [',']
33
34 stmt: simple_stmt | compound_stmt
35 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
36 small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
37              import_stmt | global_stmt | exec_stmt | assert_stmt)
38 expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
39                      ('=' (yield_expr|testlist_star_expr))*)
40 annassign: ':' test ['=' test]
41 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
42 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
43             '<<=' | '>>=' | '**=' | '//=')
44 # For normal and annotated assignments, additional restrictions enforced by the interpreter
45 print_stmt: 'print' ( [ test (',' test)* [','] ] |
46                       '>>' test [ (',' test)+ [','] ] )
47 del_stmt: 'del' exprlist
48 pass_stmt: 'pass'
49 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
50 break_stmt: 'break'
51 continue_stmt: 'continue'
52 return_stmt: 'return' [testlist]
53 yield_stmt: yield_expr
54 raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]]
55 import_stmt: import_name | import_from
56 import_name: 'import' dotted_as_names
57 import_from: ('from' ('.'* dotted_name | '.'+)
58               'import' ('*' | '(' import_as_names ')' | import_as_names))
59 import_as_name: NAME ['as' NAME]
60 dotted_as_name: dotted_name ['as' NAME]
61 import_as_names: import_as_name (',' import_as_name)* [',']
62 dotted_as_names: dotted_as_name (',' dotted_as_name)*
63 dotted_name: NAME ('.' NAME)*
64 global_stmt: ('global' | 'nonlocal') NAME (',' NAME)*
65 exec_stmt: 'exec' expr ['in' test [',' test]]
66 assert_stmt: 'assert' test [',' test]
67
68 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
69 async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
70 if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
71 while_stmt: 'while' test ':' suite ['else' ':' suite]
72 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
73 try_stmt: ('try' ':' suite
74            ((except_clause ':' suite)+
75             ['else' ':' suite]
76             ['finally' ':' suite] |
77            'finally' ':' suite))
78 with_stmt: 'with' with_item (',' with_item)*  ':' suite
79 with_item: test ['as' expr]
80 with_var: 'as' expr
81 # NB compile.c makes sure that the default except clause is last
82 except_clause: 'except' [test [(',' | 'as') test]]
83 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
84
85 # Backward compatibility cruft to support:
86 # [ x for x in lambda: True, lambda: False if x() ]
87 # even while also allowing:
88 # lambda x: 5 if x else 2
89 # (But not a mix of the two)
90 testlist_safe: old_test [(',' old_test)+ [',']]
91 old_test: or_test | old_lambdef
92 old_lambdef: 'lambda' [varargslist] ':' old_test
93
94 namedexpr_test: test [':=' test]
95 test: or_test ['if' or_test 'else' test] | lambdef
96 or_test: and_test ('or' and_test)*
97 and_test: not_test ('and' not_test)*
98 not_test: 'not' not_test | comparison
99 comparison: expr (comp_op expr)*
100 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
101 star_expr: '*' expr
102 expr: xor_expr ('|' xor_expr)*
103 xor_expr: and_expr ('^' and_expr)*
104 and_expr: shift_expr ('&' shift_expr)*
105 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
106 arith_expr: term (('+'|'-') term)*
107 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
108 factor: ('+'|'-'|'~') factor | power
109 power: [AWAIT] atom trailer* ['**' factor]
110 atom: ('(' [yield_expr|testlist_gexp] ')' |
111        '[' [listmaker] ']' |
112        '{' [dictsetmaker] '}' |
113        '`' testlist1 '`' |
114        NAME | NUMBER | STRING+ | '.' '.' '.')
115 listmaker: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] )
116 testlist_gexp: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] )
117 lambdef: 'lambda' [varargslist] ':' test
118 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
119 subscriptlist: subscript (',' subscript)* [',']
120 subscript: test | [test] ':' [test] [sliceop]
121 sliceop: ':' [test]
122 exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
123 testlist: test (',' test)* [',']
124 dictsetmaker: ( ((test ':' test | '**' expr)
125                  (comp_for | (',' (test ':' test | '**' expr))* [','])) |
126                 ((test | star_expr)
127                  (comp_for | (',' (test | star_expr))* [','])) )
128
129 classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
130
131 arglist: argument (',' argument)* [',']
132
133 # "test '=' test" is really "keyword '=' test", but we have no such token.
134 # These need to be in a single rule to avoid grammar that is ambiguous
135 # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
136 # we explicitly match '*' here, too, to give it proper precedence.
137 # Illegal combinations and orderings are blocked in ast.c:
138 # multiple (test comp_for) arguments are blocked; keyword unpackings
139 # that precede iterable unpackings are blocked; etc.
140 argument: ( test [comp_for] |
141             test ':=' test |
142             test '=' test |
143             '**' test |
144             '*' test )
145
146 comp_iter: comp_for | comp_if
147 comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
148 comp_if: 'if' old_test [comp_iter]
149
150 # As noted above, testlist_safe extends the syntax allowed in list
151 # comprehensions and generators. We can't use it indiscriminately in all
152 # derivations using a comp_for-like pattern because the testlist_safe derivation
153 # contains comma which clashes with trailing comma in arglist.
154 #
155 # This was an issue because the parser would not follow the correct derivation
156 # when parsing syntactically valid Python code. Since testlist_safe was created
157 # specifically to handle list comprehensions and generator expressions enclosed
158 # with parentheses, it's safe to only use it in those. That avoids the issue; we
159 # can parse code like set(x for x in [],).
160 #
161 # The syntax supported by this set of rules is not a valid Python 3 syntax,
162 # hence the prefix "old".
163 #
164 # See https://bugs.python.org/issue27494
165 old_comp_iter: old_comp_for | old_comp_if
166 old_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter]
167 old_comp_if: 'if' old_test [old_comp_iter]
168
169 testlist1: test (',' test)*
170
171 # not used in grammar, but may appear in "node" passed from Parser to Compiler
172 encoding_decl: NAME
173
174 yield_expr: 'yield' [yield_arg]
175 yield_arg: 'from' test | testlist