blob: e261aad226edb26a684d07dfe8a1452ea04b23a6 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
Damien Georgefa90ab12015-04-21 16:35:50 +00006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien429d7192013-10-04 19:53:11 +010027// rules for writing rules:
28// - zero_or_more is implemented using opt_rule around a one_or_more rule
29// - don't put opt_rule in arguments of or rule; instead, wrap the call to this or rule in opt_rule
30
31// # Start symbols for the grammar:
32// # single_input is a single interactive statement;
33// # file_input is a module or sequence of commands read from an input file;
34// # eval_input is the input for the eval() functions.
Damiene388f102013-12-12 15:24:38 +000035// # NB: compound_stmt in single_input is followed by extra NEWLINE! --> not in Micro Python
Damien3997be42013-10-18 19:56:48 +010036// single_input: NEWLINE | simple_stmt | compound_stmt
Damien429d7192013-10-04 19:53:11 +010037// file_input: (NEWLINE | stmt)* ENDMARKER
38// eval_input: testlist NEWLINE* ENDMARKER
39
Damien3997be42013-10-18 19:56:48 +010040DEF_RULE(single_input, nc, or(3), tok(NEWLINE), rule(simple_stmt), rule(compound_stmt))
Damien George0c1de1c2016-04-14 13:23:50 +010041DEF_RULE(file_input, c(generic_all_nodes), and_ident(1), opt_rule(file_input_2))
Damien429d7192013-10-04 19:53:11 +010042DEF_RULE(file_input_2, c(generic_all_nodes), one_or_more, rule(file_input_3))
43DEF_RULE(file_input_3, nc, or(2), tok(NEWLINE), rule(stmt))
Damien George0c1de1c2016-04-14 13:23:50 +010044DEF_RULE(eval_input, nc, and_ident(2), rule(testlist), opt_rule(eval_input_2))
Damien Georged02c6d82014-01-15 22:14:03 +000045DEF_RULE(eval_input_2, nc, and(1), tok(NEWLINE))
Damien429d7192013-10-04 19:53:11 +010046
47// decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
48// decorators: decorator+
pohmelie81ebba72016-01-27 23:23:11 +030049// decorated: decorators (classdef | funcdef | async_funcdef)
Damien429d7192013-10-04 19:53:11 +010050// funcdef: 'def' NAME parameters ['->' test] ':' suite
pohmelie81ebba72016-01-27 23:23:11 +030051// async_funcdef: 'async' funcdef
Damien429d7192013-10-04 19:53:11 +010052// parameters: '(' [typedargslist] ')'
53// typedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* [',' ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]] | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef
54// tfpdef: NAME [':' test]
55// varargslist: vfpdef ['=' test] (',' vfpdef ['=' test])* [',' ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
56// vfpdef: NAME
57
58DEF_RULE(decorator, nc, and(4), tok(DEL_AT), rule(dotted_name), opt_rule(trailer_paren), tok(NEWLINE))
Damien429d7192013-10-04 19:53:11 +010059DEF_RULE(decorators, nc, one_or_more, rule(decorator))
Damien George0c1de1c2016-04-14 13:23:50 +010060DEF_RULE(decorated, c(decorated), and_ident(2), rule(decorators), rule(decorated_body))
pohmelie81ebba72016-01-27 23:23:11 +030061#if MICROPY_PY_ASYNC_AWAIT
62DEF_RULE(decorated_body, nc, or(3), rule(classdef), rule(funcdef), rule(async_funcdef))
63DEF_RULE(async_funcdef, nc, and(2), tok(KW_ASYNC), rule(funcdef))
64#else
Damien429d7192013-10-04 19:53:11 +010065DEF_RULE(decorated_body, nc, or(2), rule(classdef), rule(funcdef))
pohmelie81ebba72016-01-27 23:23:11 +030066#endif
Damien George0c1de1c2016-04-14 13:23:50 +010067DEF_RULE(funcdef, c(funcdef), and_blank(8), tok(KW_DEF), tok(NAME), tok(DEL_PAREN_OPEN), opt_rule(typedargslist), tok(DEL_PAREN_CLOSE), opt_rule(funcdefrettype), tok(DEL_COLON), rule(suite))
68DEF_RULE(funcdefrettype, nc, and_ident(2), tok(DEL_MINUS_MORE), rule(test))
Damien George9a569122015-11-23 16:50:42 +000069// note: typedargslist lets through more than is allowed, compiler does further checks
Damien429d7192013-10-04 19:53:11 +010070DEF_RULE(typedargslist, nc, list_with_end, rule(typedargslist_item), tok(DEL_COMMA))
71DEF_RULE(typedargslist_item, nc, or(3), rule(typedargslist_name), rule(typedargslist_star), rule(typedargslist_dbl_star))
Damien George0c1de1c2016-04-14 13:23:50 +010072DEF_RULE(typedargslist_name, nc, and_ident(3), tok(NAME), opt_rule(typedargslist_colon), opt_rule(typedargslist_equal))
Damien429d7192013-10-04 19:53:11 +010073DEF_RULE(typedargslist_star, nc, and(2), tok(OP_STAR), opt_rule(tfpdef))
74DEF_RULE(typedargslist_dbl_star, nc, and(3), tok(OP_DBL_STAR), tok(NAME), opt_rule(typedargslist_colon))
Damien George0c1de1c2016-04-14 13:23:50 +010075DEF_RULE(typedargslist_colon, nc, and_ident(2), tok(DEL_COLON), rule(test))
76DEF_RULE(typedargslist_equal, nc, and_ident(2), tok(DEL_EQUAL), rule(test))
Damien429d7192013-10-04 19:53:11 +010077DEF_RULE(tfpdef, nc, and(2), tok(NAME), opt_rule(typedargslist_colon))
Damien George9a569122015-11-23 16:50:42 +000078// note: varargslist lets through more than is allowed, compiler does further checks
Damien429d7192013-10-04 19:53:11 +010079DEF_RULE(varargslist, nc, list_with_end, rule(varargslist_item), tok(DEL_COMMA))
80DEF_RULE(varargslist_item, nc, or(3), rule(varargslist_name), rule(varargslist_star), rule(varargslist_dbl_star))
Damien George0c1de1c2016-04-14 13:23:50 +010081DEF_RULE(varargslist_name, nc, and_ident(2), tok(NAME), opt_rule(varargslist_equal))
Damien429d7192013-10-04 19:53:11 +010082DEF_RULE(varargslist_star, nc, and(2), tok(OP_STAR), opt_rule(vfpdef))
83DEF_RULE(varargslist_dbl_star, nc, and(2), tok(OP_DBL_STAR), tok(NAME))
Damien George0c1de1c2016-04-14 13:23:50 +010084DEF_RULE(varargslist_equal, nc, and_ident(2), tok(DEL_EQUAL), rule(test))
85DEF_RULE(vfpdef, nc, and_ident(1), tok(NAME))
Damien429d7192013-10-04 19:53:11 +010086
Damien Georgefa90ab12015-04-21 16:35:50 +000087// stmt: compound_stmt | simple_stmt
Damien429d7192013-10-04 19:53:11 +010088
Damien Georgefa90ab12015-04-21 16:35:50 +000089DEF_RULE(stmt, nc, or(2), rule(compound_stmt), rule(simple_stmt))
Damien429d7192013-10-04 19:53:11 +010090
91// simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
92
Damien George0c1de1c2016-04-14 13:23:50 +010093DEF_RULE(simple_stmt, nc, and_ident(2), rule(simple_stmt_2), tok(NEWLINE))
Damien429d7192013-10-04 19:53:11 +010094DEF_RULE(simple_stmt_2, c(generic_all_nodes), list_with_end, rule(small_stmt), tok(DEL_SEMICOLON))
95
96// small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
97// expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | ('=' (yield_expr|testlist_star_expr))*)
98// testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
99// augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//='
100// # For normal assignments, additional restrictions enforced by the interpreter
101
102DEF_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))
103DEF_RULE(expr_stmt, c(expr_stmt), and(2), rule(testlist_star_expr), opt_rule(expr_stmt_2))
104DEF_RULE(expr_stmt_2, nc, or(2), rule(expr_stmt_augassign), rule(expr_stmt_assign_list))
Damien George0c1de1c2016-04-14 13:23:50 +0100105DEF_RULE(expr_stmt_augassign, nc, and_ident(2), rule(augassign), rule(expr_stmt_6))
Damien429d7192013-10-04 19:53:11 +0100106DEF_RULE(expr_stmt_assign_list, nc, one_or_more, rule(expr_stmt_assign))
Damien George0c1de1c2016-04-14 13:23:50 +0100107DEF_RULE(expr_stmt_assign, nc, and_ident(2), tok(DEL_EQUAL), rule(expr_stmt_6))
Damien429d7192013-10-04 19:53:11 +0100108DEF_RULE(expr_stmt_6, nc, or(2), rule(yield_expr), rule(testlist_star_expr))
109DEF_RULE(testlist_star_expr, c(generic_tuple), list_with_end, rule(testlist_star_expr_2), tok(DEL_COMMA))
110DEF_RULE(testlist_star_expr_2, nc, or(2), rule(star_expr), rule(test))
111DEF_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))
112
113// del_stmt: 'del' exprlist
114// pass_stmt: 'pass'
115// flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
116// break_stmt: 'break'
117// continue_stmt: 'continue'
118// return_stmt: 'return' [testlist]
119// yield_stmt: yield_expr
120// raise_stmt: 'raise' [test ['from' test]]
121
122DEF_RULE(del_stmt, c(del_stmt), and(2), tok(KW_DEL), rule(exprlist))
123DEF_RULE(pass_stmt, c(generic_all_nodes), and(1), tok(KW_PASS))
124DEF_RULE(flow_stmt, nc, or(5), rule(break_stmt), rule(continue_stmt), rule(return_stmt), rule(raise_stmt), rule(yield_stmt))
125DEF_RULE(break_stmt, c(break_stmt), and(1), tok(KW_BREAK))
126DEF_RULE(continue_stmt, c(continue_stmt), and(1), tok(KW_CONTINUE))
127DEF_RULE(return_stmt, c(return_stmt), and(2), tok(KW_RETURN), opt_rule(testlist))
128DEF_RULE(yield_stmt, c(yield_stmt), and(1), rule(yield_expr))
129DEF_RULE(raise_stmt, c(raise_stmt), and(2), tok(KW_RAISE), opt_rule(raise_stmt_arg))
Damien George0c1de1c2016-04-14 13:23:50 +0100130DEF_RULE(raise_stmt_arg, nc, and_ident(2), rule(test), opt_rule(raise_stmt_from))
131DEF_RULE(raise_stmt_from, nc, and_ident(2), tok(KW_FROM), rule(test))
Damien429d7192013-10-04 19:53:11 +0100132
133// import_stmt: import_name | import_from
134// import_name: 'import' dotted_as_names
135// import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) 'import' ('*' | '(' import_as_names ')' | import_as_names)
136// import_as_name: NAME ['as' NAME]
137// dotted_as_name: dotted_name ['as' NAME]
138// import_as_names: import_as_name (',' import_as_name)* [',']
139// dotted_as_names: dotted_as_name (',' dotted_as_name)*
140// dotted_name: NAME ('.' NAME)*
141// global_stmt: 'global' NAME (',' NAME)*
142// nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
143// assert_stmt: 'assert' test [',' test]
144
145DEF_RULE(import_stmt, nc, or(2), rule(import_name), rule(import_from))
146DEF_RULE(import_name, c(import_name), and(2), tok(KW_IMPORT), rule(dotted_as_names))
147DEF_RULE(import_from, c(import_from), and(4), tok(KW_FROM), rule(import_from_2), tok(KW_IMPORT), rule(import_from_3))
148DEF_RULE(import_from_2, nc, or(2), rule(dotted_name), rule(import_from_2b))
Damien George0c1de1c2016-04-14 13:23:50 +0100149DEF_RULE(import_from_2b, nc, and_ident(2), rule(one_or_more_period_or_ellipsis), opt_rule(dotted_name))
Damien429d7192013-10-04 19:53:11 +0100150DEF_RULE(import_from_3, nc, or(3), tok(OP_STAR), rule(import_as_names_paren), rule(import_as_names))
Damien George0c1de1c2016-04-14 13:23:50 +0100151DEF_RULE(import_as_names_paren, nc, and_ident(3), tok(DEL_PAREN_OPEN), rule(import_as_names), tok(DEL_PAREN_CLOSE))
Damien Georgee9906ac2014-01-04 18:44:46 +0000152DEF_RULE(one_or_more_period_or_ellipsis, nc, one_or_more, rule(period_or_ellipsis))
153DEF_RULE(period_or_ellipsis, nc, or(2), tok(DEL_PERIOD), tok(ELLIPSIS))
Damien429d7192013-10-04 19:53:11 +0100154DEF_RULE(import_as_name, nc, and(2), tok(NAME), opt_rule(as_name))
Damien George0c1de1c2016-04-14 13:23:50 +0100155DEF_RULE(dotted_as_name, nc, and_ident(2), rule(dotted_name), opt_rule(as_name))
156DEF_RULE(as_name, nc, and_ident(2), tok(KW_AS), tok(NAME))
Damien429d7192013-10-04 19:53:11 +0100157DEF_RULE(import_as_names, nc, list_with_end, rule(import_as_name), tok(DEL_COMMA))
158DEF_RULE(dotted_as_names, nc, list, rule(dotted_as_name), tok(DEL_COMMA))
159DEF_RULE(dotted_name, nc, list, tok(NAME), tok(DEL_PERIOD))
160DEF_RULE(global_stmt, c(global_stmt), and(2), tok(KW_GLOBAL), rule(name_list))
161DEF_RULE(nonlocal_stmt, c(nonlocal_stmt), and(2), tok(KW_NONLOCAL), rule(name_list))
162DEF_RULE(name_list, nc, list, tok(NAME), tok(DEL_COMMA))
163DEF_RULE(assert_stmt, c(assert_stmt), and(3), tok(KW_ASSERT), rule(test), opt_rule(assert_stmt_extra))
Damien George0c1de1c2016-04-14 13:23:50 +0100164DEF_RULE(assert_stmt_extra, nc, and_ident(2), tok(DEL_COMMA), rule(test))
Damien429d7192013-10-04 19:53:11 +0100165
pohmelie81ebba72016-01-27 23:23:11 +0300166// compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
Damien429d7192013-10-04 19:53:11 +0100167// if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
168// while_stmt: 'while' test ':' suite ['else' ':' suite]
169// for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
170// try_stmt: 'try' ':' suite ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite)
171// # NB compile.c makes sure that the default except clause is last
172// except_clause: 'except' [test ['as' NAME]]
173// with_stmt: 'with' with_item (',' with_item)* ':' suite
174// with_item: test ['as' expr]
175// suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
pohmelie81ebba72016-01-27 23:23:11 +0300176// async_stmt: 'async' (funcdef | with_stmt | for_stmt)
Damien429d7192013-10-04 19:53:11 +0100177
pohmelie81ebba72016-01-27 23:23:11 +0300178#if MICROPY_PY_ASYNC_AWAIT
179DEF_RULE(compound_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(async_stmt))
180DEF_RULE(async_stmt, c(async_stmt), and(2), tok(KW_ASYNC), rule(async_stmt_2))
181DEF_RULE(async_stmt_2, nc, or(3), rule(funcdef), rule(with_stmt), rule(for_stmt))
182#else
Damien3997be42013-10-18 19:56:48 +0100183DEF_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))
pohmelie81ebba72016-01-27 23:23:11 +0300184#endif
Damien429d7192013-10-04 19:53:11 +0100185DEF_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))
186DEF_RULE(if_stmt_elif_list, nc, one_or_more, rule(if_stmt_elif))
187DEF_RULE(if_stmt_elif, nc, and(4), tok(KW_ELIF), rule(test), tok(DEL_COLON), rule(suite))
188DEF_RULE(while_stmt, c(while_stmt), and(5), tok(KW_WHILE), rule(test), tok(DEL_COLON), rule(suite), opt_rule(else_stmt))
189DEF_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))
190DEF_RULE(try_stmt, c(try_stmt), and(4), tok(KW_TRY), tok(DEL_COLON), rule(suite), rule(try_stmt_2))
191DEF_RULE(try_stmt_2, nc, or(2), rule(try_stmt_except_and_more), rule(try_stmt_finally))
Damien George0c1de1c2016-04-14 13:23:50 +0100192DEF_RULE(try_stmt_except_and_more, nc, and_ident(3), rule(try_stmt_except_list), opt_rule(else_stmt), opt_rule(try_stmt_finally))
Damien429d7192013-10-04 19:53:11 +0100193DEF_RULE(try_stmt_except, nc, and(4), tok(KW_EXCEPT), opt_rule(try_stmt_as_name), tok(DEL_COLON), rule(suite))
Damien George0c1de1c2016-04-14 13:23:50 +0100194DEF_RULE(try_stmt_as_name, nc, and_ident(2), rule(test), opt_rule(as_name))
Damien429d7192013-10-04 19:53:11 +0100195DEF_RULE(try_stmt_except_list, nc, one_or_more, rule(try_stmt_except))
196DEF_RULE(try_stmt_finally, nc, and(3), tok(KW_FINALLY), tok(DEL_COLON), rule(suite))
Damien George0c1de1c2016-04-14 13:23:50 +0100197DEF_RULE(else_stmt, nc, and_ident(3), tok(KW_ELSE), tok(DEL_COLON), rule(suite))
Damien429d7192013-10-04 19:53:11 +0100198DEF_RULE(with_stmt, c(with_stmt), and(4), tok(KW_WITH), rule(with_stmt_list), tok(DEL_COLON), rule(suite))
199DEF_RULE(with_stmt_list, nc, list, rule(with_item), tok(DEL_COMMA))
Damien George0c1de1c2016-04-14 13:23:50 +0100200DEF_RULE(with_item, nc, and_ident(2), rule(test), opt_rule(with_item_as))
201DEF_RULE(with_item_as, nc, and_ident(2), tok(KW_AS), rule(expr))
Damien429d7192013-10-04 19:53:11 +0100202DEF_RULE(suite, nc, or(2), rule(suite_block), rule(simple_stmt))
Damien George0c1de1c2016-04-14 13:23:50 +0100203DEF_RULE(suite_block, nc, and_ident(4), tok(NEWLINE), tok(INDENT), rule(suite_block_stmts), tok(DEDENT))
Damien429d7192013-10-04 19:53:11 +0100204DEF_RULE(suite_block_stmts, c(generic_all_nodes), one_or_more, rule(stmt))
205
206// test: or_test ['if' or_test 'else' test] | lambdef
207// test_nocond: or_test | lambdef_nocond
208// lambdef: 'lambda' [varargslist] ':' test
209// lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
210
211DEF_RULE(test, nc, or(2), rule(lambdef), rule(test_if_expr))
Damien George0c1de1c2016-04-14 13:23:50 +0100212DEF_RULE(test_if_expr, c(test_if_expr), and_ident(2), rule(or_test), opt_rule(test_if_else))
Damien429d7192013-10-04 19:53:11 +0100213DEF_RULE(test_if_else, nc, and(4), tok(KW_IF), rule(or_test), tok(KW_ELSE), rule(test))
214DEF_RULE(test_nocond, nc, or(2), rule(lambdef_nocond), rule(or_test))
Damien George0c1de1c2016-04-14 13:23:50 +0100215DEF_RULE(lambdef, c(lambdef), and_blank(4), tok(KW_LAMBDA), opt_rule(varargslist), tok(DEL_COLON), rule(test))
216DEF_RULE(lambdef_nocond, c(lambdef), and_blank(4), tok(KW_LAMBDA), opt_rule(varargslist), tok(DEL_COLON), rule(test_nocond))
Damien429d7192013-10-04 19:53:11 +0100217
218// or_test: and_test ('or' and_test)*
219// and_test: not_test ('and' not_test)*
220// not_test: 'not' not_test | comparison
221// comparison: expr (comp_op expr)*
222// comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
223// star_expr: '*' expr
224// expr: xor_expr ('|' xor_expr)*
225// xor_expr: and_expr ('^' and_expr)*
226// and_expr: shift_expr ('&' shift_expr)*
227// shift_expr: arith_expr (('<<'|'>>') arith_expr)*
228// arith_expr: term (('+'|'-') term)*
229// term: factor (('*'|'/'|'%'|'//') factor)*
230// factor: ('+'|'-'|'~') factor | power
pohmelie81ebba72016-01-27 23:23:11 +0300231// power: atom_expr ['**' factor]
232// atom_expr: 'await' atom trailer* | atom trailer*
Damien429d7192013-10-04 19:53:11 +0100233
234DEF_RULE(or_test, c(or_test), list, rule(and_test), tok(KW_OR))
235DEF_RULE(and_test, c(and_test), list, rule(not_test), tok(KW_AND))
236DEF_RULE(not_test, nc, or(2), rule(not_test_2), rule(comparison))
237DEF_RULE(not_test_2, c(not_test_2), and(2), tok(KW_NOT), rule(not_test))
238DEF_RULE(comparison, c(comparison), list, rule(expr), rule(comp_op))
239DEF_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))
240DEF_RULE(comp_op_not_in, nc, and(2), tok(KW_NOT), tok(KW_IN))
241DEF_RULE(comp_op_is, nc, and(2), tok(KW_IS), opt_rule(comp_op_is_not))
242DEF_RULE(comp_op_is_not, nc, and(1), tok(KW_NOT))
243DEF_RULE(star_expr, c(star_expr), and(2), tok(OP_STAR), rule(expr))
244DEF_RULE(expr, c(expr), list, rule(xor_expr), tok(OP_PIPE))
245DEF_RULE(xor_expr, c(xor_expr), list, rule(and_expr), tok(OP_CARET))
246DEF_RULE(and_expr, c(and_expr), list, rule(shift_expr), tok(OP_AMPERSAND))
247DEF_RULE(shift_expr, c(shift_expr), list, rule(arith_expr), rule(shift_op))
248DEF_RULE(shift_op, nc, or(2), tok(OP_DBL_LESS), tok(OP_DBL_MORE))
249DEF_RULE(arith_expr, c(arith_expr), list, rule(term), rule(arith_op))
250DEF_RULE(arith_op, nc, or(2), tok(OP_PLUS), tok(OP_MINUS))
251DEF_RULE(term, c(term), list, rule(factor), rule(term_op))
252DEF_RULE(term_op, nc, or(4), tok(OP_STAR), tok(OP_SLASH), tok(OP_PERCENT), tok(OP_DBL_SLASH))
253DEF_RULE(factor, nc, or(2), rule(factor_2), rule(power))
Damien George0c1de1c2016-04-14 13:23:50 +0100254DEF_RULE(factor_2, c(factor_2), and_ident(2), rule(factor_op), rule(factor))
Damien429d7192013-10-04 19:53:11 +0100255DEF_RULE(factor_op, nc, or(3), tok(OP_PLUS), tok(OP_MINUS), tok(OP_TILDE))
Damien George0c1de1c2016-04-14 13:23:50 +0100256DEF_RULE(power, c(power), and_ident(2), rule(atom_expr), opt_rule(power_dbl_star))
pohmelie81ebba72016-01-27 23:23:11 +0300257#if MICROPY_PY_ASYNC_AWAIT
258DEF_RULE(atom_expr, nc, or(2), rule(atom_expr_await), rule(atom_expr_normal))
259DEF_RULE(atom_expr_await, c(atom_expr_await), and(3), tok(KW_AWAIT), rule(atom), opt_rule(atom_expr_trailers))
260#else
261DEF_RULE(atom_expr, nc, or(1), rule(atom_expr_normal))
262#endif
Damien George0c1de1c2016-04-14 13:23:50 +0100263DEF_RULE(atom_expr_normal, c(atom_expr_normal), and_ident(2), rule(atom), opt_rule(atom_expr_trailers))
pohmelie81ebba72016-01-27 23:23:11 +0300264DEF_RULE(atom_expr_trailers, c(atom_expr_trailers), one_or_more, rule(trailer))
Damien George0c1de1c2016-04-14 13:23:50 +0100265DEF_RULE(power_dbl_star, nc, and_ident(2), tok(OP_DBL_STAR), rule(factor))
Damien429d7192013-10-04 19:53:11 +0100266
267// atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' | '{' [dictorsetmaker] '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False'
268// testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
269// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
270
Damien George7d414a12015-02-08 01:57:40 +0000271DEF_RULE(atom, nc, or(11), tok(NAME), tok(INTEGER), tok(FLOAT_OR_IMAG), rule(atom_string), tok(ELLIPSIS), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
Damien429d7192013-10-04 19:53:11 +0100272DEF_RULE(atom_string, c(atom_string), one_or_more, rule(string_or_bytes))
273DEF_RULE(string_or_bytes, nc, or(2), tok(STRING), tok(BYTES))
274DEF_RULE(atom_paren, c(atom_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(atom_2b), tok(DEL_PAREN_CLOSE))
275DEF_RULE(atom_2b, nc, or(2), rule(yield_expr), rule(testlist_comp))
276DEF_RULE(atom_bracket, c(atom_bracket), and(3), tok(DEL_BRACKET_OPEN), opt_rule(testlist_comp), tok(DEL_BRACKET_CLOSE))
277DEF_RULE(atom_brace, c(atom_brace), and(3), tok(DEL_BRACE_OPEN), opt_rule(dictorsetmaker), tok(DEL_BRACE_CLOSE))
Damien George0c1de1c2016-04-14 13:23:50 +0100278DEF_RULE(testlist_comp, nc, and_ident(2), rule(testlist_comp_2), opt_rule(testlist_comp_3))
Damien429d7192013-10-04 19:53:11 +0100279DEF_RULE(testlist_comp_2, nc, or(2), rule(star_expr), rule(test))
280DEF_RULE(testlist_comp_3, nc, or(2), rule(comp_for), rule(testlist_comp_3b))
Damien George0c1de1c2016-04-14 13:23:50 +0100281DEF_RULE(testlist_comp_3b, nc, and_ident(2), tok(DEL_COMMA), opt_rule(testlist_comp_3c))
Damien429d7192013-10-04 19:53:11 +0100282DEF_RULE(testlist_comp_3c, nc, list_with_end, rule(testlist_comp_2), tok(DEL_COMMA))
283DEF_RULE(trailer, nc, or(3), rule(trailer_paren), rule(trailer_bracket), rule(trailer_period))
284DEF_RULE(trailer_paren, c(trailer_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(arglist), tok(DEL_PAREN_CLOSE))
285DEF_RULE(trailer_bracket, c(trailer_bracket), and(3), tok(DEL_BRACKET_OPEN), rule(subscriptlist), tok(DEL_BRACKET_CLOSE))
286DEF_RULE(trailer_period, c(trailer_period), and(2), tok(DEL_PERIOD), tok(NAME))
287
288// subscriptlist: subscript (',' subscript)* [',']
289// subscript: test | [test] ':' [test] [sliceop]
290// sliceop: ':' [test]
291
Damien George83204f32014-12-27 17:20:41 +0000292#if MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +0100293DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(subscript), tok(DEL_COMMA))
294DEF_RULE(subscript, nc, or(2), rule(subscript_3), rule(subscript_2))
Damien George0c1de1c2016-04-14 13:23:50 +0100295DEF_RULE(subscript_2, c(subscript_2), and_ident(2), rule(test), opt_rule(subscript_3))
Damien429d7192013-10-04 19:53:11 +0100296DEF_RULE(subscript_3, c(subscript_3), and(2), tok(DEL_COLON), opt_rule(subscript_3b))
297DEF_RULE(subscript_3b, nc, or(2), rule(subscript_3c), rule(subscript_3d))
298DEF_RULE(subscript_3c, nc, and(2), tok(DEL_COLON), opt_rule(test))
Damien George0c1de1c2016-04-14 13:23:50 +0100299DEF_RULE(subscript_3d, nc, and_ident(2), rule(test), opt_rule(sliceop))
Damien429d7192013-10-04 19:53:11 +0100300DEF_RULE(sliceop, nc, and(2), tok(DEL_COLON), opt_rule(test))
Damien George83204f32014-12-27 17:20:41 +0000301#else
302DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
303#endif
Damien429d7192013-10-04 19:53:11 +0100304
305// exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
306// testlist: test (',' test)* [',']
307// dictorsetmaker: (test ':' test (comp_for | (',' test ':' test)* [','])) | (test (comp_for | (',' test)* [',']))
308
309DEF_RULE(exprlist, nc, list_with_end, rule(exprlist_2), tok(DEL_COMMA))
310DEF_RULE(exprlist_2, nc, or(2), rule(star_expr), rule(expr))
311DEF_RULE(testlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
312// TODO dictorsetmaker lets through more than is allowed
Damien George0c1de1c2016-04-14 13:23:50 +0100313DEF_RULE(dictorsetmaker, nc, and_ident(2), rule(dictorsetmaker_item), opt_rule(dictorsetmaker_tail))
Damien Georgee37dcaa2014-12-27 17:07:16 +0000314#if MICROPY_PY_BUILTINS_SET
Damien George0c1de1c2016-04-14 13:23:50 +0100315DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and_ident(2), rule(test), opt_rule(dictorsetmaker_colon))
316DEF_RULE(dictorsetmaker_colon, nc, and_ident(2), tok(DEL_COLON), rule(test))
Damien Georgee37dcaa2014-12-27 17:07:16 +0000317#else
318DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and(3), rule(test), tok(DEL_COLON), rule(test))
319#endif
Damien429d7192013-10-04 19:53:11 +0100320DEF_RULE(dictorsetmaker_tail, nc, or(2), rule(comp_for), rule(dictorsetmaker_list))
321DEF_RULE(dictorsetmaker_list, nc, and(2), tok(DEL_COMMA), opt_rule(dictorsetmaker_list2))
322DEF_RULE(dictorsetmaker_list2, nc, list_with_end, rule(dictorsetmaker_item), tok(DEL_COMMA))
323
324// classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
325
Damien George0c1de1c2016-04-14 13:23:50 +0100326DEF_RULE(classdef, c(classdef), and_blank(5), tok(KW_CLASS), tok(NAME), opt_rule(classdef_2), tok(DEL_COLON), rule(suite))
327DEF_RULE(classdef_2, nc, and_ident(3), tok(DEL_PAREN_OPEN), opt_rule(arglist), tok(DEL_PAREN_CLOSE))
Damien429d7192013-10-04 19:53:11 +0100328
329// arglist: (argument ',')* (argument [','] | '*' test (',' argument)* [',' '**' test] | '**' test)
330
331// TODO arglist lets through more than is allowed, compiler needs to do further verification
Damien George2c0842b2014-04-27 16:46:51 +0100332DEF_RULE(arglist, nc, list_with_end, rule(arglist_2), tok(DEL_COMMA))
Damien429d7192013-10-04 19:53:11 +0100333DEF_RULE(arglist_2, nc, or(3), rule(arglist_star), rule(arglist_dbl_star), rule(argument))
Damien George2c0842b2014-04-27 16:46:51 +0100334DEF_RULE(arglist_star, nc, and(2), tok(OP_STAR), rule(test))
335DEF_RULE(arglist_dbl_star, nc, and(2), tok(OP_DBL_STAR), rule(test))
Damien429d7192013-10-04 19:53:11 +0100336
337// # The reason that keywords are test nodes instead of NAME is that using NAME
338// # results in an ambiguity. ast.c makes sure it's a NAME.
339// argument: test [comp_for] | test '=' test # Really [keyword '='] test
340// comp_iter: comp_for | comp_if
341// comp_for: 'for' exprlist 'in' or_test [comp_iter]
342// comp_if: 'if' test_nocond [comp_iter]
343
Damien George0c1de1c2016-04-14 13:23:50 +0100344DEF_RULE(argument, nc, and_ident(2), rule(test), opt_rule(argument_2))
Damien429d7192013-10-04 19:53:11 +0100345DEF_RULE(argument_2, nc, or(2), rule(comp_for), rule(argument_3))
Damien George0c1de1c2016-04-14 13:23:50 +0100346DEF_RULE(argument_3, nc, and_ident(2), tok(DEL_EQUAL), rule(test))
Damien429d7192013-10-04 19:53:11 +0100347DEF_RULE(comp_iter, nc, or(2), rule(comp_for), rule(comp_if))
Damien George0c1de1c2016-04-14 13:23:50 +0100348DEF_RULE(comp_for, nc, and_blank(5), tok(KW_FOR), rule(exprlist), tok(KW_IN), rule(or_test), opt_rule(comp_iter))
Damien429d7192013-10-04 19:53:11 +0100349DEF_RULE(comp_if, nc, and(3), tok(KW_IF), rule(test_nocond), opt_rule(comp_iter))
350
351// # not used in grammar, but may appear in "node" passed from Parser to Compiler
352// encoding_decl: NAME
353
354// yield_expr: 'yield' [yield_arg]
355// yield_arg: 'from' test | testlist
356
357DEF_RULE(yield_expr, c(yield_expr), and(2), tok(KW_YIELD), opt_rule(yield_arg))
358DEF_RULE(yield_arg, nc, or(2), rule(yield_arg_from), rule(testlist))
359DEF_RULE(yield_arg_from, nc, and(2), tok(KW_FROM), rule(test))