forked from runtimeverification/python-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-syntax.k
More file actions
217 lines (173 loc) · 7.91 KB
/
Copy pathpython-syntax.k
File metadata and controls
217 lines (173 loc) · 7.91 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require "complex-numbers.k"
require "bytes.k"
module PYTHON-TOKENS
imports COMPLEX-NUMBERS
imports BYTES
syntax NAME ::= Id
syntax NUMBER ::= Int | Float | Imaginary
syntax STRING ::= String | Bytes
syntax NAMES ::= NeList{NAME,","} [seqstrict, hybrid]
endmodule
module PYTHON-SYNTAX imports PYTHON-TOKENS
syntax CompFor ::= "for" Target "in" Exp
syntax CompIf ::= "if" Exp
syntax Comp ::= CompFor
| CompIf
syntax Comps ::= List{Comp,""}
syntax YieldExp ::= "yield" Exp [strict]
| "yield"
| "yield" "from" Exp [strict]
syntax KeyData ::= List{KeyDatum,","} [seqstrict]
syntax KeyDatum ::= Exp ":" Exp [seqstrict, hybrid]
syntax Arguments ::= List{Argument,","}
syntax Argument ::= Exp
| NAME "=" Exp [strict(2), klabel('keyword)]
| "*" Exp [strict, hybrid]
| "**" Exp [strict, hybrid]
//TODO: 2-part slices
syntax Slice ::= Exp
| ":" ":"
| Exp ":" ":"
| ":" Exp ":"
| ":" ":" Exp
| Exp ":" Exp ":"
| Exp ":" ":" Exp
| ":" Exp ":" Exp
| Exp ":" Exp ":" Exp
syntax TargetAndExp ::= NAME
| "tuple" "(" TargetAndExps ")" [strict, klabel('Tuple), atom]
| "[" TargetAndExps "]" [strict, atom]
| Exp "." NAME [strict(1), left, primary]
| Exp "[" Slice "]" [seqstrict, left, primary]
syntax Target ::= TargetAndExp
| "*" Target [klabel('Starred)]
| "tuple" "(" Targets ")" [klabel('Tuple), primary]
| "[" Targets "]" [primary]
syntax TargetAndExps ::= NeList{TargetAndExp,","}
syntax Targets ::= List{Target,","}
syntax NeTargets ::= NeList{Target,","}
syntax Targets ::= NeTargets
syntax Exp ::= TargetAndExp
syntax priorities atom > primary > power
syntax Exp ::= "(" Exp ")" [bracket, atom]
| NUMBER [atom]
| STRING [atom]
| "tuple" "(" Exps ")" [strict, klabel('Tuple), atom] //TODO: use List{Exp,","}
| "[" Exp Comps "]" [atom]
| "generator" "(" Exp Comps ")" [klabel('GeneratorExp), atom] //TODO: Use (__)
| "{" Exp Comps "}" [atom]
| "{" Exp ":" Exp Comps "}" [atom]
| "[" Exps "]" [strict, atom]
| "{" KeyData "}" [strict, atom]
| "{" NeExps "}" [strict, klabel('Set), atom]
| YieldExp [atom]
| "..." [atom]
| Exp "(" Arguments ")" [seqstrict, left, primary]
> Exp "**" Exp [seqstrict, right, power]
> "-" Exp [strict]
| "+" Exp [strict]
| "~" Exp [strict]
> Exp "*" Exp [seqstrict, left]
| Exp "floor/" Exp [seqstrict, left, klabel('_FloorDiv_)] //TODO: use //
| Exp "/" Exp [seqstrict, left]
| Exp "%" Exp [seqstrict, left]
> Exp "+" Exp [seqstrict, left]
| Exp "-" Exp [seqstrict, left]
> Exp "<<" Exp [seqstrict, left]
| Exp ">>" Exp [seqstrict, left]
> Exp "&" Exp [seqstrict, left]
> Exp "^" Exp [seqstrict, left]
> Exp "|" Exp [seqstrict, left]
> Exp "<" Exp [right]
| Exp ">" Exp [right]
| Exp "==" Exp [right]
| Exp ">=" Exp [right]
| Exp "<=" Exp [right]
| Exp "!=" Exp [right]
| Exp "is" Exp [right]
| Exp "is" "not" Exp [right]
| Exp "in" Exp [right]
| Exp "not" "in" Exp [right]
> "not" Exp [strict]
> Exp "and" Exp [strict(1), left]
> Exp "or" Exp [strict(1), left]
> Exp "if" Exp "else" Exp [strict(2), right]
> "lambda" Parameters ":" Exp
syntax "*" -/- [\*]
syntax Exps ::= List{Exp,","}
syntax Exps ::= NeExps
syntax NeExps ::= NeList{Exp,","}
syntax AssignTargets ::= NeList{Target,"="} [klabel('targets)]
syntax Aliases ::= NeList{Alias,","}
//TODO: separate with from import
syntax Alias ::= Exp
| Exp "as" Exp [strict(1)]
syntax RelativeModule ::= "." [onlyLabel]
| Exp
| "." RelativeModule
syntax ExceptClauses ::= NeList{ExceptClause,""}
syntax Suite ::= StmtList NEWLINE | NEWLINE INDENT Stmts DEDENT
syntax Stmt ::= StmtList NEWLINE | CompoundStmt
syntax Stmts ::= NeList{Stmt,""}
//TODO: add lexer rules
syntax NEWLINE
syntax INDENT
syntax DEDENT
syntax SimpleStmt ::= Exp [strict, klabel('Expr), onlyLabel] //TODO: remove onlyLabel
| AssignTargets "=" Exp [strict(2)]
| Target "+=" Exp [strict(2)]
| Target "-=" Exp [strict(2)]
| Target "*=" Exp [strict(2)]
| Target "/=" Exp [strict(2)]
| Target "floor/=" Exp [strict(2), klabel('_FloorDiv=_)] //TODO: use //=
| Target "%=" Exp [strict(2)]
| Target "**=" Exp [strict(2)]
| Target ">>=" Exp [strict(2)]
| Target "<<=" Exp [strict(2)]
| Target "&=" Exp [strict(2)]
| Target "^=" Exp [strict(2)]
| Target "|=" Exp [strict(2)]
| "assert" Exp
| "assert" Exp "," Exp
| "pass"
| "del" NeTargets
| "return" Exp [strict]
| "return"
| "raise"
| "raise" Exp [strict]
| "raise" Exp "from" Exp [seqstrict]
| "break"
| "continue"
| "import" Aliases
| "from" RelativeModule "import" Aliases
| "from" Exp "import" "*"
| "global" NAMES
| "nonlocal" NAMES
//TODO: elif
syntax CompoundStmt ::= "if" Exp ":" Suite
| "if" Exp ":" Suite "else" ":" Suite [strict(1)]
| "while" Exp ":" Suite
| "while" Exp ":" Suite "else" ":" Suite
| "for" Target "in" Exp ":" Suite
| "for" Target "in" Exp ":" Suite "else" ":" Suite
| "try" ":" Suite ExceptClauses
| "try" ":" Suite ExceptClauses "else" ":" Suite
| "try" ":" Suite ExceptClauses "else" ":" Suite "finally" ":" Suite
| "try" ":" Suite "finally" ":" Suite
| "with" Aliases ":" Suite [strict(1)]
| Decorated
syntax StmtList ::= List{SimpleStmt,";"}
syntax ExceptClause ::= "except" ":" Suite
| "except" Exp ":" Suite [strict(1)]
| "except" Exp "as" NAME ":" Suite [strict(1)]
syntax Decorated ::= ClassDef
| FuncDef
| "@" Exp NEWLINE Decorated [strict(1)]
syntax Parameters ::= List{Parameter,","} [seqstrict]
syntax Parameter ::= Argument
| Argument ":" Exp [klabel('annotation)]
| "*"
syntax FuncDef ::= "def" NAME "(" Parameters ")" "->" Exp ":" Suite
| "def" NAME "(" Parameters ")" ":" Suite
syntax ClassDef ::= "class" NAME "(" Arguments ")" ":" Suite [strict(2)]
endmodule