| 1 |
# Grammar for Baron |
| 2 |
# this file is a REFERENCE on what grammar baron is supposed to be implementing |
| 3 |
# to be a mixed between python2 and python3 grammar |
| 4 |
|
| 5 |
# IT IS NOT USED BY BARON DIRECTLY |
| 6 |
|
| 7 |
# Start symbols for the grammar: |
| 8 |
# single_input is a single interactive statement; |
| 9 |
# file_input is a module or sequence of commands read from an input file; |
| 10 |
# eval_input is the input for the eval() functions. |
| 11 |
# NB: compound_stmt in single_input is followed by extra NEWLINE! |
| 12 |
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 13 |
file_input: (NEWLINE | stmt)* ENDMARKER |
| 14 |
eval_input: testlist NEWLINE* ENDMARKER |
| 15 |
|
| 16 |
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 17 |
decorators: decorator+ |
| 18 |
decorated: decorators (classdef | funcdef | async_funcdef) |
| 19 |
|
| 20 |
async_funcdef: 'async' funcdef |
| 21 |
funcdef: 'def' NAME parameters ['->' test] ':' suite |
| 22 |
|
| 23 |
parameters: '(' [typedargslist] ')' |
| 24 |
typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 25 |
'*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 26 |
| '**' tfpdef [',']]] |
| 27 |
| '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 28 |
| '**' tfpdef [',']) |
| 29 |
tfpdef: NAME [':' test] |
| 30 |
varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 31 |
'*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 32 |
| '**' vfpdef [',']]] |
| 33 |
| '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 34 |
| '**' vfpdef [','] |
| 35 |
) |
| 36 |
vfpdef: NAME |
| 37 |
|
| 38 |
stmt: simple_stmt | compound_stmt |
| 39 |
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 40 |
small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | |
| 41 |
import_stmt | global_stmt | nonlocal_stmt | exec_stmt | assert_stmt) |
| 42 |
expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 43 |
('=' (yield_expr|testlist_star_expr))*) |
| 44 |
annassign: ':' test ['=' test] |
| 45 |
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 46 |
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 47 |
'<<=' | '>>=' | '**=' | '//=') |
| 48 |
# For normal assignments, additional restrictions enforced by the interpreter |
| 49 |
print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 50 |
'>>' test [ (',' test)+ [','] ] ) |
| 51 |
del_stmt: 'del' exprlist |
| 52 |
pass_stmt: 'pass' |
| 53 |
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
| 54 |
break_stmt: 'break' |
| 55 |
continue_stmt: 'continue' |
| 56 |
return_stmt: 'return' [testlist] |
| 57 |
yield_stmt: yield_expr |
| 58 |
raise_stmt: 'raise' [test [(',' test [',' test] | 'from' test)] |
| 59 |
import_stmt: import_name | import_from |
| 60 |
import_name: 'import' dotted_as_names |
| 61 |
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 62 |
import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 63 |
'import' ('*' | '(' import_as_names ')' | import_as_names)) |
| 64 |
import_as_name: NAME ['as' NAME] |
| 65 |
dotted_as_name: dotted_name ['as' NAME] |
| 66 |
import_as_names: import_as_name (',' import_as_name)* [','] |
| 67 |
dotted_as_names: dotted_as_name (',' dotted_as_name)* |
| 68 |
dotted_name: NAME ('.' NAME)* |
| 69 |
global_stmt: 'global' NAME (',' NAME)* |
| 70 |
nonlocal_stmt: 'nonlocal' NAME (',' NAME)* |
| 71 |
exec_stmt: 'exec' expr ['in' test [',' test]] |
| 72 |
assert_stmt: 'assert' test [',' test] |
| 73 |
|
| 74 |
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt |
| 75 |
async_stmt: 'async' (funcdef | with_stmt | for_stmt) |
| 76 |
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 77 |
while_stmt: 'while' test ':' suite ['else' ':' suite] |
| 78 |
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
| 79 |
try_stmt: ('try' ':' suite |
| 80 |
((except_clause ':' suite)+ |
| 81 |
['else' ':' suite] |
| 82 |
['finally' ':' suite] | |
| 83 |
'finally' ':' suite)) |
| 84 |
with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 85 |
with_item: test ['as' expr] |
| 86 |
# NB compile.c makes sure that the default except clause is last |
| 87 |
except_clause: 'except' [test [('as' | ',') test]] |
| 88 |
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
| 89 |
|
| 90 |
# Backward compatibility cruft to support: |
| 91 |
# [ x for x in lambda: True, lambda: False if x() ] |
| 92 |
# even while also allowing: |
| 93 |
# lambda x: 5 if x else 2 |
| 94 |
# (But not a mix of the two) |
| 95 |
testlist_safe: old_test [(',' old_test)+ [',']] |
| 96 |
old_test: or_test | old_lambdef |
| 97 |
old_lambdef: 'lambda' [varargslist] ':' old_test |
| 98 |
|
| 99 |
test: or_test ['if' or_test 'else' test] | lambdef |
| 100 |
lambdef: 'lambda' [varargslist] ':' test |
| 101 |
or_test: and_test ('or' and_test)* |
| 102 |
and_test: not_test ('and' not_test)* |
| 103 |
not_test: 'not' not_test | comparison |
| 104 |
comparison: expr (comp_op expr)* |
| 105 |
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 106 |
expr: xor_expr ('|' xor_expr)* |
| 107 |
xor_expr: and_expr ('^' and_expr)* |
| 108 |
and_expr: shift_expr ('&' shift_expr)* |
| 109 |
shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 110 |
arith_expr: term (('+'|'-') term)* |
| 111 |
term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
| 112 |
factor: ('+'|'-'|'~') factor | power |
| 113 |
power: atom_expr ['**' factor] |
| 114 |
atom_expr: ['await'] atom trailer* |
| 115 |
atom: ('(' [yield_expr|testlist_comp] ')' | |
| 116 |
'[' [listmaker] ']' | |
| 117 |
'{' [dictorsetmaker] '}' | |
| 118 |
'`' testlist1 '`' | |
| 119 |
NAME | NUMBER | STRING+ | '...') |
| 120 |
testlist_comp: test ( comp_for | (',' test)* [','] ) |
| 121 |
listmaker: test ( list_for | (',' test)* [','] ) |
| 122 |
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 123 |
subscriptlist: subscript (',' subscript)* [','] |
| 124 |
subscript: test | [test] ':' [test] [sliceop] |
| 125 |
sliceop: ':' [test] |
| 126 |
exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] |
| 127 |
testlist: test (',' test)* [','] |
| 128 |
dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) | |
| 129 |
(test (comp_for | (',' test)* [','])) ) |
| 130 |
|
| 131 |
classdef: 'class' NAME ['(' [(testlist | arglist)] ')'] ':' suite |
| 132 |
|
| 133 |
arglist: (argument ',')* (argument [','] |
| 134 |
|'*' test (',' argument)* [',' '**' test] |
| 135 |
|'**' test) |
| 136 |
# The reason that keywords are test nodes instead of NAME is that using NAME |
| 137 |
# results in an ambiguity. ast.c makes sure it's a NAME. |
| 138 |
argument: test [comp_for] | test '=' test |
| 139 |
|
| 140 |
list_iter: list_for | list_if |
| 141 |
list_for: 'for' exprlist 'in' testlist_safe [list_iter] |
| 142 |
list_if: 'if' old_test [list_iter] |
| 143 |
|
| 144 |
comp_iter: comp_for | comp_if |
| 145 |
sync_comp_for: 'for' exprlist 'in' or_test [comp_iter] |
| 146 |
comp_for: ['async'] sync_comp_for |
| 147 |
comp_if: 'if' old_test [comp_iter] |
| 148 |
|
| 149 |
testlist1: test (',' test)* |
| 150 |
|
| 151 |
# not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 152 |
encoding_decl: NAME |
| 153 |
|
| 154 |
yield_expr: 'yield' [yield_arg] |
| 155 |
yield_arg: 'from' test | testlist |