-
Notifications
You must be signed in to change notification settings - Fork 2
/
princess.ebnf
344 lines (273 loc) · 12.9 KB
/
princess.ebnf
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
@@grammar :: Princess
# @@whitespace :: /[\t ]+/ # Whitespace is handled inside the lexer
@@parseinfo :: True
@@namechars :: '_'
@@keyword :: if else not and or var let const type
@@keyword :: while for loop continue break in do
@@keyword :: null true false word unsigned struct enum
@@keyword :: label go_to case size_of switch align_of
@@keyword :: def return export import as from
code_body::Body = '{' n_ @:{statement} ~ '}';
program::Program = @:{statement} $;
# Whitespace
t_newline = /[\n]+/;
t_term = ';' | t_newline | &$ | &'}';
n_ = /[\n\t ]*/;
# Semantic actions handle stripping newlines inside parens
t_oparen = '(';
t_cparen = ')';
# Numeric literal
digit = /[0-9]/;
hex_digit = /[a-fA-F0-9]/;
oct_digit = /[0-8]/;
bin_digit = /[01]/;
sign = /[+-]/ | `"+"`;
# Exponent
exp = /[Ee]/ sign:sign ~ num: {digit}+;
t_float_literal::Float::Literal::Expression =
| (num: {digit}+ (("." !'.' ~ frac: ({digit}+ | `"0"`) exp:[exp]) | frac:`"0"` exp:exp))
| (num: `"0"` "." !'.' ~ frac: {digit}+ exp:[exp]);
t_dec_literal::Integer::Literal =
base: `10`
num: {digit}+;
t_hex_literal::Integer = /0x/ ~
base: `16`
num: {hex_digit}+;
t_oct_literal::Integer = /0o/ ~
base: `8`
num: {oct_digit}+;
t_bin_literal::Integer = /0b/ ~
base: `2`
num: {bin_digit}+;
t_int_literal = (t_hex_literal | t_bin_literal | t_oct_literal | t_dec_literal);
t_num_lit = (t_float_literal | t_int_literal);
# String literal
ESC_CHAR = @+:/[abfnrtv\\'\\"0]/;
ESC_SEQ =
| (/x/ ~ hex_digit hex_digit) # Hex digit
| (/u/ ~ hex_digit hex_digit hex_digit hex_digit) # Unicode
| (/U/ ~ hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit) # Extended Unicode
| ~ ESC_CHAR; # Normal escape
T_CHAR = ((/\\/ ~ @:ESC_SEQ) | /[^\0\x7f\x80"\\]+/);
T_CHAR_S = ((/\\/ ~ @:ESC_SEQ) | /[^\0\x7f\x80'\\]/);
T_CHAR_LIT::Char::Literal = "'" @:T_CHAR_S ~ "'";
T_STRING_LIT::String::Literal = '"' @:{T_CHAR} ~ '"';
@name
t_ident = /(?!\d)\w+/;
identifier::Identifier::Expression = [root: '::'] ident:'::'.{t_ident}+;
# Boolean literal
t_bool = "true" @:`True` | "false" @:`False`;
t_bool_lit::Boolean::Literal = @:t_bool;
t_null::Null::Expression = "null" @:();
# Array literal
array_element = n_ @:expression_call n_;
array_lit::Array::Literal = '[' n_ @:','.{array_element} ~ ']';
literal = (array_lit | t_num_lit | t_bool_lit | t_null | T_CHAR_LIT | T_STRING_LIT);
value = literal | identifier;
# Struct literal
struct_arg_named::StructArg =
n_ name: identifier '=' value: expression_call;
struct_arg::StructArg = n_ value: expression_call;
struct_lit::StructInit = '{' args:','.{struct_arg_named | struct_arg} n_ ~ '}';
struct_lit_typed::StructInit = >struct_lit ["!" ~ type: type];
# type { ... }
type_structural::StructuralT = '{' n_ !() ~ '}'; # TODO
# struct { ... }
stmt_struct_else::Else::Statement = "else" n_ ~ body: struct_body;
stmt_struct_else_if::ElseIf::Statement = "else" n_ "if" n_ ~ cond:expression n_ body:struct_body;
stmt_struct_if::If::Statement =
"#if" n_ ~ cond:expression n_ body:struct_body
else_if:{stmt_struct_else_if}
[n_ else: stmt_struct_else];
struct_iddecl::IdDeclStruct = n_ name: identifier [':' n_ type: type];
struct_def =
| @:() t_term
| @:struct_def_noterm t_term;
struct_def_noterm =
| t_oparen (@:() &')' | @:struct_def_noterm) t_cparen
| @:(type_struct | stmt_struct_if | struct_iddecl);
struct_body::StructBody = '{' n_ @:{struct_def} n_ '}';
struct_pragmas = @:"#union" n_;
type_struct::Struct::Type = "struct" n_ pragma: {struct_pragmas} n_ body: struct_body;
# enum [: <type>] { ... }
stmt_enum_else::Else::Statement = "else" n_ ~ body: enum_body;
stmt_enum_else_if::ElseIf::Statement = "else" n_ "if" n_ ~ cond:expression n_ body:enum_body;
stmt_enum_if::If::Statement =
"#if" n_ ~ cond:expression n_ body:enum_body
else_if:{stmt_enum_else_if}
[n_ else: stmt_enum_else];
enum_iddecl::IdDeclEnum = n_ name: identifier ['=' n_ value: expression];
enum_def =
| @:() t_term
| @:enum_def_noterm t_term;
enum_def_noterm =
| t_oparen (@:() &')' | @:enum_def_noterm) t_cparen
| @:(stmt_enum_if | enum_iddecl);
enum_body::EnumBody = '{' n_ @:{enum_def} n_ '}';
type_enum::TEnum::Type = "enum" n_ [':' n_ type: type n_] n_ body: enum_body;
type_ptr::PtrT::Type = '*' keyword: ("var" | "let" | `"var"`) [type: type_1];
type_ref::RefT::Type = '&' keyword: ("var" | "let" | `"var"`) [type: type_1];
# Array type
type_array_dyn::ArrayT::Type = '[' keyword: ("var" | "let" | `"var"`) [type: type] ']';
type_array_static::ArrayT = '[' ~ n:('?' | expression) ';'
keyword: ("var" | "let" | `"var"`) type: type ']';
type_array = type_array_dyn | type_array_static;
# Function type
type_list_elem = @:type;
type_single = @+:type;
type_list = ( t_oparen @:','.{type_list_elem} t_cparen );
type_function::FunctionT::Type =
| '->' right: [type_list | type_single]
| left: (type_list | type_single) '->' right: [type_list | type_single];
type_unsigned::Unsigned::Type = "unsigned" @:type_2;
type_word::Word::Type = "word" t_oparen @:t_int_literal t_cparen;
# Type expression
type_3 = t_oparen @:type t_cparen | type_word | identifier;
type_2 = expr_type_call | type_3;
type_1 = type_struct | type_enum | type_unsigned | type_structural | type_ptr | type_ref | type_array | type_2;
type = type_function | type_1;
call_arg_named::CallArg =
name: identifier '=' value: expression_call; # Named argument
call_arg::CallArg = value: expression_call; # Positional argument
call_args = t_oparen @:','.{call_arg_named | call_arg} ~ t_cparen;
expr_call::Call::Expression = left: expr_10 args: call_args;
expr_type_call::Call::Expression = left: type_2 args: call_args; # For macro calls in type position
expr_array_index::ArrayIndex::Expression = left: expr_10 '[' n_ right: expression n_ ~ ']';
expr_member_access::MemberAccess::Expression = left: expr_10 '.' !'.' right: value;
expr_type = "type" @:type;
post_lh = /[\d\w\(]/;
expr_postinc::PostInc::UnaryPostOp::Expression = left: expr_10 '++' !post_lh;
expr_postdec::PostDec::UnaryPostOp = left: expr_10 '--' !post_lh;
expr_cast::Cast::Expression = left: (struct_lit | expr_9) '!' !'=' ~ right: type;
expr_autocast::Cast = left: expr_10 '!!';
expr_preinc::PreInc::UnaryPreOp::Expression = '++' ~ right:expr_9;
expr_predec::PreDec::UnaryPreOp = '--' ~ right:expr_9;
expr_uminus::UMinus::UnaryPreOp = '-' ~ right:expr_9;
expr_deref::Deref::UnaryPreOp = '@' ~ right:expr_9;
expr_ptr::Ptr::UnaryPreOp = '*' ~ right:expr_9;
expr_invert::Invert::UnaryPreOp = '~' ~ right:expr_9;
expr_not::Not::UnaryPreOp = "not" ~ right:expr_9;
expr_shr::Shr::BinaryOp::Expression = left:expr_7 '>>' n_ ~ right:expr_8;
expr_shl::Shl::BinaryOp = left:expr_7 '<<' n_ ~ right:expr_8;
expr_band::BAnd::BinaryOp = left:expr_7 '&' n_ ~ right:expr_8;
expr_bor::BOr::BinaryOp = left:expr_7 '|' n_ ~ right:expr_8;
expr_xor::Xor::BinaryOp = left:expr_7 '^' n_ ~ right:expr_8;
expr_mul::Mul::BinaryOp = left:expr_6 '*' n_ ~ right:expr_7;
expr_div::Div::BinaryOp = left:expr_6 '/' n_ ~ right:expr_7;
expr_mod::Mod::BinaryOp = left:expr_6 '%' n_ ~ right:expr_7;
expr_padd::PAdd::BinaryOp = left:expr_5 '++' n_ ~ right:expr_6;
expr_psub::PSub::BinaryOp = left:expr_5 '--' n_ ~ right:expr_6;
expr_add::Add::BinaryOp = left:expr_5 '+' n_ ~ right:expr_6;
expr_sub::Sub::BinaryOp = left:expr_5 '-' n_ ~ right:expr_6;
cmp_op::CompareOp = ('<=' | '>=' | '<' | '>' | '==' | '!=');
expr_cmp = op: cmp_op n_ ~ right: expr_5;
expr_cmp_start::Compare::Expression = left:expr_5 right:{expr_cmp}+;
expr_and::And::BinaryOp = left:expr_3 "and" n_ ~ right: expr_4;
expr_or::Or::BinaryOp = left:expr_2 "or" n_ ~ right: expr_3;
assign_op::AssignOp = ('++=' | '--=' | '+=' | '-=' | '*=' | '/=' | '%=' | '|=' | '&=' | '^=' | '>>=' | '<<=');
expr_assign_op::AssignAndOp::Expression = left: expr_1 op:assign_op n_ ~ right: expr_0;
expr_assign_lhs = @:expr_2;
expr_assign_rhs = @:(struct_lit_typed | expr_0);
expr_assign::Assign::Expression = left: ','.{expr_assign_lhs}+ '=' n_ ~ right: ','.{expr_assign_rhs}+;
expr_if::IfExpr::Expression = if_true: expr_1 ("if") n_ ~ cond: expr_0 n_ ("else") n_ if_false: expr_0;
expr_do::Do::Expression = 'do' n_ ~ @:code_body;
expr_size_of::SizeOf::Expression = 'size_of' n_ ~ @:expression_no_assign;
expr_align_of::AlignOf::Expression = 'align_of' n_ ~ @:expression_no_assign;
# TODO: This allows assignents inside ranges and lets the following compile:
# a: b = 2 as a Range, this was CLEARLY not intended
expr_range::Range::Expression =
from: [expr_2] '..' n_ to: [expr_2];
expr_range_incl::RangeIncl::Expression =
from: [expr_2] '..=' n_ to: expr_2;
# Precedence ladder
expr_11 =
| t_oparen ~ @:expression t_cparen
| value;
expr_10 = expr_type | expr_call | expr_array_index | expr_member_access | expr_autocast | expr_postinc | expr_postdec | expr_11;
expr_9 = expr_preinc | expr_predec | expr_uminus | expr_deref | expr_ptr | expr_invert | expr_not | expr_10;
expr_8 = expr_cast | expr_9;
expr_7 = expr_shr | expr_shl | expr_band | expr_bor | expr_xor | expr_8;
expr_6 = expr_mul | expr_div | expr_mod | expr_7;
expr_5 = expr_padd | expr_psub | expr_add | expr_sub | expr_6;
expr_4 = expr_cmp_start | expr_5;
expr_3 = expr_and | expr_4;
expr_2 = expr_or | expr_3;
expr_1 = expr_range_incl | expr_range | expr_2;
expr_0 = expr_do | expr_size_of | expr_align_of | expr_assign_op | expr_assign | expr_if | expr_1;
expression = expr_0;
expression_no_assign = expr_do | expr_size_of | expr_align_of | expr_if | expr_2; # TODO Should if expressions be allowed?
expression_call = struct_lit_typed | expression_no_assign; # TODO might want to handle this inside the parser
import_marker::Share = "export" @:`2` | `0`;
stmt_import_module::ImportModule =
n_ name: identifier
["as" n_ alias: identifier];
stmt_import::Import =
[share: import_marker n_] "import"
modules: ','.{stmt_import_module}+;
var_iddecl::IdDecl = >struct_iddecl;
var_idassign::IdAssign = n_ t_oparen n_ @:expression n_ t_cparen;
stmt_vardecl_rhs = n_ @:(struct_lit_typed | expression);
stmt_vardecl::VarDecl::Statement =
[share: share_marker n_]
keyword: ("var" | "let" | "const") n_ ~
left: ','.{var_idassign | var_iddecl}+
['=' n_ right: ','.{stmt_vardecl_rhs}+];
stmt_typedecl_lhs = n_ @:identifier;
stmt_typedecl_rhs = n_ @:type;
stmt_typedecl::TypeDecl::Statement =
[share: share_marker n_] "type" n_ name: ','.{stmt_typedecl_lhs}+ ['=' n_ value: ','.{stmt_typedecl_rhs}+];
# if statement
stmt_else::Else::Statement = "else" n_ ~ body: code_body;
stmt_else_if::ElseIf::Statement = "else" n_ "if" n_ ~ cond:expression n_ body: code_body;
stmt_if::If::Statement =
"if" n_ ~ cond: expression n_ body: code_body
else_if:{stmt_else_if}
[n_ else: stmt_else];
# switch case
stmt_switch::Switch::Statement = "switch" n_ value: expression n_ body: code_body;
stmt_static_if::StaticIf::Statement = '#' !' ' >stmt_if;
# while loop
stmt_while_loop::While::Statement = 'while' n_ ~ cond: expression n_ body: code_body;
stmt_loop::While::Statement = 'loop' n_ ~ body: code_body;
stmt_continue::Continue::Statement = 'continue' @:();
stmt_break::Break::Statement = 'break' @:();
ret_arg = n_ @:expression_no_assign;
stmt_return::Return::Statement = 'return' @:','.{ret_arg};
# for loop
expr_in_assign::IdAssign = n_ @:identifier;
expr_in::In::Expression =
(keyword: ("var" | "let") left:','.{var_idassign | var_iddecl}+ | left: ','.{expr_in_assign}+)
'in' n_ right: ','.{stmt_vardecl_rhs}+;
stmt_for_loop::For::Statement = 'for' n_ iterator: expr_in n_ body: code_body;
# pragma
pragma_ident = /[#]{1,2}(?!\d)\w+/;
stmt_pragma::Pragma::Statement = name: pragma_ident [args: call_args];
stmt_case_rhs = n_ (@:expr_range_incl | @:expr_range | @:value);
stmt_case::Case::Statement = "case" n_ ~ [value: ','.{stmt_case_rhs}+ n_] ':' n_ statement: (statement_noterm | expression);
stmt_label::Label::Statement = "label" n_ ~ name: identifier n_ ':' n_ statement: (statement_noterm | expression);
stmt_goto::Goto::Statement = "go_to" n_ ~ @:identifier;
# function definition
return_type = n_ @:type;
def_arg::DefArg = keyword: ("let" | "var" | "type" | `"var"`) name: identifier [':' type: type] ['=' value: expression_call];
def_body = @:code_body | '=' n_ @:expression;
share_marker::Share =
| 'export' n_ 'import' @:`3`
| 'export' @:`2`
| 'import' @:`1`
| @:`0`;
stmt_def::Def::Statement =
[share: share_marker n_]
"def" n_ ~ name: identifier
[t_oparen ~ args:','.{def_arg} t_cparen]
['->' returns: ','.{return_type}]
[body: def_body];
stmt = stmt_vardecl | stmt_typedecl | stmt_if | stmt_def | stmt_import | stmt_static_if | stmt_pragma | stmt_for_loop |
stmt_while_loop | stmt_loop | stmt_switch | stmt_return | stmt_continue | stmt_break | stmt_goto;
statement =
| @:() t_term
| @:(stmt_label | stmt_case) t_term
| @:statement_noterm t_term
| @:expression t_term;
statement_noterm =
| t_oparen (@:() &')' | @:statement_noterm) t_cparen
| @:stmt;