-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver_rhino.js_
370 lines (292 loc) · 8.02 KB
/
driver_rhino.js_
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
// start closure
// ignore the warning below, i know what i'm doing :)
var Py2Web = function($) {
/*
Default driver template for JS/CC generated parsers for Mozilla/Rhino
WARNING: Do not use for parsers that should run as browser-based JavaScript!
Use driver_web.js_ instead!
Features:
- Parser trace messages
- Step-by-step parsing
- Integrated panic-mode error recovery
- Pseudo-graphical parse tree generation
Written 2007 by Jan Max Meyer, J.M.K S.F. Software Technologies
Modified 2007 from driver.js_ to support Mozilla/Rhino
by Louis P.Santillan <[email protected]>
This is in the public domain.
*/
##HEADER##
var ##PREFIX##_dbg_withparsetree = false;
var ##PREFIX##_dbg_withtrace = false;
var ##PREFIX##_dbg_withstepbystep = false;
function __##PREFIX##dbg_print( text )
{
print( text );
}
function __##PREFIX##dbg_wait()
{
var kbd = new java.io.BufferedReader(
new java.io.InputStreamReader( java.lang.System[ "in" ] ) );
kbd.readLine();
}
function __##PREFIX##lex( info )
{
var state = 0;
var match = -1;
var match_pos = 0;
var start = 0;
var pos = info.offset + 1;
do
{
pos--;
state = 0;
match = -2;
start = pos;
if( info.src.length <= start )
return ##EOF##;
do
{
##DFA##
pos++;
}
while( state > -1 );
}
while( ##WHITESPACE## > -1 && match == ##WHITESPACE## );
if( match > -1 )
{
info.att = info.src.substr( start, match_pos - start );
info.offset = match_pos;
##TERMINAL_ACTIONS##
}
else
{
info.att = new String();
match = -1;
}
return match;
}
function __##PREFIX##parse( src, err_off, err_la )
{
var sstack = new Array();
var vstack = new Array();
var err_cnt = 0;
var act;
var go;
var la;
var rval;
var parseinfo = new Function( "", "var offset; var src; var att;" );
var info = new parseinfo();
//Visual parse tree generation
var treenode = new Function( "", "var sym; var att; var child;" );
var treenodes = new Array();
var tree = new Array();
var tmptree = null;
##TABLES##
##LABELS##
info.offset = 0;
info.src = src;
info.att = new String();
if( !err_off )
err_off = new Array();
if( !err_la )
err_la = new Array();
sstack.push( 0 );
vstack.push( 0 );
la = __##PREFIX##lex( info );
while( true )
{
act = ##ERROR##;
for( var i = 0; i < act_tab[sstack[sstack.length-1]].length; i+=2 )
{
if( act_tab[sstack[sstack.length-1]][i] == la )
{
act = act_tab[sstack[sstack.length-1]][i+1];
break;
}
}
/*
_print( "state " + sstack[sstack.length-1] + " la = " + la + " info.att = >" +
info.att + "< act = " + act + " src = >" + info.src.substr( info.offset, 30 ) + "..." + "<" +
" sstack = " + sstack.join() );
*/
if( ##PREFIX##_dbg_withtrace && sstack.length > 0 )
{
__##PREFIX##dbg_print( "\nState " + sstack[sstack.length-1] + "\n" +
"\tLookahead: " + labels[la] + " (\"" + info.att + "\")\n" +
"\tAction: " + act + "\n" +
"\tSource: \"" + info.src.substr( info.offset, 30 ) + ( ( info.offset + 30 < info.src.length ) ?
"..." : "" ) + "\"\n" +
"\tStack: " + sstack.join() + "\n" +
"\tValue stack: " + vstack.join() + "\n" );
if( ##PREFIX##_dbg_withstepbystep )
__##PREFIX##dbg_wait();
}
//Panic-mode: Try recovery when parse-error occurs!
if( act == ##ERROR## )
{
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "Error detected: There is no reduce or shift on the symbol " + labels[la] );
err_cnt++;
err_off.push( info.offset - info.att.length );
err_la.push( new Array() );
for( var i = 0; i < act_tab[sstack[sstack.length-1]].length; i+=2 )
err_la[err_la.length-1].push( labels[act_tab[sstack[sstack.length-1]][i]] );
//Remember the original stack!
var rsstack = new Array();
var rvstack = new Array();
for( var i = 0; i < sstack.length; i++ )
{
rsstack[i] = sstack[i];
rvstack[i] = vstack[i];
}
while( act == ##ERROR## && la != ##EOF## )
{
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\tError recovery\n" +
"Current lookahead: " + labels[la] + " (" + info.att + ")\n" +
"Action: " + act + "\n\n" );
if( la == -1 )
info.offset++;
while( act == ##ERROR## && sstack.length > 0 )
{
sstack.pop();
vstack.pop();
if( sstack.length == 0 )
break;
act = ##ERROR##;
for( var i = 0; i < act_tab[sstack[sstack.length-1]].length; i+=2 )
{
if( act_tab[sstack[sstack.length-1]][i] == la )
{
act = act_tab[sstack[sstack.length-1]][i+1];
break;
}
}
}
if( act != ##ERROR## )
break;
for( var i = 0; i < rsstack.length; i++ )
{
sstack.push( rsstack[i] );
vstack.push( rvstack[i] );
}
la = __##PREFIX##lex( info );
}
if( act == ##ERROR## )
{
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\tError recovery failed, terminating parse process..." );
break;
}
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\tError recovery succeeded, continuing" );
}
/*
if( act == ##ERROR## )
break;
*/
//Shift
if( act > 0 )
{
//Parse tree generation
if( ##PREFIX##_dbg_withparsetree )
{
var node = new treenode();
node.sym = labels[ la ];
node.att = info.att;
node.child = new Array();
tree.push( treenodes.length );
treenodes.push( node );
}
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "Shifting symbol: " + labels[la] + " (" + info.att + ")" );
sstack.push( act );
vstack.push( info.att );
la = __##PREFIX##lex( info );
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\tNew lookahead symbol: " + labels[la] + " (" + info.att + ")" );
}
//Reduce
else
{
act *= -1;
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "Reducing by producution: " + act );
rval = void(0);
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\tPerforming semantic action..." );
##ACTIONS##
if( ##PREFIX##_dbg_withparsetree )
tmptree = new Array();
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\tPopping " + pop_tab[act][1] + " off the stack..." );
for( var i = 0; i < pop_tab[act][1]; i++ )
{
if( ##PREFIX##_dbg_withparsetree )
tmptree.push( tree.pop() );
sstack.pop();
vstack.pop();
}
go = -1;
for( var i = 0; i < goto_tab[sstack[sstack.length-1]].length; i+=2 )
{
if( goto_tab[sstack[sstack.length-1]][i] == pop_tab[act][0] )
{
go = goto_tab[sstack[sstack.length-1]][i+1];
break;
}
}
if( ##PREFIX##_dbg_withparsetree )
{
var node = new treenode();
node.sym = labels[ pop_tab[act][0] ];
node.att = new String();
node.child = tmptree.reverse();
tree.push( treenodes.length );
treenodes.push( node );
}
if( act == 0 )
break;
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\tPushing non-terminal " + labels[ pop_tab[act][0] ] );
sstack.push( go );
vstack.push( rval );
}
}
if( ##PREFIX##_dbg_withtrace )
__##PREFIX##dbg_print( "\nParse complete." );
if( ##PREFIX##_dbg_withparsetree )
{
if( err_cnt == 0 )
{
__##PREFIX##dbg_print( "\n\n--- Parse tree ---" );
__##PREFIX##dbg_parsetree( 0, treenodes, tree );
}
else
{
__##PREFIX##dbg_print( "\n\nParse tree cannot be viewed. There where parse errors." );
}
}
return err_cnt;
}
function __##PREFIX##dbg_parsetree( indent, nodes, tree )
{
var str = new String();
for( var i = 0; i < tree.length; i++ )
{
str = "";
for( var j = indent; j > 0; j-- )
str += "\t";
str += nodes[ tree[i] ].sym;
if( nodes[ tree[i] ].att != "" )
str += " >" + nodes[ tree[i] ].att + "<" ;
__##PREFIX##dbg_print( str );
if( nodes[ tree[i] ].child.length > 0 )
__##PREFIX##dbg_parsetree( indent + 1, nodes, nodes[ tree[i] ].child );
}
}
##FOOTER##
} (jQuery) // end and invoke closure
jQuery(document).ready(function() {
Py2Web.init("body")
})