blob: 285fbded2fdaea0f9a03674a81104edc4a607269 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
Damien George17839502020-06-16 21:42:44 +10006 * Copyright (c) 2013-2020 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
Damien George3f39d182020-02-26 11:58:42 +110027// *FORMAT-OFF*
28
Damien429d7192013-10-04 19:53:11 +010029// rules for writing rules:
30// - zero_or_more is implemented using opt_rule around a one_or_more rule
31// - don't put opt_rule in arguments of or rule; instead, wrap the call to this or rule in opt_rule
32
Damien George131b0de2020-06-16 22:48:46 +100033// Generic sub-rules used by multiple rules below.
34
35DEF_RULE_NC(generic_colon_test, and_ident(2), tok(DEL_COLON), rule(test))
36DEF_RULE_NC(generic_equal_test, and_ident(2), tok(DEL_EQUAL), rule(test))
37
Damien429d7192013-10-04 19:53:11 +010038// # Start symbols for the grammar:
39// # single_input is a single interactive statement;
40// # file_input is a module or sequence of commands read from an input file;
41// # eval_input is the input for the eval() functions.
Alexander Steffen55f33242017-06-30 09:22:17 +020042// # NB: compound_stmt in single_input is followed by extra NEWLINE! --> not in MicroPython
Damien3997be42013-10-18 19:56:48 +010043// single_input: NEWLINE | simple_stmt | compound_stmt
Damien429d7192013-10-04 19:53:11 +010044// file_input: (NEWLINE | stmt)* ENDMARKER
45// eval_input: testlist NEWLINE* ENDMARKER
46
Damien George71019ae2017-02-15 10:58:05 +110047DEF_RULE_NC(single_input, or(3), tok(NEWLINE), rule(simple_stmt), rule(compound_stmt))
Damien George0c1de1c2016-04-14 13:23:50 +010048DEF_RULE(file_input, c(generic_all_nodes), and_ident(1), opt_rule(file_input_2))
Damien429d7192013-10-04 19:53:11 +010049DEF_RULE(file_input_2, c(generic_all_nodes), one_or_more, rule(file_input_3))
Damien George71019ae2017-02-15 10:58:05 +110050DEF_RULE_NC(file_input_3, or(2), tok(NEWLINE), rule(stmt))
51DEF_RULE_NC(eval_input, and_ident(2), rule(testlist), opt_rule(eval_input_2))
52DEF_RULE_NC(eval_input_2, and(1), tok(NEWLINE))
Damien429d7192013-10-04 19:53:11 +010053
54// decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
55// decorators: decorator+
pohmelie81ebba72016-01-27 23:23:11 +030056// decorated: decorators (classdef | funcdef | async_funcdef)
Damien429d7192013-10-04 19:53:11 +010057// funcdef: 'def' NAME parameters ['->' test] ':' suite
pohmelie81ebba72016-01-27 23:23:11 +030058// async_funcdef: 'async' funcdef
Damien429d7192013-10-04 19:53:11 +010059// parameters: '(' [typedargslist] ')'
60// typedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* [',' ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]] | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef
61// tfpdef: NAME [':' test]
62// varargslist: vfpdef ['=' test] (',' vfpdef ['=' test])* [',' ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
63// vfpdef: NAME
64
Damien George2069c562019-07-25 13:15:54 +100065DEF_RULE_NC(decorator, and(4), tok(OP_AT), rule(dotted_name), opt_rule(trailer_paren), tok(NEWLINE))
Damien George71019ae2017-02-15 10:58:05 +110066DEF_RULE_NC(decorators, one_or_more, rule(decorator))
Damien George0c1de1c2016-04-14 13:23:50 +010067DEF_RULE(decorated, c(decorated), and_ident(2), rule(decorators), rule(decorated_body))
pohmelie81ebba72016-01-27 23:23:11 +030068#if MICROPY_PY_ASYNC_AWAIT
Damien George71019ae2017-02-15 10:58:05 +110069DEF_RULE_NC(decorated_body, or(3), rule(classdef), rule(funcdef), rule(async_funcdef))
70DEF_RULE_NC(async_funcdef, and(2), tok(KW_ASYNC), rule(funcdef))
pohmelie81ebba72016-01-27 23:23:11 +030071#else
Damien George71019ae2017-02-15 10:58:05 +110072DEF_RULE_NC(decorated_body, or(2), rule(classdef), rule(funcdef))
pohmelie81ebba72016-01-27 23:23:11 +030073#endif
Damien George0c1de1c2016-04-14 13:23:50 +010074DEF_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))
Damien George71019ae2017-02-15 10:58:05 +110075DEF_RULE_NC(funcdefrettype, and_ident(2), tok(DEL_MINUS_MORE), rule(test))
Damien George9a569122015-11-23 16:50:42 +000076// note: typedargslist lets through more than is allowed, compiler does further checks
Damien George71019ae2017-02-15 10:58:05 +110077DEF_RULE_NC(typedargslist, list_with_end, rule(typedargslist_item), tok(DEL_COMMA))
78DEF_RULE_NC(typedargslist_item, or(3), rule(typedargslist_name), rule(typedargslist_star), rule(typedargslist_dbl_star))
Damien George131b0de2020-06-16 22:48:46 +100079DEF_RULE_NC(typedargslist_name, and_ident(3), tok(NAME), opt_rule(generic_colon_test), opt_rule(generic_equal_test))
Damien George71019ae2017-02-15 10:58:05 +110080DEF_RULE_NC(typedargslist_star, and(2), tok(OP_STAR), opt_rule(tfpdef))
Damien George131b0de2020-06-16 22:48:46 +100081DEF_RULE_NC(typedargslist_dbl_star, and(3), tok(OP_DBL_STAR), tok(NAME), opt_rule(generic_colon_test))
82DEF_RULE_NC(tfpdef, and(2), tok(NAME), opt_rule(generic_colon_test))
Damien George9a569122015-11-23 16:50:42 +000083// note: varargslist lets through more than is allowed, compiler does further checks
Damien George71019ae2017-02-15 10:58:05 +110084DEF_RULE_NC(varargslist, list_with_end, rule(varargslist_item), tok(DEL_COMMA))
85DEF_RULE_NC(varargslist_item, or(3), rule(varargslist_name), rule(varargslist_star), rule(varargslist_dbl_star))
Damien George131b0de2020-06-16 22:48:46 +100086DEF_RULE_NC(varargslist_name, and_ident(2), tok(NAME), opt_rule(generic_equal_test))
Damien George71019ae2017-02-15 10:58:05 +110087DEF_RULE_NC(varargslist_star, and(2), tok(OP_STAR), opt_rule(vfpdef))
88DEF_RULE_NC(varargslist_dbl_star, and(2), tok(OP_DBL_STAR), tok(NAME))
Damien George71019ae2017-02-15 10:58:05 +110089DEF_RULE_NC(vfpdef, and_ident(1), tok(NAME))
Damien429d7192013-10-04 19:53:11 +010090
Damien Georgefa90ab12015-04-21 16:35:50 +000091// stmt: compound_stmt | simple_stmt
Damien429d7192013-10-04 19:53:11 +010092
Damien George71019ae2017-02-15 10:58:05 +110093DEF_RULE_NC(stmt, or(2), rule(compound_stmt), rule(simple_stmt))
Damien429d7192013-10-04 19:53:11 +010094
95// simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
96
Damien George71019ae2017-02-15 10:58:05 +110097DEF_RULE_NC(simple_stmt, and_ident(2), rule(simple_stmt_2), tok(NEWLINE))
Damien429d7192013-10-04 19:53:11 +010098DEF_RULE(simple_stmt_2, c(generic_all_nodes), list_with_end, rule(small_stmt), tok(DEL_SEMICOLON))
99
100// small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Damien Georgef2e267d2020-06-16 22:49:25 +1000101// expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | ('=' (yield_expr|testlist_star_expr))*)
Damien429d7192013-10-04 19:53:11 +0100102// testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
Damien Georgef2e267d2020-06-16 22:49:25 +1000103// annassign: ':' test ['=' (yield_expr|testlist_star_expr)]
Damien George2069c562019-07-25 13:15:54 +1000104// augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//='
Damien Georgef2e267d2020-06-16 22:49:25 +1000105// # For normal and annotated assignments, additional restrictions enforced by the interpreter
Damien429d7192013-10-04 19:53:11 +0100106
Damien George71019ae2017-02-15 10:58:05 +1100107DEF_RULE_NC(small_stmt, 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))
Damien429d7192013-10-04 19:53:11 +0100108DEF_RULE(expr_stmt, c(expr_stmt), and(2), rule(testlist_star_expr), opt_rule(expr_stmt_2))
Damien Georgef2e267d2020-06-16 22:49:25 +1000109DEF_RULE_NC(expr_stmt_2, or(3), rule(annassign), rule(expr_stmt_augassign), rule(expr_stmt_assign_list))
Damien George71019ae2017-02-15 10:58:05 +1100110DEF_RULE_NC(expr_stmt_augassign, and_ident(2), rule(augassign), rule(expr_stmt_6))
111DEF_RULE_NC(expr_stmt_assign_list, one_or_more, rule(expr_stmt_assign))
112DEF_RULE_NC(expr_stmt_assign, and_ident(2), tok(DEL_EQUAL), rule(expr_stmt_6))
113DEF_RULE_NC(expr_stmt_6, or(2), rule(yield_expr), rule(testlist_star_expr))
Damien429d7192013-10-04 19:53:11 +0100114DEF_RULE(testlist_star_expr, c(generic_tuple), list_with_end, rule(testlist_star_expr_2), tok(DEL_COMMA))
Damien George71019ae2017-02-15 10:58:05 +1100115DEF_RULE_NC(testlist_star_expr_2, or(2), rule(star_expr), rule(test))
Damien Georgef2e267d2020-06-16 22:49:25 +1000116DEF_RULE_NC(annassign, and(3), tok(DEL_COLON), rule(test), opt_rule(expr_stmt_assign))
Damien George2069c562019-07-25 13:15:54 +1000117DEF_RULE_NC(augassign, or(13), tok(DEL_PLUS_EQUAL), tok(DEL_MINUS_EQUAL), tok(DEL_STAR_EQUAL), tok(DEL_AT_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))
Damien429d7192013-10-04 19:53:11 +0100118
119// del_stmt: 'del' exprlist
120// pass_stmt: 'pass'
121// flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
122// break_stmt: 'break'
123// continue_stmt: 'continue'
124// return_stmt: 'return' [testlist]
125// yield_stmt: yield_expr
126// raise_stmt: 'raise' [test ['from' test]]
127
128DEF_RULE(del_stmt, c(del_stmt), and(2), tok(KW_DEL), rule(exprlist))
129DEF_RULE(pass_stmt, c(generic_all_nodes), and(1), tok(KW_PASS))
Damien George71019ae2017-02-15 10:58:05 +1100130DEF_RULE_NC(flow_stmt, or(5), rule(break_stmt), rule(continue_stmt), rule(return_stmt), rule(raise_stmt), rule(yield_stmt))
Damien Georgec1491972018-06-19 13:54:03 +1000131DEF_RULE(break_stmt, c(break_cont_stmt), and(1), tok(KW_BREAK))
132DEF_RULE(continue_stmt, c(break_cont_stmt), and(1), tok(KW_CONTINUE))
Damien429d7192013-10-04 19:53:11 +0100133DEF_RULE(return_stmt, c(return_stmt), and(2), tok(KW_RETURN), opt_rule(testlist))
134DEF_RULE(yield_stmt, c(yield_stmt), and(1), rule(yield_expr))
135DEF_RULE(raise_stmt, c(raise_stmt), and(2), tok(KW_RAISE), opt_rule(raise_stmt_arg))
Damien George71019ae2017-02-15 10:58:05 +1100136DEF_RULE_NC(raise_stmt_arg, and_ident(2), rule(test), opt_rule(raise_stmt_from))
137DEF_RULE_NC(raise_stmt_from, and_ident(2), tok(KW_FROM), rule(test))
Damien429d7192013-10-04 19:53:11 +0100138
139// import_stmt: import_name | import_from
140// import_name: 'import' dotted_as_names
141// import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) 'import' ('*' | '(' import_as_names ')' | import_as_names)
142// import_as_name: NAME ['as' NAME]
143// dotted_as_name: dotted_name ['as' NAME]
144// import_as_names: import_as_name (',' import_as_name)* [',']
145// dotted_as_names: dotted_as_name (',' dotted_as_name)*
146// dotted_name: NAME ('.' NAME)*
147// global_stmt: 'global' NAME (',' NAME)*
148// nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
149// assert_stmt: 'assert' test [',' test]
150
Damien George71019ae2017-02-15 10:58:05 +1100151DEF_RULE_NC(import_stmt, or(2), rule(import_name), rule(import_from))
Damien429d7192013-10-04 19:53:11 +0100152DEF_RULE(import_name, c(import_name), and(2), tok(KW_IMPORT), rule(dotted_as_names))
153DEF_RULE(import_from, c(import_from), and(4), tok(KW_FROM), rule(import_from_2), tok(KW_IMPORT), rule(import_from_3))
Damien George71019ae2017-02-15 10:58:05 +1100154DEF_RULE_NC(import_from_2, or(2), rule(dotted_name), rule(import_from_2b))
155DEF_RULE_NC(import_from_2b, and_ident(2), rule(one_or_more_period_or_ellipsis), opt_rule(dotted_name))
156DEF_RULE_NC(import_from_3, or(3), tok(OP_STAR), rule(import_as_names_paren), rule(import_as_names))
157DEF_RULE_NC(import_as_names_paren, and_ident(3), tok(DEL_PAREN_OPEN), rule(import_as_names), tok(DEL_PAREN_CLOSE))
158DEF_RULE_NC(one_or_more_period_or_ellipsis, one_or_more, rule(period_or_ellipsis))
159DEF_RULE_NC(period_or_ellipsis, or(2), tok(DEL_PERIOD), tok(ELLIPSIS))
160DEF_RULE_NC(import_as_name, and(2), tok(NAME), opt_rule(as_name))
161DEF_RULE_NC(dotted_as_name, and_ident(2), rule(dotted_name), opt_rule(as_name))
162DEF_RULE_NC(as_name, and_ident(2), tok(KW_AS), tok(NAME))
163DEF_RULE_NC(import_as_names, list_with_end, rule(import_as_name), tok(DEL_COMMA))
164DEF_RULE_NC(dotted_as_names, list, rule(dotted_as_name), tok(DEL_COMMA))
165DEF_RULE_NC(dotted_name, list, tok(NAME), tok(DEL_PERIOD))
Damien George1a7109d2018-06-19 14:04:05 +1000166DEF_RULE(global_stmt, c(global_nonlocal_stmt), and(2), tok(KW_GLOBAL), rule(name_list))
167DEF_RULE(nonlocal_stmt, c(global_nonlocal_stmt), and(2), tok(KW_NONLOCAL), rule(name_list))
Damien George71019ae2017-02-15 10:58:05 +1100168DEF_RULE_NC(name_list, list, tok(NAME), tok(DEL_COMMA))
Damien429d7192013-10-04 19:53:11 +0100169DEF_RULE(assert_stmt, c(assert_stmt), and(3), tok(KW_ASSERT), rule(test), opt_rule(assert_stmt_extra))
Damien George71019ae2017-02-15 10:58:05 +1100170DEF_RULE_NC(assert_stmt_extra, and_ident(2), tok(DEL_COMMA), rule(test))
Damien429d7192013-10-04 19:53:11 +0100171
pohmelie81ebba72016-01-27 23:23:11 +0300172// compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
Damien429d7192013-10-04 19:53:11 +0100173// if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
174// while_stmt: 'while' test ':' suite ['else' ':' suite]
175// for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
176// try_stmt: 'try' ':' suite ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite)
177// # NB compile.c makes sure that the default except clause is last
178// except_clause: 'except' [test ['as' NAME]]
179// with_stmt: 'with' with_item (',' with_item)* ':' suite
180// with_item: test ['as' expr]
181// suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
pohmelie81ebba72016-01-27 23:23:11 +0300182// async_stmt: 'async' (funcdef | with_stmt | for_stmt)
Damien429d7192013-10-04 19:53:11 +0100183
pohmelie81ebba72016-01-27 23:23:11 +0300184#if MICROPY_PY_ASYNC_AWAIT
Damien George71019ae2017-02-15 10:58:05 +1100185DEF_RULE_NC(compound_stmt, 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))
pohmelie81ebba72016-01-27 23:23:11 +0300186DEF_RULE(async_stmt, c(async_stmt), and(2), tok(KW_ASYNC), rule(async_stmt_2))
Damien George71019ae2017-02-15 10:58:05 +1100187DEF_RULE_NC(async_stmt_2, or(3), rule(funcdef), rule(with_stmt), rule(for_stmt))
pohmelie81ebba72016-01-27 23:23:11 +0300188#else
Damien George71019ae2017-02-15 10:58:05 +1100189DEF_RULE_NC(compound_stmt, 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 +0300190#endif
Damien George17839502020-06-16 21:42:44 +1000191DEF_RULE(if_stmt, c(if_stmt), and(6), tok(KW_IF), rule(namedexpr_test), tok(DEL_COLON), rule(suite), opt_rule(if_stmt_elif_list), opt_rule(else_stmt))
Damien George71019ae2017-02-15 10:58:05 +1100192DEF_RULE_NC(if_stmt_elif_list, one_or_more, rule(if_stmt_elif))
Damien George17839502020-06-16 21:42:44 +1000193DEF_RULE_NC(if_stmt_elif, and(4), tok(KW_ELIF), rule(namedexpr_test), tok(DEL_COLON), rule(suite))
194DEF_RULE(while_stmt, c(while_stmt), and(5), tok(KW_WHILE), rule(namedexpr_test), tok(DEL_COLON), rule(suite), opt_rule(else_stmt))
Damien429d7192013-10-04 19:53:11 +0100195DEF_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))
196DEF_RULE(try_stmt, c(try_stmt), and(4), tok(KW_TRY), tok(DEL_COLON), rule(suite), rule(try_stmt_2))
Damien George71019ae2017-02-15 10:58:05 +1100197DEF_RULE_NC(try_stmt_2, or(2), rule(try_stmt_except_and_more), rule(try_stmt_finally))
198DEF_RULE_NC(try_stmt_except_and_more, and_ident(3), rule(try_stmt_except_list), opt_rule(else_stmt), opt_rule(try_stmt_finally))
199DEF_RULE_NC(try_stmt_except, and(4), tok(KW_EXCEPT), opt_rule(try_stmt_as_name), tok(DEL_COLON), rule(suite))
200DEF_RULE_NC(try_stmt_as_name, and_ident(2), rule(test), opt_rule(as_name))
201DEF_RULE_NC(try_stmt_except_list, one_or_more, rule(try_stmt_except))
202DEF_RULE_NC(try_stmt_finally, and(3), tok(KW_FINALLY), tok(DEL_COLON), rule(suite))
203DEF_RULE_NC(else_stmt, and_ident(3), tok(KW_ELSE), tok(DEL_COLON), rule(suite))
Damien429d7192013-10-04 19:53:11 +0100204DEF_RULE(with_stmt, c(with_stmt), and(4), tok(KW_WITH), rule(with_stmt_list), tok(DEL_COLON), rule(suite))
Damien George71019ae2017-02-15 10:58:05 +1100205DEF_RULE_NC(with_stmt_list, list, rule(with_item), tok(DEL_COMMA))
206DEF_RULE_NC(with_item, and_ident(2), rule(test), opt_rule(with_item_as))
207DEF_RULE_NC(with_item_as, and_ident(2), tok(KW_AS), rule(expr))
208DEF_RULE_NC(suite, or(2), rule(suite_block), rule(simple_stmt))
209DEF_RULE_NC(suite_block, and_ident(4), tok(NEWLINE), tok(INDENT), rule(suite_block_stmts), tok(DEDENT))
Damien429d7192013-10-04 19:53:11 +0100210DEF_RULE(suite_block_stmts, c(generic_all_nodes), one_or_more, rule(stmt))
211
212// test: or_test ['if' or_test 'else' test] | lambdef
213// test_nocond: or_test | lambdef_nocond
214// lambdef: 'lambda' [varargslist] ':' test
215// lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
216
Damien George17839502020-06-16 21:42:44 +1000217#if MICROPY_PY_ASSIGN_EXPR
218DEF_RULE(namedexpr_test, c(namedexpr), and_ident(2), rule(test), opt_rule(namedexpr_test_2))
219DEF_RULE_NC(namedexpr_test_2, and_ident(2), tok(OP_ASSIGN), rule(test))
220#else
221DEF_RULE_NC(namedexpr_test, or(1), rule(test))
222#endif
Damien George71019ae2017-02-15 10:58:05 +1100223DEF_RULE_NC(test, or(2), rule(lambdef), rule(test_if_expr))
Damien George0c1de1c2016-04-14 13:23:50 +0100224DEF_RULE(test_if_expr, c(test_if_expr), and_ident(2), rule(or_test), opt_rule(test_if_else))
Damien George71019ae2017-02-15 10:58:05 +1100225DEF_RULE_NC(test_if_else, and(4), tok(KW_IF), rule(or_test), tok(KW_ELSE), rule(test))
226DEF_RULE_NC(test_nocond, or(2), rule(lambdef_nocond), rule(or_test))
Damien George0c1de1c2016-04-14 13:23:50 +0100227DEF_RULE(lambdef, c(lambdef), and_blank(4), tok(KW_LAMBDA), opt_rule(varargslist), tok(DEL_COLON), rule(test))
228DEF_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 +0100229
230// or_test: and_test ('or' and_test)*
231// and_test: not_test ('and' not_test)*
232// not_test: 'not' not_test | comparison
233// comparison: expr (comp_op expr)*
234// comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
235// star_expr: '*' expr
236// expr: xor_expr ('|' xor_expr)*
237// xor_expr: and_expr ('^' and_expr)*
238// and_expr: shift_expr ('&' shift_expr)*
239// shift_expr: arith_expr (('<<'|'>>') arith_expr)*
240// arith_expr: term (('+'|'-') term)*
Damien George2069c562019-07-25 13:15:54 +1000241// term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Damien429d7192013-10-04 19:53:11 +0100242// factor: ('+'|'-'|'~') factor | power
pohmelie81ebba72016-01-27 23:23:11 +0300243// power: atom_expr ['**' factor]
244// atom_expr: 'await' atom trailer* | atom trailer*
Damien429d7192013-10-04 19:53:11 +0100245
Damien George36e474e2018-06-19 14:10:29 +1000246DEF_RULE(or_test, c(or_and_test), list, rule(and_test), tok(KW_OR))
247DEF_RULE(and_test, c(or_and_test), list, rule(not_test), tok(KW_AND))
Damien George71019ae2017-02-15 10:58:05 +1100248DEF_RULE_NC(not_test, or(2), rule(not_test_2), rule(comparison))
Damien429d7192013-10-04 19:53:11 +0100249DEF_RULE(not_test_2, c(not_test_2), and(2), tok(KW_NOT), rule(not_test))
250DEF_RULE(comparison, c(comparison), list, rule(expr), rule(comp_op))
Damien George71019ae2017-02-15 10:58:05 +1100251DEF_RULE_NC(comp_op, 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))
252DEF_RULE_NC(comp_op_not_in, and(2), tok(KW_NOT), tok(KW_IN))
253DEF_RULE_NC(comp_op_is, and(2), tok(KW_IS), opt_rule(comp_op_is_not))
254DEF_RULE_NC(comp_op_is_not, and(1), tok(KW_NOT))
Damien429d7192013-10-04 19:53:11 +0100255DEF_RULE(star_expr, c(star_expr), and(2), tok(OP_STAR), rule(expr))
Damien George25ae98f2018-06-19 14:20:42 +1000256DEF_RULE(expr, c(binary_op), list, rule(xor_expr), tok(OP_PIPE))
257DEF_RULE(xor_expr, c(binary_op), list, rule(and_expr), tok(OP_CARET))
258DEF_RULE(and_expr, c(binary_op), list, rule(shift_expr), tok(OP_AMPERSAND))
Krzysztof Blazewicza040fb82017-04-27 21:32:50 +0200259DEF_RULE(shift_expr, c(term), list, rule(arith_expr), rule(shift_op))
Damien George71019ae2017-02-15 10:58:05 +1100260DEF_RULE_NC(shift_op, or(2), tok(OP_DBL_LESS), tok(OP_DBL_MORE))
Krzysztof Blazewicza040fb82017-04-27 21:32:50 +0200261DEF_RULE(arith_expr, c(term), list, rule(term), rule(arith_op))
Damien George71019ae2017-02-15 10:58:05 +1100262DEF_RULE_NC(arith_op, or(2), tok(OP_PLUS), tok(OP_MINUS))
Damien429d7192013-10-04 19:53:11 +0100263DEF_RULE(term, c(term), list, rule(factor), rule(term_op))
Damien George2069c562019-07-25 13:15:54 +1000264DEF_RULE_NC(term_op, or(5), tok(OP_STAR), tok(OP_AT), tok(OP_SLASH), tok(OP_PERCENT), tok(OP_DBL_SLASH))
Damien George71019ae2017-02-15 10:58:05 +1100265DEF_RULE_NC(factor, or(2), rule(factor_2), rule(power))
Damien George0c1de1c2016-04-14 13:23:50 +0100266DEF_RULE(factor_2, c(factor_2), and_ident(2), rule(factor_op), rule(factor))
Damien George71019ae2017-02-15 10:58:05 +1100267DEF_RULE_NC(factor_op, or(3), tok(OP_PLUS), tok(OP_MINUS), tok(OP_TILDE))
Damien George0c1de1c2016-04-14 13:23:50 +0100268DEF_RULE(power, c(power), and_ident(2), rule(atom_expr), opt_rule(power_dbl_star))
pohmelie81ebba72016-01-27 23:23:11 +0300269#if MICROPY_PY_ASYNC_AWAIT
Damien George71019ae2017-02-15 10:58:05 +1100270DEF_RULE_NC(atom_expr, or(2), rule(atom_expr_await), rule(atom_expr_normal))
pohmelie81ebba72016-01-27 23:23:11 +0300271DEF_RULE(atom_expr_await, c(atom_expr_await), and(3), tok(KW_AWAIT), rule(atom), opt_rule(atom_expr_trailers))
272#else
Damien George71019ae2017-02-15 10:58:05 +1100273DEF_RULE_NC(atom_expr, or(1), rule(atom_expr_normal))
pohmelie81ebba72016-01-27 23:23:11 +0300274#endif
Damien George0c1de1c2016-04-14 13:23:50 +0100275DEF_RULE(atom_expr_normal, c(atom_expr_normal), and_ident(2), rule(atom), opt_rule(atom_expr_trailers))
Damien George53359422017-04-18 22:52:18 +1000276DEF_RULE_NC(atom_expr_trailers, one_or_more, rule(trailer))
Damien George71019ae2017-02-15 10:58:05 +1100277DEF_RULE_NC(power_dbl_star, and_ident(2), tok(OP_DBL_STAR), rule(factor))
Damien429d7192013-10-04 19:53:11 +0100278
279// atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' | '{' [dictorsetmaker] '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False'
280// testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
281// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
282
Damien George534b7c32017-02-17 12:12:40 +1100283DEF_RULE_NC(atom, or(12), tok(NAME), tok(INTEGER), tok(FLOAT_OR_IMAG), tok(STRING), tok(BYTES), 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 +0100284DEF_RULE(atom_paren, c(atom_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(atom_2b), tok(DEL_PAREN_CLOSE))
Damien George71019ae2017-02-15 10:58:05 +1100285DEF_RULE_NC(atom_2b, or(2), rule(yield_expr), rule(testlist_comp))
Damien429d7192013-10-04 19:53:11 +0100286DEF_RULE(atom_bracket, c(atom_bracket), and(3), tok(DEL_BRACKET_OPEN), opt_rule(testlist_comp), tok(DEL_BRACKET_CLOSE))
287DEF_RULE(atom_brace, c(atom_brace), and(3), tok(DEL_BRACE_OPEN), opt_rule(dictorsetmaker), tok(DEL_BRACE_CLOSE))
Damien George71019ae2017-02-15 10:58:05 +1100288DEF_RULE_NC(testlist_comp, and_ident(2), rule(testlist_comp_2), opt_rule(testlist_comp_3))
Damien George17839502020-06-16 21:42:44 +1000289DEF_RULE_NC(testlist_comp_2, or(2), rule(star_expr), rule(namedexpr_test))
Damien George71019ae2017-02-15 10:58:05 +1100290DEF_RULE_NC(testlist_comp_3, or(2), rule(comp_for), rule(testlist_comp_3b))
291DEF_RULE_NC(testlist_comp_3b, and_ident(2), tok(DEL_COMMA), opt_rule(testlist_comp_3c))
292DEF_RULE_NC(testlist_comp_3c, list_with_end, rule(testlist_comp_2), tok(DEL_COMMA))
293DEF_RULE_NC(trailer, or(3), rule(trailer_paren), rule(trailer_bracket), rule(trailer_period))
Damien429d7192013-10-04 19:53:11 +0100294DEF_RULE(trailer_paren, c(trailer_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(arglist), tok(DEL_PAREN_CLOSE))
295DEF_RULE(trailer_bracket, c(trailer_bracket), and(3), tok(DEL_BRACKET_OPEN), rule(subscriptlist), tok(DEL_BRACKET_CLOSE))
296DEF_RULE(trailer_period, c(trailer_period), and(2), tok(DEL_PERIOD), tok(NAME))
297
298// subscriptlist: subscript (',' subscript)* [',']
299// subscript: test | [test] ':' [test] [sliceop]
300// sliceop: ':' [test]
301
Damien George83204f32014-12-27 17:20:41 +0000302#if MICROPY_PY_BUILTINS_SLICE
Damien429d7192013-10-04 19:53:11 +0100303DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(subscript), tok(DEL_COMMA))
Damien George71019ae2017-02-15 10:58:05 +1100304DEF_RULE_NC(subscript, or(2), rule(subscript_3), rule(subscript_2))
Damien Georged23bec32018-06-19 13:57:55 +1000305DEF_RULE(subscript_2, c(subscript), and_ident(2), rule(test), opt_rule(subscript_3))
306DEF_RULE(subscript_3, c(subscript), and(2), tok(DEL_COLON), opt_rule(subscript_3b))
Damien George71019ae2017-02-15 10:58:05 +1100307DEF_RULE_NC(subscript_3b, or(2), rule(subscript_3c), rule(subscript_3d))
308DEF_RULE_NC(subscript_3c, and(2), tok(DEL_COLON), opt_rule(test))
309DEF_RULE_NC(subscript_3d, and_ident(2), rule(test), opt_rule(sliceop))
310DEF_RULE_NC(sliceop, and(2), tok(DEL_COLON), opt_rule(test))
Damien George83204f32014-12-27 17:20:41 +0000311#else
312DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
313#endif
Damien429d7192013-10-04 19:53:11 +0100314
315// exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
316// testlist: test (',' test)* [',']
317// dictorsetmaker: (test ':' test (comp_for | (',' test ':' test)* [','])) | (test (comp_for | (',' test)* [',']))
318
Damien George71019ae2017-02-15 10:58:05 +1100319DEF_RULE_NC(exprlist, list_with_end, rule(exprlist_2), tok(DEL_COMMA))
320DEF_RULE_NC(exprlist_2, or(2), rule(star_expr), rule(expr))
Damien429d7192013-10-04 19:53:11 +0100321DEF_RULE(testlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
322// TODO dictorsetmaker lets through more than is allowed
Damien George71019ae2017-02-15 10:58:05 +1100323DEF_RULE_NC(dictorsetmaker, and_ident(2), rule(dictorsetmaker_item), opt_rule(dictorsetmaker_tail))
Damien Georgee37dcaa2014-12-27 17:07:16 +0000324#if MICROPY_PY_BUILTINS_SET
Damien George131b0de2020-06-16 22:48:46 +1000325DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and_ident(2), rule(test), opt_rule(generic_colon_test))
Damien Georgee37dcaa2014-12-27 17:07:16 +0000326#else
327DEF_RULE(dictorsetmaker_item, c(dictorsetmaker_item), and(3), rule(test), tok(DEL_COLON), rule(test))
328#endif
Damien George71019ae2017-02-15 10:58:05 +1100329DEF_RULE_NC(dictorsetmaker_tail, or(2), rule(comp_for), rule(dictorsetmaker_list))
330DEF_RULE_NC(dictorsetmaker_list, and(2), tok(DEL_COMMA), opt_rule(dictorsetmaker_list2))
331DEF_RULE_NC(dictorsetmaker_list2, list_with_end, rule(dictorsetmaker_item), tok(DEL_COMMA))
Damien429d7192013-10-04 19:53:11 +0100332
333// classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
334
Damien George0c1de1c2016-04-14 13:23:50 +0100335DEF_RULE(classdef, c(classdef), and_blank(5), tok(KW_CLASS), tok(NAME), opt_rule(classdef_2), tok(DEL_COLON), rule(suite))
Damien George71019ae2017-02-15 10:58:05 +1100336DEF_RULE_NC(classdef_2, and_ident(3), tok(DEL_PAREN_OPEN), opt_rule(arglist), tok(DEL_PAREN_CLOSE))
Damien429d7192013-10-04 19:53:11 +0100337
338// arglist: (argument ',')* (argument [','] | '*' test (',' argument)* [',' '**' test] | '**' test)
339
340// TODO arglist lets through more than is allowed, compiler needs to do further verification
Damien George71019ae2017-02-15 10:58:05 +1100341DEF_RULE_NC(arglist, list_with_end, rule(arglist_2), tok(DEL_COMMA))
342DEF_RULE_NC(arglist_2, or(3), rule(arglist_star), rule(arglist_dbl_star), rule(argument))
343DEF_RULE_NC(arglist_star, and(2), tok(OP_STAR), rule(test))
344DEF_RULE_NC(arglist_dbl_star, and(2), tok(OP_DBL_STAR), rule(test))
Damien429d7192013-10-04 19:53:11 +0100345
346// # The reason that keywords are test nodes instead of NAME is that using NAME
347// # results in an ambiguity. ast.c makes sure it's a NAME.
348// argument: test [comp_for] | test '=' test # Really [keyword '='] test
349// comp_iter: comp_for | comp_if
350// comp_for: 'for' exprlist 'in' or_test [comp_iter]
351// comp_if: 'if' test_nocond [comp_iter]
352
Damien George71019ae2017-02-15 10:58:05 +1100353DEF_RULE_NC(argument, and_ident(2), rule(test), opt_rule(argument_2))
Damien George17839502020-06-16 21:42:44 +1000354#if MICROPY_PY_ASSIGN_EXPR
Damien George131b0de2020-06-16 22:48:46 +1000355DEF_RULE_NC(argument_2, or(3), rule(comp_for), rule(generic_equal_test), rule(argument_3))
356DEF_RULE_NC(argument_3, and(2), tok(OP_ASSIGN), rule(test))
Damien George17839502020-06-16 21:42:44 +1000357#else
Damien George131b0de2020-06-16 22:48:46 +1000358DEF_RULE_NC(argument_2, or(2), rule(comp_for), rule(generic_equal_test))
Damien George17839502020-06-16 21:42:44 +1000359#endif
Damien George71019ae2017-02-15 10:58:05 +1100360DEF_RULE_NC(comp_iter, or(2), rule(comp_for), rule(comp_if))
361DEF_RULE_NC(comp_for, and_blank(5), tok(KW_FOR), rule(exprlist), tok(KW_IN), rule(or_test), opt_rule(comp_iter))
362DEF_RULE_NC(comp_if, and(3), tok(KW_IF), rule(test_nocond), opt_rule(comp_iter))
Damien429d7192013-10-04 19:53:11 +0100363
364// # not used in grammar, but may appear in "node" passed from Parser to Compiler
365// encoding_decl: NAME
366
367// yield_expr: 'yield' [yield_arg]
368// yield_arg: 'from' test | testlist
369
370DEF_RULE(yield_expr, c(yield_expr), and(2), tok(KW_YIELD), opt_rule(yield_arg))
Damien George71019ae2017-02-15 10:58:05 +1100371DEF_RULE_NC(yield_arg, or(2), rule(yield_arg_from), rule(testlist))
372DEF_RULE_NC(yield_arg_from, and(2), tok(KW_FROM), rule(test))