forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimPy.grammar
More file actions
164 lines (132 loc) · 7.14 KB
/
Copy pathSimPy.grammar
File metadata and controls
164 lines (132 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Simplified grammar for Python
start: [statements] ENDMARKER
statements: statement+
statement: compound_stmt | simple_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
# NOTE: assignment MUST precede expression, else the parser will get stuck;
# but it must follow all others, else reserved words will match a simple NAME.
small_stmt: ( return_stmt | import_stmt | 'pass' | raise_stmt | yield_stmt | assert_stmt | del_stmt | global_stmt | nonlocal_stmt
| assignment | expressions
)
compound_stmt: if_stmt | while_stmt | for_stmt | with_stmt | try_stmt | function_def | class_def
# NOTE: yield_expression may start with 'yield'; yield_expr must start with 'yield'
assignment: target ':' expression ['=' yield_expression] | (star_targets '=')+ (yield_expr | expressions) | target augassign (yield_expr | expressions)
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//=')
global_stmt: 'global' NAME (',' NAME)*
nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
yield_stmt: yield_expr
assert_stmt: 'assert' expression [',' expression]
del_stmt: 'del' targets # TODO: exclude *target
import_stmt: import_name | import_from
import_name: 'import' dotted_as_names
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
import_from: ('from' (('.' | '...')* !'import' dotted_name | ('.' | '...')+)
'import' ('*' | '(' import_as_names ')' | import_as_names))
import_as_name: NAME ['as' NAME]
dotted_as_name: dotted_name ['as' NAME]
import_as_names: import_as_name (',' import_as_name)* [',']
dotted_as_names: dotted_as_name (',' dotted_as_name)*
dotted_name: NAME ('.' NAME)*
if_stmt: 'if' full_expression ':' block elif_block* [else_block]
elif_block: 'elif' full_expression ':' block
else_block: 'else' ':' block
while_stmt: 'while' full_expression ':' block [else_block]
for_stmt: [ASYNC] 'for' star_targets 'in' expressions ':' block [else_block]
with_stmt: [ASYNC] 'with' expression ['as' target] (',' [expression ['as' target]])* ':' block
try_stmt: try_block finally_block | try_block except_block+ [else_block] [finally_block]
try_block: 'try' ':' block
except_block: 'except' [expression ['as' target]] ':' block
finally_block: 'finally' ':' block
return_stmt: 'return' [expressions]
raise_stmt: 'raise' [expression ['from' expression]]
function_def: [decorators] [ASYNC] 'def' NAME '(' [parameters] ')' ['->' annotation] ':' block
parameters: ( slash_without_default [',' plain_names] [',' names_with_default] [',' [star_etc]]
| slash_with_default [',' names_with_default] [',' [star_etc]]
| plain_names [',' names_with_default] [',' [star_etc]]
| names_with_default [',' [star_etc]]
| star_etc
)
slash_without_default: plain_names ',' '/'
slash_with_default: [plain_names ','] names_with_default ',' '/'
names_with_default: name_with_default (',' name_with_default)*
plain_names: plain_name !'=' (',' plain_name !'=')*
star_etc: ( '*' NAME [':' annotation] (',' plain_name ['=' expression])* [',' kwds] [',']
| '*' (',' plain_name ['=' expression])+ [',' kwds] [',']
| kwds [',']
)
name_with_default: plain_name '=' expression
plain_name: NAME [':' annotation]
kwds: '**' NAME [':' annotation]
annotation: expression
decorators: ('@' factor NEWLINE)+
class_def: [decorators] 'class' NAME ['(' [arguments] ')'] ':' block
block: simple_stmt | NEWLINE INDENT statements DEDENT
star_full_expressions: (star_full_expression) (',' (star_full_expression))* [',']
expressions: ('*' bitwise_or | expression) (',' ('*' bitwise_or | expression))* [',']
star_full_expression: '*' bitwise_or | full_expression
full_expression: NAME ':=' expression | expression
yield_expression: yield_expr | expression
expression: lambdef | disjunction ['if' disjunction 'else' expression]
lambdef: 'lambda' [lambda_parameters] ':' expression
lambda_parameters: ( lambda_slash_without_default [',' lambda_plain_names] [',' lambda_names_with_default] [',' [lambda_star_etc]]
| lambda_slash_with_default [',' lambda_names_with_default] [',' [lambda_star_etc]]
| lambda_plain_names [',' lambda_names_with_default] [',' [lambda_star_etc]]
| lambda_names_with_default [',' [lambda_star_etc]]
| lambda_star_etc
)
lambda_slash_without_default: lambda_plain_names ',' '/'
lambda_slash_with_default: [lambda_plain_names ','] lambda_names_with_default ',' '/'
lambda_names_with_default: lambda_name_with_default (',' lambda_name_with_default)*
lambda_plain_names: NAME !'=' (',' NAME !'=')*
lambda_star_etc: ( '*' NAME (',' NAME ['=' expression])* [',' '**' NAME] [',']
| '*' (',' NAME ['=' expression])+ [',' '**' NAME] [',']
| '**' NAME [',']
)
lambda_name_with_default: NAME '=' expression
disjunction: conjunction ('or' conjunction)*
conjunction: comparison ('and' comparison)*
comparison: 'not'* bitwise_or (compare_op bitwise_or)*
compare_op: '<' | '<=' | '==' | '>=' | '>' | '!=' | ['not'] 'in' | 'is' ['not']
bitwise_or: bitwise_xor ('|' bitwise_xor)*
bitwise_xor: bitwise_and ('^' bitwise_and)*
bitwise_and: shift_expr ('&' shift_expr)*
shift_expr: sum (('<<'|'>>') sum)*
sum: term (('+' term | '-' term))*
term: factor (('*' factor | '/' factor | '//' factor | '%' factor | '@' factor))*
factor: ('+' | '-' | '~') factor | power
power: primary '**' factor | primary
primary: [AWAIT] atom ('.' NAME | '[' slices ']' | '(' [arguments] ')')*
slices: slice (',' slice)* [',']
slice: [expression] ':' [expression] [':' [expression]] | expression
atom: list | listcomp | tuple | group | genexp | set | setcomp | dict | dictcomp | NAME | STRING+ | NUMBER | '...'
list: '[' [star_full_expressions] ']'
listcomp: '[' full_expression for_if_clauses ']'
tuple: '(' [star_full_expression ',' [star_full_expressions]] ')'
group: '(' (yield_expr | full_expression) ')'
genexp: '(' expression for_if_clauses ')'
set: '{' expressions '}'
setcomp: '{' expression for_if_clauses '}'
dict: '{' [kvpairs] '}'
dictcomp: '{' kvpair for_if_clauses '}'
kvpairs: kvpair (',' kvpair)* [',']
kvpair: '**' bitwise_or | expression ':' expression
for_if_clauses: ([ASYNC] 'for' star_targets 'in' expression ('if' expression)*)+
yield_expr: 'yield' 'from' expression | 'yield' [expressions]
arguments: expression for_if_clauses | args [',']
args: '*' expression [',' args] | kwargs | posarg [',' args] # Weird to make it work
kwargs: kwarg (',' kwarg)*
posarg: full_expression | '*' expression
kwarg: NAME '=' expression | '*' expression | '**' expression
# NOTE: star_targets may contain *NAME, targets may not.
# NOTE: the !'in' is to handle "for x, in ...".
# TODO: things like {k: v}[k] = v should also be acceptable [star] targets.
star_targets: star_target (',' !'in' star_target)* [',']
star_target: '*' bitwise_or | atom t_tail+ | star_atom t_tail*
star_atom: NAME | '(' [star_targets] ')' | '[' [star_targets] ']'
targets: target (',' target)* [',']
target: atom t_tail+ | t_atom t_tail*
t_atom: NAME | '(' [targets] ')' | '[' [targets] ']'
t_tail: call_tail* (attr_tail | index_tail)
call_tail: '(' [arguments] ')'
attr_tail: '.' NAME
index_tail: '[' slices ']'