-
Notifications
You must be signed in to change notification settings - Fork 3
/
v.lark
207 lines (168 loc) · 6.57 KB
/
v.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
///////////////////////////////////////////////////
// V Grammar
///////////////////////////////////////////////////
// This is the grammar used by the parser to parse
// the V code. I am not sure how close it is to
// the official V compiler but that is what made
// the most sense to me
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// module scope stuff
///////////////////////////////////////////////////
start: _module_item*
_module_item: fn_decl
| method_decl
| struct_decl
| module_decl
| import_decl
| interop_fn_decl
| const_decl
// Misc
module_decl: "module" NAME ("." NAME)*
import_decl: "import" NAME ("." NAME)*
// Function declaration
fn_decl: maybe_pub "fn" NAME "(" fn_params ")" fn_return stmt_list
interop_fn_decl: "fn" NAME "." NAME "(" fn_params ")" fn_return
fn_params: [fn_param ("," fn_param)*]
fn_param: NAME maybe_mut type_decl | NAME
fn_return: [type_decl | "(" type_decl ("," type_decl)+ ")"]
// Struct declaration
struct_decl: maybe_pub "struct" NAME "{" embedded_struct_field struct_fields "}"
struct_fields: (struct_field | struct_access_mod ":")*
struct_field: NAME type_decl
!struct_access_mod: "mut"
| "pub"
| "pub" "mut"
| "pub" "mut" "mut"
embedded_struct_field: (maybe_mut NAME)?
// Method declaration
method_decl: maybe_pub "fn" "(" NAME maybe_mut type_decl ")" NAME "(" fn_params ")" fn_return stmt_list
// const declaration
const_decl: "const" "(" (const_item)+ ")"
| "const" const_item
const_item: NAME "=" _expr
///////////////////////////////////////////////////
// Statements
///////////////////////////////////////////////////
?stmt: "return" [_expr ("," _expr)*] -> stmt_return
| "assert" _expr -> stmt_assert
| "for" NAME "in" _expr stmt_list -> stmt_foreach
| "for" NAME "," NAME "in" _expr stmt_list -> stmt_foreach_indexed
| "for" stmt_list-> stmt_forever
| "for" (maybe_var_decl|stmt_assignment) ";" _expr ";" (_expr | stmt_assignment) stmt_list -> stmt_for
| "break" -> stmt_break
| "continue" -> stmt_continue
| stmt_var_decl
| stmt_assignment
| stmt_if
| (expr_fn_call) -> stmt_expr
stmt_var_decl: var_decl_vars ":=" _expr
!maybe_var_decl: stmt_var_decl?
?stmt_assignment: (ident|expr_member_access|expr_index) _op_assignment _expr -> stmt_assign
!_op_assignment: ("+="|"-="|"*="|"/="|"%="|"<<="|">>="|"|="|"&="|"^="|"=")
stmt_if: "if" _expr stmt_list (stmt_else |)
?stmt_else: "else" stmt_list -> stmt_else
| "else" stmt_if -> stmt_else_if
var_decl: maybe_mut NAME
var_decl_vars: var_decl ("," var_decl)*
stmt_list: "{" stmt* "}"
///////////////////////////////////////////////////
// Expressions
///////////////////////////////////////////////////
//************************************
// the full expression precendence
//************************************
_expr: expr_logical_or
?expr_logical_or: expr_logical_and (_op_logical_or expr_logical_and)* -> expr_binary
?expr_logical_and: expr_bitwise_or (_op_logical_and expr_bitwise_or)* -> expr_binary
?expr_bitwise_or: expr_bitwise_xor (_op_bitwise_or expr_bitwise_xor)* -> expr_binary
?expr_bitwise_xor: expr_bitwise_and (_op_bitwise_xor expr_bitwise_and)* -> expr_binary
?expr_bitwise_and: expr_equality (_op_bitwise_and expr_equality)* -> expr_binary
?expr_equality: expr_relational (_op_equality expr_relational)* -> expr_binary
?expr_relational: expr_shift (_op_relational expr_shift)* -> expr_binary
?expr_shift: expr_additive (_op_shift expr_additive)* -> expr_binary
?expr_additive: expr_multiplicative (_op_additive expr_multiplicative)* -> expr_binary
?expr_multiplicative: expr_unary (_op_multiplicative expr_unary)* -> expr_binary
?expr_unary: _op_unary expr_fix -> expr_unary
| expr_fix
?expr_fix: (expr_member_access|expr_index|ident) _op_postfix -> expr_postfix
| _op_prefix (expr_member_access|expr_index|ident) -> expr_prefix
| expr_others
// all the operators
!_op_postfix: ("++"|"--")
!_op_prefix: ("++"|"--")
!_op_unary: ("-"|"!"|"~"|"&"|"*")
!_op_multiplicative: ("*"|"/"|"%")
!_op_additive: ("+"|"-")
!_op_shift: ("<<"|">>")
!_op_relational: (">"|">="|"<="|"<")
!_op_equality: ("=="|"!=")
!_op_bitwise_xor: "^"
!_op_bitwise_and: "&"
!_op_bitwise_or: "|"
!_op_logical_and: "&&"
!_op_logical_or: "||"
?expr_others: expr_fn_call
| expr_member_access
| expr_index
| expr_literals
expr_fn_call: _expr "(" (maybe_mut _expr ("," maybe_mut _expr)*)? ")"
expr_member_access: _expr "." NAME
expr_index: _expr "[" _expr "]"
?expr_literals: "(" _expr ")"
| "none" -> const_none
| "false" -> const_false
| "true" -> const_true
| string
| number
| float
| ident
| struct_literal
| struct_literal_named
| array_literal
| array_literal_uninit
///////////////////////////////////////////////////
// Literals
///////////////////////////////////////////////////
// Struct literal types
// MyStruct{1,2,3}
// MyStruct{a: 1, b: 2, c: 3}
struct_literal: maybe_ref module_path_ident "{" (_expr ("," _expr )*)? "}"
struct_literal_named: maybe_ref module_path_ident "{" struct_literal_named_item (struct_literal_named_item)* "}"
struct_literal_named_item: NAME ":" _expr
// array literal
array_literal: "[" _expr ("," _expr)* "]"
array_literal_uninit: "[" _expr "]" type_decl
number: ZERO | DEC_NUMBER | HEX_NUMBER | OCT_NUMBER | BIN_NUMBER
float: FLOAT
string: STRING
ident: NAME
module_path_ident: NAME ("." NAME)*
///////////////////////////////////////////////////
// type declarations
///////////////////////////////////////////////////
?type_decl: type_ident
| type_array
| type_map
| type_ref
| type_opt
type_array: "[" "]" type_decl
type_map: "map" "[" type_decl "]" type_decl
type_ref: "&" type_decl
type_opt: "?" type_decl
type_ident: NAME
!maybe_pub: "pub"?
!maybe_mut: "mut"?
!maybe_ref: "&"?
///////////////////////////////////////////////////
// basic stuff
///////////////////////////////////////////////////
STRING : /[ubf]?r?("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
ZERO: /0/
DEC_NUMBER: /[1-9][0-9]*/
HEX_NUMBER: /0x[0-9da-fA-F]+/
OCT_NUMBER: /0o[0-7]+/
BIN_NUMBER: /0b[01]+/
%import common.FLOAT -> FLOAT
%import common.CNAME -> NAME
%ignore /([\t \f\n\r]+)/