-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.lark
170 lines (148 loc) · 5.54 KB
/
grammar.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
// root level
start: imports* unit*
// fundamentals
imports: [module_statement] import_statement+
unit: statement | block | control_block | function_decl | class_decl
// blocks of code
block: "{" unit* "}"
func_block: "{" (unit | return_statement)* "}"
return_statement: /\breturn\b/ expression ";"
control_block: if_block | while_block | for_block | switch_block
if_block: /\bif\b/ expression block [else_block]
else_block: /\belse\b/ [control_block | block]
while_block: /\bwhile\b/ expression block
for_block: /\bfor\b/ identifier ":" expression block
switch_block: /\bswitch\b/ expression "{" switch_body "}"
switch_body: (switch_case | switch_default)*
switch_case: /\bcase\b/ expression_list "->" (func_block | expression ";")
switch_default: /\bdefault\b/ "->" (func_block | expression ";")
function_decl: /\bfunc\b/ variable_identifier params_list func_block
class_decl: /\bclass\b/ identifier params_list "{" unit* "}"
// line of code
module_statement: /\bmodule\b/ /\b\w+(\.\w+)*/ ";"
import_statement: /\bimport\b/ /\b\w+(\.\w+)*/ ";"
statement: [statement_content] ";"
statement_content: declaration | definition | reassignment | unary_reassignment
| throw_statement
| function_invocation | map_getter
declaration: var_keyword variable_identifier [declaration_contents]
declaration_contents: typed_decl | valued_decl | typed_valued_decl
typed_decl: ":" type_list
valued_decl: "=" definition_expression
typed_valued_decl: ":" type_list "=" definition_expression
definition: variable_identifier "=" definition_expression
reassignment: variable_identifier reassignment_op expression
reassignment_op: sym_pluseq | sym_mineq | sym_multeq | sym_diveq | sym_expeq
| sym_bitandeq | sym_bitoreq | sym_bitxoreq | sym_bitlshifteq | sym_bitrshifteq
| sym_logandeq | sym_logoreq
sym_pluseq: "+="
sym_mineq: "-="
sym_multeq: "*="
sym_diveq: "/="
sym_expeq: "^="
sym_bitandeq: "&="
sym_bitoreq: "|="
sym_bitxoreq: "><="
sym_bitlshifteq: "<<="
sym_bitrshifteq: ">>="
sym_logandeq: "&&="
sym_logoreq: "||="
unary_reassignment: variable_identifier unary_reassignment_op
unary_reassignment_op: sym_inverteq
sym_inverteq: "=!="
throw_statement: /\bthrow\b/ expression
// expressions
expression: null | boolean | number | string | range | lit_type
| array
| variable_identifier
| function_invocation
| array_getter | map_getter
| typed_expression
| unary_expression
| math_expression
| bitwise_expression
| logical_expression
| comparison_expression
| func_expression
| parenth_expression
definition_expression: expression | map
function_invocation: variable_identifier "(" expression_list ")" [handler]
handler: "!" (catcher | variable_identifier | func_expression | map_getter)
catcher: /\bcatch\b/ "(" function_param ")" func_block
array_getter: variable_identifier "[" expression "]"
map_getter: variable_identifier "." map_key ["(" expression_list ")"]
typed_expression: identifier ":" expression
unary_expression: unary_op expression
unary_op: sym_positive | sym_negative | sym_lognot | sym_bitnot
sym_positive: "+"
sym_negative: "-"
sym_lognot: "!"
sym_bitnot: "~"
math_expression: expression (sym_exponent) expression
| expression (sym_multiply | sym_divide) expression
| expression (sym_add | sym_subtract) expression
sym_add: "+"
sym_subtract: "-"
sym_multiply: "*"
sym_divide: "/"
sym_exponent: "^"
bitwise_expression: expression bitwise_op expression
bitwise_op: sym_bitand | sym_bitor | sym_bitxor | sym_bitlshift | sym_bitrshift
sym_bitand: "&"
sym_bitor: "|"
sym_bitxor: "><"
sym_bitlshift: "<<"
sym_bitrshift: ">>"
logical_expression: expression logical_op expression
logical_op: sym_logand | sym_logor
sym_logand: "&&"
sym_logor: "||"
comparison_expression: expression comparison_op expression
comparison_op: sym_equals | sym_nequals | sym_less | sym_leq | sym_greater | sym_geq
sym_equals: "=="
sym_nequals: "!="
sym_less: "<"
sym_leq: "<="
sym_greater: ">"
sym_geq: ">="
func_expression: function_expression | lambda_expression
function_expression: /\bfunc\b/ params_list func_block
lambda_expression: /\bfunc\b/ params_list "=>" expression
parenth_expression: "(" expression ")"
// keywords
var_keyword: /\bvar\b/ | /\bval\b/
// atomics
identifier: word_char id_char*
variable_identifier: [sym_private] identifier (sym_nullable | sym_errorable)*
sym_private: "#"
sym_nullable: "?"
sym_errorable: "!"
null: /\bnull\b/
boolean: /\btrue\b/ | /\bfalse\b/
number: based_number | numeral
numeral: /\d*\.?\d+/
based_number: /[0-9a-zA-Z]*\.?[0-9a-zA-Z]+_\d+/
string: "\"" string_char* "\""
array: "[" ([number ":"] expression [","])* "]"
map: "{" (map_key ":" expression [","])* "}"
range: expression ".." [expression] | [expression] ".." expression
type_value: identifier | "`" template_char* "`"
lit_type: "<" type_list ">"
// language shortcut elements
expression_list: (expression [","])*
params_list: "(" (function_param [","])* ")"
function_param: identifier ":" type_list
map_key: identifier | string
type_list: type_value ("|" type_value)*
// fundamentals
any: /./
digit: /[0-9]/
word_char: /[a-zA-Z_]/
string_char: /[^"]/ | "\\" any
template_char: /[^`]/ | "\\" any
id_char: word_char | digit
// comments
%ignore /\/\/.*/
%ignore /\/\*[\s\S]*\*\//
// ignores
%ignore /\s/