-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf.html
278 lines (268 loc) · 6.98 KB
/
bf.html
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
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Syntax validator</title>
<link rel='shortcut icon' type=image/x-icon href=data:image/x-icon;,>
<meta property='og:description' content='Parametric parser for Web0/JavaScript0' name='description'>
<meta name=viewport content='width=device-width, initial-scale=1'>
<meta name=referrer content=no-referrer>
<meta http-equiv=x-dns-prefetch-control content=off>
<meta http-equiv=Content-Security-Policy content="default-src 'none'; font-src data: blob:; frame-src data: blob:; img-src data: blob:; manifest-src data: blob:; media-src data: blob:; style-src data: blob: 'unsafe-inline' 'unsafe-eval'; script-src 'unsafe-inline'; base-uri 'none'; form-action javascript:; navigate-to 'none';">
<style>
html, textarea, input {
background-color: black;
color: white;
}
textarea {
min-width: 100%;
max-width: 100%;
min-height: 4em;
}
</style>
</head>
<body>
<script>
'use strict';
var tab = String.fromCharCode(9);
var nl = String.fromCharCode(10);
var bs = String.fromCharCode(92);
function reload(input, out) {
var typ = '';
if (document.doctype && (document.doctype.name !== undefined)) {
typ = '<!DOCTYPE ' + document.doctype.name + '>';
}
var head = '';
if (document.head && document.head.innerHTML && String.prototype && String.prototype.match) {
head = document.head.innerHTML;
var m = head.match('^((?:.|' + bs + 'n)*)<[^>]+http-equiv=["' + "'" + ']?Content-Security-Policy[^>]*>((?:.|' + bs + 'n)*)$');
if (m) {
head = m[1] + m[2];
}
}
document.close();
document.write(typ + '<html lang=en><head>' + head + '</head><body><pre>' + out + '</pre><form name=f action="javascript:onSubmit()"><textarea name=t>' + input + '</textarea><input type=submit></form></body></html>');
document.close();
if (window.f && window.f.t && window.f.t.focus) {
window.f.t.focus();
}
}
function jsBf() {
return (
{
'_': [
'statements'
],
'statements': [
[
'whitespace'
],
[
'statement'
],
[
'moreStatements'
],
[
'whitespace'
]
],
'moreStatements': [
0,
[
';',
[
'statements'
]
],
''
],
'statement': [
0,
[
'while(v[i]){',
[
'statements'
],
'}'
],
[
[
0,
'console.log',
'document.write'
],
'(String.fromCharCode(v[i]))'
],
[
'var ',
[
0,
'v',
'i',
'j'
]
],
[
0,
[
'scalarAssignment'
],
[
'arrayAssignment'
]
]
],
'scalarAssignment': [
[
0,
'i',
'j'
],
[
0,
'=0',
'++',
'--'
]
],
'arrayAssignment': [
'v',
[
0,
'=new Array',
[
'[i]=',
[
0,
[
'(v[i]|0)',
[
0,
'+',
'-'
],
'1'
],
'form.text.value.charCodeAt(j)|0'
]
]
]
],
'whitespace': [
0,
[
[
0,
tab,
nl,
' '
],
[
'whitespace'
]
],
''
]
}
);
}
function parse(self, g, r, s, pos, out, lim) {
if (lim <= 0) {
out.t = out.t + 'internal error: grammar recursion depth exceeded at position ' + pos + nl;
return undefined;
}
lim = lim - 1;
var i = 0;
var next;
if (typeof r === 'string') {
while ((i < r.length) && (pos < s.length) && (r.charAt(i) === s.charAt(pos))) {
pos = pos + 1;
i = i + 1;
}
if (i < r.length) {
return undefined;
}
out.t = out.t + 'debug: match literal "' + r + '" on level ' + lim + ' at position ' + pos + nl;
return pos;
} else if (typeof r !== 'object') {
out.t = out.t + 'internal error: tried to apply non-Object rule (' + (typeof r) + ') on level ' + lim + ' at position ' + pos + nl;
return undefined;
} else if (r.length === undefined) {
out.t = out.t + 'internal error: tried to apply non-Array rule on level ' + lim + ' at position ' + pos + nl;
return undefined;
} else if (r.length === 0) {
out.t = out.t + 'internal error: tried to apply empty rule on level ' + lim + ' at position ' + pos + nl;
return undefined;
} else if (r.length === 1) {
var rule = r[0];
if (g[rule] === undefined) {
out.t = out.t + 'internal error: tried to apply unknown rule "' + rule + '" on level ' + lim + ' at position ' + pos + nl;
return undefined;
}
out.t = out.t + 'debug: descend rule "' + r[0] + '" on level ' + lim + ' at position ' + pos + nl;
return self(self, g, g[rule], s, pos, out, lim);
} else if (r[0] === 0) {
i = i + 1;
while (i < r.length) {
next = self(self, g, r[i], s, pos, out, lim);
if (next !== undefined) {
out.t = out.t + 'debug: match either term ' + (i - 1) + ' on level ' + lim + ' at position ' + next + nl;
return next;
}
i = i + 1;
}
out.t = out.t + 'debug: mismatch either term ' + (i - 1) + ' on level ' + lim + ' at position ' + pos + nl;
return undefined;
} else {
while (i < r.length) {
next = self(self, g, r[i], s, pos, out, lim);
if (next === undefined) {
out.t = out.t + 'debug: mismatch seq term ' + i + ' on level ' + lim + ' at position ' + pos + nl;
return undefined;
}
pos = next;
i = i + 1;
}
out.t = out.t + 'debug: match seq term ' + (i - 1) + ' on level ' + lim + ' at position ' + pos + nl;
return pos;
}
}
function action(input) {
var out = new Object;
out.t = '';
var g = jsBf();
var pos = parse(parse, g, g['_'], input, 0, out, (3 * input.length) + 42);
if (pos === input.length) {
out.t = out.t + 'Parsed successfully' + nl;
} else {
if (pos === undefined) {
pos = 0;
}
out.t = out.t + 'Parsing failed at position ' + pos + nl;
var i = 0;
while ((i < 32) && (pos < input.length)) {
out.t = out.t + input[pos];
pos = pos + 1;
i = i + 1;
}
out.t = out.t + '...' + nl;
}
reload(input, out.t);
}
function onSubmit() {
try {
action(window.f.t.value);
} catch (e) {
console.log(e + nl);
throw(e);
}
return undefined;
}
function init() {
action(' var i; i=0;var j;j=0;var v;v=new Array;i++;v[i]=form.text.value.charCodeAt(j)|0;while(v[i]){v[i]=(v[i]|0)+1;i++;j++;v[i]=form.text.value.charCodeAt(j)|0};i--;while(v[i]){i--};i++;while(v[i]){document.write(String.fromCharCode(v[i]));i++} ');
}
init();
</script>
</body>
</html>