1/* 2 * [The "BSD license"] 3 * Copyright (c) 2010 Terence Parr 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28package org.antlr.test; 29 30import org.junit.Test; 31 32/** General code generation testing; compilation and/or execution. 33 * These tests are more about avoiding duplicate var definitions 34 * etc... than testing a particular ANTLR feature. 35 */ 36public class TestJavaCodeGeneration extends BaseTest { 37 @Test public void testDupVarDefForPinchedState() { 38 // so->s2 and s0->s3->s1 pinches back to s1 39 // LA3_1, s1 state for DFA 3, was defined twice in similar scope 40 // just wrapped in curlies and it's cool. 41 String grammar = 42 "grammar T;\n" + 43 "a : (| A | B) X Y\n" + 44 " | (| A | B) X Z\n" + 45 " ;\n" ; 46 boolean found = 47 rawGenerateAndBuildRecognizer( 48 "T.g", grammar, "TParser", null, false); 49 boolean expecting = true; // should be ok 50 assertEquals(expecting, found); 51 } 52 53 @Test public void testLabeledNotSetsInLexer() { 54 // d must be an int 55 String grammar = 56 "lexer grammar T;\n" + 57 "A : d=~('x'|'y') e='0'..'9'\n" + 58 " ; \n" ; 59 boolean found = 60 rawGenerateAndBuildRecognizer( 61 "T.g", grammar, null, "T", false); 62 boolean expecting = true; // should be ok 63 assertEquals(expecting, found); 64 } 65 66 @Test public void testLabeledSetsInLexer() { 67 // d must be an int 68 String grammar = 69 "grammar T;\n" + 70 "a : A ;\n" + 71 "A : d=('x'|'y') {System.out.println((char)$d);}\n" + 72 " ; \n" ; 73 String found = execParser("T.g", grammar, "TParser", "TLexer", 74 "a", "x", false); 75 assertEquals("x\n", found); 76 } 77 78 @Test public void testLabeledRangeInLexer() { 79 // d must be an int 80 String grammar = 81 "grammar T;\n" + 82 "a : A;\n" + 83 "A : d='a'..'z' {System.out.println((char)$d);} \n" + 84 " ; \n" ; 85 String found = execParser("T.g", grammar, "TParser", "TLexer", 86 "a", "x", false); 87 assertEquals("x\n", found); 88 } 89 90 @Test public void testLabeledWildcardInLexer() { 91 // d must be an int 92 String grammar = 93 "grammar T;\n" + 94 "a : A;\n" + 95 "A : d=. {System.out.println((char)$d);}\n" + 96 " ; \n" ; 97 String found = execParser("T.g", grammar, "TParser", "TLexer", 98 "a", "x", false); 99 assertEquals("x\n", found); 100 } 101 102 @Test public void testSynpredWithPlusLoop() { 103 String grammar = 104 "grammar T; \n" + 105 "a : (('x'+)=> 'x'+)?;\n"; 106 boolean found = 107 rawGenerateAndBuildRecognizer( 108 "T.g", grammar, "TParser", "TLexer", false); 109 boolean expecting = true; // should be ok 110 assertEquals(expecting, found); 111 } 112 113 @Test public void testDoubleQuoteEscape() { 114 String grammar = 115 "lexer grammar T; \n" + 116 "A : '\\\\\"';\n" + // this is A : '\\"', which should give "\\\"" at Java level; 117 "B : '\\\"';\n" + // this is B: '\"', which shodl give "\"" at Java level; 118 "C : '\\'\\'';\n" + // this is C: '\'\'', which shoudl give "''" at Java level 119 "D : '\\k';\n"; // this is D: '\k', which shoudl give just "k" at Java level; 120 121 boolean found = 122 rawGenerateAndBuildRecognizer( 123 "T.g", grammar, null, "T", false); 124 boolean expecting = true; // should be ok 125 assertEquals(expecting, found); 126 } 127 128 @Test public void testBlankRuleGetsNoException() { 129 String grammar = 130 "grammar T;\n" + 131 "a : sync (ID sync)* ;\n" + 132 "sync : ;\n" + 133 "ID : 'a'..'z'+;\n"; 134 boolean found = 135 rawGenerateAndBuildRecognizer( 136 "T.g", grammar, "TParser", "TLexer", false); 137 boolean expecting = true; // should be ok 138 assertEquals(expecting, found); 139 } 140} 141