Source Code Cross Referenced for TestAttributes.java in  » Parser » antlr-3.0.1 » org » antlr » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » antlr 3.0.1 » org.antlr.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         [The "BSD licence"]
0003:         Copyright (c) 2005-2006 Terence Parr
0004:         All rights reserved.
0005:
0006:         Redistribution and use in source and binary forms, with or without
0007:         modification, are permitted provided that the following conditions
0008:         are met:
0009:         1. Redistributions of source code must retain the above copyright
0010:            notice, this list of conditions and the following disclaimer.
0011:         2. Redistributions in binary form must reproduce the above copyright
0012:            notice, this list of conditions and the following disclaimer in the
0013:            documentation and/or other materials provided with the distribution.
0014:         3. The name of the author may not be used to endorse or promote products
0015:            derived from this software without specific prior written permission.
0016:
0017:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0018:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0019:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0020:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0021:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0022:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0023:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0024:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0026:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027:         */
0028:        package org.antlr.test;
0029:
0030:        import org.antlr.Tool;
0031:        import org.antlr.codegen.CodeGenerator;
0032:        import org.antlr.codegen.ActionTranslatorLexer;
0033:        import org.antlr.stringtemplate.StringTemplate;
0034:        import org.antlr.stringtemplate.StringTemplateGroup;
0035:        import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
0036:        import org.antlr.tool.*;
0037:
0038:        import java.io.StringReader;
0039:        import java.util.List;
0040:        import java.util.ArrayList;
0041:
0042:        /** Check the $x, $x.y attributes.  For checking the actual
0043:         *  translation, assume the Java target.  This is still a great test
0044:         *  for the semantics of the $x.y stuff regardless of the target.
0045:         */
0046:        public class TestAttributes extends BaseTest {
0047:
0048:            /** Public default constructor used by TestRig */
0049:            public TestAttributes() {
0050:            }
0051:
0052:            public void testEscapedLessThanInAction() throws Exception {
0053:                Grammar g = new Grammar();
0054:                Tool antlr = newTool();
0055:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0056:                String action = "i<3; '<xmltag>'";
0057:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0058:                        generator, "a", new antlr.CommonToken(
0059:                                ANTLRParser.ACTION, action), 0);
0060:                String expecting = action;
0061:                String rawTranslation = translator.translate();
0062:                StringTemplateGroup templates = new StringTemplateGroup(".",
0063:                        AngleBracketTemplateLexer.class);
0064:                StringTemplate actionST = new StringTemplate(templates,
0065:                        "<action>");
0066:                actionST.setAttribute("action", rawTranslation);
0067:                String found = actionST.toString();
0068:                assertEquals(expecting, found);
0069:            }
0070:
0071:            public void testEscaped$InAction() throws Exception {
0072:                String action = "int \\$n; \"\\$in string\\$\"";
0073:                String expecting = "int $n; \"$in string$\"";
0074:                Grammar g = new Grammar("parser grammar t;\n" + "@members {"
0075:                        + action + "}\n" + "a[User u, int i]\n" + "        : {"
0076:                        + action + "}\n" + "        ;");
0077:                Tool antlr = newTool();
0078:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0079:                g.setCodeGenerator(generator);
0080:                generator.genRecognizer(); // forces load of templates
0081:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0082:                        generator, "a", new antlr.CommonToken(
0083:                                ANTLRParser.ACTION, action), 0);
0084:                String rawTranslation = translator.translate();
0085:                StringTemplateGroup templates = new StringTemplateGroup(".",
0086:                        AngleBracketTemplateLexer.class);
0087:                StringTemplate actionST = new StringTemplate(templates,
0088:                        rawTranslation);
0089:                String found = actionST.toString();
0090:                assertEquals(expecting, found);
0091:            }
0092:
0093:            public void testArguments() throws Exception {
0094:                String action = "$i; $i.x; $u; $u.x";
0095:                String expecting = "i; i.x; u; u.x";
0096:
0097:                ErrorQueue equeue = new ErrorQueue();
0098:                ErrorManager.setErrorListener(equeue);
0099:                Grammar g = new Grammar("parser grammar t;\n"
0100:                        + "a[User u, int i]\n" + "        : {" + action + "}\n"
0101:                        + "        ;");
0102:                Tool antlr = newTool();
0103:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0104:                g.setCodeGenerator(generator);
0105:                generator.genRecognizer(); // forces load of templates
0106:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0107:                        generator, "a", new antlr.CommonToken(
0108:                                ANTLRParser.ACTION, action), 1);
0109:                String rawTranslation = translator.translate();
0110:                StringTemplateGroup templates = new StringTemplateGroup(".",
0111:                        AngleBracketTemplateLexer.class);
0112:                StringTemplate actionST = new StringTemplate(templates,
0113:                        rawTranslation);
0114:                String found = actionST.toString();
0115:                assertEquals(expecting, found);
0116:
0117:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0118:                        .size());
0119:            }
0120:
0121:            /** $x.start refs are checked during translation not before so ANTLR misses
0122:             the fact that rule r has refs to predefined attributes if the ref is after
0123:             the def of the method or self-referential.  Actually would be ok if I didn't
0124:             convert actions to strings; keep as templates.
0125:             June 9, 2006: made action translation leave templates not strings
0126:             */
0127:            public void testRefToReturnValueBeforeRefToPredefinedAttr()
0128:                    throws Exception {
0129:                String action = "$x.foo";
0130:                String expecting = "x.foo";
0131:
0132:                ErrorQueue equeue = new ErrorQueue();
0133:                ErrorManager.setErrorListener(equeue);
0134:                Grammar g = new Grammar("parser grammar t;\n" + "a : x=b {"
0135:                        + action + "} ;\n"
0136:                        + "b returns [int foo] : B {$b.start} ;\n");
0137:                Tool antlr = newTool();
0138:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0139:                g.setCodeGenerator(generator);
0140:                generator.genRecognizer(); // forces load of templates
0141:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0142:                        generator, "a", new antlr.CommonToken(
0143:                                ANTLRParser.ACTION, action), 1);
0144:                String rawTranslation = translator.translate();
0145:                StringTemplateGroup templates = new StringTemplateGroup(".",
0146:                        AngleBracketTemplateLexer.class);
0147:                StringTemplate actionST = new StringTemplate(templates,
0148:                        rawTranslation);
0149:                String found = actionST.toString();
0150:                assertEquals(expecting, found);
0151:
0152:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0153:                        .size());
0154:            }
0155:
0156:            public void testRuleLabelBeforeRefToPredefinedAttr()
0157:                    throws Exception {
0158:                // As of Mar 2007, I'm removing unused labels.  Unfortunately,
0159:                // the action is not seen until code gen.  Can't see $x.text
0160:                // before stripping unused labels.  We really need to translate
0161:                // actions first so code gen logic can use info.
0162:                String action = "$x.text";
0163:                String expecting = "input.toString(x.start,x.stop)";
0164:
0165:                ErrorQueue equeue = new ErrorQueue();
0166:                ErrorManager.setErrorListener(equeue);
0167:                Grammar g = new Grammar("parser grammar t;\n" + "a : x=b {"
0168:                        + action + "} ;\n" + "b : B ;\n");
0169:                Tool antlr = newTool();
0170:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0171:                g.setCodeGenerator(generator);
0172:                generator.genRecognizer(); // forces load of templates
0173:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0174:                        generator, "a", new antlr.CommonToken(
0175:                                ANTLRParser.ACTION, action), 1);
0176:                String rawTranslation = translator.translate();
0177:                StringTemplateGroup templates = new StringTemplateGroup(".",
0178:                        AngleBracketTemplateLexer.class);
0179:                StringTemplate actionST = new StringTemplate(templates,
0180:                        rawTranslation);
0181:                String found = actionST.toString();
0182:                assertEquals(expecting, found);
0183:
0184:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0185:                        .size());
0186:            }
0187:
0188:            public void testInvalidArguments() throws Exception {
0189:                String action = "$x";
0190:                String expecting = action;
0191:
0192:                ErrorQueue equeue = new ErrorQueue();
0193:                ErrorManager.setErrorListener(equeue);
0194:                Grammar g = new Grammar("parser grammar t;\n"
0195:                        + "a[User u, int i]\n" + "        : {" + action + "}\n"
0196:                        + "        ;");
0197:                Tool antlr = newTool();
0198:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0199:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0200:                        generator, "a", new antlr.CommonToken(
0201:                                ANTLRParser.ACTION, action), 1);
0202:                String rawTranslation = translator.translate();
0203:                StringTemplateGroup templates = new StringTemplateGroup(".",
0204:                        AngleBracketTemplateLexer.class);
0205:                StringTemplate actionST = new StringTemplate(templates,
0206:                        rawTranslation);
0207:                String found = actionST.toString();
0208:                assertEquals(expecting, found);
0209:
0210:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
0211:                Object expectedArg = "x";
0212:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0213:                        expectedMsgID, g, null, expectedArg);
0214:                checkError(equeue, expectedMessage);
0215:            }
0216:
0217:            public void testReturnValue() throws Exception {
0218:                String action = "$x.i";
0219:                String expecting = "x";
0220:
0221:                ErrorQueue equeue = new ErrorQueue();
0222:                ErrorManager.setErrorListener(equeue);
0223:                Grammar g = new Grammar("grammar t;\n" + "a returns [int i]\n"
0224:                        + "        : 'a'\n" + "        ;\n" + "b : x=a {"
0225:                        + action + "} ;\n");
0226:                Tool antlr = newTool();
0227:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0228:                g.setCodeGenerator(generator);
0229:                generator.genRecognizer(); // forces load of templates
0230:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0231:                        generator, "b", new antlr.CommonToken(
0232:                                ANTLRParser.ACTION, action), 1);
0233:                String rawTranslation = translator.translate();
0234:                StringTemplateGroup templates = new StringTemplateGroup(".",
0235:                        AngleBracketTemplateLexer.class);
0236:                StringTemplate actionST = new StringTemplate(templates,
0237:                        rawTranslation);
0238:                String found = actionST.toString();
0239:                assertEquals(expecting, found);
0240:
0241:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0242:                        .size());
0243:            }
0244:
0245:            public void testReturnValueWithNumber() throws Exception {
0246:                String action = "$x.i1";
0247:                String expecting = "x";
0248:
0249:                ErrorQueue equeue = new ErrorQueue();
0250:                ErrorManager.setErrorListener(equeue);
0251:                Grammar g = new Grammar("grammar t;\n" + "a returns [int i1]\n"
0252:                        + "        : 'a'\n" + "        ;\n" + "b : x=a {"
0253:                        + action + "} ;\n");
0254:                Tool antlr = newTool();
0255:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0256:                g.setCodeGenerator(generator);
0257:                generator.genRecognizer(); // forces load of templates
0258:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0259:                        generator, "b", new antlr.CommonToken(
0260:                                ANTLRParser.ACTION, action), 1);
0261:                String rawTranslation = translator.translate();
0262:                StringTemplateGroup templates = new StringTemplateGroup(".",
0263:                        AngleBracketTemplateLexer.class);
0264:                StringTemplate actionST = new StringTemplate(templates,
0265:                        rawTranslation);
0266:                String found = actionST.toString();
0267:                assertEquals(expecting, found);
0268:
0269:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0270:                        .size());
0271:            }
0272:
0273:            public void testReturnValues() throws Exception {
0274:                String action = "$i; $i.x; $u; $u.x";
0275:                String expecting = "retval.i; retval.i.x; retval.u; retval.u.x";
0276:
0277:                ErrorQueue equeue = new ErrorQueue();
0278:                ErrorManager.setErrorListener(equeue);
0279:                Grammar g = new Grammar("parser grammar t;\n"
0280:                        + "a returns [User u, int i]\n" + "        : {"
0281:                        + action + "}\n" + "        ;");
0282:                Tool antlr = newTool();
0283:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0284:                g.setCodeGenerator(generator);
0285:                generator.genRecognizer(); // forces load of templates
0286:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0287:                        generator, "a", new antlr.CommonToken(
0288:                                ANTLRParser.ACTION, action), 1);
0289:                String rawTranslation = translator.translate();
0290:                StringTemplateGroup templates = new StringTemplateGroup(".",
0291:                        AngleBracketTemplateLexer.class);
0292:                StringTemplate actionST = new StringTemplate(templates,
0293:                        rawTranslation);
0294:                String found = actionST.toString();
0295:                assertEquals(expecting, found);
0296:
0297:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0298:                        .size());
0299:            }
0300:
0301:            /* regression test for ANTLR-46 */
0302:            public void testReturnWithMultipleRuleRefs() throws Exception {
0303:                String action1 = "$obj = $rule2.obj;";
0304:                String action2 = "$obj = $rule3.obj;";
0305:                String expecting1 = "obj = rule21;";
0306:                String expecting2 = "obj = rule32;";
0307:
0308:                ErrorQueue equeue = new ErrorQueue();
0309:                ErrorManager.setErrorListener(equeue);
0310:                Grammar g = new Grammar("grammar t;\n"
0311:                        + "rule1 returns [ Object obj ]\n" + ":	rule2 { "
0312:                        + action1 + " }\n" + "|	rule3 { " + action2 + " }\n"
0313:                        + ";\n" + "rule2 returns [ Object obj ]\n"
0314:                        + ":	foo='foo' { $obj = $foo.text; }\n" + ";\n"
0315:                        + "rule3 returns [ Object obj ]\n"
0316:                        + ":	bar='bar' { $obj = $bar.text; }\n" + ";");
0317:                Tool antlr = newTool();
0318:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0319:                g.setCodeGenerator(generator);
0320:                generator.genRecognizer(); // forces load of templates
0321:                int i = 0;
0322:                String action = action1;
0323:                String expecting = expecting1;
0324:                do {
0325:                    ActionTranslatorLexer translator = new ActionTranslatorLexer(
0326:                            generator, "rule1", new antlr.CommonToken(
0327:                                    ANTLRParser.ACTION, action), i + 1);
0328:                    String rawTranslation = translator.translate();
0329:                    StringTemplateGroup templates = new StringTemplateGroup(
0330:                            ".", AngleBracketTemplateLexer.class);
0331:                    StringTemplate actionST = new StringTemplate(templates,
0332:                            rawTranslation);
0333:                    String found = actionST.toString();
0334:                    assertEquals(expecting, found);
0335:                    action = action2;
0336:                    expecting = expecting2;
0337:                } while (i++ < 1);
0338:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0339:                        .size());
0340:            }
0341:
0342:            public void testInvalidReturnValues() throws Exception {
0343:                String action = "$x";
0344:                String expecting = action;
0345:
0346:                ErrorQueue equeue = new ErrorQueue();
0347:                ErrorManager.setErrorListener(equeue);
0348:                Grammar g = new Grammar("parser grammar t;\n"
0349:                        + "a returns [User u, int i]\n" + "        : {"
0350:                        + action + "}\n" + "        ;");
0351:                Tool antlr = newTool();
0352:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0353:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0354:                        generator, "a", new antlr.CommonToken(
0355:                                ANTLRParser.ACTION, action), 1);
0356:                String rawTranslation = translator.translate();
0357:                StringTemplateGroup templates = new StringTemplateGroup(".",
0358:                        AngleBracketTemplateLexer.class);
0359:                StringTemplate actionST = new StringTemplate(templates,
0360:                        rawTranslation);
0361:                String found = actionST.toString();
0362:                assertEquals(expecting, found);
0363:
0364:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
0365:                Object expectedArg = "x";
0366:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0367:                        expectedMsgID, g, null, expectedArg);
0368:                checkError(equeue, expectedMessage);
0369:            }
0370:
0371:            public void testTokenLabels() throws Exception {
0372:                String action = "$id; $f; $id.text; $id.getText(); $id.dork "
0373:                        + "$id.type; $id.line; $id.pos; "
0374:                        + "$id.channel; $id.index;";
0375:                String expecting = "id; f; id.getText(); id.getText(); id.dork "
0376:                        + "id.getType(); id.getLine(); id.getCharPositionInLine(); "
0377:                        + "id.getChannel(); id.getTokenIndex();";
0378:
0379:                ErrorQueue equeue = new ErrorQueue();
0380:                ErrorManager.setErrorListener(equeue);
0381:                Grammar g = new Grammar("parser grammar t;\n"
0382:                        + "a : id=ID f=FLOAT {" + action + "}\n" + "  ;");
0383:                Tool antlr = newTool();
0384:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0385:                g.setCodeGenerator(generator);
0386:                generator.genRecognizer(); // forces load of templates
0387:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0388:                        generator, "a", new antlr.CommonToken(
0389:                                ANTLRParser.ACTION, action), 1);
0390:                String rawTranslation = translator.translate();
0391:                StringTemplateGroup templates = new StringTemplateGroup(".",
0392:                        AngleBracketTemplateLexer.class);
0393:                StringTemplate actionST = new StringTemplate(templates,
0394:                        rawTranslation);
0395:                String found = actionST.toString();
0396:                assertEquals(expecting, found);
0397:
0398:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0399:                        .size());
0400:            }
0401:
0402:            public void testRuleLabels() throws Exception {
0403:                String action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.stop;";
0404:                String expecting = "r.x; ((Token)r.start); ((Token)r.stop); ((Object)r.tree); r.x; ((Token)r.stop);";
0405:
0406:                ErrorQueue equeue = new ErrorQueue();
0407:                ErrorManager.setErrorListener(equeue);
0408:                Grammar g = new Grammar("parser grammar t;\n"
0409:                        + "a returns [int x]\n" + "  :\n" + "  ;\n"
0410:                        + "b : r=a {###" + action + "!!!}\n" + "  ;");
0411:                Tool antlr = newTool();
0412:                antlr.setOutputDirectory(null); // write to /dev/null
0413:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0414:                g.setCodeGenerator(generator);
0415:                generator.genRecognizer(); // codegen phase sets some vars we need
0416:                StringTemplate codeST = generator.getRecognizerST();
0417:                String code = codeST.toString();
0418:                String found = code.substring(code.indexOf("###") + 3, code
0419:                        .indexOf("!!!"));
0420:                assertEquals(expecting, found);
0421:
0422:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0423:                        .size());
0424:            }
0425:
0426:            public void testRuleLabelsWithSpecialToken() throws Exception {
0427:                String action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.stop;";
0428:                String expecting = "r.x; ((MYTOKEN)r.start); ((MYTOKEN)r.stop); ((Object)r.tree); r.x; ((MYTOKEN)r.stop);";
0429:
0430:                ErrorQueue equeue = new ErrorQueue();
0431:                ErrorManager.setErrorListener(equeue);
0432:                Grammar g = new Grammar("parser grammar t;\n"
0433:                        + "options {TokenLabelType=MYTOKEN;}\n"
0434:                        + "a returns [int x]\n" + "  :\n" + "  ;\n"
0435:                        + "b : r=a {###" + action + "!!!}\n" + "  ;");
0436:                Tool antlr = newTool();
0437:                antlr.setOutputDirectory(null); // write to /dev/null
0438:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0439:                g.setCodeGenerator(generator);
0440:                generator.genRecognizer(); // codegen phase sets some vars we need
0441:
0442:                StringTemplate codeST = generator.getRecognizerST();
0443:                String code = codeST.toString();
0444:                String found = code.substring(code.indexOf("###") + 3, code
0445:                        .indexOf("!!!"));
0446:                assertEquals(expecting, found);
0447:
0448:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0449:                        .size());
0450:            }
0451:
0452:            public void testForwardRefRuleLabels() throws Exception {
0453:                String action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.tree;";
0454:                String expecting = "r.x; ((Token)r.start); ((Token)r.stop); ((Object)r.tree); r.x; ((Object)r.tree);";
0455:
0456:                ErrorQueue equeue = new ErrorQueue();
0457:                ErrorManager.setErrorListener(equeue);
0458:                Grammar g = new Grammar("parser grammar t;\n" + "b : r=a {###"
0459:                        + action + "!!!}\n" + "  ;\n" + "a returns [int x]\n"
0460:                        + "  : ;\n");
0461:                Tool antlr = newTool();
0462:                antlr.setOutputDirectory(null); // write to /dev/null
0463:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0464:                g.setCodeGenerator(generator);
0465:                generator.genRecognizer(); // codegen phase sets some vars we need
0466:
0467:                StringTemplate codeST = generator.getRecognizerST();
0468:                String code = codeST.toString();
0469:                String found = code.substring(code.indexOf("###") + 3, code
0470:                        .indexOf("!!!"));
0471:                assertEquals(expecting, found);
0472:
0473:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0474:                        .size());
0475:            }
0476:
0477:            public void testInvalidRuleLabelAccessesParameter()
0478:                    throws Exception {
0479:                String action = "$r.z";
0480:                String expecting = action;
0481:
0482:                ErrorQueue equeue = new ErrorQueue();
0483:                ErrorManager.setErrorListener(equeue);
0484:                Grammar g = new Grammar("parser grammar t;\n"
0485:                        + "a[int z] returns [int x]\n" + "  :\n" + "  ;\n"
0486:                        + "b : r=a[3] {" + action + "}\n" + "  ;");
0487:                Tool antlr = newTool();
0488:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0489:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0490:                        generator, "b", new antlr.CommonToken(
0491:                                ANTLRParser.ACTION, action), 1);
0492:                String rawTranslation = translator.translate();
0493:                StringTemplateGroup templates = new StringTemplateGroup(".",
0494:                        AngleBracketTemplateLexer.class);
0495:                StringTemplate actionST = new StringTemplate(templates,
0496:                        rawTranslation);
0497:                String found = actionST.toString();
0498:                assertEquals(expecting, found);
0499:
0500:                int expectedMsgID = ErrorManager.MSG_INVALID_RULE_PARAMETER_REF;
0501:                Object expectedArg = "a";
0502:                Object expectedArg2 = "z";
0503:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0504:                        expectedMsgID, g, null, expectedArg, expectedArg2);
0505:                checkError(equeue, expectedMessage);
0506:            }
0507:
0508:            public void testInvalidRuleLabelAccessesScopeAttribute()
0509:                    throws Exception {
0510:                String action = "$r.n";
0511:                String expecting = action;
0512:
0513:                ErrorQueue equeue = new ErrorQueue();
0514:                ErrorManager.setErrorListener(equeue);
0515:                Grammar g = new Grammar("parser grammar t;\n" + "a\n"
0516:                        + "scope { int n; }\n" + "  :\n" + "  ;\n"
0517:                        + "b : r=a[3] {" + action + "}\n" + "  ;");
0518:                Tool antlr = newTool();
0519:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0520:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0521:                        generator, "b", new antlr.CommonToken(
0522:                                ANTLRParser.ACTION, action), 1);
0523:                String rawTranslation = translator.translate();
0524:                StringTemplateGroup templates = new StringTemplateGroup(".",
0525:                        AngleBracketTemplateLexer.class);
0526:                StringTemplate actionST = new StringTemplate(templates,
0527:                        rawTranslation);
0528:                String found = actionST.toString();
0529:                assertEquals(expecting, found);
0530:
0531:                int expectedMsgID = ErrorManager.MSG_INVALID_RULE_SCOPE_ATTRIBUTE_REF;
0532:                Object expectedArg = "a";
0533:                Object expectedArg2 = "n";
0534:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0535:                        expectedMsgID, g, null, expectedArg, expectedArg2);
0536:                checkError(equeue, expectedMessage);
0537:            }
0538:
0539:            public void testInvalidRuleAttribute() throws Exception {
0540:                String action = "$r.blort";
0541:                String expecting = action;
0542:
0543:                ErrorQueue equeue = new ErrorQueue();
0544:                ErrorManager.setErrorListener(equeue);
0545:                Grammar g = new Grammar("parser grammar t;\n"
0546:                        + "a[int z] returns [int x]\n" + "  :\n" + "  ;\n"
0547:                        + "b : r=a[3] {" + action + "}\n" + "  ;");
0548:                Tool antlr = newTool();
0549:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0550:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0551:                        generator, "b", new antlr.CommonToken(
0552:                                ANTLRParser.ACTION, action), 1);
0553:                String rawTranslation = translator.translate();
0554:                StringTemplateGroup templates = new StringTemplateGroup(".",
0555:                        AngleBracketTemplateLexer.class);
0556:                StringTemplate actionST = new StringTemplate(templates,
0557:                        rawTranslation);
0558:                String found = actionST.toString();
0559:                assertEquals(expecting, found);
0560:
0561:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_RULE_ATTRIBUTE;
0562:                Object expectedArg = "a";
0563:                Object expectedArg2 = "blort";
0564:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0565:                        expectedMsgID, g, null, expectedArg, expectedArg2);
0566:                checkError(equeue, expectedMessage);
0567:            }
0568:
0569:            public void testMissingRuleAttribute() throws Exception {
0570:                String action = "$r";
0571:                String expecting = action;
0572:
0573:                ErrorQueue equeue = new ErrorQueue();
0574:                ErrorManager.setErrorListener(equeue);
0575:                Grammar g = new Grammar("parser grammar t;\n"
0576:                        + "a[int z] returns [int x]\n" + "  :\n" + "  ;\n"
0577:                        + "b : r=a[3] {" + action + "}\n" + "  ;");
0578:                Tool antlr = newTool();
0579:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0580:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0581:                        generator, "b", new antlr.CommonToken(
0582:                                ANTLRParser.ACTION, action), 1);
0583:                String rawTranslation = translator.translate();
0584:
0585:                int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE;
0586:                Object expectedArg = "r";
0587:                Object expectedArg2 = null;
0588:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0589:                        expectedMsgID, g, null, expectedArg, expectedArg2);
0590:                checkError(equeue, expectedMessage);
0591:            }
0592:
0593:            public void testMissingUnlabeledRuleAttribute() throws Exception {
0594:                String action = "$a";
0595:                String expecting = action;
0596:
0597:                ErrorQueue equeue = new ErrorQueue();
0598:                ErrorManager.setErrorListener(equeue);
0599:                Grammar g = new Grammar("parser grammar t;\n"
0600:                        + "a returns [int x]:\n" + "  ;\n" + "b : a {" + action
0601:                        + "}\n" + "  ;");
0602:                Tool antlr = newTool();
0603:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0604:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0605:                        generator, "b", new antlr.CommonToken(
0606:                                ANTLRParser.ACTION, action), 1);
0607:                String rawTranslation = translator.translate();
0608:
0609:                int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE;
0610:                Object expectedArg = "a";
0611:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0612:                        expectedMsgID, g, null, expectedArg);
0613:                checkError(equeue, expectedMessage);
0614:            }
0615:
0616:            public void testNonDynamicAttributeOutsideRule() throws Exception {
0617:                String action = "public void foo() { $x; }";
0618:                String expecting = action;
0619:
0620:                ErrorQueue equeue = new ErrorQueue();
0621:                ErrorManager.setErrorListener(equeue);
0622:                Grammar g = new Grammar("parser grammar t;\n"
0623:                        + "@members {'+action+'}\n" + "a : ;\n");
0624:                Tool antlr = newTool();
0625:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0626:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0627:                        generator, null, new antlr.CommonToken(
0628:                                ANTLRParser.ACTION, action), 0);
0629:                String rawTranslation = translator.translate();
0630:                StringTemplateGroup templates = new StringTemplateGroup(".",
0631:                        AngleBracketTemplateLexer.class);
0632:                StringTemplate actionST = new StringTemplate(templates,
0633:                        rawTranslation);
0634:                String found = actionST.toString();
0635:                assertEquals(expecting, found);
0636:
0637:                int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE;
0638:                Object expectedArg = "x";
0639:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0640:                        expectedMsgID, g, null, expectedArg);
0641:                checkError(equeue, expectedMessage);
0642:            }
0643:
0644:            public void testNonDynamicAttributeOutsideRule2() throws Exception {
0645:                String action = "public void foo() { $x.y; }";
0646:                String expecting = action;
0647:
0648:                ErrorQueue equeue = new ErrorQueue();
0649:                ErrorManager.setErrorListener(equeue);
0650:                Grammar g = new Grammar("parser grammar t;\n"
0651:                        + "@members {'+action+'}\n" + "a : ;\n");
0652:                Tool antlr = newTool();
0653:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0654:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0655:                        generator, null, new antlr.CommonToken(
0656:                                ANTLRParser.ACTION, action), 0);
0657:                String rawTranslation = translator.translate();
0658:                StringTemplateGroup templates = new StringTemplateGroup(".",
0659:                        AngleBracketTemplateLexer.class);
0660:                StringTemplate actionST = new StringTemplate(templates,
0661:                        rawTranslation);
0662:                String found = actionST.toString();
0663:                assertEquals(expecting, found);
0664:
0665:                int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE;
0666:                Object expectedArg = "x";
0667:                Object expectedArg2 = "y";
0668:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0669:                        expectedMsgID, g, null, expectedArg, expectedArg2);
0670:                checkError(equeue, expectedMessage);
0671:            }
0672:
0673:            // D Y N A M I C A L L Y  S C O P E D  A T T R I B U T E S
0674:
0675:            public void testBasicGlobalScope() throws Exception {
0676:                String action = "$Symbols::names.add($id.text);";
0677:                String expecting = "((Symbols_scope)Symbols_stack.peek()).names.add(id.getText());";
0678:
0679:                ErrorQueue equeue = new ErrorQueue();
0680:                ErrorManager.setErrorListener(equeue);
0681:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
0682:                        + "  int n;\n" + "  List names;\n" + "}\n"
0683:                        + "a scope Symbols; : (id=ID ';' {" + action + "} )+\n"
0684:                        + "  ;\n" + "ID : 'a';\n");
0685:                Tool antlr = newTool();
0686:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0687:                g.setCodeGenerator(generator);
0688:                generator.genRecognizer(); // forces load of templates
0689:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0690:                        generator, "a", new antlr.CommonToken(
0691:                                ANTLRParser.ACTION, action), 1);
0692:                String rawTranslation = translator.translate();
0693:                StringTemplateGroup templates = new StringTemplateGroup(".",
0694:                        AngleBracketTemplateLexer.class);
0695:                StringTemplate actionST = new StringTemplate(templates,
0696:                        rawTranslation);
0697:                String found = actionST.toString();
0698:                assertEquals(expecting, found);
0699:
0700:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0701:                        .size());
0702:            }
0703:
0704:            public void testUnknownGlobalScope() throws Exception {
0705:                String action = "$Symbols::names.add($id.text);";
0706:
0707:                ErrorQueue equeue = new ErrorQueue();
0708:                ErrorManager.setErrorListener(equeue);
0709:                Grammar g = new Grammar("grammar t;\n"
0710:                        + "a scope Symbols; : (id=ID ';' {" + action + "} )+\n"
0711:                        + "  ;\n" + "ID : 'a';\n");
0712:                Tool antlr = newTool();
0713:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0714:                g.setCodeGenerator(generator);
0715:                generator.genRecognizer(); // forces load of templates
0716:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0717:                        generator, "a", new antlr.CommonToken(
0718:                                ANTLRParser.ACTION, action), 1);
0719:
0720:                assertEquals("unexpected errors: " + equeue, 2, equeue.errors
0721:                        .size());
0722:
0723:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE;
0724:                Object expectedArg = "Symbols";
0725:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0726:                        expectedMsgID, g, null, expectedArg);
0727:                checkError(equeue, expectedMessage);
0728:            }
0729:
0730:            public void testIndexedGlobalScope() throws Exception {
0731:                String action = "$Symbols[-1]::names.add($id.text);";
0732:                String expecting = "((Symbols_scope)Symbols_stack.elementAt(Symbols_stack.size()-1-1)).names.add(id.getText());";
0733:
0734:                ErrorQueue equeue = new ErrorQueue();
0735:                ErrorManager.setErrorListener(equeue);
0736:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
0737:                        + "  int n;\n" + "  List names;\n" + "}\n"
0738:                        + "a scope Symbols; : (id=ID ';' {" + action + "} )+\n"
0739:                        + "  ;\n" + "ID : 'a';\n");
0740:                Tool antlr = newTool();
0741:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0742:                g.setCodeGenerator(generator);
0743:                generator.genRecognizer(); // forces load of templates
0744:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0745:                        generator, "a", new antlr.CommonToken(
0746:                                ANTLRParser.ACTION, action), 1);
0747:                String rawTranslation = translator.translate();
0748:                StringTemplateGroup templates = new StringTemplateGroup(".",
0749:                        AngleBracketTemplateLexer.class);
0750:                StringTemplate actionST = new StringTemplate(templates,
0751:                        rawTranslation);
0752:                String found = actionST.toString();
0753:                assertEquals(expecting, found);
0754:
0755:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0756:                        .size());
0757:            }
0758:
0759:            public void test0IndexedGlobalScope() throws Exception {
0760:                String action = "$Symbols[0]::names.add($id.text);";
0761:                String expecting = "((Symbols_scope)Symbols_stack.elementAt(0)).names.add(id.getText());";
0762:
0763:                ErrorQueue equeue = new ErrorQueue();
0764:                ErrorManager.setErrorListener(equeue);
0765:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
0766:                        + "  int n;\n" + "  List names;\n" + "}\n"
0767:                        + "a scope Symbols; : (id=ID ';' {" + action + "} )+\n"
0768:                        + "  ;\n" + "ID : 'a';\n");
0769:                Tool antlr = newTool();
0770:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0771:                g.setCodeGenerator(generator);
0772:                generator.genRecognizer(); // forces load of templates
0773:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0774:                        generator, "a", new antlr.CommonToken(
0775:                                ANTLRParser.ACTION, action), 1);
0776:                String rawTranslation = translator.translate();
0777:                StringTemplateGroup templates = new StringTemplateGroup(".",
0778:                        AngleBracketTemplateLexer.class);
0779:                StringTemplate actionST = new StringTemplate(templates,
0780:                        rawTranslation);
0781:                String found = actionST.toString();
0782:                assertEquals(expecting, found);
0783:
0784:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0785:                        .size());
0786:            }
0787:
0788:            public void testAbsoluteIndexedGlobalScope() throws Exception {
0789:                String action = "$Symbols[3]::names.add($id.text);";
0790:                String expecting = "((Symbols_scope)Symbols_stack.elementAt(3)).names.add(id.getText());";
0791:
0792:                ErrorQueue equeue = new ErrorQueue();
0793:                ErrorManager.setErrorListener(equeue);
0794:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
0795:                        + "  int n;\n" + "  List names;\n" + "}\n"
0796:                        + "a scope Symbols; : (id=ID ';' {" + action + "} )+\n"
0797:                        + "  ;\n" + "ID : 'a';\n");
0798:                Tool antlr = newTool();
0799:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0800:                g.setCodeGenerator(generator);
0801:                generator.genRecognizer(); // forces load of templates
0802:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0803:                        generator, "a", new antlr.CommonToken(
0804:                                ANTLRParser.ACTION, action), 1);
0805:                String rawTranslation = translator.translate();
0806:                StringTemplateGroup templates = new StringTemplateGroup(".",
0807:                        AngleBracketTemplateLexer.class);
0808:                StringTemplate actionST = new StringTemplate(templates,
0809:                        rawTranslation);
0810:                String found = actionST.toString();
0811:                assertEquals(expecting, found);
0812:
0813:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0814:                        .size());
0815:            }
0816:
0817:            public void testScopeAndAttributeWithUnderscore() throws Exception {
0818:                String action = "$foo_bar::a_b;";
0819:                String expecting = "((foo_bar_scope)foo_bar_stack.peek()).a_b;";
0820:
0821:                ErrorQueue equeue = new ErrorQueue();
0822:                ErrorManager.setErrorListener(equeue);
0823:                Grammar g = new Grammar("grammar t;\n" + "scope foo_bar {\n"
0824:                        + "  int a_b;\n" + "}\n" + "a scope foo_bar; : (ID {"
0825:                        + action + "} )+\n" + "  ;\n" + "ID : 'a';\n");
0826:                Tool antlr = newTool();
0827:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0828:                g.setCodeGenerator(generator);
0829:                generator.genRecognizer(); // forces load of templates
0830:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0831:                        generator, "a", new antlr.CommonToken(
0832:                                ANTLRParser.ACTION, action), 1);
0833:                String rawTranslation = translator.translate();
0834:                StringTemplateGroup templates = new StringTemplateGroup(".",
0835:                        AngleBracketTemplateLexer.class);
0836:                StringTemplate actionST = new StringTemplate(templates,
0837:                        rawTranslation);
0838:                String found = actionST.toString();
0839:
0840:                assertEquals(expecting, found);
0841:
0842:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0843:                        .size());
0844:            }
0845:
0846:            public void testSharedGlobalScope() throws Exception {
0847:                String action = "$Symbols::x;";
0848:                String expecting = "((Symbols_scope)Symbols_stack.peek()).x;";
0849:
0850:                ErrorQueue equeue = new ErrorQueue();
0851:                ErrorManager.setErrorListener(equeue);
0852:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
0853:                        + "  String x;\n" + "}\n" + "a\n"
0854:                        + "scope { int y; }\n" + "scope Symbols;\n" + " : b {"
0855:                        + action + "}\n" + " ;\n"
0856:                        + "b : ID {$Symbols::x=$ID.text} ;\n" + "ID : 'a';\n");
0857:                Tool antlr = newTool();
0858:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0859:                g.setCodeGenerator(generator);
0860:                generator.genRecognizer(); // forces load of templates
0861:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0862:                        generator, "a", new antlr.CommonToken(
0863:                                ANTLRParser.ACTION, action), 1);
0864:                String rawTranslation = translator.translate();
0865:                StringTemplateGroup templates = new StringTemplateGroup(".",
0866:                        AngleBracketTemplateLexer.class);
0867:                StringTemplate actionST = new StringTemplate(templates,
0868:                        rawTranslation);
0869:                String found = actionST.toString();
0870:                assertEquals(expecting, found);
0871:
0872:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0873:                        .size());
0874:            }
0875:
0876:            public void testGlobalScopeOutsideRule() throws Exception {
0877:                String action = "public void foo() {$Symbols::names.add('foo');}";
0878:                String expecting = "public void foo() {((Symbols_scope)Symbols_stack.peek()).names.add('foo');}";
0879:
0880:                ErrorQueue equeue = new ErrorQueue();
0881:                ErrorManager.setErrorListener(equeue);
0882:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
0883:                        + "  int n;\n" + "  List names;\n" + "}\n"
0884:                        + "@members {'+action+'}\n" + "a : \n" + "  ;\n");
0885:                Tool antlr = newTool();
0886:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0887:                g.setCodeGenerator(generator);
0888:                generator.genRecognizer(); // forces load of templates
0889:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0890:                        generator, "a", new antlr.CommonToken(
0891:                                ANTLRParser.ACTION, action), 1);
0892:                String rawTranslation = translator.translate();
0893:                StringTemplateGroup templates = new StringTemplateGroup(".",
0894:                        AngleBracketTemplateLexer.class);
0895:                StringTemplate actionST = new StringTemplate(templates,
0896:                        rawTranslation);
0897:                String found = actionST.toString();
0898:                assertEquals(expecting, found);
0899:
0900:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0901:                        .size());
0902:            }
0903:
0904:            public void testRuleScopeOutsideRule() throws Exception {
0905:                String action = "public void foo() {$a::name;}";
0906:                String expecting = "public void foo() {((a_scope)a_stack.peek()).name;}";
0907:
0908:                ErrorQueue equeue = new ErrorQueue();
0909:                ErrorManager.setErrorListener(equeue);
0910:                Grammar g = new Grammar("grammar t;\n" + "@members {" + action
0911:                        + "}\n" + "a\n" + "scope { int name; }\n"
0912:                        + "  : {foo();}\n" + "  ;\n");
0913:                Tool antlr = newTool();
0914:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0915:                g.setCodeGenerator(generator);
0916:                generator.genRecognizer(); // forces load of templates
0917:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0918:                        generator, null, new antlr.CommonToken(
0919:                                ANTLRParser.ACTION, action), 0);
0920:                String rawTranslation = translator.translate();
0921:                StringTemplateGroup templates = new StringTemplateGroup(".",
0922:                        AngleBracketTemplateLexer.class);
0923:                StringTemplate actionST = new StringTemplate(templates,
0924:                        rawTranslation);
0925:                String found = actionST.toString();
0926:                assertEquals(expecting, found);
0927:
0928:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0929:                        .size());
0930:            }
0931:
0932:            public void testBasicRuleScope() throws Exception {
0933:                String action = "$a::n;";
0934:                String expecting = "((a_scope)a_stack.peek()).n;";
0935:
0936:                ErrorQueue equeue = new ErrorQueue();
0937:                ErrorManager.setErrorListener(equeue);
0938:                Grammar g = new Grammar("grammar t;\n" + "a\n" + "scope {\n"
0939:                        + "  int n;\n" + "} : {" + action + "}\n" + "  ;\n");
0940:                Tool antlr = newTool();
0941:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0942:                g.setCodeGenerator(generator);
0943:                generator.genRecognizer(); // forces load of templates
0944:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0945:                        generator, "a", new antlr.CommonToken(
0946:                                ANTLRParser.ACTION, action), 1);
0947:                String rawTranslation = translator.translate();
0948:                StringTemplateGroup templates = new StringTemplateGroup(".",
0949:                        AngleBracketTemplateLexer.class);
0950:                StringTemplate actionST = new StringTemplate(templates,
0951:                        rawTranslation);
0952:                String found = actionST.toString();
0953:                assertEquals(expecting, found);
0954:
0955:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
0956:                        .size());
0957:            }
0958:
0959:            public void testUnqualifiedRuleScopeAccessInsideRule()
0960:                    throws Exception {
0961:                String action = "$n;";
0962:                String expecting = action;
0963:
0964:                ErrorQueue equeue = new ErrorQueue();
0965:                ErrorManager.setErrorListener(equeue);
0966:                Grammar g = new Grammar("grammar t;\n" + "a\n" + "scope {\n"
0967:                        + "  int n;\n" + "} : {" + action + "}\n" + "  ;\n");
0968:                Tool antlr = newTool();
0969:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0970:                g.setCodeGenerator(generator);
0971:                generator.genRecognizer(); // forces load of templates
0972:
0973:                int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_ATTRIBUTE;
0974:                Object expectedArg = "n";
0975:                Object expectedArg2 = null;
0976:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
0977:                        expectedMsgID, g, null, expectedArg, expectedArg2);
0978:                checkError(equeue, expectedMessage);
0979:            }
0980:
0981:            public void testIsolatedDynamicRuleScopeRef() throws Exception {
0982:                String action = "$a;"; // refers to stack not top of stack
0983:                String expecting = "a_stack;";
0984:
0985:                ErrorQueue equeue = new ErrorQueue();
0986:                ErrorManager.setErrorListener(equeue);
0987:                Grammar g = new Grammar("grammar t;\n" + "a\n" + "scope {\n"
0988:                        + "  int n;\n" + "} : b ;\n" + "b : {" + action + "}\n"
0989:                        + "  ;\n");
0990:                Tool antlr = newTool();
0991:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
0992:                g.setCodeGenerator(generator);
0993:                generator.genRecognizer(); // forces load of templates
0994:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
0995:                        generator, "b", new antlr.CommonToken(
0996:                                ANTLRParser.ACTION, action), 1);
0997:                String rawTranslation = translator.translate();
0998:                StringTemplateGroup templates = new StringTemplateGroup(".",
0999:                        AngleBracketTemplateLexer.class);
1000:                StringTemplate actionST = new StringTemplate(templates,
1001:                        rawTranslation);
1002:                String found = actionST.toString();
1003:                assertEquals(expecting, found);
1004:
1005:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1006:                        .size());
1007:            }
1008:
1009:            public void testDynamicRuleScopeRefInSubrule() throws Exception {
1010:                String action = "$a::n;";
1011:                String expecting = "((a_scope)a_stack.peek()).n;";
1012:
1013:                ErrorQueue equeue = new ErrorQueue();
1014:                ErrorManager.setErrorListener(equeue);
1015:                Grammar g = new Grammar("grammar t;\n" + "a\n" + "scope {\n"
1016:                        + "  int n;\n" + "} : b ;\n" + "b : {" + action + "}\n"
1017:                        + "  ;\n");
1018:                Tool antlr = newTool();
1019:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1020:                g.setCodeGenerator(generator);
1021:                generator.genRecognizer(); // forces load of templates
1022:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1023:                        generator, "b", new antlr.CommonToken(
1024:                                ANTLRParser.ACTION, action), 1);
1025:                String rawTranslation = translator.translate();
1026:                StringTemplateGroup templates = new StringTemplateGroup(".",
1027:                        AngleBracketTemplateLexer.class);
1028:                StringTemplate actionST = new StringTemplate(templates,
1029:                        rawTranslation);
1030:                String found = actionST.toString();
1031:                assertEquals(expecting, found);
1032:
1033:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1034:                        .size());
1035:            }
1036:
1037:            public void testIsolatedGlobalScopeRef() throws Exception {
1038:                String action = "$Symbols;";
1039:                String expecting = "Symbols_stack;";
1040:
1041:                ErrorQueue equeue = new ErrorQueue();
1042:                ErrorManager.setErrorListener(equeue);
1043:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
1044:                        + "  String x;\n" + "}\n" + "a\n"
1045:                        + "scope { int y; }\n" + "scope Symbols;\n" + " : b {"
1046:                        + action + "}\n" + " ;\n"
1047:                        + "b : ID {$Symbols::x=$ID.text} ;\n" + "ID : 'a';\n");
1048:                Tool antlr = newTool();
1049:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1050:                g.setCodeGenerator(generator);
1051:                generator.genRecognizer(); // forces load of templates
1052:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1053:                        generator, "a", new antlr.CommonToken(
1054:                                ANTLRParser.ACTION, action), 1);
1055:                String rawTranslation = translator.translate();
1056:                StringTemplateGroup templates = new StringTemplateGroup(".",
1057:                        AngleBracketTemplateLexer.class);
1058:                StringTemplate actionST = new StringTemplate(templates,
1059:                        rawTranslation);
1060:                String found = actionST.toString();
1061:                assertEquals(expecting, found);
1062:
1063:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1064:                        .size());
1065:            }
1066:
1067:            public void testRuleScopeFromAnotherRule() throws Exception {
1068:                String action = "$a::n;"; // must be qualified
1069:                String expecting = "((a_scope)a_stack.peek()).n;";
1070:
1071:                ErrorQueue equeue = new ErrorQueue();
1072:                ErrorManager.setErrorListener(equeue);
1073:                Grammar g = new Grammar("grammar t;\n" + "a\n" + "scope {\n"
1074:                        + "  int n;\n" + "} : b\n" + "  ;\n" + "b : {" + action
1075:                        + "}\n" + "  ;\n");
1076:                Tool antlr = newTool();
1077:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1078:                g.setCodeGenerator(generator);
1079:                generator.genRecognizer(); // forces load of templates
1080:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1081:                        generator, "b", new antlr.CommonToken(
1082:                                ANTLRParser.ACTION, action), 1);
1083:                String rawTranslation = translator.translate();
1084:                StringTemplateGroup templates = new StringTemplateGroup(".",
1085:                        AngleBracketTemplateLexer.class);
1086:                StringTemplate actionST = new StringTemplate(templates,
1087:                        rawTranslation);
1088:                String found = actionST.toString();
1089:                assertEquals(expecting, found);
1090:
1091:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1092:                        .size());
1093:            }
1094:
1095:            public void testFullyQualifiedRefToCurrentRuleParameter()
1096:                    throws Exception {
1097:                String action = "$a.i;";
1098:                String expecting = "i;";
1099:
1100:                ErrorQueue equeue = new ErrorQueue();
1101:                ErrorManager.setErrorListener(equeue);
1102:                Grammar g = new Grammar("grammar t;\n" + "a[int i]: {" + action
1103:                        + "}\n" + "  ;\n");
1104:                Tool antlr = newTool();
1105:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1106:                g.setCodeGenerator(generator);
1107:                generator.genRecognizer(); // forces load of templates
1108:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1109:                        generator, "a", new antlr.CommonToken(
1110:                                ANTLRParser.ACTION, action), 1);
1111:                String rawTranslation = translator.translate();
1112:                StringTemplateGroup templates = new StringTemplateGroup(".",
1113:                        AngleBracketTemplateLexer.class);
1114:                StringTemplate actionST = new StringTemplate(templates,
1115:                        rawTranslation);
1116:                String found = actionST.toString();
1117:                assertEquals(expecting, found);
1118:
1119:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1120:                        .size());
1121:            }
1122:
1123:            public void testFullyQualifiedRefToCurrentRuleRetVal()
1124:                    throws Exception {
1125:                String action = "$a.i;";
1126:                String expecting = "retval.i;";
1127:
1128:                ErrorQueue equeue = new ErrorQueue();
1129:                ErrorManager.setErrorListener(equeue);
1130:                Grammar g = new Grammar("grammar t;\n"
1131:                        + "a returns [int i, int j]: {" + action + "}\n"
1132:                        + "  ;\n");
1133:                Tool antlr = newTool();
1134:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1135:                g.setCodeGenerator(generator);
1136:                generator.genRecognizer(); // forces load of templates
1137:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1138:                        generator, "a", new antlr.CommonToken(
1139:                                ANTLRParser.ACTION, action), 1);
1140:                String rawTranslation = translator.translate();
1141:                StringTemplateGroup templates = new StringTemplateGroup(".",
1142:                        AngleBracketTemplateLexer.class);
1143:                StringTemplate actionST = new StringTemplate(templates,
1144:                        rawTranslation);
1145:                String found = actionST.toString();
1146:                assertEquals(expecting, found);
1147:
1148:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1149:                        .size());
1150:            }
1151:
1152:            public void testSetFullyQualifiedRefToCurrentRuleRetVal()
1153:                    throws Exception {
1154:                String action = "$a.i = 1;";
1155:                String expecting = "retval.i = 1;";
1156:
1157:                ErrorQueue equeue = new ErrorQueue();
1158:                ErrorManager.setErrorListener(equeue);
1159:                Grammar g = new Grammar("grammar t;\n"
1160:                        + "a returns [int i, int j]: {" + action + "}\n"
1161:                        + "  ;\n");
1162:                Tool antlr = newTool();
1163:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1164:                g.setCodeGenerator(generator);
1165:                generator.genRecognizer(); // forces load of templates
1166:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1167:                        generator, "a", new antlr.CommonToken(
1168:                                ANTLRParser.ACTION, action), 1);
1169:                String rawTranslation = translator.translate();
1170:                StringTemplateGroup templates = new StringTemplateGroup(".",
1171:                        AngleBracketTemplateLexer.class);
1172:                StringTemplate actionST = new StringTemplate(templates,
1173:                        rawTranslation);
1174:                String found = actionST.toString();
1175:                assertEquals(expecting, found);
1176:
1177:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1178:                        .size());
1179:            }
1180:
1181:            public void testIsolatedRefToCurrentRule() throws Exception {
1182:                String action = "$a;";
1183:                String expecting = "";
1184:
1185:                ErrorQueue equeue = new ErrorQueue();
1186:                ErrorManager.setErrorListener(equeue);
1187:                Grammar g = new Grammar("grammar t;\n" + "a : 'a' {" + action
1188:                        + "}\n" + "  ;\n");
1189:                Tool antlr = newTool();
1190:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1191:                g.setCodeGenerator(generator);
1192:                generator.genRecognizer(); // forces load of templates
1193:
1194:                int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE;
1195:                Object expectedArg = "a";
1196:                Object expectedArg2 = null;
1197:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1198:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1199:                checkError(equeue, expectedMessage);
1200:            }
1201:
1202:            public void testIsolatedRefToRule() throws Exception {
1203:                String action = "$x;";
1204:
1205:                ErrorQueue equeue = new ErrorQueue();
1206:                ErrorManager.setErrorListener(equeue);
1207:                Grammar g = new Grammar("grammar t;\n" + "a : x=b {" + action
1208:                        + "}\n" + "  ;\n" + "b : 'b' ;\n");
1209:                Tool antlr = newTool();
1210:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1211:                g.setCodeGenerator(generator);
1212:                generator.genRecognizer(); // forces load of templates
1213:
1214:                int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE;
1215:                Object expectedArg = "x";
1216:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1217:                        expectedMsgID, g, null, expectedArg);
1218:                checkError(equeue, expectedMessage);
1219:            }
1220:
1221:            /*  I think these have to be errors $a.x makes no sense.
1222:            public void testFullyQualifiedRefToLabelInCurrentRule() throws Exception {
1223:            		String action = "$a.x;";
1224:            		String expecting = "x;";
1225:
1226:            		ErrorQueue equeue = new ErrorQueue();
1227:            		ErrorManager.setErrorListener(equeue);
1228:            		Grammar g = new Grammar(
1229:            			"grammar t;\n"+
1230:            				"a : x='a' {"+action+"}\n" +
1231:            				"  ;\n");
1232:            		Tool antlr = newTool();
1233:            		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1234:            		g.setCodeGenerator(generator);
1235:            		generator.genRecognizer(); // forces load of templates
1236:            		ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"a",
1237:            														   new antlr.CommonToken(ANTLRParser.ACTION,action),1);
1238:            		String rawTranslation =
1239:            			translator.translate();
1240:            		StringTemplateGroup templates =
1241:            			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
1242:            		StringTemplate actionST = new StringTemplate(templates, rawTranslation);
1243:            		String found = actionST.toString();
1244:            		assertEquals(expecting, found);
1245:
1246:            		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
1247:            	}
1248:
1249:            public void testFullyQualifiedRefToListLabelInCurrentRule() throws Exception {
1250:            	String action = "$a.x;"; // must be qualified
1251:            	String expecting = "list_x;";
1252:
1253:            	ErrorQueue equeue = new ErrorQueue();
1254:            	ErrorManager.setErrorListener(equeue);
1255:            	Grammar g = new Grammar(
1256:            		"grammar t;\n"+
1257:            			"a : x+='a' {"+action+"}\n" +
1258:            			"  ;\n");
1259:            	Tool antlr = newTool();
1260:            	CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1261:            	g.setCodeGenerator(generator);
1262:            	generator.genRecognizer(); // forces load of templates
1263:            	ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"a",
1264:            													   new antlr.CommonToken(ANTLRParser.ACTION,action),1);
1265:            	String rawTranslation =
1266:            		translator.translate();
1267:            	StringTemplateGroup templates =
1268:            		new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
1269:            	StringTemplate actionST = new StringTemplate(templates, rawTranslation);
1270:            	String found = actionST.toString();
1271:            	assertEquals(expecting, found);
1272:
1273:            	assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
1274:            }
1275:             */
1276:            public void testFullyQualifiedRefToTemplateAttributeInCurrentRule()
1277:                    throws Exception {
1278:                String action = "$a.st;"; // can be qualified
1279:                String expecting = "retval.st;";
1280:
1281:                ErrorQueue equeue = new ErrorQueue();
1282:                ErrorManager.setErrorListener(equeue);
1283:                Grammar g = new Grammar("parser grammar t;\n"
1284:                        + "options {output=template;}\n"
1285:                        + "a : (A->{$A.text}) {" + action + "}\n" + "  ;\n");
1286:                Tool antlr = newTool();
1287:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1288:                g.setCodeGenerator(generator);
1289:                generator.genRecognizer(); // forces load of templates
1290:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1291:                        generator, "a", new antlr.CommonToken(
1292:                                ANTLRParser.ACTION, action), 1);
1293:                String rawTranslation = translator.translate();
1294:                StringTemplateGroup templates = new StringTemplateGroup(".",
1295:                        AngleBracketTemplateLexer.class);
1296:                StringTemplate actionST = new StringTemplate(templates,
1297:                        rawTranslation);
1298:                String found = actionST.toString();
1299:                assertEquals(expecting, found);
1300:
1301:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1302:                        .size());
1303:            }
1304:
1305:            public void testRuleRefWhenRuleHasScope() throws Exception {
1306:                String action = "$b.start;";
1307:                String expecting = "((Token)b1.start);";
1308:
1309:                ErrorQueue equeue = new ErrorQueue();
1310:                ErrorManager.setErrorListener(equeue);
1311:                Grammar g = new Grammar("grammar t;\n" + "a : b {###" + action
1312:                        + "!!!} ;\n" + "b\n" + "scope {\n" + "  int n;\n"
1313:                        + "} : 'b' \n" + "  ;\n");
1314:                Tool antlr = newTool();
1315:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1316:                g.setCodeGenerator(generator);
1317:                generator.genRecognizer(); // forces load of templates
1318:
1319:                StringTemplate codeST = generator.getRecognizerST();
1320:                String code = codeST.toString();
1321:                String found = code.substring(code.indexOf("###") + 3, code
1322:                        .indexOf("!!!"));
1323:                assertEquals(expecting, found);
1324:
1325:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1326:                        .size());
1327:            }
1328:
1329:            public void testDynamicScopeRefOkEvenThoughRuleRefExists()
1330:                    throws Exception {
1331:                String action = "$b::n;";
1332:                String expecting = "((b_scope)b_stack.peek()).n;";
1333:
1334:                ErrorQueue equeue = new ErrorQueue();
1335:                ErrorManager.setErrorListener(equeue);
1336:                Grammar g = new Grammar("grammar t;\n" + "s : b ;\n" + "b\n"
1337:                        + "scope {\n" + "  int n;\n" + "} : '(' b ')' {"
1338:                        + action + "}\n" + // refers to current invocation's n
1339:                        "  ;\n");
1340:                Tool antlr = newTool();
1341:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1342:                g.setCodeGenerator(generator);
1343:                generator.genRecognizer(); // forces load of templates
1344:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1345:                        generator, "b", new antlr.CommonToken(
1346:                                ANTLRParser.ACTION, action), 1);
1347:                String rawTranslation = translator.translate();
1348:                StringTemplateGroup templates = new StringTemplateGroup(".",
1349:                        AngleBracketTemplateLexer.class);
1350:                StringTemplate actionST = new StringTemplate(templates,
1351:                        rawTranslation);
1352:                String found = actionST.toString();
1353:                assertEquals(expecting, found);
1354:
1355:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1356:                        .size());
1357:            }
1358:
1359:            public void testRefToTemplateAttributeForCurrentRule()
1360:                    throws Exception {
1361:                String action = "$st=null;";
1362:                String expecting = "retval.st =null;";
1363:
1364:                ErrorQueue equeue = new ErrorQueue();
1365:                ErrorManager.setErrorListener(equeue);
1366:                Grammar g = new Grammar("parser grammar t;\n"
1367:                        + "options {output=template;}\n" + "a : {" + action
1368:                        + "}\n" + "  ;\n");
1369:                Tool antlr = newTool();
1370:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1371:                g.setCodeGenerator(generator);
1372:                generator.genRecognizer(); // forces load of templates
1373:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1374:                        generator, "a", new antlr.CommonToken(
1375:                                ANTLRParser.ACTION, action), 1);
1376:                String rawTranslation = translator.translate();
1377:                StringTemplateGroup templates = new StringTemplateGroup(".",
1378:                        AngleBracketTemplateLexer.class);
1379:                StringTemplate actionST = new StringTemplate(templates,
1380:                        rawTranslation);
1381:                String found = actionST.toString();
1382:                assertEquals(expecting, found);
1383:
1384:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1385:                        .size());
1386:            }
1387:
1388:            public void testRefToTextAttributeForCurrentRule() throws Exception {
1389:                String action = "$text";
1390:                String expecting = "input.toString(retval.start,input.LT(-1))";
1391:
1392:                ErrorQueue equeue = new ErrorQueue();
1393:                ErrorManager.setErrorListener(equeue);
1394:                Grammar g = new Grammar("parser grammar t;\n"
1395:                        + "options {output=template;}\n" + "a : {" + action
1396:                        + "}\n" + "  ;\n");
1397:                Tool antlr = newTool();
1398:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1399:                g.setCodeGenerator(generator);
1400:                generator.genRecognizer(); // forces load of templates
1401:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1402:                        generator, "a", new antlr.CommonToken(
1403:                                ANTLRParser.ACTION, action), 1);
1404:                String rawTranslation = translator.translate();
1405:                StringTemplateGroup templates = new StringTemplateGroup(".",
1406:                        AngleBracketTemplateLexer.class);
1407:                StringTemplate actionST = new StringTemplate(templates,
1408:                        rawTranslation);
1409:                String found = actionST.toString();
1410:                assertEquals(expecting, found);
1411:
1412:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1413:                        .size());
1414:            }
1415:
1416:            public void testRefToStartAttributeForCurrentRule()
1417:                    throws Exception {
1418:                String action = "$start;";
1419:                String expecting = "((Token)retval.start);";
1420:
1421:                ErrorQueue equeue = new ErrorQueue();
1422:                ErrorManager.setErrorListener(equeue);
1423:                Grammar g = new Grammar("parser grammar t;\n" + "a : {###"
1424:                        + action + "!!!}\n" + "  ;\n");
1425:                Tool antlr = newTool();
1426:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1427:                g.setCodeGenerator(generator);
1428:                generator.genRecognizer(); // forces load of templates
1429:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1430:                        generator, "a", new antlr.CommonToken(
1431:                                ANTLRParser.ACTION, action), 1);
1432:                StringTemplate codeST = generator.getRecognizerST();
1433:                String code = codeST.toString();
1434:                String found = code.substring(code.indexOf("###") + 3, code
1435:                        .indexOf("!!!"));
1436:                assertEquals(expecting, found);
1437:
1438:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1439:                        .size());
1440:            }
1441:
1442:            public void testTokenLabelFromMultipleAlts() throws Exception {
1443:                String action = "$ID.text;"; // must be qualified
1444:                String action2 = "$INT.text;"; // must be qualified
1445:                String expecting = "ID1.getText();";
1446:                String expecting2 = "INT2.getText();";
1447:
1448:                ErrorQueue equeue = new ErrorQueue();
1449:                ErrorManager.setErrorListener(equeue);
1450:                Grammar g = new Grammar("grammar t;\n" + "a : ID {" + action
1451:                        + "}\n" + "  | INT {" + action2 + "}\n" + "  ;\n"
1452:                        + "ID : 'a';\n" + "INT : '0';\n");
1453:                Tool antlr = newTool();
1454:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1455:                g.setCodeGenerator(generator);
1456:                generator.genRecognizer(); // forces load of templates
1457:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1458:                        generator, "a", new antlr.CommonToken(
1459:                                ANTLRParser.ACTION, action), 1);
1460:                String rawTranslation = translator.translate();
1461:                StringTemplateGroup templates = new StringTemplateGroup(".",
1462:                        AngleBracketTemplateLexer.class);
1463:                StringTemplate actionST = new StringTemplate(templates,
1464:                        rawTranslation);
1465:                String found = actionST.toString();
1466:                assertEquals(expecting, found);
1467:
1468:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1469:                        .size());
1470:                translator = new ActionTranslatorLexer(generator, "a",
1471:                        new antlr.CommonToken(ANTLRParser.ACTION, action2), 2);
1472:                rawTranslation = translator.translate();
1473:                templates = new StringTemplateGroup(".",
1474:                        AngleBracketTemplateLexer.class);
1475:                actionST = new StringTemplate(templates, rawTranslation);
1476:                found = actionST.toString();
1477:
1478:                assertEquals(expecting2, found);
1479:
1480:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1481:                        .size());
1482:            }
1483:
1484:            public void testRuleLabelFromMultipleAlts() throws Exception {
1485:                String action = "$b.text;"; // must be qualified
1486:                String action2 = "$c.text;"; // must be qualified
1487:                String expecting = "input.toString(b1.start,b1.stop);";
1488:                String expecting2 = "input.toString(c2.start,c2.stop);";
1489:
1490:                ErrorQueue equeue = new ErrorQueue();
1491:                ErrorManager.setErrorListener(equeue);
1492:                Grammar g = new Grammar("grammar t;\n" + "a : b {" + action
1493:                        + "}\n" + "  | c {" + action2 + "}\n" + "  ;\n"
1494:                        + "b : 'a';\n" + "c : '0';\n");
1495:                Tool antlr = newTool();
1496:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1497:                g.setCodeGenerator(generator);
1498:                generator.genRecognizer(); // forces load of templates
1499:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1500:                        generator, "a", new antlr.CommonToken(
1501:                                ANTLRParser.ACTION, action), 1);
1502:                String rawTranslation = translator.translate();
1503:                StringTemplateGroup templates = new StringTemplateGroup(".",
1504:                        AngleBracketTemplateLexer.class);
1505:                StringTemplate actionST = new StringTemplate(templates,
1506:                        rawTranslation);
1507:                String found = actionST.toString();
1508:                assertEquals(expecting, found);
1509:
1510:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1511:                        .size());
1512:                translator = new ActionTranslatorLexer(generator, "a",
1513:                        new antlr.CommonToken(ANTLRParser.ACTION, action2), 2);
1514:                rawTranslation = translator.translate();
1515:                templates = new StringTemplateGroup(".",
1516:                        AngleBracketTemplateLexer.class);
1517:                actionST = new StringTemplate(templates, rawTranslation);
1518:                found = actionST.toString();
1519:
1520:                assertEquals(expecting2, found);
1521:
1522:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1523:                        .size());
1524:            }
1525:
1526:            public void testUnknownDynamicAttribute() throws Exception {
1527:                String action = "$a::x";
1528:                String expecting = action;
1529:
1530:                ErrorQueue equeue = new ErrorQueue();
1531:                ErrorManager.setErrorListener(equeue);
1532:                Grammar g = new Grammar("grammar t;\n" + "a\n" + "scope {\n"
1533:                        + "  int n;\n" + "} : {" + action + "}\n" + "  ;\n");
1534:                Tool antlr = newTool();
1535:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1536:                g.setCodeGenerator(generator);
1537:                generator.genRecognizer(); // forces load of templates
1538:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1539:                        generator, "a", new antlr.CommonToken(
1540:                                ANTLRParser.ACTION, action), 1);
1541:                String rawTranslation = translator.translate();
1542:                StringTemplateGroup templates = new StringTemplateGroup(".",
1543:                        AngleBracketTemplateLexer.class);
1544:                StringTemplate actionST = new StringTemplate(templates,
1545:                        rawTranslation);
1546:                String found = actionST.toString();
1547:                assertEquals(expecting, found);
1548:
1549:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE;
1550:                Object expectedArg = "a";
1551:                Object expectedArg2 = "x";
1552:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1553:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1554:                checkError(equeue, expectedMessage);
1555:            }
1556:
1557:            public void testUnknownGlobalDynamicAttribute() throws Exception {
1558:                String action = "$Symbols::x";
1559:                String expecting = action;
1560:
1561:                ErrorQueue equeue = new ErrorQueue();
1562:                ErrorManager.setErrorListener(equeue);
1563:                Grammar g = new Grammar("grammar t;\n" + "scope Symbols {\n"
1564:                        + "  int n;\n" + "}\n" + "a : {'+action+'}\n" + "  ;\n");
1565:                Tool antlr = newTool();
1566:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1567:                g.setCodeGenerator(generator);
1568:                generator.genRecognizer(); // forces load of templates
1569:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1570:                        generator, "a", new antlr.CommonToken(
1571:                                ANTLRParser.ACTION, action), 1);
1572:                String rawTranslation = translator.translate();
1573:                StringTemplateGroup templates = new StringTemplateGroup(".",
1574:                        AngleBracketTemplateLexer.class);
1575:                StringTemplate actionST = new StringTemplate(templates,
1576:                        rawTranslation);
1577:                String found = actionST.toString();
1578:                assertEquals(expecting, found);
1579:
1580:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE;
1581:                Object expectedArg = "Symbols";
1582:                Object expectedArg2 = "x";
1583:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1584:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1585:                checkError(equeue, expectedMessage);
1586:            }
1587:
1588:            public void testUnqualifiedRuleScopeAttribute() throws Exception {
1589:                String action = "$n;"; // must be qualified
1590:                String expecting = "$n;";
1591:
1592:                ErrorQueue equeue = new ErrorQueue();
1593:                ErrorManager.setErrorListener(equeue);
1594:                Grammar g = new Grammar("grammar t;\n" + "a\n" + "scope {\n"
1595:                        + "  int n;\n" + "} : b\n" + "  ;\n"
1596:                        + "b : {'+action+'}\n" + "  ;\n");
1597:                Tool antlr = newTool();
1598:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1599:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1600:                        generator, "b", new antlr.CommonToken(
1601:                                ANTLRParser.ACTION, action), 1);
1602:                String rawTranslation = translator.translate();
1603:                StringTemplateGroup templates = new StringTemplateGroup(".",
1604:                        AngleBracketTemplateLexer.class);
1605:                StringTemplate actionST = new StringTemplate(templates,
1606:                        rawTranslation);
1607:                String found = actionST.toString();
1608:                assertEquals(expecting, found);
1609:
1610:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;
1611:                Object expectedArg = "n";
1612:                Object expectedArg2 = null;
1613:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1614:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1615:                checkError(equeue, expectedMessage);
1616:            }
1617:
1618:            public void testRuleAndTokenLabelTypeMismatch() throws Exception {
1619:                ErrorQueue equeue = new ErrorQueue();
1620:                ErrorManager.setErrorListener(equeue);
1621:                Grammar g = new Grammar("grammar t;\n" + "a : id='foo' id=b\n"
1622:                        + "  ;\n" + "b : ;\n");
1623:                int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT;
1624:                Object expectedArg = "id";
1625:                Object expectedArg2 = "rule!=token";
1626:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1627:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1628:                checkError(equeue, expectedMessage);
1629:            }
1630:
1631:            public void testListAndTokenLabelTypeMismatch() throws Exception {
1632:                ErrorQueue equeue = new ErrorQueue();
1633:                ErrorManager.setErrorListener(equeue);
1634:                Grammar g = new Grammar("grammar t;\n"
1635:                        + "a : ids+='a' ids='b'\n" + "  ;\n" + "b : ;\n");
1636:                int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT;
1637:                Object expectedArg = "ids";
1638:                Object expectedArg2 = "token!=token-list";
1639:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1640:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1641:                checkError(equeue, expectedMessage);
1642:            }
1643:
1644:            public void testListAndRuleLabelTypeMismatch() throws Exception {
1645:                ErrorQueue equeue = new ErrorQueue();
1646:                ErrorManager.setErrorListener(equeue);
1647:                Grammar g = new Grammar("grammar t;\n"
1648:                        + "options {output=AST;}\n" + "a : bs+=b bs=b\n"
1649:                        + "  ;\n" + "b : 'b';\n");
1650:                int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT;
1651:                Object expectedArg = "bs";
1652:                Object expectedArg2 = "rule!=rule-list";
1653:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1654:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1655:                checkError(equeue, expectedMessage);
1656:            }
1657:
1658:            public void testArgReturnValueMismatch() throws Exception {
1659:                ErrorQueue equeue = new ErrorQueue();
1660:                ErrorManager.setErrorListener(equeue);
1661:                Grammar g = new Grammar("grammar t;\n"
1662:                        + "a[int i] returns [int x, int i]\n" + "  : \n"
1663:                        + "  ;\n" + "b : ;\n");
1664:                int expectedMsgID = ErrorManager.MSG_ARG_RETVAL_CONFLICT;
1665:                Object expectedArg = "i";
1666:                Object expectedArg2 = "a";
1667:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1668:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1669:                checkError(equeue, expectedMessage);
1670:            }
1671:
1672:            public void testSimplePlusEqualLabel() throws Exception {
1673:                String action = "$ids.size();"; // must be qualified
1674:                String expecting = "list_ids.size();";
1675:
1676:                ErrorQueue equeue = new ErrorQueue();
1677:                ErrorManager.setErrorListener(equeue);
1678:                Grammar g = new Grammar("parser grammar t;\n"
1679:                        + "a : ids+=ID ( COMMA ids+=ID {" + action + "})* ;\n");
1680:                Tool antlr = newTool();
1681:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1682:                g.setCodeGenerator(generator);
1683:                generator.genRecognizer(); // forces load of templates
1684:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1685:                        generator, "a", new antlr.CommonToken(
1686:                                ANTLRParser.ACTION, action), 1);
1687:                String rawTranslation = translator.translate();
1688:                StringTemplateGroup templates = new StringTemplateGroup(".",
1689:                        AngleBracketTemplateLexer.class);
1690:                StringTemplate actionST = new StringTemplate(templates,
1691:                        rawTranslation);
1692:                String found = actionST.toString();
1693:                assertEquals(expecting, found);
1694:
1695:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1696:                        .size());
1697:            }
1698:
1699:            public void testPlusEqualStringLabel() throws Exception {
1700:                String action = "$ids.size();"; // must be qualified
1701:                String expecting = "list_ids.size();";
1702:
1703:                ErrorQueue equeue = new ErrorQueue();
1704:                ErrorManager.setErrorListener(equeue);
1705:                Grammar g = new Grammar("grammar t;\n"
1706:                        + "a : ids+='if' ( ',' ids+=ID {" + action + "})* ;"
1707:                        + "ID : 'a';\n");
1708:                Tool antlr = newTool();
1709:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1710:                g.setCodeGenerator(generator);
1711:                generator.genRecognizer(); // forces load of templates
1712:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1713:                        generator, "a", new antlr.CommonToken(
1714:                                ANTLRParser.ACTION, action), 1);
1715:                String rawTranslation = translator.translate();
1716:                StringTemplateGroup templates = new StringTemplateGroup(".",
1717:                        AngleBracketTemplateLexer.class);
1718:                StringTemplate actionST = new StringTemplate(templates,
1719:                        rawTranslation);
1720:                String found = actionST.toString();
1721:                assertEquals(expecting, found);
1722:
1723:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1724:                        .size());
1725:            }
1726:
1727:            public void testPlusEqualSetLabel() throws Exception {
1728:                String action = "$ids.size();"; // must be qualified
1729:                String expecting = "list_ids.size();";
1730:
1731:                ErrorQueue equeue = new ErrorQueue();
1732:                ErrorManager.setErrorListener(equeue);
1733:                Grammar g = new Grammar("grammar t;\n"
1734:                        + "a : ids+=('a'|'b') ( ',' ids+=ID {" + action
1735:                        + "})* ;" + "ID : 'a';\n");
1736:                Tool antlr = newTool();
1737:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1738:                g.setCodeGenerator(generator);
1739:                generator.genRecognizer(); // forces load of templates
1740:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1741:                        generator, "a", new antlr.CommonToken(
1742:                                ANTLRParser.ACTION, action), 1);
1743:                String rawTranslation = translator.translate();
1744:                StringTemplateGroup templates = new StringTemplateGroup(".",
1745:                        AngleBracketTemplateLexer.class);
1746:                StringTemplate actionST = new StringTemplate(templates,
1747:                        rawTranslation);
1748:                String found = actionST.toString();
1749:                assertEquals(expecting, found);
1750:
1751:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1752:                        .size());
1753:            }
1754:
1755:            public void testPlusEqualWildcardLabel() throws Exception {
1756:                String action = "$ids.size();"; // must be qualified
1757:                String expecting = "list_ids.size();";
1758:
1759:                ErrorQueue equeue = new ErrorQueue();
1760:                ErrorManager.setErrorListener(equeue);
1761:                Grammar g = new Grammar("grammar t;\n"
1762:                        + "a : ids+=. ( ',' ids+=ID {" + action + "})* ;"
1763:                        + "ID : 'a';\n");
1764:                Tool antlr = newTool();
1765:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1766:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1767:                        generator, "a", new antlr.CommonToken(
1768:                                ANTLRParser.ACTION, action), 1);
1769:                g.setCodeGenerator(generator);
1770:                generator.genRecognizer(); // forces load of templates
1771:                String rawTranslation = translator.translate();
1772:                StringTemplateGroup templates = new StringTemplateGroup(".",
1773:                        AngleBracketTemplateLexer.class);
1774:                StringTemplate actionST = new StringTemplate(templates,
1775:                        rawTranslation);
1776:                String found = actionST.toString();
1777:                assertEquals(expecting, found);
1778:
1779:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1780:                        .size());
1781:            }
1782:
1783:            public void testImplicitTokenLabel() throws Exception {
1784:                String action = "$ID; $ID.text; $ID.getText()";
1785:                String expecting = "ID1; ID1.getText(); ID1.getText()";
1786:
1787:                ErrorQueue equeue = new ErrorQueue();
1788:                ErrorManager.setErrorListener(equeue);
1789:                Grammar g = new Grammar("grammar t;\n" + "a : ID {" + action
1790:                        + "} ;" + "ID : 'a';\n");
1791:                Tool antlr = newTool();
1792:                antlr.setOutputDirectory(null); // write to /dev/null
1793:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1794:
1795:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1796:                        generator, "a", new antlr.CommonToken(
1797:                                ANTLRParser.ACTION, action), 1);
1798:                g.setCodeGenerator(generator);
1799:                generator.genRecognizer(); // forces load of templates
1800:                String rawTranslation = translator.translate();
1801:                StringTemplateGroup templates = new StringTemplateGroup(".",
1802:                        AngleBracketTemplateLexer.class);
1803:                StringTemplate actionST = new StringTemplate(templates,
1804:                        rawTranslation);
1805:                String found = actionST.toString();
1806:                assertEquals(expecting, found);
1807:
1808:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1809:                        .size());
1810:            }
1811:
1812:            public void testImplicitRuleLabel() throws Exception {
1813:                String action = "$r.start;";
1814:                String expecting = "((Token)r1.start);";
1815:
1816:                ErrorQueue equeue = new ErrorQueue();
1817:                ErrorManager.setErrorListener(equeue);
1818:                Grammar g = new Grammar("grammar t;\n" + "a : r {###" + action
1819:                        + "!!!} ;" + "r : 'a';\n");
1820:                Tool antlr = newTool();
1821:                antlr.setOutputDirectory(null); // write to /dev/null
1822:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1823:                g.setCodeGenerator(generator);
1824:                generator.genRecognizer();
1825:
1826:                StringTemplate codeST = generator.getRecognizerST();
1827:                String code = codeST.toString();
1828:                String found = code.substring(code.indexOf("###") + 3, code
1829:                        .indexOf("!!!"));
1830:                assertEquals(expecting, found);
1831:
1832:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1833:                        .size());
1834:            }
1835:
1836:            public void testReuseExistingLabelWithImplicitRuleLabel()
1837:                    throws Exception {
1838:                String action = "$r.start;";
1839:                String expecting = "((Token)x.start);";
1840:
1841:                ErrorQueue equeue = new ErrorQueue();
1842:                ErrorManager.setErrorListener(equeue);
1843:                Grammar g = new Grammar("grammar t;\n" + "a : x=r {###"
1844:                        + action + "!!!} ;" + "r : 'a';\n");
1845:                Tool antlr = newTool();
1846:                antlr.setOutputDirectory(null); // write to /dev/null
1847:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1848:                g.setCodeGenerator(generator);
1849:                generator.genRecognizer();
1850:
1851:                StringTemplate codeST = generator.getRecognizerST();
1852:                String code = codeST.toString();
1853:                String found = code.substring(code.indexOf("###") + 3, code
1854:                        .indexOf("!!!"));
1855:                assertEquals(expecting, found);
1856:
1857:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1858:                        .size());
1859:            }
1860:
1861:            public void testReuseExistingListLabelWithImplicitRuleLabel()
1862:                    throws Exception {
1863:                String action = "$r.start;";
1864:                String expecting = "((Token)x.start);";
1865:
1866:                ErrorQueue equeue = new ErrorQueue();
1867:                ErrorManager.setErrorListener(equeue);
1868:                Grammar g = new Grammar("grammar t;\n"
1869:                        + "options {output=AST;}\n" + "a : x+=r {###" + action
1870:                        + "!!!} ;" + "r : 'a';\n");
1871:                Tool antlr = newTool();
1872:                antlr.setOutputDirectory(null); // write to /dev/null
1873:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1874:                g.setCodeGenerator(generator);
1875:                generator.genRecognizer();
1876:
1877:                StringTemplate codeST = generator.getRecognizerST();
1878:                String code = codeST.toString();
1879:                String found = code.substring(code.indexOf("###") + 3, code
1880:                        .indexOf("!!!"));
1881:                assertEquals(expecting, found);
1882:
1883:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1884:                        .size());
1885:            }
1886:
1887:            public void testReuseExistingLabelWithImplicitTokenLabel()
1888:                    throws Exception {
1889:                String action = "$ID.text;";
1890:                String expecting = "x.getText();";
1891:
1892:                ErrorQueue equeue = new ErrorQueue();
1893:                ErrorManager.setErrorListener(equeue);
1894:                Grammar g = new Grammar("grammar t;\n" + "a : x=ID {" + action
1895:                        + "} ;" + "ID : 'a';\n");
1896:                Tool antlr = newTool();
1897:                antlr.setOutputDirectory(null); // write to /dev/null
1898:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1899:                g.setCodeGenerator(generator);
1900:                generator.genRecognizer();
1901:
1902:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1903:                        generator, "a", new antlr.CommonToken(
1904:                                ANTLRParser.ACTION, action), 1);
1905:                String rawTranslation = translator.translate();
1906:                StringTemplateGroup templates = new StringTemplateGroup(".",
1907:                        AngleBracketTemplateLexer.class);
1908:                StringTemplate actionST = new StringTemplate(templates,
1909:                        rawTranslation);
1910:                String found = actionST.toString();
1911:                assertEquals(expecting, found);
1912:
1913:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1914:                        .size());
1915:            }
1916:
1917:            public void testReuseExistingListLabelWithImplicitTokenLabel()
1918:                    throws Exception {
1919:                String action = "$ID.text;";
1920:                String expecting = "x.getText();";
1921:
1922:                ErrorQueue equeue = new ErrorQueue();
1923:                ErrorManager.setErrorListener(equeue);
1924:                Grammar g = new Grammar("grammar t;\n" + "a : x+=ID {" + action
1925:                        + "} ;" + "ID : 'a';\n");
1926:                Tool antlr = newTool();
1927:                antlr.setOutputDirectory(null); // write to /dev/null
1928:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1929:                g.setCodeGenerator(generator);
1930:                generator.genRecognizer();
1931:
1932:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
1933:                        generator, "a", new antlr.CommonToken(
1934:                                ANTLRParser.ACTION, action), 1);
1935:                String rawTranslation = translator.translate();
1936:                StringTemplateGroup templates = new StringTemplateGroup(".",
1937:                        AngleBracketTemplateLexer.class);
1938:                StringTemplate actionST = new StringTemplate(templates,
1939:                        rawTranslation);
1940:                String found = actionST.toString();
1941:                assertEquals(expecting, found);
1942:
1943:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
1944:                        .size());
1945:            }
1946:
1947:            public void testRuleLabelWithoutOutputOption() throws Exception {
1948:                ErrorQueue equeue = new ErrorQueue();
1949:                ErrorManager.setErrorListener(equeue);
1950:                Grammar g = new Grammar("grammar T;\n" + "s : x+=a ;"
1951:                        + "a : 'a';\n" + "b : 'b';\n" + "WS : ' '|'\n';\n");
1952:                Tool antlr = newTool();
1953:                antlr.setOutputDirectory(null); // write to /dev/null
1954:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1955:                g.setCodeGenerator(generator);
1956:                generator.genRecognizer();
1957:
1958:                int expectedMsgID = ErrorManager.MSG_LIST_LABEL_INVALID_UNLESS_RETVAL_STRUCT;
1959:                Object expectedArg = "x";
1960:                Object expectedArg2 = null;
1961:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
1962:                        expectedMsgID, g, null, expectedArg, expectedArg2);
1963:                checkError(equeue, expectedMessage);
1964:            }
1965:
1966:            public void testRuleLabelOnTwoDifferentRulesAST() throws Exception {
1967:                String grammar = "grammar T;\n" + "options {output=AST;}\n"
1968:                        + "s : x+=a x+=b {System.out.println($x);} ;"
1969:                        + "a : 'a';\n" + "b : 'b';\n"
1970:                        + "WS : (' '|'\n') {skip();};\n";
1971:                String expecting = "[a, b]\na b\n";
1972:                String found = execParser("T.g", grammar, "TParser", "TLexer",
1973:                        "s", "a b", false);
1974:                assertEquals(expecting, found);
1975:            }
1976:
1977:            public void testRuleLabelOnTwoDifferentRulesTemplate()
1978:                    throws Exception {
1979:                String grammar = "grammar T;\n"
1980:                        + "options {output=template;}\n"
1981:                        + "s : x+=a x+=b {System.out.println($x);} ;"
1982:                        + "a : 'a' -> {%{\"hi\"}} ;\n"
1983:                        + "b : 'b' -> {%{\"mom\"}} ;\n"
1984:                        + "WS : (' '|'\n') {skip();};\n";
1985:                String expecting = "[hi, mom]\n";
1986:                String found = execParser("T.g", grammar, "TParser", "TLexer",
1987:                        "s", "a b", false);
1988:                assertEquals(expecting, found);
1989:            }
1990:
1991:            public void testMissingArgs() throws Exception {
1992:                ErrorQueue equeue = new ErrorQueue();
1993:                ErrorManager.setErrorListener(equeue);
1994:                Grammar g = new Grammar("grammar t;\n" + "a : r ;"
1995:                        + "r[int i] : 'a';\n");
1996:                Tool antlr = newTool();
1997:                antlr.setOutputDirectory(null); // write to /dev/null
1998:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
1999:                g.setCodeGenerator(generator);
2000:                generator.genRecognizer();
2001:
2002:                int expectedMsgID = ErrorManager.MSG_MISSING_RULE_ARGS;
2003:                Object expectedArg = "r";
2004:                Object expectedArg2 = null;
2005:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2006:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2007:                checkError(equeue, expectedMessage);
2008:            }
2009:
2010:            public void testArgsWhenNoneDefined() throws Exception {
2011:                ErrorQueue equeue = new ErrorQueue();
2012:                ErrorManager.setErrorListener(equeue);
2013:                Grammar g = new Grammar("grammar t;\n" + "a : r[32,34] ;"
2014:                        + "r : 'a';\n");
2015:                Tool antlr = newTool();
2016:                antlr.setOutputDirectory(null); // write to /dev/null
2017:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2018:                g.setCodeGenerator(generator);
2019:                generator.genRecognizer();
2020:
2021:                int expectedMsgID = ErrorManager.MSG_RULE_HAS_NO_ARGS;
2022:                Object expectedArg = "r";
2023:                Object expectedArg2 = null;
2024:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2025:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2026:                checkError(equeue, expectedMessage);
2027:            }
2028:
2029:            public void testReturnInitValue() throws Exception {
2030:                ErrorQueue equeue = new ErrorQueue();
2031:                ErrorManager.setErrorListener(equeue);
2032:                Grammar g = new Grammar("grammar t;\n" + "a : r ;\n"
2033:                        + "r returns [int x=0] : 'a' {$x = 4;} ;\n");
2034:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2035:                        .size());
2036:
2037:                Rule r = g.getRule("r");
2038:                AttributeScope retScope = r.returnScope;
2039:                List parameters = retScope.getAttributes();
2040:                assertNotNull("missing return action", parameters);
2041:                assertEquals(1, parameters.size());
2042:                String found = parameters.get(0).toString();
2043:                String expecting = "int x=0";
2044:                assertEquals(expecting, found);
2045:            }
2046:
2047:            public void testMultipleReturnInitValue() throws Exception {
2048:                ErrorQueue equeue = new ErrorQueue();
2049:                ErrorManager.setErrorListener(equeue);
2050:                Grammar g = new Grammar(
2051:                        "grammar t;\n"
2052:                                + "a : r ;\n"
2053:                                + "r returns [int x=0, int y, String s=new String(\"foo\")] : 'a' {$x = 4;} ;\n");
2054:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2055:                        .size());
2056:
2057:                Rule r = g.getRule("r");
2058:                AttributeScope retScope = r.returnScope;
2059:                List parameters = retScope.getAttributes();
2060:                assertNotNull("missing return action", parameters);
2061:                assertEquals(3, parameters.size());
2062:                assertEquals("int x=0", parameters.get(0).toString());
2063:                assertEquals("int y", parameters.get(1).toString());
2064:                assertEquals("String s=new String(\"foo\")", parameters.get(2)
2065:                        .toString());
2066:            }
2067:
2068:            public void testCStyleReturnInitValue() throws Exception {
2069:                ErrorQueue equeue = new ErrorQueue();
2070:                ErrorManager.setErrorListener(equeue);
2071:                Grammar g = new Grammar("grammar t;\n" + "a : r ;\n"
2072:                        + "r returns [int (*x)()=NULL] : 'a' ;\n");
2073:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2074:                        .size());
2075:
2076:                Rule r = g.getRule("r");
2077:                AttributeScope retScope = r.returnScope;
2078:                List parameters = retScope.getAttributes();
2079:                assertNotNull("missing return action", parameters);
2080:                assertEquals(1, parameters.size());
2081:                String found = parameters.get(0).toString();
2082:                String expecting = "int (*)() x=NULL";
2083:                assertEquals(expecting, found);
2084:            }
2085:
2086:            public void testArgsWithInitValues() throws Exception {
2087:                ErrorQueue equeue = new ErrorQueue();
2088:                ErrorManager.setErrorListener(equeue);
2089:                Grammar g = new Grammar("grammar t;\n" + "a : r[32,34] ;"
2090:                        + "r[int x, int y=3] : 'a';\n");
2091:                Tool antlr = newTool();
2092:                antlr.setOutputDirectory(null); // write to /dev/null
2093:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2094:                g.setCodeGenerator(generator);
2095:                generator.genRecognizer();
2096:
2097:                int expectedMsgID = ErrorManager.MSG_ARG_INIT_VALUES_ILLEGAL;
2098:                Object expectedArg = "y";
2099:                Object expectedArg2 = null;
2100:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2101:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2102:                checkError(equeue, expectedMessage);
2103:            }
2104:
2105:            public void testArgsOnToken() throws Exception {
2106:                ErrorQueue equeue = new ErrorQueue();
2107:                ErrorManager.setErrorListener(equeue);
2108:                Grammar g = new Grammar("grammar t;\n" + "a : ID[32,34] ;"
2109:                        + "ID : 'a';\n");
2110:                Tool antlr = newTool();
2111:                antlr.setOutputDirectory(null); // write to /dev/null
2112:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2113:                g.setCodeGenerator(generator);
2114:                generator.genRecognizer();
2115:
2116:                int expectedMsgID = ErrorManager.MSG_ARGS_ON_TOKEN_REF;
2117:                Object expectedArg = "ID";
2118:                Object expectedArg2 = null;
2119:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2120:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2121:                checkError(equeue, expectedMessage);
2122:            }
2123:
2124:            public void testArgsOnTokenInLexer() throws Exception {
2125:                ErrorQueue equeue = new ErrorQueue();
2126:                ErrorManager.setErrorListener(equeue);
2127:                Grammar g = new Grammar("lexer grammar t;\n"
2128:                        + "R : 'z' ID[32,34] ;" + "ID : 'a';\n");
2129:                Tool antlr = newTool();
2130:                antlr.setOutputDirectory(null); // write to /dev/null
2131:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2132:                g.setCodeGenerator(generator);
2133:                generator.genRecognizer();
2134:
2135:                int expectedMsgID = ErrorManager.MSG_RULE_HAS_NO_ARGS;
2136:                Object expectedArg = "ID";
2137:                Object expectedArg2 = null;
2138:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2139:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2140:                checkError(equeue, expectedMessage);
2141:            }
2142:
2143:            public void testLabelOnRuleRefInLexer() throws Exception {
2144:                String action = "$i.text";
2145:                String expecting = "i.getText()";
2146:                ErrorQueue equeue = new ErrorQueue();
2147:                ErrorManager.setErrorListener(equeue);
2148:                Grammar g = new Grammar("lexer grammar t;\n" + "R : 'z' i=ID {"
2149:                        + action + "};" + "fragment ID : 'a';\n");
2150:                Tool antlr = newTool();
2151:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2152:                g.setCodeGenerator(generator);
2153:                generator.genRecognizer(); // forces load of templates
2154:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2155:                        generator, "R", new antlr.CommonToken(
2156:                                ANTLRParser.ACTION, action), 1);
2157:                String rawTranslation = translator.translate();
2158:                StringTemplateGroup templates = new StringTemplateGroup(".",
2159:                        AngleBracketTemplateLexer.class);
2160:                StringTemplate actionST = new StringTemplate(templates,
2161:                        rawTranslation);
2162:                String found = actionST.toString();
2163:
2164:                assertEquals(expecting, found);
2165:
2166:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2167:                        .size());
2168:            }
2169:
2170:            public void testRefToRuleRefInLexer() throws Exception {
2171:                String action = "$ID.text";
2172:                String expecting = "ID1.getText()";
2173:                ErrorQueue equeue = new ErrorQueue();
2174:                ErrorManager.setErrorListener(equeue);
2175:                Grammar g = new Grammar("lexer grammar t;\n" + "R : 'z' ID {"
2176:                        + action + "};" + "ID : 'a';\n");
2177:                Tool antlr = newTool();
2178:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2179:                g.setCodeGenerator(generator);
2180:                generator.genRecognizer(); // forces load of templates
2181:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2182:                        generator, "R", new antlr.CommonToken(
2183:                                ANTLRParser.ACTION, action), 1);
2184:                String rawTranslation = translator.translate();
2185:                StringTemplateGroup templates = new StringTemplateGroup(".",
2186:                        AngleBracketTemplateLexer.class);
2187:                StringTemplate actionST = new StringTemplate(templates,
2188:                        rawTranslation);
2189:                String found = actionST.toString();
2190:
2191:                assertEquals(expecting, found);
2192:
2193:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2194:                        .size());
2195:            }
2196:
2197:            public void testRefToRuleRefInLexerNoAttribute() throws Exception {
2198:                String action = "$ID";
2199:                String expecting = "ID1";
2200:                ErrorQueue equeue = new ErrorQueue();
2201:                ErrorManager.setErrorListener(equeue);
2202:                Grammar g = new Grammar("lexer grammar t;\n" + "R : 'z' ID {"
2203:                        + action + "};" + "ID : 'a';\n");
2204:                Tool antlr = newTool();
2205:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2206:                g.setCodeGenerator(generator);
2207:                generator.genRecognizer(); // forces load of templates
2208:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2209:                        generator, "R", new antlr.CommonToken(
2210:                                ANTLRParser.ACTION, action), 1);
2211:                String rawTranslation = translator.translate();
2212:                StringTemplateGroup templates = new StringTemplateGroup(".",
2213:                        AngleBracketTemplateLexer.class);
2214:                StringTemplate actionST = new StringTemplate(templates,
2215:                        rawTranslation);
2216:                String found = actionST.toString();
2217:
2218:                assertEquals(expecting, found);
2219:
2220:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2221:                        .size());
2222:            }
2223:
2224:            public void testCharLabelInLexer() throws Exception {
2225:                ErrorQueue equeue = new ErrorQueue();
2226:                ErrorManager.setErrorListener(equeue);
2227:                Grammar g = new Grammar("lexer grammar t;\n" + "R : x='z' ;\n");
2228:
2229:                Tool antlr = newTool();
2230:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2231:                g.setCodeGenerator(generator);
2232:                generator.genRecognizer(); // forces load of templates
2233:
2234:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2235:                        .size());
2236:            }
2237:
2238:            public void testCharListLabelInLexer() throws Exception {
2239:                ErrorQueue equeue = new ErrorQueue();
2240:                ErrorManager.setErrorListener(equeue);
2241:                Grammar g = new Grammar("lexer grammar t;\n" + "R : x+='z' ;\n");
2242:
2243:                Tool antlr = newTool();
2244:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2245:                g.setCodeGenerator(generator);
2246:                generator.genRecognizer(); // forces load of templates
2247:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2248:                        .size());
2249:            }
2250:
2251:            public void testWildcardCharLabelInLexer() throws Exception {
2252:                ErrorQueue equeue = new ErrorQueue();
2253:                ErrorManager.setErrorListener(equeue);
2254:                Grammar g = new Grammar("lexer grammar t;\n" + "R : x=. ;\n");
2255:
2256:                Tool antlr = newTool();
2257:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2258:                g.setCodeGenerator(generator);
2259:                generator.genRecognizer(); // forces load of templates
2260:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2261:                        .size());
2262:            }
2263:
2264:            public void testWildcardCharListLabelInLexer() throws Exception {
2265:                ErrorQueue equeue = new ErrorQueue();
2266:                ErrorManager.setErrorListener(equeue);
2267:                Grammar g = new Grammar("lexer grammar t;\n" + "R : x+=. ;\n");
2268:
2269:                Tool antlr = newTool();
2270:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2271:                g.setCodeGenerator(generator);
2272:                generator.genRecognizer(); // forces load of templates
2273:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2274:                        .size());
2275:            }
2276:
2277:            public void testMissingArgsInLexer() throws Exception {
2278:                ErrorQueue equeue = new ErrorQueue();
2279:                ErrorManager.setErrorListener(equeue);
2280:                Grammar g = new Grammar("lexer grammar t;\n" + "A : R ;"
2281:                        + "R[int i] : 'a';\n");
2282:                Tool antlr = newTool();
2283:                antlr.setOutputDirectory(null); // write to /dev/null
2284:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2285:                g.setCodeGenerator(generator);
2286:                generator.genRecognizer();
2287:
2288:                int expectedMsgID = ErrorManager.MSG_MISSING_RULE_ARGS;
2289:                Object expectedArg = "R";
2290:                Object expectedArg2 = null;
2291:                // getting a second error @1:12, probably from nextToken
2292:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2293:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2294:                checkError(equeue, expectedMessage);
2295:            }
2296:
2297:            public void testLexerRulePropertyRefs() throws Exception {
2298:                String action = "$text $type $line $pos $channel $index $start $stop";
2299:                String expecting = "getText() _type tokenStartLine tokenStartCharPositionInLine channel -1 tokenStartCharIndex (getCharIndex()-1)";
2300:                ErrorQueue equeue = new ErrorQueue();
2301:                ErrorManager.setErrorListener(equeue);
2302:                Grammar g = new Grammar("lexer grammar t;\n" + "R : 'r' {"
2303:                        + action + "};\n");
2304:                Tool antlr = newTool();
2305:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2306:                g.setCodeGenerator(generator);
2307:                generator.genRecognizer(); // forces load of templates
2308:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2309:                        generator, "R", new antlr.CommonToken(
2310:                                ANTLRParser.ACTION, action), 1);
2311:                String rawTranslation = translator.translate();
2312:                StringTemplateGroup templates = new StringTemplateGroup(".",
2313:                        AngleBracketTemplateLexer.class);
2314:                StringTemplate actionST = new StringTemplate(templates,
2315:                        rawTranslation);
2316:                String found = actionST.toString();
2317:
2318:                assertEquals(expecting, found);
2319:
2320:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2321:                        .size());
2322:            }
2323:
2324:            public void testLexerLabelRefs() throws Exception {
2325:                String action = "$a $b.text $c $d.text";
2326:                String expecting = "a b.getText() c d.getText()";
2327:                ErrorQueue equeue = new ErrorQueue();
2328:                ErrorManager.setErrorListener(equeue);
2329:                Grammar g = new Grammar("lexer grammar t;\n"
2330:                        + "R : a='c' b='hi' c=. d=DUH {" + action + "};\n"
2331:                        + "DUH : 'd' ;\n");
2332:                Tool antlr = newTool();
2333:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2334:                g.setCodeGenerator(generator);
2335:                generator.genRecognizer(); // forces load of templates
2336:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2337:                        generator, "R", new antlr.CommonToken(
2338:                                ANTLRParser.ACTION, action), 1);
2339:                String rawTranslation = translator.translate();
2340:                StringTemplateGroup templates = new StringTemplateGroup(".",
2341:                        AngleBracketTemplateLexer.class);
2342:                StringTemplate actionST = new StringTemplate(templates,
2343:                        rawTranslation);
2344:                String found = actionST.toString();
2345:
2346:                assertEquals(expecting, found);
2347:
2348:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2349:                        .size());
2350:            }
2351:
2352:            public void testSettingLexerRulePropertyRefs() throws Exception {
2353:                String action = "$text $type=1 $line=1 $pos=1 $channel=1 $index";
2354:                String expecting = "getText() _type=1 tokenStartLine=1 tokenStartCharPositionInLine=1 channel=1 -1";
2355:                ErrorQueue equeue = new ErrorQueue();
2356:                ErrorManager.setErrorListener(equeue);
2357:                Grammar g = new Grammar("lexer grammar t;\n" + "R : 'r' {"
2358:                        + action + "};\n");
2359:                Tool antlr = newTool();
2360:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2361:                g.setCodeGenerator(generator);
2362:                generator.genRecognizer(); // forces load of templates
2363:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2364:                        generator, "R", new antlr.CommonToken(
2365:                                ANTLRParser.ACTION, action), 1);
2366:                String rawTranslation = translator.translate();
2367:                StringTemplateGroup templates = new StringTemplateGroup(".",
2368:                        AngleBracketTemplateLexer.class);
2369:                StringTemplate actionST = new StringTemplate(templates,
2370:                        rawTranslation);
2371:                String found = actionST.toString();
2372:
2373:                assertEquals(expecting, found);
2374:
2375:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2376:                        .size());
2377:            }
2378:
2379:            public void testArgsOnTokenInLexerRuleOfCombined() throws Exception {
2380:                ErrorQueue equeue = new ErrorQueue();
2381:                ErrorManager.setErrorListener(equeue);
2382:                Grammar g = new Grammar("grammar t;\n" + "a : R;\n"
2383:                        + "R : 'z' ID[32] ;\n" + "ID : 'a';\n");
2384:
2385:                String lexerGrammarStr = g.getLexerGrammar();
2386:                StringReader sr = new StringReader(lexerGrammarStr);
2387:                Grammar lexerGrammar = new Grammar();
2388:                lexerGrammar.setFileName("<internally-generated-lexer>");
2389:                lexerGrammar.importTokenVocabulary(g);
2390:                lexerGrammar.setGrammarContent(sr);
2391:                sr.close();
2392:
2393:                Tool antlr = newTool();
2394:                antlr.setOutputDirectory(null); // write to /dev/null
2395:                CodeGenerator generator = new CodeGenerator(antlr,
2396:                        lexerGrammar, "Java");
2397:                lexerGrammar.setCodeGenerator(generator);
2398:                generator.genRecognizer();
2399:
2400:                int expectedMsgID = ErrorManager.MSG_RULE_HAS_NO_ARGS;
2401:                Object expectedArg = "ID";
2402:                Object expectedArg2 = null;
2403:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2404:                        expectedMsgID, lexerGrammar, null, expectedArg,
2405:                        expectedArg2);
2406:                checkError(equeue, expectedMessage);
2407:            }
2408:
2409:            public void testMissingArgsOnTokenInLexerRuleOfCombined()
2410:                    throws Exception {
2411:                ErrorQueue equeue = new ErrorQueue();
2412:                ErrorManager.setErrorListener(equeue);
2413:                Grammar g = new Grammar("grammar t;\n" + "a : R;\n"
2414:                        + "R : 'z' ID ;\n" + "ID[int i] : 'a';\n");
2415:
2416:                String lexerGrammarStr = g.getLexerGrammar();
2417:                StringReader sr = new StringReader(lexerGrammarStr);
2418:                Grammar lexerGrammar = new Grammar();
2419:                lexerGrammar.setFileName("<internally-generated-lexer>");
2420:                lexerGrammar.importTokenVocabulary(g);
2421:                lexerGrammar.setGrammarContent(sr);
2422:                sr.close();
2423:
2424:                Tool antlr = newTool();
2425:                antlr.setOutputDirectory(null); // write to /dev/null
2426:                CodeGenerator generator = new CodeGenerator(antlr,
2427:                        lexerGrammar, "Java");
2428:                lexerGrammar.setCodeGenerator(generator);
2429:                generator.genRecognizer();
2430:
2431:                int expectedMsgID = ErrorManager.MSG_MISSING_RULE_ARGS;
2432:                Object expectedArg = "ID";
2433:                Object expectedArg2 = null;
2434:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2435:                        expectedMsgID, lexerGrammar, null, expectedArg,
2436:                        expectedArg2);
2437:                checkError(equeue, expectedMessage);
2438:            }
2439:
2440:            // T R E E S
2441:
2442:            public void testTokenLabelTreeProperty() throws Exception {
2443:                String action = "$id.tree;";
2444:                String expecting = "id_tree;";
2445:
2446:                ErrorQueue equeue = new ErrorQueue();
2447:                ErrorManager.setErrorListener(equeue);
2448:                Grammar g = new Grammar("grammar t;\n" + "a : id=ID {" + action
2449:                        + "} ;\n" + "ID : 'a';\n");
2450:
2451:                Tool antlr = newTool();
2452:                antlr.setOutputDirectory(null); // write to /dev/null
2453:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2454:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2455:                        generator, "a", new antlr.CommonToken(
2456:                                ANTLRParser.ACTION, action), 1);
2457:                g.setCodeGenerator(generator);
2458:                generator.genRecognizer(); // forces load of templates
2459:                String rawTranslation = translator.translate();
2460:                StringTemplateGroup templates = new StringTemplateGroup(".",
2461:                        AngleBracketTemplateLexer.class);
2462:                StringTemplate actionST = new StringTemplate(templates,
2463:                        rawTranslation);
2464:                String found = actionST.toString();
2465:                assertEquals(expecting, found);
2466:
2467:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2468:                        .size());
2469:            }
2470:
2471:            public void testTokenRefTreeProperty() throws Exception {
2472:                String action = "$ID.tree;";
2473:                String expecting = "ID1_tree;";
2474:
2475:                ErrorQueue equeue = new ErrorQueue();
2476:                ErrorManager.setErrorListener(equeue);
2477:                Grammar g = new Grammar("grammar t;\n" + "a : ID {" + action
2478:                        + "} ;" + "ID : 'a';\n");
2479:                Tool antlr = newTool();
2480:                antlr.setOutputDirectory(null); // write to /dev/null
2481:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2482:                g.setCodeGenerator(generator);
2483:                generator.genRecognizer();
2484:
2485:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2486:                        generator, "a", new antlr.CommonToken(
2487:                                ANTLRParser.ACTION, action), 1);
2488:                String rawTranslation = translator.translate();
2489:                StringTemplateGroup templates = new StringTemplateGroup(".",
2490:                        AngleBracketTemplateLexer.class);
2491:                StringTemplate actionST = new StringTemplate(templates,
2492:                        rawTranslation);
2493:                String found = actionST.toString();
2494:                assertEquals(expecting, found);
2495:            }
2496:
2497:            public void testAmbiguousTokenRef() throws Exception {
2498:                String action = "$ID;";
2499:                String expecting = "";
2500:
2501:                ErrorQueue equeue = new ErrorQueue();
2502:                ErrorManager.setErrorListener(equeue);
2503:                Grammar g = new Grammar("grammar t;\n" + "a : ID ID {" + action
2504:                        + "};" + "ID : 'a';\n");
2505:                Tool antlr = newTool();
2506:                antlr.setOutputDirectory(null); // write to /dev/null
2507:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2508:                g.setCodeGenerator(generator);
2509:                generator.genRecognizer();
2510:
2511:                int expectedMsgID = ErrorManager.MSG_NONUNIQUE_REF;
2512:                Object expectedArg = "ID";
2513:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2514:                        expectedMsgID, g, null, expectedArg);
2515:                checkError(equeue, expectedMessage);
2516:            }
2517:
2518:            public void testAmbiguousTokenRefWithProp() throws Exception {
2519:                String action = "$ID.text;";
2520:                String expecting = "";
2521:
2522:                ErrorQueue equeue = new ErrorQueue();
2523:                ErrorManager.setErrorListener(equeue);
2524:                Grammar g = new Grammar("grammar t;\n" + "a : ID ID {" + action
2525:                        + "};" + "ID : 'a';\n");
2526:                Tool antlr = newTool();
2527:                antlr.setOutputDirectory(null); // write to /dev/null
2528:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2529:                g.setCodeGenerator(generator);
2530:                generator.genRecognizer();
2531:
2532:                int expectedMsgID = ErrorManager.MSG_NONUNIQUE_REF;
2533:                Object expectedArg = "ID";
2534:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2535:                        expectedMsgID, g, null, expectedArg);
2536:                checkError(equeue, expectedMessage);
2537:            }
2538:
2539:            public void testRuleRefWithDynamicScope() throws Exception {
2540:                String action = "$field::x = $field.st;";
2541:                String expecting = "((field_scope)field_stack.peek()).x = retval.st;";
2542:
2543:                ErrorQueue equeue = new ErrorQueue();
2544:                ErrorManager.setErrorListener(equeue);
2545:                Grammar g = new Grammar("grammar a;\n" + "field\n"
2546:                        + "scope { StringTemplate x; }\n" + "    :   'y' {"
2547:                        + action + "}\n" + "    ;\n");
2548:                Tool antlr = newTool();
2549:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2550:                g.setCodeGenerator(generator);
2551:                generator.genRecognizer(); // forces load of templates
2552:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2553:                        generator, "field", new antlr.CommonToken(
2554:                                ANTLRParser.ACTION, action), 1);
2555:                String rawTranslation = translator.translate();
2556:                StringTemplateGroup templates = new StringTemplateGroup(".",
2557:                        AngleBracketTemplateLexer.class);
2558:                StringTemplate actionST = new StringTemplate(templates,
2559:                        rawTranslation);
2560:                String found = actionST.toString();
2561:                assertEquals(expecting, found);
2562:
2563:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2564:                        .size());
2565:            }
2566:
2567:            public void testAssignToOwnRulenameAttr() throws Exception {
2568:                String action = "$rule.tree = null;";
2569:                String expecting = "retval.tree = null;";
2570:                ErrorQueue equeue = new ErrorQueue();
2571:                ErrorManager.setErrorListener(equeue);
2572:                Grammar g = new Grammar("grammar a;\n" + "rule\n"
2573:                        + "    : 'y' {" + action + "}\n" + "    ;");
2574:                Tool antlr = newTool();
2575:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2576:                g.setCodeGenerator(generator);
2577:                generator.genRecognizer(); // forces load of templates
2578:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2579:                        generator, "rule", new antlr.CommonToken(
2580:                                ANTLRParser.ACTION, action), 1);
2581:                String rawTranslation = translator.translate();
2582:                StringTemplateGroup templates = new StringTemplateGroup(".",
2583:                        AngleBracketTemplateLexer.class);
2584:                StringTemplate actionST = new StringTemplate(templates,
2585:                        rawTranslation);
2586:                String found = actionST.toString();
2587:                assertEquals(expecting, found);
2588:
2589:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2590:                        .size());
2591:            }
2592:
2593:            public void testAssignToOwnParamAttr() throws Exception {
2594:                String action = "$rule.i = 42; $i = 23;";
2595:                String expecting = "i = 42; i = 23;";
2596:                ErrorQueue equeue = new ErrorQueue();
2597:                ErrorManager.setErrorListener(equeue);
2598:                Grammar g = new Grammar("grammar a;\n" + "rule[int i]\n"
2599:                        + "    : 'y' {" + action + "}\n" + "    ;");
2600:                Tool antlr = newTool();
2601:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2602:                g.setCodeGenerator(generator);
2603:                generator.genRecognizer(); // forces load of templates
2604:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2605:                        generator, "rule", new antlr.CommonToken(
2606:                                ANTLRParser.ACTION, action), 1);
2607:                String rawTranslation = translator.translate();
2608:                StringTemplateGroup templates = new StringTemplateGroup(".",
2609:                        AngleBracketTemplateLexer.class);
2610:                StringTemplate actionST = new StringTemplate(templates,
2611:                        rawTranslation);
2612:                String found = actionST.toString();
2613:                assertEquals(expecting, found);
2614:
2615:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2616:                        .size());
2617:            }
2618:
2619:            public void testIllegalAssignToOwnRulenameAttr() throws Exception {
2620:                String action = "$rule.stop = 0;";
2621:                ErrorQueue equeue = new ErrorQueue();
2622:                ErrorManager.setErrorListener(equeue);
2623:                Grammar g = new Grammar("grammar a;\n" + "rule\n"
2624:                        + "    : 'y' {" + action + "}\n" + "    ;");
2625:                Tool antlr = newTool();
2626:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2627:                g.setCodeGenerator(generator);
2628:                generator.genRecognizer(); // forces load of templates
2629:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2630:                        generator, "rule", new antlr.CommonToken(
2631:                                ANTLRParser.ACTION, action), 1);
2632:                String rawTranslation = translator.translate();
2633:
2634:                int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
2635:                Object expectedArg = "rule";
2636:                Object expectedArg2 = "stop";
2637:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2638:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2639:                checkError(equeue, expectedMessage);
2640:            }
2641:
2642:            public void testIllegalAssignToLocalAttr() throws Exception {
2643:                String action = "$tree = null; $st = null; $start = 0; $stop = 0; $text = 0;";
2644:                String expecting = "retval.tree = null; retval.st = null;   ";
2645:                ErrorQueue equeue = new ErrorQueue();
2646:                ErrorManager.setErrorListener(equeue);
2647:                Grammar g = new Grammar("grammar a;\n" + "rule\n"
2648:                        + "    : 'y' {" + action + "}\n" + "    ;");
2649:                Tool antlr = newTool();
2650:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2651:                g.setCodeGenerator(generator);
2652:                generator.genRecognizer(); // forces load of templates
2653:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2654:                        generator, "rule", new antlr.CommonToken(
2655:                                ANTLRParser.ACTION, action), 1);
2656:                String rawTranslation = translator.translate();
2657:
2658:                int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
2659:                ArrayList expectedErrors = new ArrayList(3);
2660:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2661:                        expectedMsgID, g, null, "start", "");
2662:                expectedErrors.add(expectedMessage);
2663:                GrammarSemanticsMessage expectedMessage2 = new GrammarSemanticsMessage(
2664:                        expectedMsgID, g, null, "stop", "");
2665:                expectedErrors.add(expectedMessage2);
2666:                GrammarSemanticsMessage expectedMessage3 = new GrammarSemanticsMessage(
2667:                        expectedMsgID, g, null, "text", "");
2668:                expectedErrors.add(expectedMessage3);
2669:                checkErrors(equeue, expectedErrors);
2670:
2671:                StringTemplateGroup templates = new StringTemplateGroup(".",
2672:                        AngleBracketTemplateLexer.class);
2673:                StringTemplate actionST = new StringTemplate(templates,
2674:                        rawTranslation);
2675:                String found = actionST.toString();
2676:                assertEquals(expecting, found);
2677:            }
2678:
2679:            public void testIllegalAssignRuleRefAttr() throws Exception {
2680:                String action = "$other.tree = null;";
2681:                ErrorQueue equeue = new ErrorQueue();
2682:                ErrorManager.setErrorListener(equeue);
2683:                Grammar g = new Grammar("grammar a;\n"
2684:                        + "options { output = AST;}" + "otherrule\n"
2685:                        + "    : 'y' ;" + "rule\n" + "    : other=otherrule {"
2686:                        + action + "}\n" + "    ;");
2687:                Tool antlr = newTool();
2688:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2689:                g.setCodeGenerator(generator);
2690:                generator.genRecognizer(); // forces load of templates
2691:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2692:                        generator, "rule", new antlr.CommonToken(
2693:                                ANTLRParser.ACTION, action), 1);
2694:                String rawTranslation = translator.translate();
2695:
2696:                int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
2697:                Object expectedArg = "other";
2698:                Object expectedArg2 = "tree";
2699:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2700:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2701:                checkError(equeue, expectedMessage);
2702:            }
2703:
2704:            public void testIllegalAssignTokenRefAttr() throws Exception {
2705:                String action = "$ID.text = \"test\";";
2706:                ErrorQueue equeue = new ErrorQueue();
2707:                ErrorManager.setErrorListener(equeue);
2708:                Grammar g = new Grammar("grammar a;\n" + "ID\n" + "    : 'y' ;"
2709:                        + "rule\n" + "    : ID {" + action + "}\n" + "    ;");
2710:                Tool antlr = newTool();
2711:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2712:                g.setCodeGenerator(generator);
2713:                generator.genRecognizer(); // forces load of templates
2714:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2715:                        generator, "rule", new antlr.CommonToken(
2716:                                ANTLRParser.ACTION, action), 1);
2717:                String rawTranslation = translator.translate();
2718:
2719:                int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
2720:                Object expectedArg = "ID";
2721:                Object expectedArg2 = "text";
2722:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2723:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2724:                checkError(equeue, expectedMessage);
2725:            }
2726:
2727:            public void testAssignToTreeNodeAttribute() throws Exception {
2728:                String action = "$tree.scope = localScope;";
2729:                String expecting = "(()retval.tree).scope = localScope;";
2730:                ErrorQueue equeue = new ErrorQueue();
2731:                ErrorManager.setErrorListener(equeue);
2732:                Grammar g = new Grammar("grammar a;\n"
2733:                        + "options { output=AST; }" + "rule\n" + "@init {\n"
2734:                        + "   Scope localScope=null;\n" + "}\n" + "@after {\n"
2735:                        + "   $tree.scope = localScope;\n" + "}\n"
2736:                        + "   : 'a' -> ^('a')\n" + ";");
2737:                Tool antlr = newTool();
2738:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2739:                g.setCodeGenerator(generator);
2740:                generator.genRecognizer(); // forces load of templates
2741:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2742:                        generator, "rule", new antlr.CommonToken(
2743:                                ANTLRParser.ACTION, action), 1);
2744:                String rawTranslation = translator.translate();
2745:                StringTemplateGroup templates = new StringTemplateGroup(".",
2746:                        AngleBracketTemplateLexer.class);
2747:                StringTemplate actionST = new StringTemplate(templates,
2748:                        rawTranslation);
2749:                String found = actionST.toString();
2750:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2751:                        .size());
2752:                assertEquals(expecting, found);
2753:            }
2754:
2755:            public void testDoNotTranslateAttributeCompare() throws Exception {
2756:                String action = "$a.line == $b.line";
2757:                String expecting = "a.getLine() == b.getLine()";
2758:                ErrorQueue equeue = new ErrorQueue();
2759:                ErrorManager.setErrorListener(equeue);
2760:                Grammar g = new Grammar("lexer grammar a;\n" + "RULE:\n"
2761:                        + "     a=ID b=ID {" + action + "}" + "    ;\n"
2762:                        + "ID : 'id';");
2763:                Tool antlr = newTool();
2764:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2765:                g.setCodeGenerator(generator);
2766:                generator.genRecognizer();
2767:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2768:                        generator, "RULE", new antlr.CommonToken(
2769:                                ANTLRParser.ACTION, action), 1);
2770:                String rawTranslation = translator.translate();
2771:                StringTemplateGroup templates = new StringTemplateGroup(".",
2772:                        AngleBracketTemplateLexer.class);
2773:                StringTemplate actionST = new StringTemplate(templates,
2774:                        rawTranslation);
2775:                String found = actionST.toString();
2776:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2777:                        .size());
2778:                assertEquals(expecting, found);
2779:            }
2780:
2781:            public void testDoNotTranslateScopeAttributeCompare()
2782:                    throws Exception {
2783:                String action = "if ($rule::foo == \"foo\" || 1) { System.out.println(\"ouch\"); }";
2784:                String expecting = "if (((rule_scope)rule_stack.peek()).foo == \"foo\" || 1) { System.out.println(\"ouch\"); }";
2785:                ErrorQueue equeue = new ErrorQueue();
2786:                ErrorManager.setErrorListener(equeue);
2787:                Grammar g = new Grammar("grammar a;\n" + "rule\n" + "scope {\n"
2788:                        + "   String foo;" + "} :\n" + "     twoIDs"
2789:                        + "    ;\n" + "twoIDs:\n" + "    ID ID {" + action
2790:                        + "}\n" + "    ;\n" + "ID : 'id';");
2791:                Tool antlr = newTool();
2792:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2793:                g.setCodeGenerator(generator);
2794:                generator.genRecognizer();
2795:                ActionTranslatorLexer translator = new ActionTranslatorLexer(
2796:                        generator, "twoIDs", new antlr.CommonToken(
2797:                                ANTLRParser.ACTION, action), 1);
2798:                String rawTranslation = translator.translate();
2799:                // check that we didn't use scopeSetAttributeRef int translation!
2800:                boolean foundScopeSetAttributeRef = false;
2801:                for (int i = 0; i < translator.chunks.size(); i++) {
2802:                    Object chunk = translator.chunks.get(i);
2803:                    if (chunk instanceof  StringTemplate) {
2804:                        if (((StringTemplate) chunk).getName().equals(
2805:                                "scopeSetAttributeRef")) {
2806:                            foundScopeSetAttributeRef = true;
2807:                        }
2808:                    }
2809:                }
2810:                assertFalse(
2811:                        "action translator used scopeSetAttributeRef template in comparison!",
2812:                        foundScopeSetAttributeRef);
2813:                StringTemplateGroup templates = new StringTemplateGroup(".",
2814:                        AngleBracketTemplateLexer.class);
2815:                StringTemplate actionST = new StringTemplate(templates,
2816:                        rawTranslation);
2817:                String found = actionST.toString();
2818:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2819:                        .size());
2820:                assertEquals(expecting, found);
2821:            }
2822:
2823:            public void testTreeRuleStopAttributeIsInvalid() throws Exception {
2824:                String action = "$r.x; $r.start; $r.stop";
2825:                String expecting = "r.x; ((CommonTree)r.start); $r.stop";
2826:
2827:                ErrorQueue equeue = new ErrorQueue();
2828:                ErrorManager.setErrorListener(equeue);
2829:                Grammar g = new Grammar("tree grammar t;\n"
2830:                        + "options {ASTLabelType=CommonTree;}\n"
2831:                        + "a returns [int x]\n" + "  :\n" + "  ;\n"
2832:                        + "b : r=a {###" + action + "!!!}\n" + "  ;");
2833:                Tool antlr = newTool();
2834:                antlr.setOutputDirectory(null); // write to /dev/null
2835:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2836:                g.setCodeGenerator(generator);
2837:                generator.genRecognizer(); // codegen phase sets some vars we need
2838:                StringTemplate codeST = generator.getRecognizerST();
2839:                String code = codeST.toString();
2840:                String found = code.substring(code.indexOf("###") + 3, code
2841:                        .indexOf("!!!"));
2842:                assertEquals(expecting, found);
2843:
2844:                int expectedMsgID = ErrorManager.MSG_UNKNOWN_RULE_ATTRIBUTE;
2845:                Object expectedArg = "a";
2846:                Object expectedArg2 = "stop";
2847:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
2848:                        expectedMsgID, g, null, expectedArg, expectedArg2);
2849:                System.out.println("equeue:" + equeue);
2850:                checkError(equeue, expectedMessage);
2851:            }
2852:
2853:            public void testRefToTextAttributeForCurrentTreeRule()
2854:                    throws Exception {
2855:                String action = "$text";
2856:                String expecting = "input.getTokenStream().toString(\n"
2857:                        + "              input.getTreeAdaptor().getTokenStartIndex(retval.start),\n"
2858:                        + "              input.getTreeAdaptor().getTokenStopIndex(retval.start))";
2859:
2860:                ErrorQueue equeue = new ErrorQueue();
2861:                ErrorManager.setErrorListener(equeue);
2862:                Grammar g = new Grammar("tree grammar t;\n"
2863:                        + "options {ASTLabelType=CommonTree;}\n" + "a : {###"
2864:                        + action + "!!!}\n" + "  ;\n");
2865:
2866:                Tool antlr = newTool();
2867:                antlr.setOutputDirectory(null); // write to /dev/null
2868:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
2869:                g.setCodeGenerator(generator);
2870:                generator.genRecognizer(); // codegen phase sets some vars we need
2871:                StringTemplate codeST = generator.getRecognizerST();
2872:                String code = codeST.toString();
2873:                String found = code.substring(code.indexOf("###") + 3, code
2874:                        .indexOf("!!!"));
2875:                assertEquals(expecting, found);
2876:
2877:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
2878:                        .size());
2879:            }
2880:
2881:            // S U P P O R T
2882:
2883:            protected void checkError(ErrorQueue equeue,
2884:                    GrammarSemanticsMessage expectedMessage) throws Exception {
2885:                /*
2886:                System.out.println(equeue.infos);
2887:                System.out.println(equeue.warnings);
2888:                System.out.println(equeue.errors);
2889:                 */
2890:                Message foundMsg = null;
2891:                for (int i = 0; i < equeue.errors.size(); i++) {
2892:                    Message m = (Message) equeue.errors.get(i);
2893:                    if (m.msgID == expectedMessage.msgID) {
2894:                        foundMsg = m;
2895:                    }
2896:                }
2897:                assertTrue("no error; " + expectedMessage.msgID + " expected",
2898:                        equeue.errors.size() > 0);
2899:                assertNotNull("couldn't find expected error: "
2900:                        + expectedMessage.msgID, foundMsg);
2901:                assertTrue("error is not a GrammarSemanticsMessage",
2902:                        foundMsg instanceof  GrammarSemanticsMessage);
2903:                assertEquals(expectedMessage.arg, foundMsg.arg);
2904:                assertEquals(expectedMessage.arg2, foundMsg.arg2);
2905:            }
2906:
2907:            /** Allow checking for multiple errors in one test */
2908:            protected void checkErrors(ErrorQueue equeue,
2909:                    ArrayList expectedMessages) throws Exception {
2910:                ArrayList messageExpected = new ArrayList(equeue.errors.size());
2911:                for (int i = 0; i < equeue.errors.size(); i++) {
2912:                    Message m = (Message) equeue.errors.get(i);
2913:                    boolean foundMsg = false;
2914:                    for (int j = 0; j < expectedMessages.size(); j++) {
2915:                        Message em = (Message) expectedMessages.get(j);
2916:                        if (m.msgID == em.msgID && m.arg.equals(em.arg)
2917:                                && m.arg2.equals(em.arg2)) {
2918:                            foundMsg = true;
2919:                        }
2920:                    }
2921:                    if (foundMsg) {
2922:                        messageExpected.add(i, Boolean.TRUE);
2923:                    } else
2924:                        messageExpected.add(i, Boolean.FALSE);
2925:                }
2926:                for (int i = 0; i < equeue.errors.size(); i++) {
2927:                    assertTrue("unexpected error:" + equeue.errors.get(i),
2928:                            ((Boolean) messageExpected.get(i)).booleanValue());
2929:                }
2930:            }
2931:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.