• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4<meta http-equiv="content-type" content="text/html;charset=utf-8" />
5<title>t057autoAST</title>
6
7<!-- ANTLR includes -->
8<script type="text/javascript" src="../../lib/antlr3-all.js"></script>
9
10<script type="text/javascript" src="t057autoAST1Lexer.js"></script>
11<script type="text/javascript" src="t057autoAST1Parser.js"></script>
12<script type="text/javascript" src="t057autoAST2Lexer.js"></script>
13<script type="text/javascript" src="t057autoAST2Parser.js"></script>
14<script type="text/javascript" src="t057autoAST3Lexer.js"></script>
15<script type="text/javascript" src="t057autoAST3Parser.js"></script>
16<script type="text/javascript" src="t057autoAST4Lexer.js"></script>
17<script type="text/javascript" src="t057autoAST4Parser.js"></script>
18<script type="text/javascript" src="t057autoAST5Lexer.js"></script>
19<script type="text/javascript" src="t057autoAST5Parser.js"></script>
20<script type="text/javascript" src="t057autoAST6Lexer.js"></script>
21<script type="text/javascript" src="t057autoAST6Parser.js"></script>
22<script type="text/javascript" src="t057autoAST7Lexer.js"></script>
23<script type="text/javascript" src="t057autoAST7Parser.js"></script>
24<script type="text/javascript" src="t057autoAST8Lexer.js"></script>
25<script type="text/javascript" src="t057autoAST8Parser.js"></script>
26<script type="text/javascript" src="t057autoAST9Lexer.js"></script>
27<script type="text/javascript" src="t057autoAST9Parser.js"></script>
28<script type="text/javascript" src="t057autoAST10Lexer.js"></script>
29<script type="text/javascript" src="t057autoAST10Parser.js"></script>
30<script type="text/javascript" src="t057autoAST11Lexer.js"></script>
31<script type="text/javascript" src="t057autoAST11Parser.js"></script>
32<script type="text/javascript" src="t057autoAST12Lexer.js"></script>
33<script type="text/javascript" src="t057autoAST12Parser.js"></script>
34<script type="text/javascript" src="t057autoAST13Lexer.js"></script>
35<script type="text/javascript" src="t057autoAST13Parser.js"></script>
36<script type="text/javascript" src="t057autoAST14Lexer.js"></script>
37<script type="text/javascript" src="t057autoAST14Parser.js"></script>
38<script type="text/javascript" src="t057autoAST15Lexer.js"></script>
39<script type="text/javascript" src="t057autoAST15Parser.js"></script>
40<script type="text/javascript" src="t057autoAST16Lexer.js"></script>
41<script type="text/javascript" src="t057autoAST16Parser.js"></script>
42<script type="text/javascript" src="t057autoAST17Lexer.js"></script>
43<script type="text/javascript" src="t057autoAST17Parser.js"></script>
44<script type="text/javascript" src="t057autoAST18Lexer.js"></script>
45<script type="text/javascript" src="t057autoAST18Parser.js"></script>
46<script type="text/javascript" src="t057autoAST19Lexer.js"></script>
47<script type="text/javascript" src="t057autoAST19Parser.js"></script>
48<script type="text/javascript" src="t057autoAST20Lexer.js"></script>
49<script type="text/javascript" src="t057autoAST20Parser.js"></script>
50<script type="text/javascript" src="t057autoAST21Lexer.js"></script>
51<script type="text/javascript" src="t057autoAST21Parser.js"></script>
52<script type="text/javascript" src="t057autoAST22Lexer.js"></script>
53<script type="text/javascript" src="t057autoAST22Parser.js"></script>
54<script type="text/javascript" src="t057autoAST23Lexer.js"></script>
55<script type="text/javascript" src="t057autoAST23Parser.js"></script>
56<script type="text/javascript" src="t057autoAST24Lexer.js"></script>
57<script type="text/javascript" src="t057autoAST24Parser.js"></script>
58<script type="text/javascript" src="t057autoAST25Lexer.js"></script>
59<script type="text/javascript" src="t057autoAST25Parser.js"></script>
60<script type="text/javascript" src="t057autoAST26Lexer.js"></script>
61<script type="text/javascript" src="t057autoAST26Parser.js"></script>
62<script type="text/javascript" src="t057autoAST27Lexer.js"></script>
63<script type="text/javascript" src="t057autoAST27Parser.js"></script>
64<script type="text/javascript" src="t057autoAST28Lexer.js"></script>
65<script type="text/javascript" src="t057autoAST28Parser.js"></script>
66<script type="text/javascript" src="t057autoAST29Lexer.js"></script>
67<script type="text/javascript" src="t057autoAST29Parser.js"></script>
68<script type="text/javascript" src="t057autoAST30Lexer.js"></script>
69<script type="text/javascript" src="t057autoAST30Parser.js"></script>
70<script type="text/javascript" src="t057autoAST31Lexer.js"></script>
71<script type="text/javascript" src="t057autoAST31Parser.js"></script>
72<script type="text/javascript" src="t057autoAST32Lexer.js"></script>
73<script type="text/javascript" src="t057autoAST32Parser.js"></script>
74<script type="text/javascript" src="t057autoAST33Lexer.js"></script>
75<script type="text/javascript" src="t057autoAST33Parser.js"></script>
76<script type="text/javascript" src="t057autoAST34Lexer.js"></script>
77<script type="text/javascript" src="t057autoAST34Parser.js"></script>
78<script type="text/javascript" src="t057autoAST35Lexer.js"></script>
79<script type="text/javascript" src="t057autoAST35Parser.js"></script>
80<script type="text/javascript" src="t057autoAST36Lexer.js"></script>
81<script type="text/javascript" src="t057autoAST36Parser.js"></script>
82<script type="text/javascript" src="t057autoAST37Lexer.js"></script>
83<script type="text/javascript" src="t057autoAST37Parser.js"></script>
84<script type="text/javascript" src="t057autoAST38Lexer.js"></script>
85<script type="text/javascript" src="t057autoAST38Parser.js"></script>
86<script type="text/javascript" src="t057autoAST39Lexer.js"></script>
87<script type="text/javascript" src="t057autoAST39Parser.js"></script>
88<script type="text/javascript" src="t057autoAST40Lexer.js"></script>
89<script type="text/javascript" src="t057autoAST40Parser.js"></script>
90<script type="text/javascript" src="t057autoAST41Lexer.js"></script>
91<script type="text/javascript" src="t057autoAST41Parser.js"></script>
92<script type="text/javascript" src="t057autoAST42Lexer.js"></script>
93<script type="text/javascript" src="t057autoAST42Parser.js"></script>
94<script type="text/javascript" src="t057autoAST43Lexer.js"></script>
95<script type="text/javascript" src="t057autoAST43Parser.js"></script>
96<script type="text/javascript" src="t057autoAST44Lexer.js"></script>
97<script type="text/javascript" src="t057autoAST44Parser.js"></script>
98<script type="text/javascript" src="t057autoAST45Lexer.js"></script>
99<script type="text/javascript" src="t057autoAST45Parser.js"></script>
100<script type="text/javascript" src="t057autoAST46Lexer.js"></script>
101<script type="text/javascript" src="t057autoAST46Parser.js"></script>
102<script type="text/javascript" src="t057autoAST47Lexer.js"></script>
103<script type="text/javascript" src="t057autoAST47Parser.js"></script>
104<script type="text/javascript" src="t057autoAST48Lexer.js"></script>
105<script type="text/javascript" src="t057autoAST48Parser.js"></script>
106<script type="text/javascript" src="t057autoAST49Lexer.js"></script>
107<script type="text/javascript" src="t057autoAST49Parser.js"></script>
108<script type="text/javascript" src="t057autoAST50Lexer.js"></script>
109<script type="text/javascript" src="t057autoAST50Parser.js"></script>
110<script type="text/javascript" src="t057autoAST51Lexer.js"></script>
111<script type="text/javascript" src="t057autoAST51Parser.js"></script>
112<script type="text/javascript" src="t057autoAST52Lexer.js"></script>
113<script type="text/javascript" src="t057autoAST52Parser.js"></script>
114<script type="text/javascript" src="t057autoAST53Lexer.js"></script>
115<script type="text/javascript" src="t057autoAST53Parser.js"></script>
116<script type="text/javascript" src="t057autoAST54Lexer.js"></script>
117<script type="text/javascript" src="t057autoAST54Parser.js"></script>
118<script type="text/javascript" src="t057autoAST55Lexer.js"></script>
119<script type="text/javascript" src="t057autoAST55Parser.js"></script>
120<script type="text/javascript" src="t057autoAST56Lexer.js"></script>
121<script type="text/javascript" src="t057autoAST56Parser.js"></script>
122<script type="text/javascript" src="t057autoAST57Lexer.js"></script>
123<script type="text/javascript" src="t057autoAST57Parser.js"></script>
124
125<!-- JsUnit include -->
126<script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>
127
128<!-- Test Code -->
129<script type="text/javascript">
130    // Parser Tests
131
132    function execParser(lexerCls, parserCls, grammarEntry, xinput, expectErrors) {
133        var cstream = new org.antlr.runtime.ANTLRStringStream(xinput),
134            lexer = new (EnhancedParser(lexerCls))(cstream),
135            tstream = new org.antlr.runtime.CommonTokenStream(lexer),
136            parser = new (EnhancedParser(parserCls))(tstream);
137        var r = parser[grammarEntry]();
138
139        if (!expectErrors) {
140            assertEquals(parser._errors.length, 0);
141        }
142
143        var result = "";
144        if (r) {
145            if (r.result) {
146                result += r.result
147            }
148            if (r.getTree()) {
149                result += r.getTree().toStringTree();
150            }
151        }
152
153        if (!expectErrors) {
154            return result;
155        } else {
156            return [result, parser._errors];
157        }
158    }
159
160    function EnhancedParser(base) {
161        var T = function() {
162            T.superclass.constructor.apply(this, arguments);
163            this.traces = [];
164            this.buf = "";
165            this._errors = [];
166        };
167
168        org.antlr.lang.extend(T, base, {
169            capture: function(t) {
170                this.buf += t;
171            },
172            traceIn: function(ruleName, ruleIndex) {
173                this.traces.push(">"+ruleName);
174            },
175            traceOut: function(ruleName, ruleIndex) {
176                this.traces.push("<"+ruleName);
177            },
178            emitErrorMessage: function(msg) {
179                this._errors.push(msg);
180            }
181        });
182
183        return T;
184    }
185
186function testTokenList() {
187    var found = execParser(t057autoAST1Lexer, t057autoAST1Parser, "a", "abc 34");
188    assertEquals("abc 34", found);
189}
190
191function testTokenListInSingleAltBlock() {
192    var found = execParser(t057autoAST2Lexer, t057autoAST2Parser,"a", "abc 34");
193    assertEquals("abc 34", found);
194}
195
196function testSimpleRootAtOuterLevel() {
197    var found = execParser(t057autoAST3Lexer, t057autoAST3Parser, "a", "abc 34");
198    assertEquals("(abc 34)", found);
199}
200
201function testSimpleRootAtOuterLevelReverse() {
202    var found = execParser(t057autoAST4Lexer, t057autoAST4Parser, "a", "34 abc");
203    assertEquals("(abc 34)", found);
204}
205
206function testBang() {
207    var found = execParser(t057autoAST5Lexer, t057autoAST5Parser, "a", "abc 34 dag 4532");
208    assertEquals("abc 4532", found);
209}
210
211function testOptionalThenRoot() {
212    var found = execParser(t057autoAST6Lexer, t057autoAST6Parser, "a", "a 1 b");
213    assertEquals("(b a 1)", found);
214}
215
216function testLabeledStringRoot() {
217    var found = execParser(t057autoAST7Lexer, t057autoAST7Parser, "a", "void foo;");
218    assertEquals("(void foo ;)", found);
219}
220
221function testWildcard() {
222    var found = execParser(t057autoAST8Lexer, t057autoAST8Parser, "a", "void foo;");
223    assertEquals("(void foo ;)", found);
224}
225
226function testWildcardRoot() {
227    var found = execParser(t057autoAST9Lexer, t057autoAST9Parser, "a", "void foo;");
228    assertEquals("(foo void ;)", found);
229}
230
231function testWildcardRootWithLabel() {
232    var found = execParser(t057autoAST10Lexer, t057autoAST10Parser, "a", "void foo;");
233    assertEquals("(foo void ;)", found);
234}
235
236function testWildcardRootWithListLabel() {
237    var found = execParser(t057autoAST11Lexer, t057autoAST11Parser, "a", "void foo;");
238    assertEquals("(foo void ;)", found);
239}
240
241function testRootRoot() {
242    var found = execParser(t057autoAST12Lexer, t057autoAST12Parser, "a", "a 34 c");
243    assertEquals("(34 a c)", found);
244}
245
246function testRootRoot2() {
247    var found = execParser(t057autoAST13Lexer, t057autoAST13Parser, "a", "a 34 c");
248    assertEquals("(c (34 a))", found);
249}
250
251function testRootThenRootInLoop() {
252    var found = execParser(t057autoAST14Lexer, t057autoAST14Parser, "a", "a 34 * b 9 * c");
253    assertEquals("(* (* (a 34) b 9) c)", found);
254}
255
256function testNestedSubrule() {
257    var found = execParser(t057autoAST15Lexer, t057autoAST15Parser, "a", "void a b;");
258    assertEquals("void a b ;", found);
259}
260
261function testInvokeRule() {
262    var found = execParser(t057autoAST16Lexer, t057autoAST16Parser, "a", "int a");
263    assertEquals("int a", found);
264}
265
266function testInvokeRuleAsRoot() {
267    var found = execParser(t057autoAST17Lexer, t057autoAST17Parser, "a", "int a");
268    assertEquals("(int a)", found);
269}
270
271function testInvokeRuleAsRootWithLabel() {
272    var found = execParser(t057autoAST18Lexer, t057autoAST18Parser, "a", "int a");
273    assertEquals("(int a)", found);
274}
275
276function testInvokeRuleAsRootWithListLabel() {
277    var found = execParser(t057autoAST19Lexer, t057autoAST19Parser, "a", "int a");
278    assertEquals("(int a)", found);
279}
280
281function testRuleRootInLoop() {
282    var found = execParser(t057autoAST20Lexer, t057autoAST20Parser, "a", "a+b+c+d");
283    assertEquals("(+ (+ (+ a b) c) d)", found);
284}
285
286function testRuleInvocationRuleRootInLoop() {
287    var found = execParser(t057autoAST21Lexer, t057autoAST21Parser, "a", "a+b+c-d");
288    assertEquals("(- (+ (+ a b) c) d)", found);
289}
290
291function testTailRecursion() {
292    var found = execParser(t057autoAST22Lexer, t057autoAST22Parser, "s", "3 exp 4 exp 5");
293    assertEquals("(exp 3 (exp 4 5))", found);
294}
295
296function testSet() {
297    var found = execParser(t057autoAST23Lexer, t057autoAST23Parser, "a", "abc");
298    assertEquals("abc", found);
299}
300
301function testSetRoot() {
302    var found = execParser(t057autoAST24Lexer, t057autoAST24Parser, "a", "+abc");
303    assertEquals("(+ abc)", found);
304}
305
306//@testbase.broken("FAILS until antlr.g rebuilt in v3", RuntimeError);
307function testSetRootWithLabel() {
308    var found = execParser(t057autoAST25Lexer, t057autoAST25Parser, "a", "+abc");
309    assertEquals("(+ abc)", found);
310}
311
312function testSetAsRuleRootInLoop() {
313    var found = execParser(t057autoAST26Lexer, t057autoAST26Parser, "a", "a+b-c");
314    assertEquals("(- (+ a b) c)", found);
315}
316
317function testNotSet() {
318    var found = execParser(t057autoAST27Lexer, t057autoAST27Parser, "a", "34+2");
319    assertEquals("34 + 2", found);
320}
321
322function testNotSetWithLabel() {
323    var found = execParser(t057autoAST28Lexer, t057autoAST28Parser, "a", "34+2");
324    assertEquals("34 + 2", found);
325}
326
327function testNotSetWithListLabel() {
328    var found = execParser(t057autoAST29Lexer, t057autoAST29Parser, "a", "34+2");
329    assertEquals("34 + 2", found);
330}
331
332function testNotSetRoot() {
333    var found = execParser(t057autoAST30Lexer, t057autoAST30Parser, "a", "34 55");
334    assertEquals("(34 55)", found);
335}
336
337function testNotSetRootWithLabel() {
338    var found = execParser(t057autoAST31Lexer, t057autoAST31Parser, "a", "34 55");
339    assertEquals("(34 55)", found);
340}
341
342function testNotSetRootWithListLabel() {
343    var found = execParser(t057autoAST32Lexer, t057autoAST32Parser, "a", "34 55");
344    assertEquals("(34 55)", found);
345}
346
347function testNotSetRuleRootInLoop() {
348    var found = execParser(t057autoAST33Lexer, t057autoAST33Parser, "a", "3+4+5");
349    assertEquals("(+ (+ 3 4) 5)", found);
350}
351
352//@testbase.broken("FIXME: What happened to the semicolon?", AssertionError);
353function testTokenLabelReuse() {
354// check for compilation problem due to multiple defines
355    var found = execParser(t057autoAST34Lexer, t057autoAST34Parser, "a", "a b");
356    assertEquals("2nd id=b;a b", found);
357}
358
359function testTokenLabelReuse2() {
360// check for compilation problem due to multiple defines
361    var found = execParser(t057autoAST35Lexer, t057autoAST35Parser, "a", "a b");
362    assertEquals("2nd id=b,(b a)", found);
363}
364
365function testTokenListLabelReuse() {
366// check for compilation problem due to multiple defines
367// make sure ids has both ID tokens
368    var found = execParser(t057autoAST36Lexer, t057autoAST36Parser, "a", "a b");
369    expecting = "id list=[a,b],a b"
370        assertEquals(expecting, found);
371}
372
373function testTokenListLabelReuse2() {
374// check for compilation problem due to multiple defines
375// make sure ids has both ID tokens
376    var found = execParser(t057autoAST37Lexer, t057autoAST37Parser, "a", "a b");
377    expecting = "id list=[a,b],(a b)"
378        assertEquals(expecting, found);
379}
380
381function testTokenListLabelRuleRoot() {
382    var found = execParser(t057autoAST38Lexer, t057autoAST38Parser, "a", "a");
383    assertEquals("a", found);
384}
385
386function testTokenListLabelBang() {
387    var found = execParser(t057autoAST39Lexer, t057autoAST39Parser, "a", "a");
388    assertEquals("", found);
389}
390
391function testRuleListLabel() {
392    var found = execParser(t057autoAST40Lexer, t057autoAST40Parser, "a", "a b");
393    assertEquals("2nd x=b,a b", found);
394}
395
396function testRuleListLabelRuleRoot() {
397    var found = execParser(t057autoAST41Lexer, t057autoAST41Parser, "a", "a b");
398    assertEquals("x=(b a),(b a)", found);
399}
400
401function testRuleListLabelBang() {
402    var found = execParser(t057autoAST42Lexer, t057autoAST42Parser, "a", "a b");
403    assertEquals("1st x=a,b", found);
404}
405
406function testComplicatedMelange() {
407// check for compilation problem
408    var found = execParser(t057autoAST43Lexer, t057autoAST43Parser, "a", "a b b c c d");
409    assertEquals("a b b c c d", found);
410}
411
412function testReturnValueWithAST() {
413    var found = execParser(t057autoAST44Lexer, t057autoAST44Parser, "a", "abc 34");
414    assertEquals("34\nabc 34", found);
415}
416
417function testSetLoop() {
418    var found = execParser(t057autoAST45Lexer, t057autoAST45Parser, "r", "abc 34 d");
419    assertEquals("abc 34 d", found);
420}
421
422function testExtraTokenInSimpleDecl() {
423    found, errors = execParser(t057autoAST46Lexer, t057autoAST46Parser, "decl", "int 34 x=1;",
424            true);
425    assertEquals(["line 1:4 extraneous input u'34' expecting ID"],
426            errors);
427    assertEquals("(int x 1)", found); // tree gets correct x and 1 tokens
428}
429
430function testMissingIDInSimpleDecl() {
431    found, errors = execParser(t057autoAST47Lexer, t057autoAST47Parser, "decl", "int =1;",
432            true);
433    assertEquals(["line 1:4 missing ID at u'='"], errors);
434    assertEquals("(int <missing ID> 1)", found); // tree gets invented ID token
435}
436
437function testMissingSetInSimpleDecl() {
438    found, errors = execParser(t057autoAST48Lexer, t057autoAST48Parser, "decl", "x=1;",
439            true);
440    assertEquals(["line 1:0 mismatched input u'x' expecting set None"], errors);
441    assertEquals("(<error: x> x 1)", found); // tree gets invented ID token
442}
443
444function testMissingTokenGivesErrorNode() {
445    found, errors = execParser(t057autoAST49Lexer, t057autoAST49Parser, "a", "abc", true);
446    assertEquals(["line 0:-1 missing INT at '<EOF>'"], errors);
447    assertEquals("abc <missing INT>", found);
448}
449
450function testMissingTokenGivesErrorNodeInInvokedRule() {
451    found, errors = execParser(t057autoAST50Lexer, t057autoAST50Parser, "a", "abc", true);
452    assertEquals(["line 0:-1 missing INT at '<EOF>'"], errors);
453    assertEquals("abc <missing INT>", found);
454}
455
456function testExtraTokenGivesErrorNode() {
457    found, errors = execParser(t057autoAST51Lexer, t057autoAST51Parser, "a", "abc ick 34",
458            true);
459    assertEquals(["line 1:4 extraneous input u'ick' expecting INT"],
460            errors);
461    assertEquals("abc 34", found);
462}
463
464function testMissingFirstTokenGivesErrorNode() {
465    found, errors = execParser(t057autoAST52Lexer, t057autoAST52Parser, "a", "34", true);
466    assertEquals(["line 1:0 missing ID at u'34'"], errors);
467    assertEquals("<missing ID> 34", found);
468}
469
470function testMissingFirstTokenGivesErrorNode2() {
471    found, errors = execParser(t057autoAST53Lexer, t057autoAST53Parser, "a", "34", true);
472// finds an error at the first token, 34, and re-syncs.
473// re-synchronizing does not consume a token because 34 follows
474// ref to rule b (start of c). It then matches 34 in c.
475    assertEquals(["line 1:0 missing ID at u'34'"], errors);
476    assertEquals("<missing ID> 34", found);
477}
478
479function testNoViableAltGivesErrorNode() {
480    found, errors = execParser(t057autoAST54Lexer, t057autoAST54Parser, "a", "*", true);
481    assertEquals(["line 1:0 no viable alternative at input u'*'"],
482            errors);
483    assertEquals("<unexpected: [@0,0:0=u'*',<6>,1:0], resync=*>",
484            found);
485}
486
487</script>
488
489</head>
490<body>
491    <h1>t057autoAST</h1>
492</body>
493</html>
494
495
496