• 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>t058rewriteAST</title>
6
7<!-- ANTLR includes -->
8<script type="text/javascript" src="../../lib/antlr3-all.js"></script>
9
10<script type="text/javascript" src="t058rewriteAST1Lexer.js"></script>
11<script type="text/javascript" src="t058rewriteAST1Parser.js"></script>
12<script type="text/javascript" src="t058rewriteAST2Lexer.js"></script>
13<script type="text/javascript" src="t058rewriteAST2Parser.js"></script>
14<script type="text/javascript" src="t058rewriteAST3Lexer.js"></script>
15<script type="text/javascript" src="t058rewriteAST3Parser.js"></script>
16<script type="text/javascript" src="t058rewriteAST4Lexer.js"></script>
17<script type="text/javascript" src="t058rewriteAST4Parser.js"></script>
18<script type="text/javascript" src="t058rewriteAST5Lexer.js"></script>
19<script type="text/javascript" src="t058rewriteAST5Parser.js"></script>
20<script type="text/javascript" src="t058rewriteAST6Lexer.js"></script>
21<script type="text/javascript" src="t058rewriteAST6Parser.js"></script>
22<script type="text/javascript" src="t058rewriteAST7Lexer.js"></script>
23<script type="text/javascript" src="t058rewriteAST7Parser.js"></script>
24<script type="text/javascript" src="t058rewriteAST8Lexer.js"></script>
25<script type="text/javascript" src="t058rewriteAST8Parser.js"></script>
26<script type="text/javascript" src="t058rewriteAST9Lexer.js"></script>
27<script type="text/javascript" src="t058rewriteAST9Parser.js"></script>
28<script type="text/javascript" src="t058rewriteAST10Lexer.js"></script>
29<script type="text/javascript" src="t058rewriteAST10Parser.js"></script>
30<script type="text/javascript" src="t058rewriteAST11Lexer.js"></script>
31<script type="text/javascript" src="t058rewriteAST11Parser.js"></script>
32<script type="text/javascript" src="t058rewriteAST12Lexer.js"></script>
33<script type="text/javascript" src="t058rewriteAST12Parser.js"></script>
34<script type="text/javascript" src="t058rewriteAST13Lexer.js"></script>
35<script type="text/javascript" src="t058rewriteAST13Parser.js"></script>
36<script type="text/javascript" src="t058rewriteAST14Lexer.js"></script>
37<script type="text/javascript" src="t058rewriteAST14Parser.js"></script>
38<script type="text/javascript" src="t058rewriteAST15Lexer.js"></script>
39<script type="text/javascript" src="t058rewriteAST15Parser.js"></script>
40<script type="text/javascript" src="t058rewriteAST16Lexer.js"></script>
41<script type="text/javascript" src="t058rewriteAST16Parser.js"></script>
42<script type="text/javascript" src="t058rewriteAST17Lexer.js"></script>
43<script type="text/javascript" src="t058rewriteAST17Parser.js"></script>
44<script type="text/javascript" src="t058rewriteAST18Lexer.js"></script>
45<script type="text/javascript" src="t058rewriteAST18Parser.js"></script>
46<script type="text/javascript" src="t058rewriteAST19Lexer.js"></script>
47<script type="text/javascript" src="t058rewriteAST19Parser.js"></script>
48<script type="text/javascript" src="t058rewriteAST20Lexer.js"></script>
49<script type="text/javascript" src="t058rewriteAST20Parser.js"></script>
50<script type="text/javascript" src="t058rewriteAST21Lexer.js"></script>
51<script type="text/javascript" src="t058rewriteAST21Parser.js"></script>
52<script type="text/javascript" src="t058rewriteAST22Lexer.js"></script>
53<script type="text/javascript" src="t058rewriteAST22Parser.js"></script>
54<script type="text/javascript" src="t058rewriteAST23Lexer.js"></script>
55<script type="text/javascript" src="t058rewriteAST23Parser.js"></script>
56<script type="text/javascript" src="t058rewriteAST24Lexer.js"></script>
57<script type="text/javascript" src="t058rewriteAST24Parser.js"></script>
58<script type="text/javascript" src="t058rewriteAST25Lexer.js"></script>
59<script type="text/javascript" src="t058rewriteAST25Parser.js"></script>
60<script type="text/javascript" src="t058rewriteAST26Lexer.js"></script>
61<script type="text/javascript" src="t058rewriteAST26Parser.js"></script>
62<script type="text/javascript" src="t058rewriteAST27Lexer.js"></script>
63<script type="text/javascript" src="t058rewriteAST27Parser.js"></script>
64<script type="text/javascript" src="t058rewriteAST28Lexer.js"></script>
65<script type="text/javascript" src="t058rewriteAST28Parser.js"></script>
66<script type="text/javascript" src="t058rewriteAST29Lexer.js"></script>
67<script type="text/javascript" src="t058rewriteAST29Parser.js"></script>
68<script type="text/javascript" src="t058rewriteAST30Lexer.js"></script>
69<script type="text/javascript" src="t058rewriteAST30Parser.js"></script>
70<script type="text/javascript" src="t058rewriteAST31Lexer.js"></script>
71<script type="text/javascript" src="t058rewriteAST31Parser.js"></script>
72<script type="text/javascript" src="t058rewriteAST32Lexer.js"></script>
73<script type="text/javascript" src="t058rewriteAST32Parser.js"></script>
74<script type="text/javascript" src="t058rewriteAST33Lexer.js"></script>
75<script type="text/javascript" src="t058rewriteAST33Parser.js"></script>
76<script type="text/javascript" src="t058rewriteAST34Lexer.js"></script>
77<script type="text/javascript" src="t058rewriteAST34Parser.js"></script>
78<script type="text/javascript" src="t058rewriteAST35Lexer.js"></script>
79<script type="text/javascript" src="t058rewriteAST35Parser.js"></script>
80<script type="text/javascript" src="t058rewriteAST36Lexer.js"></script>
81<script type="text/javascript" src="t058rewriteAST36Parser.js"></script>
82<script type="text/javascript" src="t058rewriteAST37Lexer.js"></script>
83<script type="text/javascript" src="t058rewriteAST37Parser.js"></script>
84<script type="text/javascript" src="t058rewriteAST38Lexer.js"></script>
85<script type="text/javascript" src="t058rewriteAST38Parser.js"></script>
86<script type="text/javascript" src="t058rewriteAST39Lexer.js"></script>
87<script type="text/javascript" src="t058rewriteAST39Parser.js"></script>
88<script type="text/javascript" src="t058rewriteAST40Lexer.js"></script>
89<script type="text/javascript" src="t058rewriteAST40Parser.js"></script>
90<script type="text/javascript" src="t058rewriteAST41Lexer.js"></script>
91<script type="text/javascript" src="t058rewriteAST41Parser.js"></script>
92<script type="text/javascript" src="t058rewriteAST42Lexer.js"></script>
93<script type="text/javascript" src="t058rewriteAST42Parser.js"></script>
94<script type="text/javascript" src="t058rewriteAST43Lexer.js"></script>
95<script type="text/javascript" src="t058rewriteAST43Parser.js"></script>
96<script type="text/javascript" src="t058rewriteAST44Lexer.js"></script>
97<script type="text/javascript" src="t058rewriteAST44Parser.js"></script>
98<script type="text/javascript" src="t058rewriteAST45Lexer.js"></script>
99<script type="text/javascript" src="t058rewriteAST45Parser.js"></script>
100<script type="text/javascript" src="t058rewriteAST46Lexer.js"></script>
101<script type="text/javascript" src="t058rewriteAST46Parser.js"></script>
102<script type="text/javascript" src="t058rewriteAST47Lexer.js"></script>
103<script type="text/javascript" src="t058rewriteAST47Parser.js"></script>
104<script type="text/javascript" src="t058rewriteAST48Lexer.js"></script>
105<script type="text/javascript" src="t058rewriteAST48Parser.js"></script>
106<script type="text/javascript" src="t058rewriteAST49Lexer.js"></script>
107<script type="text/javascript" src="t058rewriteAST49Parser.js"></script>
108<script type="text/javascript" src="t058rewriteAST50Lexer.js"></script>
109<script type="text/javascript" src="t058rewriteAST50Parser.js"></script>
110<script type="text/javascript" src="t058rewriteAST51Lexer.js"></script>
111<script type="text/javascript" src="t058rewriteAST51Parser.js"></script>
112<script type="text/javascript" src="t058rewriteAST52Lexer.js"></script>
113<script type="text/javascript" src="t058rewriteAST52Parser.js"></script>
114<script type="text/javascript" src="t058rewriteAST53Lexer.js"></script>
115<script type="text/javascript" src="t058rewriteAST53Parser.js"></script>
116<script type="text/javascript" src="t058rewriteAST54Lexer.js"></script>
117<script type="text/javascript" src="t058rewriteAST54Parser.js"></script>
118<script type="text/javascript" src="t058rewriteAST55Lexer.js"></script>
119<script type="text/javascript" src="t058rewriteAST55Parser.js"></script>
120<script type="text/javascript" src="t058rewriteAST56Lexer.js"></script>
121<script type="text/javascript" src="t058rewriteAST56Parser.js"></script>
122<script type="text/javascript" src="t058rewriteAST57Lexer.js"></script>
123<script type="text/javascript" src="t058rewriteAST57Parser.js"></script>
124<script type="text/javascript" src="t058rewriteAST58Lexer.js"></script>
125<script type="text/javascript" src="t058rewriteAST58Parser.js"></script>
126<script type="text/javascript" src="t058rewriteAST59Lexer.js"></script>
127<script type="text/javascript" src="t058rewriteAST59Parser.js"></script>
128<script type="text/javascript" src="t058rewriteAST60Lexer.js"></script>
129<script type="text/javascript" src="t058rewriteAST60Parser.js"></script>
130<script type="text/javascript" src="t058rewriteAST61Lexer.js"></script>
131<script type="text/javascript" src="t058rewriteAST61Parser.js"></script>
132<script type="text/javascript" src="t058rewriteAST62Lexer.js"></script>
133<script type="text/javascript" src="t058rewriteAST62Parser.js"></script>
134<script type="text/javascript" src="t058rewriteAST63Lexer.js"></script>
135<script type="text/javascript" src="t058rewriteAST63Parser.js"></script>
136<script type="text/javascript" src="t058rewriteAST64Lexer.js"></script>
137<script type="text/javascript" src="t058rewriteAST64Parser.js"></script>
138<script type="text/javascript" src="t058rewriteAST65Lexer.js"></script>
139<script type="text/javascript" src="t058rewriteAST65Parser.js"></script>
140<script type="text/javascript" src="t058rewriteAST66Lexer.js"></script>
141<script type="text/javascript" src="t058rewriteAST66Parser.js"></script>
142<script type="text/javascript" src="t058rewriteAST67Lexer.js"></script>
143<script type="text/javascript" src="t058rewriteAST67Parser.js"></script>
144<script type="text/javascript" src="t058rewriteAST68Lexer.js"></script>
145<script type="text/javascript" src="t058rewriteAST68Parser.js"></script>
146<script type="text/javascript" src="t058rewriteAST69Lexer.js"></script>
147<script type="text/javascript" src="t058rewriteAST69Parser.js"></script>
148<script type="text/javascript" src="t058rewriteAST70Lexer.js"></script>
149<script type="text/javascript" src="t058rewriteAST70Parser.js"></script>
150<script type="text/javascript" src="t058rewriteAST71Lexer.js"></script>
151<script type="text/javascript" src="t058rewriteAST71Parser.js"></script>
152<script type="text/javascript" src="t058rewriteAST72Lexer.js"></script>
153<script type="text/javascript" src="t058rewriteAST72Parser.js"></script>
154<script type="text/javascript" src="t058rewriteAST73Lexer.js"></script>
155<script type="text/javascript" src="t058rewriteAST73Parser.js"></script>
156<script type="text/javascript" src="t058rewriteAST74Lexer.js"></script>
157<script type="text/javascript" src="t058rewriteAST74Parser.js"></script>
158<script type="text/javascript" src="t058rewriteAST75Lexer.js"></script>
159<script type="text/javascript" src="t058rewriteAST75Parser.js"></script>
160<script type="text/javascript" src="t058rewriteAST76Lexer.js"></script>
161<script type="text/javascript" src="t058rewriteAST76Parser.js"></script>
162<script type="text/javascript" src="t058rewriteAST77Lexer.js"></script>
163<script type="text/javascript" src="t058rewriteAST77Parser.js"></script>
164<script type="text/javascript" src="t058rewriteAST78Lexer.js"></script>
165<script type="text/javascript" src="t058rewriteAST78Parser.js"></script>
166<script type="text/javascript" src="t058rewriteAST79Lexer.js"></script>
167<script type="text/javascript" src="t058rewriteAST79Parser.js"></script>
168<script type="text/javascript" src="t058rewriteAST80Lexer.js"></script>
169<script type="text/javascript" src="t058rewriteAST80Parser.js"></script>
170<script type="text/javascript" src="t058rewriteAST81Lexer.js"></script>
171<script type="text/javascript" src="t058rewriteAST81Parser.js"></script>
172<script type="text/javascript" src="t058rewriteAST82Lexer.js"></script>
173<script type="text/javascript" src="t058rewriteAST82Parser.js"></script>
174<script type="text/javascript" src="t058rewriteAST83Lexer.js"></script>
175<script type="text/javascript" src="t058rewriteAST83Parser.js"></script>
176
177<!-- JsUnit include -->
178<script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>
179
180<!-- Test Code -->
181<script type="text/javascript">
182    // Parser Tests
183
184    function execParser(lexerCls, parserCls, grammarEntry, xinput, expectErrors) {
185        var cstream = new org.antlr.runtime.ANTLRStringStream(xinput),
186            lexer = new (EnhancedParser(lexerCls))(cstream),
187            tstream = new org.antlr.runtime.CommonTokenStream(lexer),
188            parser = new (EnhancedParser(parserCls))(tstream);
189        var r = parser[grammarEntry]();
190
191        if (!expectErrors) {
192            throw parser._errors[0];
193            assertEquals(parser._errors.length, 0);
194        }
195
196        var result = "";
197        if (r) {
198            if (r.result) {
199                result += r.result
200            }
201            if (r.getTree()) {
202                result += r.getTree().toStringTree();
203            }
204        }
205
206        if (!expectErrors) {
207            return result;
208        } else {
209            return [result, parser._errors];
210        }
211    }
212
213    function EnhancedParser(base) {
214        var T = function() {
215            T.superclass.constructor.apply(this, arguments);
216            this.traces = [];
217            this.buf = "";
218            this._errors = [];
219        };
220
221        org.antlr.lang.extend(T, base, {
222            capture: function(t) {
223                this.buf += t;
224            },
225            traceIn: function(ruleName, ruleIndex) {
226                this.traces.push(">"+ruleName);
227            },
228            traceOut: function(ruleName, ruleIndex) {
229                this.traces.push("<"+ruleName);
230            },
231            emitErrorMessage: function(msg) {
232                this._errors.push(msg);
233            }
234        });
235
236        return T;
237    }
238
239function testDelete() {
240    var found = execParser(t058rewriteAST1Lexer, t058rewriteAST1Parser, "a", "abc 34");
241    assertEquals("", found);
242}
243
244function testSingleToken() {
245    var found = execParser(t058rewriteAST2Lexer, t058rewriteAST2Parser, "a", "abc");
246    assertEquals("abc", found);
247}
248
249function testSingleTokenToNewNode() {
250    var found = execParser(t058rewriteAST3Lexer, t058rewriteAST3Parser, "a", "abc");
251    assertEquals("x", found);
252}
253
254function testSingleTokenToNewNodeRoot() {
255    var found = execParser(t058rewriteAST4Lexer, t058rewriteAST4Parser, "a", "abc");
256    assertEquals("(x INT)", found);
257}
258
259function testSingleTokenToNewNode2() {
260    // Allow creation of new nodes w/o args.
261    var found = execParser(t058rewriteAST5Lexer, t058rewriteAST5Parser, "a", "abc");
262    assertEquals("ID", found);
263}
264
265function testSingleCharLiteral() {
266    var found = execParser(t058rewriteAST6Lexer, t058rewriteAST6Parser, "a", "c");
267    assertEquals("c", found);
268}
269
270function testSingleStringLiteral() {
271    var found = execParser(t058rewriteAST7Lexer, t058rewriteAST7Parser, "a", "ick");
272    assertEquals("ick", found);
273}
274
275function testSingleRule() {
276    var found = execParser(t058rewriteAST8Lexer, t058rewriteAST8Parser, "a", "abc");
277    assertEquals("abc", found);
278}
279
280function testReorderTokens() {
281    var found = execParser(t058rewriteAST9Lexer, t058rewriteAST9Parser, "a", "abc 34");
282    assertEquals("34 abc", found);
283}
284
285function testReorderTokenAndRule() {
286    var found = execParser(t058rewriteAST10Lexer, t058rewriteAST10Parser, "a", "abc 34");
287    assertEquals("34 abc", found);
288}
289
290function testTokenTree() {
291    var found = execParser(t058rewriteAST11Lexer, t058rewriteAST11Parser, "a", "abc 34");
292    assertEquals("(34 abc)", found);
293}
294
295function testTokenTreeAfterOtherStuff() {
296    var found = execParser(t058rewriteAST12Lexer, t058rewriteAST12Parser, "a", "void abc 34");
297    assertEquals("void (34 abc)", found);
298}
299
300function testNestedTokenTreeWithOuterLoop() {
301    // verify that ID and INT both iterate over outer index variable
302    var found = execParser(t058rewriteAST13Lexer, t058rewriteAST13Parser, "a", "a 1 b 2");
303    assertEquals("(DUH a (DUH 1)) (DUH b (DUH 2))", found);
304}
305
306function testOptionalSingleToken() {
307    var found = execParser(t058rewriteAST14Lexer, t058rewriteAST14Parser, "a", "abc");
308    assertEquals("abc", found);
309}
310
311function testClosureSingleToken() {
312    var found = execParser(t058rewriteAST15Lexer, t058rewriteAST15Parser, "a", "a b");
313    assertEquals("a b", found);
314}
315
316function testPositiveClosureSingleToken() {
317    var found = execParser(t058rewriteAST16Lexer, t058rewriteAST16Parser, "a", "a b");
318    assertEquals("a b", found);
319}
320
321function testOptionalSingleRule() {
322    var found = execParser(t058rewriteAST17Lexer, t058rewriteAST17Parser, "a", "abc");
323    assertEquals("abc", found);
324}
325
326function testClosureSingleRule() {
327    var found = execParser(t058rewriteAST18Lexer, t058rewriteAST18Parser, "a", "a b");
328    assertEquals("a b", found);
329}
330
331function testClosureOfLabel() {
332    var found = execParser(t058rewriteAST19Lexer, t058rewriteAST19Parser, "a", "a b");
333    assertEquals("a b", found);
334}
335
336function testOptionalLabelNoListLabel() {
337    var found = execParser(t058rewriteAST20Lexer, t058rewriteAST20Parser, "a", "a");
338    assertEquals("a", found);
339}
340
341function testPositiveClosureSingleRule() {
342    var found = execParser(t058rewriteAST21Lexer, t058rewriteAST21Parser, "a", "a b");
343    assertEquals("a b", found);
344}
345
346function testSinglePredicateT() {
347    var found = execParser(t058rewriteAST22Lexer, t058rewriteAST22Parser, "a", "abc");
348    assertEquals("abc", found);
349}
350
351function testSinglePredicateF() {
352    var found = execParser(t058rewriteAST23Lexer, t058rewriteAST23Parser, "a", "abc");
353    assertEquals("", found);
354}
355
356function testMultiplePredicate() {
357    var found = execParser(t058rewriteAST24Lexer, t058rewriteAST24Parser, "a", "a 2");
358    assertEquals("2", found);
359}
360
361function testMultiplePredicateTrees() {
362    var found = execParser(t058rewriteAST25Lexer, t058rewriteAST25Parser, "a", "a 2");
363    assertEquals("(2 a)", found);
364}
365
366function testSimpleTree() {
367    var found = execParser(t058rewriteAST26Lexer, t058rewriteAST26Parser, "a", "-34");
368    assertEquals("(- 34)", found);
369}
370
371function testSimpleTree2() {
372    var found = execParser(t058rewriteAST27Lexer, t058rewriteAST27Parser, "a", "+ 34");
373    assertEquals("(34 +)", found);
374}
375
376function testNestedTrees() {
377    var found = execParser(t058rewriteAST28Lexer, t058rewriteAST28Parser, "a", "var a:int; b:float;");
378    assertEquals("(var (: a int) (: b float))", found);
379}
380
381function testImaginaryTokenCopy() {
382    var found = execParser(t058rewriteAST29Lexer, t058rewriteAST29Parser, "a", "a,b,c");
383    assertEquals("(VAR a) (VAR b) (VAR c)", found);
384}
385
386function testTokenUnreferencedOnLeftButDefined() {
387    var found = execParser(t058rewriteAST30Lexer, t058rewriteAST30Parser, "a", "a");
388    assertEquals("ID", found);
389}
390
391function testImaginaryTokenCopySetText() {
392    var found = execParser(t058rewriteAST31Lexer, t058rewriteAST31Parser, "a", "a,b,c");
393    assertEquals("(var a) (var b) (var c)", found);
394}
395
396function testImaginaryTokenNoCopyFromToken() {
397    var found = execParser(t058rewriteAST32Lexer, t058rewriteAST32Parser, "a", "{a b c}");
398    assertEquals("({ a b c)", found);
399}
400
401function testImaginaryTokenNoCopyFromTokenSetText() {
402    var found = execParser(t058rewriteAST33Lexer, t058rewriteAST33Parser, "a", "{a b c}");
403    assertEquals("(block a b c)", found);
404}
405
406function testMixedRewriteAndAutoAST() {
407    var found = execParser(t058rewriteAST34Lexer, t058rewriteAST34Parser, "a", "a 1 2");
408    assertEquals("(2 1 a)", found);
409}
410
411function testSubruleWithRewrite() {
412    var found = execParser(t058rewriteAST35Lexer, t058rewriteAST35Parser, "a", "a 1 2 3");
413    assertEquals("1 a 2 3", found);
414}
415
416function testSubruleWithRewrite2() {
417    var found = execParser(t058rewriteAST36Lexer, t058rewriteAST36Parser, "a", "int a; int b=3;");
418    assertEquals("(TYPE int a) (TYPE int b 3)", found);
419}
420
421function testNestedRewriteShutsOffAutoAST() {
422    var found = execParser(t058rewriteAST37Lexer, t058rewriteAST37Parser, "a", "a b c d; 42");
423    assertEquals("d 42", found);
424}
425
426function testRewriteActions() {
427    var found = execParser(t058rewriteAST38Lexer, t058rewriteAST38Parser, "a", "3");
428    assertEquals("(9 3)", found);
429}
430
431function testRewriteActions2() {
432    var found = execParser(t058rewriteAST39Lexer, t058rewriteAST39Parser, "a", "3");
433    assertEquals("9 3", found);
434}
435
436function testRefToOldValue() {
437    var found = execParser(t058rewriteAST40Lexer, t058rewriteAST40Parser, "a", "3+4+5");
438    assertEquals("(+ (+ 3 4) 5)", found);
439}
440
441function testCopySemanticsForRules() {
442    var found = execParser(t058rewriteAST41Lexer, t058rewriteAST41Parser, "a", "3");
443    assertEquals("(3 3)", found);
444}
445
446function testCopySemanticsForRules2() {
447    // copy type as a root for each invocation of (...)+ in rewrite
448    var found = execParser(t058rewriteAST42Lexer, t058rewriteAST42Parser, "a", "int a,b,c;");
449    assertEquals("(int a) (int b) (int c)", found);
450}
451
452function testCopySemanticsForRules3() {
453    // copy type *and* modifier even though it's optional
454    // for each invocation of (...)+ in rewrite
455    var found = execParser(t058rewriteAST43Lexer, t058rewriteAST43Parser, "a", "public int a,b,c;");
456    assertEquals("(int public a) (int public b) (int public c)", found);
457}
458
459function testCopySemanticsForRules3Double() {
460    // copy type *and* modifier even though it's optional
461    // for each invocation of (...)+ in rewrite
462    var found = execParser(t058rewriteAST44Lexer, t058rewriteAST44Parser, "a", "public int a,b,c;");
463    assertEquals("(int public a) (int public b) (int public c) (int public a) (int public b) (int public c)", found);
464}
465
466function testCopySemanticsForRules4() {
467    // copy type *and* modifier even though it's optional
468    // for each invocation of (...)+ in rewrite
469    var found = execParser(t058rewriteAST45Lexer, t058rewriteAST45Parser, "a", "public int a,b,c;");
470    assertEquals("(int (MOD public) a) (int (MOD public) b) (int (MOD public) c)", found);
471}
472
473function testCopySemanticsLists() {
474    var found = execParser(t058rewriteAST46Lexer, t058rewriteAST46Parser, "a", "a,b,c;");
475    assertEquals("a b c a b c", found);
476}
477
478function testCopyRuleLabel() {
479    var found = execParser(t058rewriteAST47Lexer, t058rewriteAST47Parser, "a", "a");
480    assertEquals("a a", found);
481}
482
483function testCopyRuleLabel2() {
484    var found = execParser(t058rewriteAST48Lexer, t058rewriteAST48Parser, "a", "a");
485    assertEquals("(a a)", found);
486}
487
488function testQueueingOfTokens() {
489    var found = execParser(t058rewriteAST49Lexer, t058rewriteAST49Parser, "a", "int a,b,c;");
490    assertEquals("(int a b c)", found);
491}
492
493function testCopyOfTokens() {
494    var found = execParser(t058rewriteAST50Lexer, t058rewriteAST50Parser, "a", "int a;");
495    assertEquals("int a int a", found);
496}
497
498function testTokenCopyInLoop() {
499    var found = execParser(t058rewriteAST51Lexer, t058rewriteAST51Parser, "a", "int a,b,c;");
500    assertEquals("(int a) (int b) (int c)", found);
501}
502
503function testTokenCopyInLoopAgainstTwoOthers() {
504    // must smear 'int' copies across as root of multiple trees
505    var found = execParser(t058rewriteAST52Lexer, t058rewriteAST52Parser, "a", "int a:1,b:2,c:3;");
506    assertEquals("(int a 1) (int b 2) (int c 3)", found);
507}
508
509function testListRefdOneAtATime() {
510    var found = execParser(t058rewriteAST53Lexer, t058rewriteAST53Parser, "a", "a b c");
511    assertEquals("a b c", found);
512}
513
514function testSplitListWithLabels() {
515    var found = execParser(t058rewriteAST54Lexer, t058rewriteAST54Parser, "a", "a b c");
516    assertEquals("a VAR b c", found);
517}
518
519function testComplicatedMelange() {
520    var found = execParser(t058rewriteAST55Lexer, t058rewriteAST55Parser, "a", "a a b b b c c c d");
521    assertEquals("a a b b b c c c d", found);
522}
523
524function testRuleLabel() {
525    var found = execParser(t058rewriteAST56Lexer, t058rewriteAST56Parser, "a", "a");
526    assertEquals("a", found);
527}
528
529function testAmbiguousRule() {
530    var found = execParser(t058rewriteAST57Lexer, t058rewriteAST57Parser,
531            "a", "abc 34");
532    assertEquals("34", found);
533}
534
535function testRuleListLabel() {
536    var found = execParser(t058rewriteAST58Lexer, t058rewriteAST58Parser, "a", "a b");
537    assertEquals("a b", found);
538}
539
540function testRuleListLabel2() {
541    var found = execParser(t058rewriteAST59Lexer, t058rewriteAST59Parser, "a", "a b");
542    assertEquals("a b", found);
543}
544
545function testOptional() {
546    var found = execParser(t058rewriteAST60Lexer, t058rewriteAST60Parser, "a", "a");
547    assertEquals("a", found);
548}
549
550function testOptional2() {
551    var found = execParser(t058rewriteAST61Lexer, t058rewriteAST61Parser, "a", "a b");
552    assertEquals("a b", found);
553}
554
555function testOptional3() {
556    var found = execParser(t058rewriteAST62Lexer, t058rewriteAST62Parser, "a", "a b");
557    assertEquals("a b", found);
558}
559
560function testOptional4() {
561    var found = execParser(t058rewriteAST63Lexer, t058rewriteAST63Parser, "a", "a b");
562    assertEquals("a b", found);
563}
564
565function testOptional5() {
566    var found = execParser(t058rewriteAST64Lexer, t058rewriteAST64Parser, "a", "a");
567    assertEquals("a", found);
568}
569
570function testArbitraryExprType() {
571    var found = execParser(t058rewriteAST65Lexer, t058rewriteAST65Parser, "a", "a b");
572    assertEquals("", found);
573}
574
575function testSet() {
576    var found = execParser(t058rewriteAST66Lexer, t058rewriteAST66Parser, "a", "2 a 34 de");
577    assertEquals("2 34 a de", found);
578}
579
580function testSet2() {
581    var found = execParser(t058rewriteAST67Lexer, t058rewriteAST67Parser, "a", "2");
582    assertEquals("2", found);
583}
584
585/*
586 * @fails - the grammar shouldn't be valid, but is
587 *
588function testSetWithLabel() {
589    var found = execParser(t058rewriteAST68Lexer, t058rewriteAST68Parser, "a", "2");
590    assertEquals("2", found);
591}
592*/
593
594function testRewriteAction() {
595    var found = execParser(t058rewriteAST69Lexer, t058rewriteAST69Parser, "r", "25");
596    assertEquals("25.0", found);
597}
598
599function testOptionalSubruleWithoutRealElements() {
600    // copy type *and* modifier even though it's optional
601    // for each invocation of (...)+ in rewrite
602    var found = execParser(t058rewriteAST70Lexer, t058rewriteAST70Parser, "modulo", "modulo abc (x y)");
603    assertEquals("(modulo abc (PARMS x y))", found);
604}
605
606//// C A R D I N A L I T Y  I S S U E S
607
608function testCardinality() {
609    try {
610        execParser(t058rewriteAST71Lexer, t058rewriteAST71Parser, "a", "a b 3 4 5");
611        fail();
612    } catch(e) {
613        assertEquals(e.getMessage(), "token ID");
614    }
615}
616
617
618function testCardinality2() {
619    try {
620        execParser(t058rewriteAST72Lexer, t058rewriteAST72Parser, "a", "a b");
621        fail();
622    } catch(e) {
623        assertEquals(e.getMessage(), "token ID");
624    }
625}
626
627function testCardinality3() {
628    try {
629        execParser(t058rewriteAST73Lexer, t058rewriteAST73Parser, "a", "3");
630        fail();
631    } catch(e) {
632        assertEquals(e.getMessage(), "token ID");
633    }
634}
635
636function testLoopCardinality() {
637    try {
638        execParser(t058rewriteAST74Lexer, t058rewriteAST74Parser, "a", "3");
639        fail();
640    } catch(e) {
641        assert(e instanceof org.antlr.runtime.tree.RewriteEarlyExitException);
642    }
643}
644
645function testWildcard() {
646    var found = execParser(t058rewriteAST75Lexer, t058rewriteAST75Parser, "a", "abc 34");
647    assertEquals("34\n", found);
648}
649
650// E R R O R S
651
652function testExtraTokenInSimpleDecl() {
653    var retval = execParser(t058rewriteAST76Lexer, t058rewriteAST76Parser, "decl", "int 34 x=1;",
654            true);
655    var found = retval[0], errors = retval[1];
656    assertArrayEquals(["line 1:4 extraneous input '34' expecting ID"],
657            errors);
658    assertEquals("(EXPR int x 1)", found) // tree gets correct x and 1 tokens
659}
660
661
662function testMissingIDInSimpleDecl() {
663    var retval = execParser(t058rewriteAST77Lexer, t058rewriteAST77Parser, "decl", "int =1;",
664            true);
665    var found = retval[0], errors = retval[1];
666    assertArrayEquals(["line 1:4 missing ID at '='"], errors);
667    assertEquals("(EXPR int <missing ID> 1)", found) // tree gets invented ID token
668}
669
670function testMissingSetInSimpleDecl() {
671    var retval = execParser(t058rewriteAST78Lexer, t058rewriteAST78Parser, "decl", "x=1;",
672            true);
673    var found = retval[0], errors = retval[1];
674    assertArrayEquals(["line 1:0 mismatched input 'x' expecting set null"],
675            errors);
676    assertEquals("(EXPR <error: x> x 1)", found) // tree gets invented ID token
677}
678
679function testMissingTokenGivesErrorNode() {
680    var retval = execParser(t058rewriteAST79Lexer, t058rewriteAST79Parser, "a", "abc",
681            true);
682    var found = retval[0], errors = retval[1];
683    assertArrayEquals(["line 0:-1 missing INT at '<EOF>'"], errors);
684    // doesn't do in-line recovery for sets (yet?);
685    assertEquals("abc <missing INT>", found);
686}
687
688function testExtraTokenGivesErrorNode() {
689    var retval = execParser(t058rewriteAST80Lexer, t058rewriteAST80Parser, "a", "abc ick 34",
690            true);
691    var found = retval[0], errors = retval[1];
692    assertArrayEquals(["line 1:4 extraneous input 'ick' expecting INT"],
693            errors);
694    assertEquals("abc 34", found);
695}
696
697function testMissingFirstTokenGivesErrorNode() {
698    var retval = execParser(t058rewriteAST81Lexer, t058rewriteAST81Parser, "a", "34", true);
699    var found = retval[0], errors = retval[1];
700    assertArrayEquals(["line 1:0 missing ID at '34'"], errors);
701    assertEquals("<missing ID> 34", found);
702}
703
704function testMissingFirstTokenGivesErrorNode2() {
705    var retval = execParser(t058rewriteAST82Lexer, t058rewriteAST82Parser, "a", "34", true);
706    var found = retval[0], errors = retval[1];
707    // finds an error at the first token, 34, and re-syncs.
708    // re-synchronizing does not consume a token because 34 follows
709    // ref to rule b (start of c). It then matches 34 in c.
710    assertArrayEquals(["line 1:0 mismatched input '34' expecting ID"], errors);
711    assertEquals("<mismatched token: [@0,0:1='34',<5>,1:0], resync=34> 34", found);
712}
713
714function testNoViableAltGivesErrorNode() {
715    var retval = execParser(t058rewriteAST83Lexer, t058rewriteAST83Parser, "a", "*", true);
716    var found = retval[0], errors = retval[1];
717    // finds an error at the first token, 34, and re-syncs.
718    // re-synchronizing does not consume a token because 34 follows
719    // ref to rule b (start of c). It then matches 34 in c.
720    assertArrayEquals(["line 1:0 no viable alternative at input '*'"],
721            errors);
722    assertEquals("<unexpected: [@0,0:0='*',<6>,1:0], resync=*>",
723            found);
724}
725</script>
726
727</head>
728<body>
729    <h1>t058rewriteAST</h1>
730</body>
731</html>
732
733