Source Code Cross Referenced for TestSymbolDefinitions.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) 


001:        /*
002:         [The "BSD licence"]
003:         Copyright (c) 2005-2006 Terence Parr
004:         All rights reserved.
005:
006:         Redistribution and use in source and binary forms, with or without
007:         modification, are permitted provided that the following conditions
008:         are met:
009:         1. Redistributions of source code must retain the above copyright
010:            notice, this list of conditions and the following disclaimer.
011:         2. Redistributions in binary form must reproduce the above copyright
012:            notice, this list of conditions and the following disclaimer in the
013:            documentation and/or other materials provided with the distribution.
014:         3. The name of the author may not be used to endorse or promote products
015:            derived from this software without specific prior written permission.
016:
017:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:         */
028:        package org.antlr.test;
029:
030:        import org.antlr.Tool;
031:        import org.antlr.analysis.Label;
032:        import org.antlr.codegen.CodeGenerator;
033:        import org.antlr.stringtemplate.StringTemplate;
034:        import org.antlr.tool.*;
035:
036:        import java.io.StringReader;
037:        import java.util.*;
038:
039:        public class TestSymbolDefinitions extends BaseTest {
040:
041:            /** Public default constructor used by TestRig */
042:            public TestSymbolDefinitions() {
043:            }
044:
045:            public void testParserSimpleTokens() throws Exception {
046:                Grammar g = new Grammar("parser grammar t;\n" + "a : A | B;\n"
047:                        + "b : C ;");
048:                String rules = "a, b";
049:                String tokenNames = "A, B, C";
050:                checkSymbols(g, rules, tokenNames);
051:            }
052:
053:            public void testParserTokensSection() throws Exception {
054:                Grammar g = new Grammar("parser grammar t;\n" + "tokens {\n"
055:                        + "  C;\n" + "  D;" + "}\n" + "a : A | B;\n"
056:                        + "b : C ;");
057:                String rules = "a, b";
058:                String tokenNames = "A, B, C, D";
059:                checkSymbols(g, rules, tokenNames);
060:            }
061:
062:            public void testLexerTokensSection() throws Exception {
063:                Grammar g = new Grammar("lexer grammar t;\n" + "tokens {\n"
064:                        + "  C;\n" + "  D;" + "}\n" + "A : 'a';\n"
065:                        + "C : 'c' ;");
066:                String rules = "A, C, Tokens";
067:                String tokenNames = "A, C, D";
068:                checkSymbols(g, rules, tokenNames);
069:            }
070:
071:            public void testTokensSectionWithAssignmentSection()
072:                    throws Exception {
073:                Grammar g = new Grammar("grammar t;\n" + "tokens {\n"
074:                        + "  C='c';\n" + "  D;" + "}\n" + "a : A | B;\n"
075:                        + "b : C ;");
076:                String rules = "a, b";
077:                String tokenNames = "A, B, C, D, 'c'";
078:                checkSymbols(g, rules, tokenNames);
079:            }
080:
081:            public void testCombinedGrammarLiterals() throws Exception {
082:                Grammar g = new Grammar("grammar t;\n"
083:                        + "a : 'begin' b 'end';\n" + "b : C ';' ;\n"
084:                        + "ID : 'a' ;\n" + "FOO : 'foo' ;\n" + // "foo" is not a token name
085:                        "C : 'c' ;\n"); // nor is 'c'
086:                String rules = "a, b";
087:                String tokenNames = "C, FOO, ID, 'begin', 'end', ';'";
088:                checkSymbols(g, rules, tokenNames);
089:            }
090:
091:            public void testLiteralInParserAndLexer() throws Exception {
092:                // 'x' is token and char in lexer rule
093:                Grammar g = new Grammar("grammar t;\n" + "a : 'x' E ; \n"
094:                        + "E: 'x' '0' ;\n"); // nor is 'c'
095:                String literals = "['x']";
096:                String foundLiterals = g.getStringLiterals().toString();
097:                assertEquals(literals, foundLiterals);
098:
099:                String implicitLexer = "lexer grammar t;\n" + "\n"
100:                        + "T5 : 'x' ;\n" + "\n"
101:                        + "// $ANTLR src \"<string>\" 3\n" + "E: 'x' '0' ;\n";
102:                assertEquals(implicitLexer, g.getLexerGrammar());
103:            }
104:
105:            public void testCombinedGrammarWithRefToLiteralButNoTokenIDRef()
106:                    throws Exception {
107:                Grammar g = new Grammar("grammar t;\n" + "a : 'a' ;\n"
108:                        + "A : 'a' ;\n");
109:                String rules = "a";
110:                String tokenNames = "A, 'a'";
111:                checkSymbols(g, rules, tokenNames);
112:            }
113:
114:            public void testSetDoesNotMissTokenAliases() throws Exception {
115:                Grammar g = new Grammar("grammar t;\n" + "a : 'a'|'b' ;\n"
116:                        + "A : 'a' ;\n" + "B : 'b' ;\n");
117:                String rules = "a";
118:                String tokenNames = "A, 'a', B, 'b'";
119:                checkSymbols(g, rules, tokenNames);
120:            }
121:
122:            public void testSimplePlusEqualLabel() throws Exception {
123:                Grammar g = new Grammar("parser grammar t;\n"
124:                        + "a : ids+=ID ( COMMA ids+=ID )* ;\n");
125:                String rule = "a";
126:                String tokenLabels = "ids";
127:                String ruleLabels = null;
128:                checkPlusEqualsLabels(g, rule, tokenLabels, ruleLabels);
129:            }
130:
131:            public void testMixedPlusEqualLabel() throws Exception {
132:                Grammar g = new Grammar("grammar t;\n"
133:                        + "options {output=AST;}\n"
134:                        + "a : id+=ID ( ',' e+=expr )* ;\n" + "expr : 'e';\n"
135:                        + "ID : 'a';\n");
136:                String rule = "a";
137:                String tokenLabels = "id";
138:                String ruleLabels = "e";
139:                checkPlusEqualsLabels(g, rule, tokenLabels, ruleLabels);
140:            }
141:
142:            // T E S T  L I T E R A L  E S C A P E S
143:
144:            public void testParserCharLiteralWithEscape() throws Exception {
145:                Grammar g = new Grammar("grammar t;\n" + "a : '\\n';\n");
146:                Set literals = g.getStringLiterals();
147:                // must store literals how they appear in the antlr grammar
148:                assertEquals("'\\n'", literals.toArray()[0]);
149:            }
150:
151:            public void testTokenInTokensSectionAndTokenRuleDef()
152:                    throws Exception {
153:                // this must return A not I to the parser; calling a nonfragment rule
154:                // from a nonfragment rule does not set the overall token.
155:                String grammar = "grammar P;\n" + "tokens { B='}'; }\n"
156:                        + "a : A B {System.out.println(input);} ;\n"
157:                        + "A : 'a' ;\n" + "B : '}' ;\n"
158:                        + "WS : (' '|'\\n') {$channel=HIDDEN;} ;";
159:                String found = execParser("P.g", grammar, "PParser", "PLexer",
160:                        "a", "a}", false);
161:                assertEquals("a}\n", found);
162:            }
163:
164:            public void testTokenInTokensSectionAndTokenRuleDef2()
165:                    throws Exception {
166:                // this must return A not I to the parser; calling a nonfragment rule
167:                // from a nonfragment rule does not set the overall token.
168:                String grammar = "grammar P;\n" + "tokens { B='}'; }\n"
169:                        + "a : A '}' {System.out.println(input);} ;\n"
170:                        + "A : 'a' ;\n" + "B : '}' {/* */} ;\n"
171:                        + "WS : (' '|'\\n') {$channel=HIDDEN;} ;";
172:                String found = execParser("P.g", grammar, "PParser", "PLexer",
173:                        "a", "a}", false);
174:                assertEquals("a}\n", found);
175:            }
176:
177:            public void testRefToRuleWithNoReturnValue() throws Exception {
178:                ErrorQueue equeue = new ErrorQueue();
179:                ErrorManager.setErrorListener(equeue);
180:
181:                String grammarStr = "grammar P;\n" + "a : x=b ;\n"
182:                        + "b : B ;\n" + "B : 'b' ;\n";
183:                Grammar g = new Grammar(grammarStr);
184:
185:                Tool antlr = newTool();
186:                CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
187:                g.setCodeGenerator(generator);
188:                StringTemplate recogST = generator.genRecognizer();
189:                String code = recogST.toString();
190:                assertTrue("not expecting label", code.indexOf("x=b();") < 0);
191:
192:                assertEquals("unexpected errors: " + equeue, 0, equeue.errors
193:                        .size());
194:            }
195:
196:            // T E S T  E R R O R S
197:
198:            public void testParserStringLiterals() throws Exception {
199:                ErrorQueue equeue = new ErrorQueue();
200:                ErrorManager.setErrorListener(equeue);
201:                Grammar g = new Grammar("parser grammar t;\n"
202:                        + "a : 'begin' b ;\n" + "b : C ;");
203:                Object expectedArg = "'begin'";
204:                int expectedMsgID = ErrorManager.MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE;
205:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
206:                        expectedMsgID, g, null, expectedArg);
207:                checkError(equeue, expectedMessage);
208:            }
209:
210:            public void testParserCharLiterals() throws Exception {
211:                ErrorQueue equeue = new ErrorQueue();
212:                ErrorManager.setErrorListener(equeue);
213:                Grammar g = new Grammar("parser grammar t;\n" + "a : '(' b ;\n"
214:                        + "b : C ;");
215:                Object expectedArg = "'('";
216:                int expectedMsgID = ErrorManager.MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE;
217:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
218:                        expectedMsgID, g, null, expectedArg);
219:                checkError(equeue, expectedMessage);
220:            }
221:
222:            public void testEmptyNotChar() throws Exception {
223:                ErrorQueue equeue = new ErrorQueue();
224:                ErrorManager.setErrorListener(equeue);
225:                Grammar g = new Grammar("grammar foo;\n" + "a : (~'x')+ ;\n");
226:                g.createNFAs();
227:                Object expectedArg = "'x'";
228:                int expectedMsgID = ErrorManager.MSG_EMPTY_COMPLEMENT;
229:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
230:                        expectedMsgID, g, null, expectedArg);
231:                checkError(equeue, expectedMessage);
232:            }
233:
234:            public void testEmptyNotToken() throws Exception {
235:                ErrorQueue equeue = new ErrorQueue();
236:                ErrorManager.setErrorListener(equeue);
237:                Grammar g = new Grammar("grammar foo;\n" + "a : (~A)+ ;\n");
238:                g.createNFAs();
239:                Object expectedArg = "A";
240:                int expectedMsgID = ErrorManager.MSG_EMPTY_COMPLEMENT;
241:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
242:                        expectedMsgID, g, null, expectedArg);
243:                checkError(equeue, expectedMessage);
244:            }
245:
246:            public void testEmptyNotSet() throws Exception {
247:                ErrorQueue equeue = new ErrorQueue();
248:                ErrorManager.setErrorListener(equeue);
249:                Grammar g = new Grammar("grammar foo;\n" + "a : (~(A|B))+ ;\n");
250:                g.createNFAs();
251:                Object expectedArg = null;
252:                int expectedMsgID = ErrorManager.MSG_EMPTY_COMPLEMENT;
253:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
254:                        expectedMsgID, g, null, expectedArg);
255:                checkError(equeue, expectedMessage);
256:            }
257:
258:            public void testStringLiteralInParserTokensSection()
259:                    throws Exception {
260:                ErrorQueue equeue = new ErrorQueue();
261:                ErrorManager.setErrorListener(equeue); // unique listener per thread
262:                Grammar g = new Grammar("parser grammar t;\n" + "tokens {\n"
263:                        + "  B='begin';\n" + "}\n" + "a : A B;\n" + "b : C ;");
264:                Object expectedArg = "'begin'";
265:                int expectedMsgID = ErrorManager.MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE;
266:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
267:                        expectedMsgID, g, null, expectedArg);
268:                checkError(equeue, expectedMessage);
269:            }
270:
271:            public void testCharLiteralInParserTokensSection() throws Exception {
272:                ErrorQueue equeue = new ErrorQueue();
273:                ErrorManager.setErrorListener(equeue); // unique listener per thread
274:                Grammar g = new Grammar("parser grammar t;\n" + "tokens {\n"
275:                        + "  B='(';\n" + "}\n" + "a : A B;\n" + "b : C ;");
276:                Object expectedArg = "'('";
277:                int expectedMsgID = ErrorManager.MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE;
278:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
279:                        expectedMsgID, g, null, expectedArg);
280:                checkError(equeue, expectedMessage);
281:            }
282:
283:            public void testCharLiteralInLexerTokensSection() throws Exception {
284:                ErrorQueue equeue = new ErrorQueue();
285:                ErrorManager.setErrorListener(equeue); // unique listener per thread
286:                Grammar g = new Grammar("lexer grammar t;\n" + "tokens {\n"
287:                        + "  B='(';\n" + "}\n" + "ID : 'a';\n");
288:                Object expectedArg = "'('";
289:                int expectedMsgID = ErrorManager.MSG_CANNOT_ALIAS_TOKENS_IN_LEXER;
290:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
291:                        expectedMsgID, g, null, expectedArg);
292:                checkError(equeue, expectedMessage);
293:            }
294:
295:            public void testRuleRedefinition() throws Exception {
296:                ErrorQueue equeue = new ErrorQueue();
297:                ErrorManager.setErrorListener(equeue); // unique listener per thread
298:                Grammar g = new Grammar("parser grammar t;\n" + "a : A | B;\n"
299:                        + "a : C ;");
300:
301:                Object expectedArg = "a";
302:                int expectedMsgID = ErrorManager.MSG_RULE_REDEFINITION;
303:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
304:                        expectedMsgID, g, null, expectedArg);
305:                checkError(equeue, expectedMessage);
306:            }
307:
308:            public void testLexerRuleRedefinition() throws Exception {
309:                ErrorQueue equeue = new ErrorQueue();
310:                ErrorManager.setErrorListener(equeue); // unique listener per thread
311:                Grammar g = new Grammar("lexer grammar t;\n" + "ID : 'a' ;\n"
312:                        + "ID : 'd' ;");
313:
314:                Object expectedArg = "ID";
315:                int expectedMsgID = ErrorManager.MSG_RULE_REDEFINITION;
316:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
317:                        expectedMsgID, g, null, expectedArg);
318:                checkError(equeue, expectedMessage);
319:            }
320:
321:            public void testCombinedRuleRedefinition() throws Exception {
322:                ErrorQueue equeue = new ErrorQueue();
323:                ErrorManager.setErrorListener(equeue); // unique listener per thread
324:                Grammar g = new Grammar("grammar t;\n" + "x : ID ;\n"
325:                        + "ID : 'a' ;\n" + "x : ID ID ;");
326:
327:                Object expectedArg = "x";
328:                int expectedMsgID = ErrorManager.MSG_RULE_REDEFINITION;
329:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
330:                        expectedMsgID, g, null, expectedArg);
331:                checkError(equeue, expectedMessage);
332:            }
333:
334:            public void testUndefinedToken() throws Exception {
335:                ErrorQueue equeue = new ErrorQueue();
336:                ErrorManager.setErrorListener(equeue); // unique listener per thread
337:                Grammar g = new Grammar("grammar t;\n" + "x : ID ;");
338:
339:                Object expectedArg = "ID";
340:                int expectedMsgID = ErrorManager.MSG_NO_TOKEN_DEFINITION;
341:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
342:                        expectedMsgID, g, null, expectedArg);
343:                checkWarning(equeue, expectedMessage);
344:            }
345:
346:            public void testUndefinedTokenOkInParser() throws Exception {
347:                ErrorQueue equeue = new ErrorQueue();
348:                ErrorManager.setErrorListener(equeue); // unique listener per thread
349:                Grammar g = new Grammar("parser grammar t;\n" + "x : ID ;");
350:                assertEquals("should not be an error", 0, equeue.errors.size());
351:            }
352:
353:            public void testUndefinedRule() throws Exception {
354:                ErrorQueue equeue = new ErrorQueue();
355:                ErrorManager.setErrorListener(equeue); // unique listener per thread
356:                Grammar g = new Grammar("grammar t;\n" + "x : r ;");
357:
358:                Object expectedArg = "r";
359:                int expectedMsgID = ErrorManager.MSG_UNDEFINED_RULE_REF;
360:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
361:                        expectedMsgID, g, null, expectedArg);
362:                checkError(equeue, expectedMessage);
363:            }
364:
365:            public void testLexerRuleInParser() throws Exception {
366:                ErrorQueue equeue = new ErrorQueue();
367:                ErrorManager.setErrorListener(equeue); // unique listener per thread
368:                Grammar g = new Grammar("parser grammar t;\n" + "X : ;");
369:
370:                Object expectedArg = "X";
371:                int expectedMsgID = ErrorManager.MSG_LEXER_RULES_NOT_ALLOWED;
372:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
373:                        expectedMsgID, g, null, expectedArg);
374:                checkError(equeue, expectedMessage);
375:            }
376:
377:            public void testParserRuleInLexer() throws Exception {
378:                ErrorQueue equeue = new ErrorQueue();
379:                ErrorManager.setErrorListener(equeue); // unique listener per thread
380:                Grammar g = new Grammar("lexer grammar t;\n" + "a : ;");
381:
382:                Object expectedArg = "a";
383:                int expectedMsgID = ErrorManager.MSG_PARSER_RULES_NOT_ALLOWED;
384:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
385:                        expectedMsgID, g, null, expectedArg);
386:                checkError(equeue, expectedMessage);
387:            }
388:
389:            public void testRuleScopeConflict() throws Exception {
390:                ErrorQueue equeue = new ErrorQueue();
391:                ErrorManager.setErrorListener(equeue); // unique listener per thread
392:                Grammar g = new Grammar("grammar t;\n" + "scope a {\n"
393:                        + "  int n;\n" + "}\n" + "a : \n" + "  ;\n");
394:
395:                Object expectedArg = "a";
396:                int expectedMsgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
397:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
398:                        expectedMsgID, g, null, expectedArg);
399:                checkError(equeue, expectedMessage);
400:            }
401:
402:            public void testTokenRuleScopeConflict() throws Exception {
403:                ErrorQueue equeue = new ErrorQueue();
404:                ErrorManager.setErrorListener(equeue); // unique listener per thread
405:                Grammar g = new Grammar("grammar t;\n" + "scope ID {\n"
406:                        + "  int n;\n" + "}\n" + "ID : 'a'\n" + "  ;\n");
407:
408:                Object expectedArg = "ID";
409:                int expectedMsgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
410:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
411:                        expectedMsgID, g, null, expectedArg);
412:                checkError(equeue, expectedMessage);
413:            }
414:
415:            public void testTokenScopeConflict() throws Exception {
416:                ErrorQueue equeue = new ErrorQueue();
417:                ErrorManager.setErrorListener(equeue); // unique listener per thread
418:                Grammar g = new Grammar("grammar t;\n" + "tokens { ID; }\n"
419:                        + "scope ID {\n" + "  int n;\n" + "}\n" + "a : \n"
420:                        + "  ;\n");
421:
422:                Object expectedArg = "ID";
423:                int expectedMsgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
424:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
425:                        expectedMsgID, g, null, expectedArg);
426:                checkError(equeue, expectedMessage);
427:            }
428:
429:            public void testTokenRuleScopeConflictInLexerGrammar()
430:                    throws Exception {
431:                ErrorQueue equeue = new ErrorQueue();
432:                ErrorManager.setErrorListener(equeue); // unique listener per thread
433:                Grammar g = new Grammar("lexer grammar t;\n" + "scope ID {\n"
434:                        + "  int n;\n" + "}\n" + "ID : 'a'\n" + "  ;\n");
435:
436:                Object expectedArg = "ID";
437:                int expectedMsgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
438:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
439:                        expectedMsgID, g, null, expectedArg);
440:                checkError(equeue, expectedMessage);
441:            }
442:
443:            public void testTokenLabelScopeConflict() throws Exception {
444:                ErrorQueue equeue = new ErrorQueue();
445:                ErrorManager.setErrorListener(equeue); // unique listener per thread
446:                Grammar g = new Grammar("parser grammar t;\n" + "scope s {\n"
447:                        + "  int n;\n" + "}\n" + "a : s=ID \n" + "  ;\n");
448:
449:                Object expectedArg = "s";
450:                int expectedMsgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
451:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
452:                        expectedMsgID, g, null, expectedArg);
453:                checkError(equeue, expectedMessage);
454:            }
455:
456:            public void testRuleLabelScopeConflict() throws Exception {
457:                ErrorQueue equeue = new ErrorQueue();
458:                ErrorManager.setErrorListener(equeue); // unique listener per thread
459:                Grammar g = new Grammar("parser grammar t;\n" + "scope s {\n"
460:                        + "  int n;\n" + "}\n" + "a : s=b \n" + "  ;\n"
461:                        + "b : ;\n");
462:
463:                Object expectedArg = "s";
464:                int expectedMsgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
465:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
466:                        expectedMsgID, g, null, expectedArg);
467:                checkError(equeue, expectedMessage);
468:            }
469:
470:            public void testLabelAndRuleNameConflict() throws Exception {
471:                ErrorQueue equeue = new ErrorQueue();
472:                ErrorManager.setErrorListener(equeue); // unique listener per thread
473:                Grammar g = new Grammar("parser grammar t;\n" + "a : c=b \n"
474:                        + "  ;\n" + "b : ;\n" + "c : ;\n");
475:
476:                Object expectedArg = "c";
477:                int expectedMsgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_RULE;
478:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
479:                        expectedMsgID, g, null, expectedArg);
480:                checkError(equeue, expectedMessage);
481:            }
482:
483:            public void testLabelAndTokenNameConflict() throws Exception {
484:                ErrorQueue equeue = new ErrorQueue();
485:                ErrorManager.setErrorListener(equeue); // unique listener per thread
486:                Grammar g = new Grammar("parser grammar t;\n" + "a : ID=b \n"
487:                        + "  ;\n" + "b : ID ;\n" + "c : ;\n");
488:
489:                Object expectedArg = "ID";
490:                int expectedMsgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_TOKEN;
491:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
492:                        expectedMsgID, g, null, expectedArg);
493:                checkError(equeue, expectedMessage);
494:            }
495:
496:            public void testLabelAndArgConflict() throws Exception {
497:                ErrorQueue equeue = new ErrorQueue();
498:                ErrorManager.setErrorListener(equeue); // unique listener per thread
499:                Grammar g = new Grammar("parser grammar t;\n"
500:                        + "a[int i] returns [int x]: i=ID \n" + "  ;\n");
501:
502:                Object expectedArg = "i";
503:                int expectedMsgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL;
504:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
505:                        expectedMsgID, g, null, expectedArg);
506:                checkError(equeue, expectedMessage);
507:            }
508:
509:            public void testLabelAndParameterConflict() throws Exception {
510:                ErrorQueue equeue = new ErrorQueue();
511:                ErrorManager.setErrorListener(equeue); // unique listener per thread
512:                Grammar g = new Grammar("parser grammar t;\n"
513:                        + "a[int i] returns [int x]: x=ID \n" + "  ;\n");
514:
515:                Object expectedArg = "x";
516:                int expectedMsgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL;
517:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
518:                        expectedMsgID, g, null, expectedArg);
519:                checkError(equeue, expectedMessage);
520:            }
521:
522:            public void testLabelRuleScopeConflict() throws Exception {
523:                ErrorQueue equeue = new ErrorQueue();
524:                ErrorManager.setErrorListener(equeue); // unique listener per thread
525:                Grammar g = new Grammar("parser grammar t;\n" + "a\n"
526:                        + "scope {" + "  int n;" + "}\n" + "  : n=ID\n"
527:                        + "  ;\n");
528:
529:                Object expectedArg = "n";
530:                Object expectedArg2 = "a";
531:                int expectedMsgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE;
532:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
533:                        expectedMsgID, g, null, expectedArg, expectedArg2);
534:                checkError(equeue, expectedMessage);
535:            }
536:
537:            public void testRuleScopeArgConflict() throws Exception {
538:                ErrorQueue equeue = new ErrorQueue();
539:                ErrorManager.setErrorListener(equeue); // unique listener per thread
540:                Grammar g = new Grammar("parser grammar t;\n" + "a[int n]\n"
541:                        + "scope {" + "  int n;" + "}\n" + "  : \n" + "  ;\n");
542:
543:                Object expectedArg = "n";
544:                Object expectedArg2 = "a";
545:                int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL;
546:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
547:                        expectedMsgID, g, null, expectedArg, expectedArg2);
548:                checkError(equeue, expectedMessage);
549:            }
550:
551:            public void testRuleScopeReturnValueConflict() throws Exception {
552:                ErrorQueue equeue = new ErrorQueue();
553:                ErrorManager.setErrorListener(equeue); // unique listener per thread
554:                Grammar g = new Grammar("parser grammar t;\n"
555:                        + "a returns [int n]\n" + "scope {" + "  int n;"
556:                        + "}\n" + "  : \n" + "  ;\n");
557:
558:                Object expectedArg = "n";
559:                Object expectedArg2 = "a";
560:                int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL;
561:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
562:                        expectedMsgID, g, null, expectedArg, expectedArg2);
563:                checkError(equeue, expectedMessage);
564:            }
565:
566:            public void testRuleScopeRuleNameConflict() throws Exception {
567:                ErrorQueue equeue = new ErrorQueue();
568:                ErrorManager.setErrorListener(equeue); // unique listener per thread
569:                Grammar g = new Grammar("parser grammar t;\n" + "a\n"
570:                        + "scope {" + "  int a;" + "}\n" + "  : \n" + "  ;\n");
571:
572:                Object expectedArg = "a";
573:                Object expectedArg2 = null;
574:                int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_CONFLICTS_WITH_RULE;
575:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
576:                        expectedMsgID, g, null, expectedArg, expectedArg2);
577:                checkError(equeue, expectedMessage);
578:            }
579:
580:            public void testBadGrammarOption() throws Exception {
581:                ErrorQueue equeue = new ErrorQueue();
582:                ErrorManager.setErrorListener(equeue); // unique listener per thread
583:                Tool antlr = newTool();
584:                Grammar g = new Grammar(antlr, "t", new StringReader(
585:                        "grammar t;\n" + "options {foo=3; language=Java;}\n"
586:                                + "a : 'a';\n"));
587:
588:                Object expectedArg = "foo";
589:                int expectedMsgID = ErrorManager.MSG_ILLEGAL_OPTION;
590:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
591:                        expectedMsgID, g, null, expectedArg);
592:                checkError(equeue, expectedMessage);
593:            }
594:
595:            public void testBadRuleOption() throws Exception {
596:                ErrorQueue equeue = new ErrorQueue();
597:                ErrorManager.setErrorListener(equeue); // unique listener per thread
598:                Grammar g = new Grammar("grammar t;\n" + "a\n"
599:                        + "options {k=3; tokenVocab=blort;}\n" + "  : 'a';\n");
600:
601:                Object expectedArg = "tokenVocab";
602:                int expectedMsgID = ErrorManager.MSG_ILLEGAL_OPTION;
603:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
604:                        expectedMsgID, g, null, expectedArg);
605:                checkError(equeue, expectedMessage);
606:            }
607:
608:            public void testBadSubRuleOption() throws Exception {
609:                ErrorQueue equeue = new ErrorQueue();
610:                ErrorManager.setErrorListener(equeue); // unique listener per thread
611:                Grammar g = new Grammar("grammar t;\n"
612:                        + "a : ( options {k=3; language=Java;}\n"
613:                        + "    : 'a'\n" + "    | 'b'\n" + "    )\n" + "  ;\n");
614:                Object expectedArg = "language";
615:                int expectedMsgID = ErrorManager.MSG_ILLEGAL_OPTION;
616:                GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
617:                        expectedMsgID, g, null, expectedArg);
618:                checkError(equeue, expectedMessage);
619:            }
620:
621:            protected void checkError(ErrorQueue equeue,
622:                    GrammarSemanticsMessage expectedMessage) throws Exception {
623:                /*
624:                System.out.println(equeue.infos);
625:                System.out.println(equeue.warnings);
626:                System.out.println(equeue.errors);
627:                assertTrue("number of errors mismatch", n, equeue.errors.size());
628:                 */
629:                Message foundMsg = null;
630:                for (int i = 0; i < equeue.errors.size(); i++) {
631:                    Message m = (Message) equeue.errors.get(i);
632:                    if (m.msgID == expectedMessage.msgID) {
633:                        foundMsg = m;
634:                    }
635:                }
636:                assertNotNull("no error; " + expectedMessage.msgID
637:                        + " expected", foundMsg);
638:                assertTrue("error is not a GrammarSemanticsMessage",
639:                        foundMsg instanceof  GrammarSemanticsMessage);
640:                assertEquals(expectedMessage.arg, foundMsg.arg);
641:            }
642:
643:            protected void checkWarning(ErrorQueue equeue,
644:                    GrammarSemanticsMessage expectedMessage) throws Exception {
645:                Message foundMsg = null;
646:                for (int i = 0; i < equeue.warnings.size(); i++) {
647:                    Message m = (Message) equeue.warnings.get(i);
648:                    if (m.msgID == expectedMessage.msgID) {
649:                        foundMsg = m;
650:                    }
651:                }
652:                assertNotNull("no error; " + expectedMessage.msgID
653:                        + " expected", foundMsg);
654:                assertTrue("error is not a GrammarSemanticsMessage",
655:                        foundMsg instanceof  GrammarSemanticsMessage);
656:                assertEquals(expectedMessage.arg, foundMsg.arg);
657:            }
658:
659:            protected void checkPlusEqualsLabels(Grammar g, String ruleName,
660:                    String tokenLabelsStr, String ruleLabelsStr)
661:                    throws Exception {
662:                // make sure expected += labels are there
663:                Rule r = g.getRule(ruleName);
664:                StringTokenizer st = new StringTokenizer(tokenLabelsStr, ", ");
665:                Set tokenLabels = null;
666:                while (st.hasMoreTokens()) {
667:                    if (tokenLabels == null) {
668:                        tokenLabels = new HashSet();
669:                    }
670:                    String labelName = st.nextToken();
671:                    tokenLabels.add(labelName);
672:                }
673:                Set ruleLabels = null;
674:                if (ruleLabelsStr != null) {
675:                    st = new StringTokenizer(ruleLabelsStr, ", ");
676:                    ruleLabels = new HashSet();
677:                    while (st.hasMoreTokens()) {
678:                        String labelName = st.nextToken();
679:                        ruleLabels.add(labelName);
680:                    }
681:                }
682:                assertTrue(
683:                        "token += labels mismatch; " + tokenLabels + "!="
684:                                + r.tokenListLabels,
685:                        (tokenLabels != null && r.tokenListLabels != null)
686:                                || (tokenLabels == null && r.tokenListLabels == null));
687:                assertTrue(
688:                        "rule += labels mismatch; " + ruleLabels + "!="
689:                                + r.ruleListLabels,
690:                        (ruleLabels != null && r.ruleListLabels != null)
691:                                || (ruleLabels == null && r.ruleListLabels == null));
692:                if (tokenLabels != null) {
693:                    assertEquals(tokenLabels, r.tokenListLabels.keySet());
694:                }
695:                if (ruleLabels != null) {
696:                    assertEquals(ruleLabels, r.ruleListLabels.keySet());
697:                }
698:            }
699:
700:            protected void checkSymbols(Grammar g, String rulesStr,
701:                    String tokensStr) throws Exception {
702:                Set tokens = g.getTokenDisplayNames();
703:
704:                // make sure expected tokens are there
705:                StringTokenizer st = new StringTokenizer(tokensStr, ", ");
706:                while (st.hasMoreTokens()) {
707:                    String tokenName = st.nextToken();
708:                    assertTrue("token " + tokenName + " expected", g
709:                            .getTokenType(tokenName) != Label.INVALID);
710:                    tokens.remove(tokenName);
711:                }
712:                // make sure there are not any others (other than <EOF> etc...)
713:                for (Iterator iter = tokens.iterator(); iter.hasNext();) {
714:                    String tokenName = (String) iter.next();
715:                    assertTrue("unexpected token name " + tokenName, g
716:                            .getTokenType(tokenName) < Label.MIN_TOKEN_TYPE);
717:                }
718:
719:                // make sure all expected rules are there
720:                st = new StringTokenizer(rulesStr, ", ");
721:                int n = 0;
722:                while (st.hasMoreTokens()) {
723:                    String ruleName = st.nextToken();
724:                    assertNotNull("rule " + ruleName + " expected", g
725:                            .getRule(ruleName));
726:                    n++;
727:                }
728:                Collection rules = g.getRules();
729:                //System.out.println("rules="+rules);
730:                // make sure there are no extra rules
731:                assertEquals("number of rules mismatch; expecting " + n
732:                        + "; found " + rules.size(), n, rules.size());
733:
734:            }
735:
736:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.