]> 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:

4905c91e635e8a40a10b255258db4c96a303a56f
[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' test ':' suite ('elif' 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 test: or_test ['if' or_test 'else' test] | lambdef
95 or_test: and_test ('or' and_test)*
96 and_test: not_test ('and' not_test)*
97 not_test: 'not' not_test | comparison
98 comparison: expr (comp_op expr)*
99 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
100 star_expr: '*' expr
101 expr: xor_expr ('|' xor_expr)*
102 xor_expr: and_expr ('^' and_expr)*
103 and_expr: shift_expr ('&' shift_expr)*
104 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
105 arith_expr: term (('+'|'-') term)*
106 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
107 factor: ('+'|'-'|'~') factor | power
108 power: [AWAIT] atom trailer* ['**' factor]
109 atom: ('(' [yield_expr|testlist_gexp] ')' |
110        '[' [listmaker] ']' |
111        '{' [dictsetmaker] '}' |
112        '`' testlist1 '`' |
113        NAME | NUMBER | STRING+ | '.' '.' '.')
114 listmaker: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
115 testlist_gexp: (test|star_expr) ( old_comp_for | (',' (test|star_expr))* [','] )
116 lambdef: 'lambda' [varargslist] ':' test
117 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
118 subscriptlist: subscript (',' subscript)* [',']
119 subscript: test | [test] ':' [test] [sliceop]
120 sliceop: ':' [test]
121 exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
122 testlist: test (',' test)* [',']
123 dictsetmaker: ( ((test ':' test | '**' expr)
124                  (comp_for | (',' (test ':' test | '**' expr))* [','])) |
125                 ((test | star_expr)
126                  (comp_for | (',' (test | star_expr))* [','])) )
127
128 classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
129
130 arglist: argument (',' argument)* [',']
131
132 # "test '=' test" is really "keyword '=' test", but we have no such token.
133 # These need to be in a single rule to avoid grammar that is ambiguous
134 # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
135 # we explicitly match '*' here, too, to give it proper precedence.
136 # Illegal combinations and orderings are blocked in ast.c:
137 # multiple (test comp_for) arguments are blocked; keyword unpackings
138 # that precede iterable unpackings are blocked; etc.
139 argument: ( test [comp_for] |
140             test '=' test |
141             '**' expr |
142             star_expr )
143
144 comp_iter: comp_for | comp_if
145 comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
146 comp_if: 'if' old_test [comp_iter]
147
148 # As noted above, testlist_safe extends the syntax allowed in list
149 # comprehensions and generators. We can't use it indiscriminately in all
150 # derivations using a comp_for-like pattern because the testlist_safe derivation
151 # contains comma which clashes with trailing comma in arglist.
152 #
153 # This was an issue because the parser would not follow the correct derivation
154 # when parsing syntactically valid Python code. Since testlist_safe was created
155 # specifically to handle list comprehensions and generator expressions enclosed
156 # with parentheses, it's safe to only use it in those. That avoids the issue; we
157 # can parse code like set(x for x in [],).
158 #
159 # The syntax supported by this set of rules is not a valid Python 3 syntax,
160 # hence the prefix "old".
161 #
162 # See https://bugs.python.org/issue27494
163 old_comp_iter: old_comp_for | old_comp_if
164 old_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter]
165 old_comp_if: 'if' old_test [old_comp_iter]
166
167 testlist1: test (',' test)*
168
169 # not used in grammar, but may appear in "node" passed from Parser to Compiler
170 encoding_decl: NAME
171
172 yield_expr: 'yield' [yield_arg]
173 yield_arg: 'from' test | testlist