-
Notifications
You must be signed in to change notification settings - Fork 75
/
bs-js-compiler.txt
222 lines (217 loc) · 16.4 KB
/
bs-js-compiler.txt
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
ometa BSJSParser {
space = ^space | fromTo('//', '\n') | fromTo('/*', '*/'),
nameFirst = letter | '$' | '_',
nameRest = nameFirst | digit,
iName = <nameFirst nameRest*>,
isKeyword :x = ?BSJSParser._isKeyword(x),
name = iName:n ~isKeyword(n) -> [#name, n=='self' ? '$elf' : n],
keyword = iName:k isKeyword(k) -> [k, k],
hexDigit = char:x {this.hexDigits.indexOf(x.toLowerCase())}:v ?(v >= 0) -> v,
hexLit = hexLit:n hexDigit:d -> (n * 16 + d)
| hexDigit,
number = ``0x'' hexLit:n -> [#number, n]
| <digit+ ('.' digit+)?>:f -> [#number, parseFloat(f)],
escapeChar = <'\\' ( 'u' hexDigit hexDigit hexDigit hexDigit
| 'x' hexDigit hexDigit
| char )>:s -> unescape(s),
str = seq('"""') (~seq('"""') char)*:cs seq('"""') -> [#string, cs.join('')]
| '\'' (escapeChar | ~'\'' char)*:cs '\'' -> [#string, cs.join('')]
| '"' (escapeChar | ~'"' char)*:cs '"' -> [#string, cs.join('')]
| ('#' | '`') iName:n -> [#string, n],
special = ( '(' | ')' | '{' | '}' | '[' | ']' | ','
| ';' | '?' | ':' | ``!=='' | ``!='' | ``==='' | ``==''
| ``='' | ``>='' | '>' | ``<='' | '<' | ``++'' | ``+=''
| '+' | ``--'' | ``-='' | '-' | ``*='' | '*' | ``/=''
| '/' | ``%='' | '%' | ``&&='' | ``&&'' | ``||='' | ``||''
| '.' | '!' ):s -> [s, s],
tok = spaces (name | keyword | number | str | special),
toks = token*:ts spaces end -> ts,
token :tt = tok:t ?(t[0] == tt) -> t[1],
spacesNoNl = (~'\n' space)*,
expr = orExpr:e ( "?" expr:t ":" expr:f -> [#condExpr, e, t, f]
| "=" expr:rhs -> [#set, e, rhs]
| "+=" expr:rhs -> [#mset, e, "+", rhs]
| "-=" expr:rhs -> [#mset, e, "-", rhs]
| "*=" expr:rhs -> [#mset, e, "*", rhs]
| "/=" expr:rhs -> [#mset, e, "/", rhs]
| "%=" expr:rhs -> [#mset, e, "%", rhs]
| "&&=" expr:rhs -> [#mset, e, "&&", rhs]
| "||=" expr:rhs -> [#mset, e, "||", rhs]
| empty -> e
),
orExpr = orExpr:x "||" andExpr:y -> [#binop, "||", x, y]
| andExpr,
andExpr = andExpr:x "&&" eqExpr:y -> [#binop, "&&", x, y]
| eqExpr,
eqExpr = eqExpr:x ( "==" relExpr:y -> [#binop, "==", x, y]
| "!=" relExpr:y -> [#binop, "!=", x, y]
| "===" relExpr:y -> [#binop, "===", x, y]
| "!==" relExpr:y -> [#binop, "!==", x, y]
)
| relExpr,
relExpr = relExpr:x ( ">" addExpr:y -> [#binop, ">", x, y]
| ">=" addExpr:y -> [#binop, ">=", x, y]
| "<" addExpr:y -> [#binop, "<", x, y]
| "<=" addExpr:y -> [#binop, "<=", x, y]
| "instanceof" addExpr:y -> [#binop, "instanceof", x, y]
)
| addExpr,
addExpr = addExpr:x "+" mulExpr:y -> [#binop, "+", x, y]
| addExpr:x "-" mulExpr:y -> [#binop, "-", x, y]
| mulExpr,
mulExpr = mulExpr:x "*" unary:y -> [#binop, "*", x, y]
| mulExpr:x "/" unary:y -> [#binop, "/", x, y]
| mulExpr:x "%" unary:y -> [#binop, "%", x, y]
| unary,
unary = "-" postfix:p -> [#unop, "-", p]
| "+" postfix:p -> [#unop, "+", p]
| "++" postfix:p -> [#preop, "++", p]
| "--" postfix:p -> [#preop, "--", p]
| "!" unary:p -> [#unop, "!", p]
| "void" unary:p -> [#unop, "void", p]
| "delete" unary:p -> [#unop, "delete", p]
| "typeof" unary:p -> [#unop, "typeof", p]
| postfix,
postfix = primExpr:p ( spacesNoNl "++" -> [#postop, "++", p]
| spacesNoNl "--" -> [#postop, "--", p]
| empty -> p
),
primExpr = primExpr:p ( "[" expr:i "]" -> [#getp, i, p]
| "." "name":m "(" listOf(#expr, ','):as ")" -> [#send, m, p].concat(as)
| "." "name":f -> [#getp, [#string, f], p]
| "(" listOf(#expr, ','):as ")" -> [#call, p].concat(as)
)
| primExprHd,
primExprHd = "(" expr:e ")" -> e
| "this" -> [#this]
| "name":n -> [#get, n]
| "number":n -> [#number, n]
| "string":s -> [#string, s]
| "function" funcRest
| "new" "name":n "(" listOf(#expr, ','):as ")" -> [#new, n].concat(as)
| "[" listOf(#expr, ','):es "]" -> [#arr].concat(es)
| json
| re,
json = "{" listOf(#jsonBinding, ','):bs "}" -> [#json].concat(bs),
jsonBinding = jsonPropName:n ":" expr:v -> [#binding, n, v],
jsonPropName = "name" | "number" | "string",
re = spaces <'/' reBody '/' reFlag*>:x -> [#regExpr, x],
reBody = re1stChar reChar*,
re1stChar = ~('*' | '\\' | '/' | '[') reNonTerm
| escapeChar
| reClass,
reChar = re1stChar | '*',
reNonTerm = ~('\n' | '\r') char,
reClass = '[' reClassChar* ']',
reClassChar = ~('[' | ']') reChar,
reFlag = nameFirst,
formal = spaces "name",
funcRest = "(" listOf(#formal, ','):fs ")" "{" srcElems:body "}" -> [#func, fs, body],
sc = spacesNoNl ('\n' | &'}' | end)
| ";",
binding = "name":n ( "=" expr
| empty -> [#get, 'undefined'] ):v -> [#var, n, v],
block = "{" srcElems:ss "}" -> ss,
stmt = block
| "var" listOf(#binding, ','):bs sc -> [#begin].concat(bs)
| "if" "(" expr:c ")" stmt:t ( "else" stmt
| empty -> [#get, 'undefined'] ):f -> [#if, c, t, f]
| "while" "(" expr:c ")" stmt:s -> [#while, c, s]
| "do" stmt:s "while" "(" expr:c ")" sc -> [#doWhile, s, c]
| "for" "(" ( "var" binding
| expr
| empty -> [#get, 'undefined'] ):i
";" ( expr
| empty -> [#get, 'true'] ):c
";" ( expr
| empty -> [#get, 'undefined'] ):u
")" stmt:s -> [#for, i, c, u, s]
| "for" "(" ( "var" "name":n -> [#var, n, [#get, 'undefined']]
| expr ):v
"in" expr:e
")" stmt:s -> [#forIn, v, e, s]
| "switch" "(" expr:e ")" "{"
( "case" expr:c ":" srcElems:cs -> [#case, c, cs]
| "default" ":" srcElems:cs -> [#default, cs] )*:cs
"}" -> [#switch, e].concat(cs)
| "break" sc -> [#break]
| "continue" sc -> [#continue]
| "throw" spacesNoNl expr:e sc -> [#throw, e]
| "try" block:t "catch" "(" "name":e ")" block:c
( "finally" block
| empty -> [#get, 'undefined'] ):f -> [#try, t, e, c, f]
| "return" ( expr
| empty -> [#get, 'undefined'] ):e sc -> [#return, e]
| "with" "(" expr:x ")" stmt:s -> [#with, x, s]
| expr:e sc -> e
| ";" -> [#get, "undefined"],
srcElem = "function" "name":n funcRest:f -> [#var, n, f]
| stmt,
srcElems = srcElem*:ss -> [#begin].concat(ss),
topLevel = srcElems:r spaces end -> r
}
BSJSParser.hexDigits = "0123456789abcdef"
BSJSParser.keywords = { }
keywords = ["break", "case", "catch", "continue", "default", "delete", "do", "else", "finally", "for", "function", "if", "in",
"instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with", "ometa"]
for (var idx = 0; idx < keywords.length; idx++)
BSJSParser.keywords[keywords[idx]] = true
BSJSParser._isKeyword = function(k) { return this.keywords.hasOwnProperty(k) }
ometa BSSemActionParser <: BSJSParser {
curlySemAction = "{" expr:r sc "}" spaces -> r
| "{" (srcElem:s &srcElem -> s)*:ss
( expr:r sc -> [#return, r] | srcElem):s {ss.push(s)}
"}" spaces -> [#send, #call,
[#func, [], [#begin].concat(ss)],
[#this]],
semAction = curlySemAction
| primExpr:r spaces -> r
}
ometa BSJSTranslator {
trans = [:t apply(t):ans] -> ans,
curlyTrans = [#begin curlyTrans:r] -> r
| [#begin trans*:rs] -> ('{' + rs.join(';') + '}')
| trans:r -> ('{' + r + '}'),
this -> 'this',
break -> 'break',
continue -> 'continue',
number :n -> ('(' + n + ')'),
string :s -> s.toProgramString(),
regExpr :x -> x,
arr trans*:xs -> ('[' + xs.join(',') + ']'),
unop :op trans:x -> ('(' + op + ' ' + x + ')'),
getp trans:fd trans:x -> (x + '[' + fd + ']'),
get :x -> x,
set trans:lhs trans:rhs -> ('(' + lhs + '=' + rhs + ')'),
mset trans:lhs :op trans:rhs -> ('(' + lhs + op + '=' + rhs + ')'),
binop :op trans:x trans:y -> ('(' + x + ' ' + op + ' ' + y + ')'),
preop :op trans:x -> (op + x),
postop :op trans:x -> (x + op),
return trans:x -> ('return ' + x),
with trans:x curlyTrans:s -> ('with(' + x + ')' + s),
if trans:cond curlyTrans:t curlyTrans:e -> ('if(' + cond + ')' + t + 'else' + e),
condExpr trans:cond trans:t trans:e -> ('(' + cond + '?' + t + ':' + e + ')'),
while trans:cond curlyTrans:body -> ('while(' + cond + ')' + body),
doWhile curlyTrans:body trans:cond -> ('do' + body + 'while(' + cond + ')'),
for trans:init trans:cond trans:upd
curlyTrans:body -> ('for(' + init + ';' + cond + ';' + upd + ')' + body),
forIn trans:x trans:arr curlyTrans:body -> ('for(' + x + ' in ' + arr + ')' + body),
begin trans:x end -> x,
begin (trans:x
( (?(x[x.length - 1] == '}') | end) -> x
| empty -> (x + ';')
)
)*:xs -> ('{' + xs.join('') + '}'),
func :args curlyTrans:body -> ('(function (' + args.join(',') + ')' + body + ')'),
call trans:fn trans*:args -> (fn + '(' + args.join(',') + ')'),
send :msg trans:recv trans*:args -> (recv + '.' + msg + '(' + args.join(',') + ')'),
new :cls trans*:args -> ('new ' + cls + '(' + args.join(',') + ')'),
var :name trans:val -> ('var ' + name + '=' + val),
throw trans:x -> ('throw ' + x),
try curlyTrans:x :name curlyTrans:c curlyTrans:f -> ('try ' + x + 'catch(' + name + ')' + c + 'finally' + f),
json trans*:props -> ('({' + props.join(',') + '})'),
binding :name trans:val -> (name.toProgramString() + ': ' + val),
switch trans:x trans*:cases -> ('switch(' + x + '){' + cases.join(';') + '}'),
case trans:x trans:y -> ('case ' + x + ': '+ y),
default trans:y -> ('default: ' + y)
}