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 */ 28 package org.antlr.test; 29 30 import org.junit.Test; 31 32 import static org.junit.Assert.*; 33 34 /** General code generation testing; compilation and/or execution. 35 * These tests are more about avoiding duplicate var definitions 36 * etc... than testing a particular ANTLR feature. 37 */ 38 public class TestJavaCodeGeneration extends BaseTest { testDupVarDefForPinchedState()39 @Test public void testDupVarDefForPinchedState() { 40 // so->s2 and s0->s3->s1 pinches back to s1 41 // LA3_1, s1 state for DFA 3, was defined twice in similar scope 42 // just wrapped in curlies and it's cool. 43 String grammar = 44 "grammar T;\n" + 45 "a : (| A | B) X Y\n" + 46 " | (| A | B) X Z\n" + 47 " ;\n" ; 48 boolean found = 49 rawGenerateAndBuildRecognizer( 50 "T.g", grammar, "TParser", null, false); 51 boolean expecting = true; // should be ok 52 assertEquals(expecting, found); 53 } 54 testLabeledNotSetsInLexer()55 @Test public void testLabeledNotSetsInLexer() { 56 // d must be an int 57 String grammar = 58 "lexer grammar T;\n" + 59 "A : d=~('x'|'y') e='0'..'9'\n" + 60 " ; \n" ; 61 boolean found = 62 rawGenerateAndBuildRecognizer( 63 "T.g", grammar, null, "T", false); 64 boolean expecting = true; // should be ok 65 assertEquals(expecting, found); 66 } 67 testLabeledSetsInLexer()68 @Test public void testLabeledSetsInLexer() { 69 // d must be an int 70 String grammar = 71 "grammar T;\n" + 72 "a : A ;\n" + 73 "A : d=('x'|'y') {System.out.println((char)$d);}\n" + 74 " ; \n" ; 75 String found = execParser("T.g", grammar, "TParser", "TLexer", 76 "a", "x", false); 77 assertEquals("x\n", found); 78 } 79 testLabeledRangeInLexer()80 @Test public void testLabeledRangeInLexer() { 81 // d must be an int 82 String grammar = 83 "grammar T;\n" + 84 "a : A;\n" + 85 "A : d='a'..'z' {System.out.println((char)$d);} \n" + 86 " ; \n" ; 87 String found = execParser("T.g", grammar, "TParser", "TLexer", 88 "a", "x", false); 89 assertEquals("x\n", found); 90 } 91 testLabeledWildcardInLexer()92 @Test public void testLabeledWildcardInLexer() { 93 // d must be an int 94 String grammar = 95 "grammar T;\n" + 96 "a : A;\n" + 97 "A : d=. {System.out.println((char)$d);}\n" + 98 " ; \n" ; 99 String found = execParser("T.g", grammar, "TParser", "TLexer", 100 "a", "x", false); 101 assertEquals("x\n", found); 102 } 103 testSynpredWithPlusLoop()104 @Test public void testSynpredWithPlusLoop() { 105 String grammar = 106 "grammar T; \n" + 107 "a : (('x'+)=> 'x'+)?;\n"; 108 boolean found = 109 rawGenerateAndBuildRecognizer( 110 "T.g", grammar, "TParser", "TLexer", false); 111 boolean expecting = true; // should be ok 112 assertEquals(expecting, found); 113 } 114 testDoubleQuoteEscape()115 @Test public void testDoubleQuoteEscape() { 116 String grammar = 117 "lexer grammar T; \n" + 118 "A : '\\\\\"';\n" + // this is A : '\\"', which should give "\\\"" at Java level; 119 "B : '\\\"';\n" + // this is B: '\"', which shodl give "\"" at Java level; 120 "C : '\\'\\'';\n" + // this is C: '\'\'', which shoudl give "''" at Java level 121 "D : '\\k';\n"; // this is D: '\k', which shoudl give just "k" at Java level; 122 123 boolean found = 124 rawGenerateAndBuildRecognizer( 125 "T.g", grammar, null, "T", false); 126 boolean expecting = true; // should be ok 127 assertEquals(expecting, found); 128 } 129 testBlankRuleGetsNoException()130 @Test public void testBlankRuleGetsNoException() { 131 String grammar = 132 "grammar T;\n" + 133 "a : sync (ID sync)* ;\n" + 134 "sync : ;\n" + 135 "ID : 'a'..'z'+;\n"; 136 boolean found = 137 rawGenerateAndBuildRecognizer( 138 "T.g", grammar, "TParser", "TLexer", false); 139 boolean expecting = true; // should be ok 140 assertEquals(expecting, found); 141 } 142 143 /** 144 * This is a regression test for antlr/antlr3#20: StackOverflow error when 145 * compiling grammar with backtracking. 146 * https://github.com/antlr/antlr3/issues/20 147 */ 148 @Test testSemanticPredicateAnalysisStackOverflow()149 public void testSemanticPredicateAnalysisStackOverflow() throws Exception { 150 String grammar = 151 "grammar T;\n" 152 + "\n" 153 + "options {\n" 154 + " backtrack=true;\n" 155 + "}\n" 156 + "\n" 157 + "main : ('x'*)*;\n"; 158 boolean success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false); 159 assertTrue(success); 160 } 161 } 162