-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsyntax.lark
262 lines (236 loc) · 8.23 KB
/
syntax.lark
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
start: [main_instruction* last_instruction]
main_instruction: instruction (";" | "\n")
| if (";" | "\n")?
| ifelse (";" | "\n")?
| for (";" | "\n")?
| while (";" | "\n")?
last_instruction: instruction (";" | "\n")?
| if (";" | "\n")?
| ifelse (";" | "\n")?
| for (";" | "\n")?
| while (";" | "\n")?
instruction: declare
| function_callback
| function_callback_pipe
| await_expression
| imp
| return
| continue
| break
| enum_definition
| alias_definition
| class_definition
| assert
| assign_value
// functions
declare: "let" modifier name_type "=" expression
enum_definition: "type" modifier declaration_with_typevars "=" enum_constructors
alias_definition: "alias" modifier declaration_with_typevars "=" types
function_callback: function_call
function_callback_pipe: literal "|>" pipe_function_call
| function_callback_pipe "|>" pipe_function_call
for: "for" "(" name_type "in" value ")" code_block
while: "while" "(" expression ")" code_block
imp: "import" NAME
impn: "imp" (STRING | NAME)
return: "return" expression
continue: "continue"
break: "break"
if: "if" condition ("{" instruction "}" | code_block)
ifelse: "if" condition ("{" instruction "}" | code_block) "else" ("{" instruction "}" | code_block | ifelse | if)
ifelse_expr: "if" condition "{" expression "}" "else" ("{" expression "}" | ifelse_expr)
?condition: expression
| conditional_let
conditional_let: "let" pattern "=" expression
class_definition: "class" modifier NAME arguments code_block
match: "match" "(" value ")" (match_enum_block | match_block)
assert: "assert" assert_value
assign_value: NAME assign_operator expression
//helpers
name_type: ( pattern [":" types] | "(" pattern [":" types] ")")
function_dec_call: NAME (" " [name_type (" " name_type)*])?
function_call: literal "(" [function_call_arg ("," function_call_arg)*] (",")? ")"
pipe_function_call: literal ["(" [function_call_arg ("," function_call_arg)*] (",")? ")"]
code_block: "{" [main_instruction* last_instruction] "}"
function_def: arguments ("->" types)? code_block
arguments: generic_declaration? (name_type ("->" name_type)* | "(" [name_type ("," name_type)*] ")")
generic_declaration: "[" (NAME ",")* NAME (",")? "]"
char: "\\" ( escape_code | "{" /./ "}" | "u{" hex_pattern "}")
hex_pattern: /[0-9a-fA-F]+/
escape_code: /[nrtv0fb]/
tupledef: "(" types ("," types)+ (",")? ")"
recorddef: "{" (record_entry_lit ",")* record_entry_lit (",")? "}"
spread: ".." expression
record_entry_lit: CNAME ":" types
with_typevars: module_type "[" types ("," types)* (",")? "]"
declaration_with_typevars: NAME ("[" NAME ("," NAME)* (",")? "]")?
tupleval: "(" expression ("," expression)+ (",")? ")"
listval: "[" (list_expression ("," list_expression)* (",")?)? "]"
recordval: "{" (record_entry ",")* record_entry (",")? "}"
enum_constructors: ("|")? enum_constructor ("|" enum_constructor)*
match_block: "{" (match_entry (";" | "\n"))* "}"
match_entry: value "->" value
match_enum_block: "{" (match_enum_entry (";" | "\n"))* "}"
match_enum_entry: pattern "->" value
enum_constructor: modifier NAME "(" [types ("," types)*] ")"
| modifier NAME
assert_value: assert_val
| assert_type
assert_val: "value" expression
assert_type: "type" expression ":" types
modifier: PUBLIC? MUTABLE?
?function_call_arg: spread
| expression
?record_entry: CNAME ":" expression
| NAME
| spread
?pattern: pattern_value
| tuple_pattern
?pattern_value: NAME
| "_"
| "(" pattern ")"
| record_pattern
| list_pattern
| enum_pattern
| UNIT
tuple_pattern: "(" (name_type ",")+ name_type ")"
record_pattern: "{" (record_entry_pattern ",")* record_entry_pattern (",")? "}"
?record_entry_pattern: NAME
| CNAME ":" pattern
list_pattern: "[" (pattern_value ("," pattern_value)*)? "]"
enum_pattern: literal "(" [pattern_value ("," pattern_value)*] ")"
// Deal with a syntax ambiguity where [a:type[b]] could be seen as a list
// destructuring in the second argument.
?definite_pattern: NAME
| "_"
| "(" pattern ")"
| record_pattern
| tuple_pattern
// Boolean and number expressions, with order of operations.
// Question mark "inlines" the branch, so we don't get nested
// boolean_expression(or_expression(and_expression etc)).
// Square brackets mean that the stuff inside it can appear 0 or 1 time. Same as
// (whatever)?
?types: ( "(" not_func_type ")" | not_func_type )
| ( "(" func_type ")" | func_type )
?not_func_type: module_type
| tupledef
| recorddef
| with_typevars
| UNIT
module_type: [NAME "."]* NAME
func_type: generic_declaration? (func_inner_type |"(" [func_inner_type ("," func_inner_type)*] ")") "->" not_func_type
?func_inner_type: not_func_type
| func_type
?list_expression: expression
| spread
?expression: ifelse_expr
| boolean_expression
| function_callback_pipe
| match
?boolean_expression: or_expression
?or_expression: [or_expression OR] and_expression
?and_expression: [and_expression AND] xor_expression
?xor_expression: [xor_expression XOR] not_expression
?not_expression: compare_expression
| in_expression
?in_expression: number_expression IN number_expression
?compare_expression: [compare_expression compare_operator] number_expression
?compare_operator: EQUALS
| GORE
| LORE
| LESS
| GREATER
| NEQUALS
?number_expression: sum_expression
?sum_expression: [sum_expression (ADD | SUBTRACT)] product_expression
?product_expression: [product_expression (MULTIPLY | DIVIDE | MODULO | SHIFTL | SHIFTR)] exponent_expression
// Exponentiation right to left associative
?exponent_expression: unary_expression [EXPONENT exponent_expression]
?unary_expression: await_expression
| NOT unary_expression
| SUBTRACT unary_expression
?await_expression: record_access
| await_expression AWAIT
?record_access: value_access
| await_expression "." CNAME
?value_access: value
| await_expression VALUEACCESS expression "]"
value: NUMBER
| BOOLEAN
| STRING
| NAME
| "(" expression ")"
| UNIT
| function_callback
| char
| tupleval
| listval
| recordval
| impn
| HEX
| BINARY
| OCTAL
| function_def
assign_operator: ASSIGN_EQUAL
| DIV_EQUAL
| MUL_EQUAL
| ADD_EQUAL
| MIN_EQUAL
| OR_EQUAL
| AND_EQUAL
| MOD_EQUAL
// Make sure that keywords are not in NAME
NAME: /(?!(\b(let|type|var|alias|for|in|import|imp|return|if|else|class|pub|break|continue|with|yield|do|not|match|while)\b))/ CNAME
// Alias for unary_expression. Used for values that visually look tightly bound,
// which is important for function arguments since they're separated by spaces.
// For example, <myFunction 1 + 1 2> looks ambiguous; <myFunction (1 + 1) 2>
// should be used instead. However, <myFunction -num a.b> is pretty clearly
// <myFunction (-num) (a.b)> so they don't need parentheses.
?literal: unary_expression
//constants
BOOLEAN: ("true" | "false")
HEX: /0x[0-9a-fA-F]+/
BINARY: /0b[01]+/
OCTAL: /0o[0-8]+/
COMMENT: "//" /[^\n]/*
| /\/\*(.|\n)*?\*\//
OR: ("||" | "|")
AND: ("&&" | "&")
XOR: "^^"
EQUALS: "=="
ASSIGN_EQUAL: "="
GORE: ">="
LORE: "<="
SHIFTL: "<<"
SHIFTR: ">>"
IN: "in"
LESS: "<"
GREATER: ">"
NEQUALS: "~="
NOT: "~"
ADD: "+"
SUBTRACT: "-"
MULTIPLY: "*"
DIVIDE: "/"
MODULO: "%"
EXPONENT: "^"
AWAIT: "!"
PUBLIC: "pub"
MUTABLE: "mut"
UNIT: "()"
EMPTY: "_"
VALUEACCESS: "["
STRING: /"(?:[^\r\n\\\"]|\\(?:[nrtv0fb\"\\]|u\{[0-9a-fA-F]+\}|\{.\}))*"/
DIV_EQUAL: "/="
MUL_EQUAL: "*="
ADD_EQUAL: "+="
MIN_EQUAL: "-="
OR_EQUAL: "|="
AND_EQUAL: "&="
MOD_EQUAL: "%="
%import common.SIGNED_NUMBER -> NUMBER
%import common.CNAME
%import common.WS
%ignore WS
%ignore COMMENT