This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrs.ne
385 lines (322 loc) · 15.8 KB
/
brs.ne
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# reserved words
# And, Box, CreateObject, Dim, Each, Else, ElseIf, End, EndFunction, EndIf, EndSub, EndWhile, Eval, Exit, ExitWhile,
# False, For, Function, GetGlobalAA, GetLastRunCompileError, GetLastRunRunTimeError, Goto, If, Invalid, Let, LINE_NUM,
# Next, Not, ObjFun, Or, Pos, Print, Rem, Return, Run, Step, Stop, Sub, Tab, Then, To, True, Type, While
# lexer clenup regex: \(lexer\.has\(".*?"\) \? (\{.*?\}).*?\)
@{%
const moo = require('./moo/moo')
const ast = require('./ast')
const caseInsensitiveKeywords = map => {
const transform = moo.keywords(map)
return text => transform(text.toLowerCase())
}
let lexer = moo.compile({
comment: { match: /[ \t]*(?:REM(?![\w!#$%])|').*?(?:\r?\n[ \t]*|$)/, lineBreaks: true },
NL: { match: /(?:[ \t]*\r?\n[ \t]*)+/, lineBreaks: true },
ws: { match: /[ \t]+/ },
IDENTIFIER: { match: /[a-zA-Z_][\w]*[$%!#]?/, value: x=>x.toLowerCase(),
type: caseInsensitiveKeywords({
true: ['true'], false: ['false'], invalid: ['invalid'],
and: ['and'], dim: ['dim'], each: ['each'], else: ['else'], elseif: ['elseif'], end: ['end'], endfunction: ['endfunction'],
endfor: ['endfor'], endif: ['endif'], endsub: ['endsub'], endwhile: ['endwhile'], exit: ['exit'], exitwhile: ['exitwhile'],
for: ['for'], function: ['function'], goto: ['goto'], if: ['if'], let: ['let'], next: ['next'], not: ['not'], or: ['or'],
print: ['print'], return: ['return'], step: ['step'], stop: ['stop'], sub: ['sub'], //tab: ['tab'],
then: ['then'], to: ['to'], while: ['while'], mod: ['mod'], try: ['try'], catch: ['catch'], endtry: ['endtry'], throw: ['throw'],
// non reserved keywords can be used as variable names
library: ['library'], boolean: ['boolean'], object: ['object'], dynamic: ['dynamic'], void: ['void'], integer: ['integer'],
longinteger: ['longinteger'], float: ['float'], double: ['double'], string: ['string'], in: ['in'], as: ['as']
})
},
numberLit: /\d+%|\d*\.?\d+(?:[edED][+-]?\d+)?[!#&]?|&[hH][0-9ABCDEFabcdef]+/,
stringLit: /"(?:[^"\n\r]*(?:"")*)*"/,
op: /<>|<=|>=|<<|>>|\+=|-=|\*=|\/=|\\=|<<=|>>=/,
othr: /./
})
const u = d => d[0][0]
const l = (s) => (d) => s
const flat = d => {
let a = []
for (const e of d) {
if (Array.isArray(e)) {
a.push(...e)
} else {
a.push(e)
}
}
return a
}
%}
@lexer lexer
program -> libs functions (statement_separators | _) {% ast.program %}
libs -> null
| statement_separators:? library (statement_separators library):* {% ast.libs %}
library -> %library __ string {% ast.lib %}
functions -> (statement_separators:? function):* {% ast.functions %}
function -> func {%id%} | sub {%id%}
func ->
%function __ NAME _ "(" params ")" (_ %as __ rtype):?
statement_list end_function {% ast.func %}
end_function -> %end __ %function | %endfunction
sub ->
%sub __ NAME _ "(" params ")" (_ %as __ rtype):?
statement_list (%end __ %sub | %endsub) {% ast.func %}
anonymous_function ->
%function _ "(" params ")" (_ %as __ rtype):? statement_list end_function {% ast.afunc %}
| %sub _ "(" params ")" (_ %as __ rtype):? statement_list (%end _ %sub | %endsub) {% ast.afunc %}
params -> _ param (_ "," _ param):* _ {% ast.params %}
| _ {% ast.params %}
param -> IDENTIFIER param_default:? param_type:? {% ast.param %}
param_default -> _ "=" _ rval
param_type -> _ %as __ ptype
ptype -> type {% u %}
rtype -> type {% u %} | %void {% id %}
type -> %boolean | %integer | %longinteger | %float | %double | %string | %object | %dynamic | %function
# "Interface" is not allowed in param or return
statement_list ->
(statement_separators statement):* statement_separators {% ast.statements %}
statement_separators -> statement_separator:+ _ {% ast.statement_separators %}
statement_separator -> NL | _ ":"
statement ->
if_statement
| dim_statement
| for_loop
| for_each
| while_loop
| try_catch
| throw
| exit_loop
| return_statement
| stop_statement
| end_statement
# do we ever want goto?
#| goto_label
#| goto_statement
| assign_statement
| call_statement
| print_statement
oneline_statement ->
oneline_if
| return_statement
| stop_statement
| end_statement
#| goto_statement
| exit_loop
| assign_statement
| call_statement
| print_statement
| throw
# if ------------------------
if_statement ->
%if if_body
else_if:*
(%else statement_list_or_space):?
endif {% ast.if %}
| oneline_if {% id %}
else_if -> elseif if_body
# using EXPR for conditional because while roku allows to have literal arrays and
# objects and functions in conditions, during complilation they allways fail at runtime
# so it is better to use EXPR and catch those errors early.
if_body -> _ EXPR (_ %then):? statement_list
elseif -> %else __ %if
| %elseif
endif -> %end __ %if
| %endif
# space in "else if" below is important! Will error on %elseif
oneline_if -> %if _ EXPR (_ %then):? _ oneline_statement
(_ %else __ oneline_statement):? {% ast.oneline_if %}
# enables following "valid" BRS code
# if bool then op()
# else endif
# notice that there is no statement separator between else and endif
statement_list_or_space -> statement_list {% id %} | __ {% id %}
# end if -------------------
dim_statement -> %dim __ IDENTIFIER _ "[" _ expression_list _ "]" {% ast.dim %}
expression_list -> (EXPR _ "," _):* EXPR {% ast.expr_list %}
for_loop ->
%for __ IDENTIFIER _ "=" _ EXPR _ %to _ EXPR (_ %step _ EXPR):?
statement_list end_for {% ast.for %}
end_for -> %end __ %for | %endfor | %next (__ IDENTIFIER):?
# `endfor :` <- results in error, `next :` is ok :(
for_each ->
(%for __ %each) __ IDENTIFIER __
%in _ rval statement_list end_for_each {% ast.foreach %}
end_for_each -> %end __ %for | %endfor | %next {% id %}
while_loop ->
%while _ EXPR statement_list (%end __ %while | %endwhile) {% ast.while %}
# `exitfor` not allowed, must be `exit for`
exit_loop -> %exit __ %while {% ast.exit %}
| %exitwhile {% ast.exit %}
| %exit __ %for {% ast.exit %}
try_catch ->
%try statement_list %catch (__ IDENTIFIER):? statement_list end_try {% ast.try %}
end_try -> %end __ %try | %endtry
throw -> %throw __ (string | object_literal | IDENTIFIER access_or_call:*) {% ast.throw %}
return_statement -> %return (_ rval):? {% ast.return %}
stop_statement -> %stop {% ast.stop %}
end_statement -> %end {% ast.end %}
goto_label -> IDENTIFIER _ ":"
goto_statement -> %goto __ IDENTIFIER
print_statement -> print print_items {% ast.print %}
print -> %print {% id %}
| "?" {% id %}
print_items ->
psep:* {% id %}
| psep:* EXPR (_ PEXPR | pxsp EXPR):* (psep:* ppp):? {% ast.print_items %}
psep-> ";" {%id%} | "," {%id%} | __ {%id%}
ppp-> ";" {%id%} | "," {%id%}
pxsp-> _ ppp psep:* {% flat %}
call_statement -> IDENTIFIER access_or_call:* call {% ast.lval %}
assign_statement -> lval _ assign_op _ rval {% ast.assign %}
| lval _ incdec {% ast.incdec %}
assign_op -> "=" | "+=" | "-=" | "*=" | "/=" | "\\=" | "<<=" | ">>="
incdec -> "+" "+" {% l('++') %}
| "-" "-" {% l('--') %}
lval ->
IDENTIFIER {% id %}
| IDENTIFIER access_or_call:* access {% ast.lval %}
access ->
_ "." _ PROP_NAME {% ast.prop %}
| _ "[" _ EXPR (_ "," _ EXPR):* _ "]" {% ast.index %}
call -> _ "(" _ rval (_ "," _ rval):* _ ")" {% ast.call %}
| _ "(" _ ")" {% ast.call %}
xmlattr -> _ "@" _ ATTR_NAME {% ast.xmlattr %}
PROP_NAME ->
IDENTIFIER {% ast.name %}
| RESERVED {% ast.name %}
| constant {% ast.name %}
| string {% ast.name %}
ATTR_NAME ->
IDENTIFIER {% ast.name %}
| RESERVED {% ast.name %}
access_or_call ->
access {% id %}
| call {% id %}
# property, index, call or xml attribute: picx `a.prop[5]@attr.toInt()`
picx ->
access {% id %}
| call {% id %}
| xmlattr {% id %}
rval -> EXPR {% id %}
| object_literal {% id %}
| array_literal {% id %}
| anonymous_function {% id %}
object_literal ->
"{" _NL (prop lisep):* prop (_ | lisep) "}" {% ast.object %}
| "{" _NL "}" {% ast.object %}
array_literal ->
"[" _NL (rval lisep):* rval (_ | lisep) "]" {% ast.array %}
| "[" _NL "]" {% ast.array %}
prop -> PROP_NAME _ ":" _ rval {% ast.propdef %}
lisep -> _ "," _NL | NL:+
val ->
IDENTIFIER {% id %}
| number {% id %}
| string {% id %}
| constant {% id %}
# Keeping array_literal and object_literal out of val because they are not valid in expressions
# I.e. `[5] + 5`, `[1] + [2,3]`, `{ t:5 } * a` etc. not allowed
# Roku compiler allows [], {} and func in if condition, but it allways fails at runtime with Type Missmatch error
# ax = []
# if ax = [] ? "ok" <- runtime error
# ax = {}
# if ax = {} ? "ok" <- runtime error
# ax = function () : end function
# if ax = function () : end function ? "<O_O>" <- runtime error
EXPR -> O {% ast.expr %}
P -> "(" _ O _ ")" {% ast.parenthesis %}
| val {% id %}
AOP -> P picx:+ {% ast.access %}
| P {% id %}
U -> ("-"|"+") _ U {% ast.uop %}
| AOP {% id %}
E -> U _ "^" _ E {% ast.bop %}
| U {% id %}
M -> M _ mul_op _ E {% ast.bop %}
| E {% id %}
A -> A _ add_op _ M {% ast.bop %}
| M {% id %}
S -> S _ shft_op _ A {% ast.bop %}
| A {% id %}
C -> C _ comp_op _ S {% ast.bop %}
| S {% id %}
N -> %not _ N {% ast.uop %}
| C {% id %}
D -> D _ %and _ N {% ast.bop %}
| N {% id %}
O -> O _ %or _ D {% ast.bop %}
| D {% id %}
# resolve ambiguity in print statment ie `? 1 -1` is it ?(1-1) or ?(1), (-1)
# by disallowing left unary op and enclosing parenthesis.
# Alaso `? bar (1)` can only mean function bar called with 1
PEXPR -> PO {% ast.expr %} # PEXP can not start with -|+ or (
PAOP -> val picx:+ {% ast.access %}
| val {% id %}
PE -> PAOP _ "^" _ E {% ast.bop %}
| PAOP {% id %}
PM -> PM _ mul_op _ E {% ast.bop %}
| PE {% id %}
PA -> PA _ add_op _ M {% ast.bop %}
| PM {% id %}
PS -> PS _ shft_op _ A {% ast.bop %}
| PA {% id %}
PC -> PC _ comp_op _ S {% ast.bop %}
| PS {% id %}
PN -> %not _ N {% ast.uop %}
| PC {% id %}
PD -> PD _ %and _ N {% ast.bop %}
| PN {% id %}
PO -> PO _ %or _ D {% ast.bop %}
| PD {% id %}
###########
add_op -> "+" | "-"
mul_op -> "*" | "/" | "\\" | %mod
shft_op -> ">>" | "<<"
comp_op -> "=" | "<>" | "<" | ">" | "<=" | ">="
_ -> %ws:? {% id %}
__ -> %ws {% id %}
comment -> %comment {% ast.comment %}
NAME -> %IDENTIFIER {% ast.identifier %}
| UNRESERVED {% ast.identifier %}
IDENTIFIER -> %IDENTIFIER {% ast.identifier %}
| UNRESERVED {% ast.identifier %}
number -> %numberLit {% ast.number %}
string -> %stringLit {% ast.string %}
constant -> %true {% ast.constant %}
| %false {% ast.constant %}
| %invalid {% ast.constant %}
NL -> %comment {% ast.comment %}
| %NL {% id %}
_NL -> _ | NL:+
RESERVED ->
%and {%id%} | %dim {%id%} | %each {%id%} | %else {%id%} |
%elseif {%id%} | %end {%id%} | %endfunction {%id%} | %endif {%id%} |
%endsub {%id%} | %endwhile {%id%} | %for {%id%} | %function {%id%} |
%goto {%id%} | %if {%id%} | %let {%id%} | %next {%id%} |
%not {%id%} | %or {%id%} | %print {%id%} | %return {%id%} |
%step {%id%} | %stop {%id%} | %sub {%id%} |
%then {%id%} | %to {%id%} | %while {%id%} | %exit {%id%} |
%exitwhile {%id%} | %mod {%id%} | %endfor {%id%} |
%try {%id%} | %catch {%id%} | %endtry {%id%} | %throw {%id%}
UNRESERVED ->
%library {%id%} | %boolean {%id%} | %object {%id%} | %dynamic {%id%} | %void {%id%} | %integer {%id%} |
%longinteger {%id%} | %float {%id%} | %double {%id%} | %string {%id%} | %in {%id%} | %as {%id%}
#######################################
# custom extensions (code comments)
#######################################
xdeclaration -> (NL (interface | enum)):* NL
interface -> "interface" __ NAME
interface_members
"end" __ "interface"
interface_members -> (NL interface_member):* NL
interface_member ->
"property" __ NAME __ "as" __ IDENTIFIER
| "function" _ "(" xparams ")" (__ "as" __ IDENTIFIER):?
xparams -> _ xparam (_ "," _ xparam):* _ {% ast.params %}
| _ {% ast.params %}
xparam -> IDENTIFIER param_default:? xparam_type:? {% ast.param %}
xparam_type -> _ "as" __ IDENTIFIER
enum -> "enum" __ NAME
enum_members
"end" __ "enum"
enum_members -> (NL enum_member):* NL
enum_member -> NAME _ "=" _ (string | number)