blob: b1faab79e1a6f30f82e0d0920b2c02388cc79675 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001// rules for writing rules:
2// - zero_or_more is implemented using opt_rule around a one_or_more rule
3// - don't put opt_rule in arguments of or rule; instead, wrap the call to this or rule in opt_rule
4
5// # Start symbols for the grammar:
6// # single_input is a single interactive statement;
7// # file_input is a module or sequence of commands read from an input file;
8// # eval_input is the input for the eval() functions.
Damiene388f102013-12-12 15:24:38 +00009// # NB: compound_stmt in single_input is followed by extra NEWLINE! --> not in Micro Python
Damien3997be42013-10-18 19:56:48 +010010// single_input: NEWLINE | simple_stmt | compound_stmt
Damien429d7192013-10-04 19:53:11 +010011// file_input: (NEWLINE | stmt)* ENDMARKER
12// eval_input: testlist NEWLINE* ENDMARKER
13
Damien3997be42013-10-18 19:56:48 +010014DEF_RULE(single_input, nc, or(3), tok(NEWLINE), rule(simple_stmt), rule(compound_stmt))
Damien429d7192013-10-04 19:53:11 +010015DEF_RULE(file_input, nc, and(1), opt_rule(file_input_2))
16DEF_RULE(file_input_2, c(generic_all_nodes), one_or_more, rule(file_input_3))
17DEF_RULE(file_input_3, nc, or(2), tok(NEWLINE), rule(stmt))
18
19// decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
20// decorators: decorator+
21// decorated: decorators (classdef | funcdef)
22// funcdef: 'def' NAME parameters ['->' test] ':' suite
23// parameters: '(' [typedargslist] ')'
24// typedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* [',' ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]] | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef
25// tfpdef: NAME [':' test]
26// varargslist: vfpdef ['=' test] (',' vfpdef ['=' test])* [',' ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
27// vfpdef: NAME
28
29DEF_RULE(decorator, nc, and(4), tok(DEL_AT), rule(dotted_name), opt_rule(trailer_paren), tok(NEWLINE))
30//DEF_RULE(decorator_2, nc, and(3), tok(DEL_PAREN_OPEN), opt_rule(arglist), tok(DEL_PAREN_CLOSE))
31DEF_RULE(decorators, nc, one_or_more, rule(decorator))
32DEF_RULE(decorated, c(decorated), and(2), rule(decorators), rule(decorated_body))
33DEF_RULE(decorated_body, nc, or(2), rule(classdef), rule(funcdef))
34DEF_RULE(funcdef, c(funcdef), and(8), tok(KW_DEF), tok(NAME), tok(DEL_PAREN_OPEN), opt_rule(typedargslist), tok(DEL_PAREN_CLOSE), opt_rule(funcdef_2), tok(DEL_COLON), rule(suite))
35DEF_RULE(funcdef_2, nc, and(2), tok(DEL_MINUS_MORE), rule(test))
36// TODO typedargslist lets through more than is allowed
37DEF_RULE(typedargslist, nc, list_with_end, rule(typedargslist_item), tok(DEL_COMMA))
38DEF_RULE(typedargslist_item, nc, or(3), rule(typedargslist_name), rule(typedargslist_star), rule(typedargslist_dbl_star))
39DEF_RULE(typedargslist_name, nc, and(3), tok(NAME), opt_rule(typedargslist_colon), opt_rule(typedargslist_equal))
40DEF_RULE(typedargslist_star, nc, and(2), tok(OP_STAR), opt_rule(tfpdef))
41DEF_RULE(typedargslist_dbl_star, nc, and(3), tok(OP_DBL_STAR), tok(NAME), opt_rule(typedargslist_colon))
42DEF_RULE(typedargslist_colon, nc, and(2), tok(DEL_COLON), rule(test))
43DEF_RULE(typedargslist_equal, nc, and(2), tok(DEL_EQUAL), rule(test))
44DEF_RULE(tfpdef, nc, and(2), tok(NAME), opt_rule(typedargslist_colon))
45// TODO varargslist lets through more than is allowed
46DEF_RULE(varargslist, nc, list_with_end, rule(varargslist_item), tok(DEL_COMMA))
47DEF_RULE(varargslist_item, nc, or(3), rule(varargslist_name), rule(varargslist_star), rule(varargslist_dbl_star))
48DEF_RULE(varargslist_name, nc, and(2), tok(NAME), opt_rule(varargslist_equal))
49DEF_RULE(varargslist_star, nc, and(2), tok(OP_STAR), opt_rule(vfpdef))
50DEF_RULE(varargslist_dbl_star, nc, and(2), tok(OP_DBL_STAR), tok(NAME))
51DEF_RULE(varargslist_equal, nc, and(2), tok(DEL_EQUAL), rule(test))
52DEF_RULE(vfpdef, nc, and(1), tok(NAME))
53
54// stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | simple_stmt
55
56DEF_RULE(stmt, nc, or(9), rule(if_stmt), rule(while_stmt), rule(for_stmt), rule(try_stmt), rule(with_stmt), rule(funcdef), rule(classdef), rule(decorated), rule(simple_stmt))
57
58// simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
59
60DEF_RULE(simple_stmt, nc, and(2), rule(simple_stmt_2), tok(NEWLINE))
61DEF_RULE(simple_stmt_2, c(generic_all_nodes), list_with_end, rule(small_stmt), tok(DEL_SEMICOLON))
62
63// small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
64// expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | ('=' (yield_expr|testlist_star_expr))*)
65// testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
66// augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//='
67// # For normal assignments, additional restrictions enforced by the interpreter
68
69DEF_RULE(small_stmt, nc, or(8), rule(del_stmt), rule(pass_stmt), rule(flow_stmt), rule(import_stmt), rule(global_stmt), rule(nonlocal_stmt), rule(assert_stmt), rule(expr_stmt))
70DEF_RULE(expr_stmt, c(expr_stmt), and(2), rule(testlist_star_expr), opt_rule(expr_stmt_2))
71DEF_RULE(expr_stmt_2, nc, or(2), rule(expr_stmt_augassign), rule(expr_stmt_assign_list))
72DEF_RULE(expr_stmt_augassign, nc, and(2), rule(augassign), rule(expr_stmt_6))
73DEF_RULE(expr_stmt_assign_list, nc, one_or_more, rule(expr_stmt_assign))
74DEF_RULE(expr_stmt_assign, nc, and(2), tok(DEL_EQUAL), rule(expr_stmt_6))
75DEF_RULE(expr_stmt_6, nc, or(2), rule(yield_expr), rule(testlist_star_expr))
76DEF_RULE(testlist_star_expr, c(generic_tuple), list_with_end, rule(testlist_star_expr_2), tok(DEL_COMMA))
77DEF_RULE(testlist_star_expr_2, nc, or(2), rule(star_expr), rule(test))
78DEF_RULE(augassign, nc, or(12), tok(DEL_PLUS_EQUAL), tok(DEL_MINUS_EQUAL), tok(DEL_STAR_EQUAL), tok(DEL_SLASH_EQUAL), tok(DEL_PERCENT_EQUAL), tok(DEL_AMPERSAND_EQUAL), tok(DEL_PIPE_EQUAL), tok(DEL_CARET_EQUAL), tok(DEL_DBL_LESS_EQUAL), tok(DEL_DBL_MORE_EQUAL), tok(DEL_DBL_STAR_EQUAL), tok(DEL_DBL_SLASH_EQUAL))
79
80// del_stmt: 'del' exprlist
81// pass_stmt: 'pass'
82// flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
83// break_stmt: 'break'
84// continue_stmt: 'continue'
85// return_stmt: 'return' [testlist]
86// yield_stmt: yield_expr
87// raise_stmt: 'raise' [test ['from' test]]
88
89DEF_RULE(del_stmt, c(del_stmt), and(2), tok(KW_DEL), rule(exprlist))
90DEF_RULE(pass_stmt, c(generic_all_nodes), and(1), tok(KW_PASS))
91DEF_RULE(flow_stmt, nc, or(5), rule(break_stmt), rule(continue_stmt), rule(return_stmt), rule(raise_stmt), rule(yield_stmt))
92DEF_RULE(break_stmt, c(break_stmt), and(1), tok(KW_BREAK))
93DEF_RULE(continue_stmt, c(continue_stmt), and(1), tok(KW_CONTINUE))
94DEF_RULE(return_stmt, c(return_stmt), and(2), tok(KW_RETURN), opt_rule(testlist))
95DEF_RULE(yield_stmt, c(yield_stmt), and(1), rule(yield_expr))
96DEF_RULE(raise_stmt, c(raise_stmt), and(2), tok(KW_RAISE), opt_rule(raise_stmt_arg))
97DEF_RULE(raise_stmt_arg, nc, and(2), rule(test), opt_rule(raise_stmt_from))
98DEF_RULE(raise_stmt_from, nc, and(2), tok(KW_FROM), rule(test))
99
100// import_stmt: import_name | import_from
101// import_name: 'import' dotted_as_names
102// import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) 'import' ('*' | '(' import_as_names ')' | import_as_names)
103// import_as_name: NAME ['as' NAME]
104// dotted_as_name: dotted_name ['as' NAME]
105// import_as_names: import_as_name (',' import_as_name)* [',']
106// dotted_as_names: dotted_as_name (',' dotted_as_name)*
107// dotted_name: NAME ('.' NAME)*
108// global_stmt: 'global' NAME (',' NAME)*
109// nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
110// assert_stmt: 'assert' test [',' test]
111
112DEF_RULE(import_stmt, nc, or(2), rule(import_name), rule(import_from))
113DEF_RULE(import_name, c(import_name), and(2), tok(KW_IMPORT), rule(dotted_as_names))
114DEF_RULE(import_from, c(import_from), and(4), tok(KW_FROM), rule(import_from_2), tok(KW_IMPORT), rule(import_from_3))
115DEF_RULE(import_from_2, nc, or(2), rule(dotted_name), rule(import_from_2b))
116DEF_RULE(import_from_2b, nc, and(2), rule(one_or_more_period_or_ellipses), opt_rule(dotted_name))
117DEF_RULE(import_from_3, nc, or(3), tok(OP_STAR), rule(import_as_names_paren), rule(import_as_names))
118DEF_RULE(import_as_names_paren, nc, and(3), tok(DEL_PAREN_OPEN), rule(import_as_names), tok(DEL_PAREN_CLOSE))
119DEF_RULE(one_or_more_period_or_ellipses, nc, one_or_more, rule(period_or_ellipses))
120DEF_RULE(period_or_ellipses, nc, or(2), tok(DEL_PERIOD), tok(ELLIPSES))
121DEF_RULE(import_as_name, nc, and(2), tok(NAME), opt_rule(as_name))
122DEF_RULE(dotted_as_name, nc, and(2), rule(dotted_name), opt_rule(as_name))
123DEF_RULE(as_name, nc, and(2), tok(KW_AS), tok(NAME))
124DEF_RULE(import_as_names, nc, list_with_end, rule(import_as_name), tok(DEL_COMMA))
125DEF_RULE(dotted_as_names, nc, list, rule(dotted_as_name), tok(DEL_COMMA))
126DEF_RULE(dotted_name, nc, list, tok(NAME), tok(DEL_PERIOD))
127DEF_RULE(global_stmt, c(global_stmt), and(2), tok(KW_GLOBAL), rule(name_list))
128DEF_RULE(nonlocal_stmt, c(nonlocal_stmt), and(2), tok(KW_NONLOCAL), rule(name_list))
129DEF_RULE(name_list, nc, list, tok(NAME), tok(DEL_COMMA))
130DEF_RULE(assert_stmt, c(assert_stmt), and(3), tok(KW_ASSERT), rule(test), opt_rule(assert_stmt_extra))
131DEF_RULE(assert_stmt_extra, nc, and(2), tok(DEL_COMMA), rule(test))
132
Damien3997be42013-10-18 19:56:48 +0100133// compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
Damien429d7192013-10-04 19:53:11 +0100134// if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
135// while_stmt: 'while' test ':' suite ['else' ':' suite]
136// for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
137// try_stmt: 'try' ':' suite ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite)
138// # NB compile.c makes sure that the default except clause is last
139// except_clause: 'except' [test ['as' NAME]]
140// with_stmt: 'with' with_item (',' with_item)* ':' suite
141// with_item: test ['as' expr]
142// suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
143
Damien3997be42013-10-18 19:56:48 +0100144DEF_RULE(compound_stmt, nc, or(8), rule(if_stmt), rule(while_stmt), rule(for_stmt), rule(try_stmt), rule(with_stmt), rule(funcdef), rule(classdef), rule(decorated))
Damien429d7192013-10-04 19:53:11 +0100145DEF_RULE(if_stmt, c(if_stmt), and(6), tok(KW_IF), rule(test), tok(DEL_COLON), rule(suite), opt_rule(if_stmt_elif_list), opt_rule(else_stmt))
146DEF_RULE(if_stmt_elif_list, nc, one_or_more, rule(if_stmt_elif))
147DEF_RULE(if_stmt_elif, nc, and(4), tok(KW_ELIF), rule(test), tok(DEL_COLON), rule(suite))
148DEF_RULE(while_stmt, c(while_stmt), and(5), tok(KW_WHILE), rule(test), tok(DEL_COLON), rule(suite), opt_rule(else_stmt))
149DEF_RULE(for_stmt, c(for_stmt), and(7), tok(KW_FOR), rule(exprlist), tok(KW_IN), rule(testlist), tok(DEL_COLON), rule(suite), opt_rule(else_stmt))
150DEF_RULE(try_stmt, c(try_stmt), and(4), tok(KW_TRY), tok(DEL_COLON), rule(suite), rule(try_stmt_2))
151DEF_RULE(try_stmt_2, nc, or(2), rule(try_stmt_except_and_more), rule(try_stmt_finally))
152DEF_RULE(try_stmt_except_and_more, nc, and(3), rule(try_stmt_except_list), opt_rule(else_stmt), opt_rule(try_stmt_finally))
153DEF_RULE(try_stmt_except, nc, and(4), tok(KW_EXCEPT), opt_rule(try_stmt_as_name), tok(DEL_COLON), rule(suite))
154DEF_RULE(try_stmt_as_name, nc, and(2), rule(test), opt_rule(as_name))
155DEF_RULE(try_stmt_except_list, nc, one_or_more, rule(try_stmt_except))
156DEF_RULE(try_stmt_finally, nc, and(3), tok(KW_FINALLY), tok(DEL_COLON), rule(suite))
157DEF_RULE(else_stmt, nc, and(3), tok(KW_ELSE), tok(DEL_COLON), rule(suite))
158DEF_RULE(with_stmt, c(with_stmt), and(4), tok(KW_WITH), rule(with_stmt_list), tok(DEL_COLON), rule(suite))
159DEF_RULE(with_stmt_list, nc, list, rule(with_item), tok(DEL_COMMA))
160DEF_RULE(with_item, nc, and(2), rule(test), opt_rule(with_item_as))
161DEF_RULE(with_item_as, nc, and(2), tok(KW_AS), rule(expr))
162DEF_RULE(suite, nc, or(2), rule(suite_block), rule(simple_stmt))
163DEF_RULE(suite_block, nc, and(4), tok(NEWLINE), tok(INDENT), rule(suite_block_stmts), tok(DEDENT))
164DEF_RULE(suite_block_stmts, c(generic_all_nodes), one_or_more, rule(stmt))
165
166// test: or_test ['if' or_test 'else' test] | lambdef
167// test_nocond: or_test | lambdef_nocond
168// lambdef: 'lambda' [varargslist] ':' test
169// lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
170
171DEF_RULE(test, nc, or(2), rule(lambdef), rule(test_if_expr))
172DEF_RULE(test_if_expr, c(test_if_expr), and(2), rule(or_test), opt_rule(test_if_else))
173DEF_RULE(test_if_else, nc, and(4), tok(KW_IF), rule(or_test), tok(KW_ELSE), rule(test))
174DEF_RULE(test_nocond, nc, or(2), rule(lambdef_nocond), rule(or_test))
175DEF_RULE(lambdef, c(lambdef), and(4), tok(KW_LAMBDA), opt_rule(varargslist), tok(DEL_COLON), rule(test))
176DEF_RULE(lambdef_nocond, c(lambdef), and(4), tok(KW_LAMBDA), opt_rule(varargslist), tok(DEL_COLON), rule(test_nocond))
177
178// or_test: and_test ('or' and_test)*
179// and_test: not_test ('and' not_test)*
180// not_test: 'not' not_test | comparison
181// comparison: expr (comp_op expr)*
182// comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
183// star_expr: '*' expr
184// expr: xor_expr ('|' xor_expr)*
185// xor_expr: and_expr ('^' and_expr)*
186// and_expr: shift_expr ('&' shift_expr)*
187// shift_expr: arith_expr (('<<'|'>>') arith_expr)*
188// arith_expr: term (('+'|'-') term)*
189// term: factor (('*'|'/'|'%'|'//') factor)*
190// factor: ('+'|'-'|'~') factor | power
191// power: atom trailer* ['**' factor]
192
193DEF_RULE(or_test, c(or_test), list, rule(and_test), tok(KW_OR))
194DEF_RULE(and_test, c(and_test), list, rule(not_test), tok(KW_AND))
195DEF_RULE(not_test, nc, or(2), rule(not_test_2), rule(comparison))
196DEF_RULE(not_test_2, c(not_test_2), and(2), tok(KW_NOT), rule(not_test))
197DEF_RULE(comparison, c(comparison), list, rule(expr), rule(comp_op))
198DEF_RULE(comp_op, nc, or(9), tok(OP_LESS), tok(OP_MORE), tok(OP_DBL_EQUAL), tok(OP_LESS_EQUAL), tok(OP_MORE_EQUAL), tok(OP_NOT_EQUAL), tok(KW_IN), rule(comp_op_not_in), rule(comp_op_is))
199DEF_RULE(comp_op_not_in, nc, and(2), tok(KW_NOT), tok(KW_IN))
200DEF_RULE(comp_op_is, nc, and(2), tok(KW_IS), opt_rule(comp_op_is_not))
201DEF_RULE(comp_op_is_not, nc, and(1), tok(KW_NOT))
202DEF_RULE(star_expr, c(star_expr), and(2), tok(OP_STAR), rule(expr))
203DEF_RULE(expr, c(expr), list, rule(xor_expr), tok(OP_PIPE))
204DEF_RULE(xor_expr, c(xor_expr), list, rule(and_expr), tok(OP_CARET))
205DEF_RULE(and_expr, c(and_expr), list, rule(shift_expr), tok(OP_AMPERSAND))
206DEF_RULE(shift_expr, c(shift_expr), list, rule(arith_expr), rule(shift_op))
207DEF_RULE(shift_op, nc, or(2), tok(OP_DBL_LESS), tok(OP_DBL_MORE))
208DEF_RULE(arith_expr, c(arith_expr), list, rule(term), rule(arith_op))
209DEF_RULE(arith_op, nc, or(2), tok(OP_PLUS), tok(OP_MINUS))
210DEF_RULE(term, c(term), list, rule(factor), rule(term_op))
211DEF_RULE(term_op, nc, or(4), tok(OP_STAR), tok(OP_SLASH), tok(OP_PERCENT), tok(OP_DBL_SLASH))
212DEF_RULE(factor, nc, or(2), rule(factor_2), rule(power))
213DEF_RULE(factor_2, c(factor_2), and(2), rule(factor_op), rule(factor))
214DEF_RULE(factor_op, nc, or(3), tok(OP_PLUS), tok(OP_MINUS), tok(OP_TILDE))
215DEF_RULE(power, c(generic_all_nodes), and(3), rule(atom), opt_rule(power_trailers), opt_rule(power_dbl_star))
216DEF_RULE(power_trailers, c(power_trailers), one_or_more, rule(trailer))
217DEF_RULE(power_dbl_star, c(power_dbl_star), and(2), tok(OP_DBL_STAR), rule(factor))
218
219// atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' | '{' [dictorsetmaker] '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False'
220// testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
221// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
222
223DEF_RULE(atom, nc, or(10), tok(NAME), tok(NUMBER), rule(atom_string), tok(ELLIPSES), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
224DEF_RULE(atom_string, c(atom_string), one_or_more, rule(string_or_bytes))
225DEF_RULE(string_or_bytes, nc, or(2), tok(STRING), tok(BYTES))
226DEF_RULE(atom_paren, c(atom_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(atom_2b), tok(DEL_PAREN_CLOSE))
227DEF_RULE(atom_2b, nc, or(2), rule(yield_expr), rule(testlist_comp))
228DEF_RULE(atom_bracket, c(atom_bracket), and(3), tok(DEL_BRACKET_OPEN), opt_rule(testlist_comp), tok(DEL_BRACKET_CLOSE))
229DEF_RULE(atom_brace, c(atom_brace), and(3), tok(DEL_BRACE_OPEN), opt_rule(dictorsetmaker), tok(DEL_BRACE_CLOSE))
230DEF_RULE(testlist_comp, nc, and(2), rule(testlist_comp_2), opt_rule(testlist_comp_3))
231DEF_RULE(testlist_comp_2, nc, or(2), rule(star_expr), rule(test))
232DEF_RULE(testlist_comp_3, nc, or(2), rule(comp_for), rule(testlist_comp_3b))
233DEF_RULE(testlist_comp_3b, nc, and(2), tok(DEL_COMMA), opt_rule(testlist_comp_3c))
234DEF_RULE(testlist_comp_3c, nc, list_with_end, rule(testlist_comp_2), tok(DEL_COMMA))
235DEF_RULE(trailer, nc, or(3), rule(trailer_paren), rule(trailer_bracket), rule(trailer_period))
236DEF_RULE(trailer_paren, c(trailer_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(arglist), tok(DEL_PAREN_CLOSE))
237DEF_RULE(trailer_bracket, c(trailer_bracket), and(3), tok(DEL_BRACKET_OPEN), rule(subscriptlist), tok(DEL_BRACKET_CLOSE))
238DEF_RULE(trailer_period, c(trailer_period), and(2), tok(DEL_PERIOD), tok(NAME))
239
240// subscriptlist: subscript (',' subscript)* [',']
241// subscript: test | [test] ':' [test] [sliceop]
242// sliceop: ':' [test]
243
244DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(subscript), tok(DEL_COMMA))
245DEF_RULE(subscript, nc, or(2), rule(subscript_3), rule(subscript_2))
246DEF_RULE(subscript_2, c(subscript_2), and(2), rule(test), opt_rule(subscript_3))
247DEF_RULE(subscript_3, c(subscript_3), and(2), tok(DEL_COLON), opt_rule(subscript_3b))
248DEF_RULE(subscript_3b, nc, or(2), rule(subscript_3c), rule(subscript_3d))
249DEF_RULE(subscript_3c, nc, and(2), tok(DEL_COLON), opt_rule(test))
250DEF_RULE(subscript_3d, nc, and(2), rule(test), opt_rule(sliceop))
251DEF_RULE(sliceop, nc, and(2), tok(DEL_COLON), opt_rule(test))
252
253// exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
254// testlist: test (',' test)* [',']
255// dictorsetmaker: (test ':' test (comp_for | (',' test ':' test)* [','])) | (test (comp_for | (',' test)* [',']))
256
257DEF_RULE(exprlist, nc, list_with_end, rule(exprlist_2), tok(DEL_COMMA))
258DEF_RULE(exprlist_2, nc, or(2), rule(star_expr), rule(expr))
259DEF_RULE(testlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
260// TODO dictorsetmaker lets through more than is allowed
261DEF_RULE(dictorsetmaker, nc, and(2), rule(dictorsetmaker_item), opt_rule(dictorsetmaker_tail))
262DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and(2), rule(test), opt_rule(dictorsetmaker_colon))
263DEF_RULE(dictorsetmaker_colon, nc, and(2), tok(DEL_COLON), rule(test))
264DEF_RULE(dictorsetmaker_tail, nc, or(2), rule(comp_for), rule(dictorsetmaker_list))
265DEF_RULE(dictorsetmaker_list, nc, and(2), tok(DEL_COMMA), opt_rule(dictorsetmaker_list2))
266DEF_RULE(dictorsetmaker_list2, nc, list_with_end, rule(dictorsetmaker_item), tok(DEL_COMMA))
267
268// classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
269
270DEF_RULE(classdef, c(classdef), and(5), tok(KW_CLASS), tok(NAME), opt_rule(classdef_2), tok(DEL_COLON), rule(suite))
271DEF_RULE(classdef_2, nc, and(3), tok(DEL_PAREN_OPEN), opt_rule(arglist), tok(DEL_PAREN_CLOSE))
272
273// arglist: (argument ',')* (argument [','] | '*' test (',' argument)* [',' '**' test] | '**' test)
274
275// TODO arglist lets through more than is allowed, compiler needs to do further verification
276DEF_RULE(arglist, c(generic_all_nodes), list_with_end, rule(arglist_2), tok(DEL_COMMA))
277DEF_RULE(arglist_2, nc, or(3), rule(arglist_star), rule(arglist_dbl_star), rule(argument))
278DEF_RULE(arglist_star, c(arglist_star), and(2), tok(OP_STAR), rule(test))
279DEF_RULE(arglist_dbl_star, c(arglist_dbl_star), and(2), tok(OP_DBL_STAR), rule(test))
280
281// # The reason that keywords are test nodes instead of NAME is that using NAME
282// # results in an ambiguity. ast.c makes sure it's a NAME.
283// argument: test [comp_for] | test '=' test # Really [keyword '='] test
284// comp_iter: comp_for | comp_if
285// comp_for: 'for' exprlist 'in' or_test [comp_iter]
286// comp_if: 'if' test_nocond [comp_iter]
287
288DEF_RULE(argument, c(argument), and(2), rule(test), opt_rule(argument_2))
289DEF_RULE(argument_2, nc, or(2), rule(comp_for), rule(argument_3))
290DEF_RULE(argument_3, nc, and(2), tok(DEL_EQUAL), rule(test))
291DEF_RULE(comp_iter, nc, or(2), rule(comp_for), rule(comp_if))
292DEF_RULE(comp_for, nc, and(5), tok(KW_FOR), rule(exprlist), tok(KW_IN), rule(or_test), opt_rule(comp_iter))
293DEF_RULE(comp_if, nc, and(3), tok(KW_IF), rule(test_nocond), opt_rule(comp_iter))
294
295// # not used in grammar, but may appear in "node" passed from Parser to Compiler
296// encoding_decl: NAME
297
298// yield_expr: 'yield' [yield_arg]
299// yield_arg: 'from' test | testlist
300
301DEF_RULE(yield_expr, c(yield_expr), and(2), tok(KW_YIELD), opt_rule(yield_arg))
302DEF_RULE(yield_arg, nc, or(2), rule(yield_arg_from), rule(testlist))
303DEF_RULE(yield_arg_from, nc, and(2), tok(KW_FROM), rule(test))