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 /** Test the set stuff in lexer and parser */ 35 public class TestSets extends BaseTest { 36 protected boolean debug = false; 37 38 /** Public default constructor used by TestRig */ TestSets()39 public TestSets() { 40 } 41 testSeqDoesNotBecomeSet()42 @Test public void testSeqDoesNotBecomeSet() throws Exception { 43 // this must return A not I to the parser; calling a nonfragment rule 44 // from a nonfragment rule does not set the overall token. 45 String grammar = 46 "grammar P;\n" + 47 "a : C {System.out.println(input);} ;\n" + 48 "fragment A : '1' | '2';\n" + 49 "fragment B : '3' '4';\n" + 50 "C : A | B;\n"; 51 String found = execParser("P.g", grammar, "PParser", "PLexer", 52 "a", "34", debug); 53 assertEquals("34\n", found); 54 } 55 testParserSet()56 @Test public void testParserSet() throws Exception { 57 String grammar = 58 "grammar T;\n" + 59 "a : t=('x'|'y') {System.out.println($t.text);} ;\n"; 60 String found = execParser("T.g", grammar, "TParser", "TLexer", 61 "a", "x", debug); 62 assertEquals("x\n", found); 63 } 64 testParserNotSet()65 @Test public void testParserNotSet() throws Exception { 66 String grammar = 67 "grammar T;\n" + 68 "a : t=~('x'|'y') 'z' {System.out.println($t.text);} ;\n"; 69 String found = execParser("T.g", grammar, "TParser", "TLexer", 70 "a", "zz", debug); 71 assertEquals("z\n", found); 72 } 73 testParserNotToken()74 @Test public void testParserNotToken() throws Exception { 75 String grammar = 76 "grammar T;\n" + 77 "a : ~'x' 'z' {System.out.println(input);} ;\n"; 78 String found = execParser("T.g", grammar, "TParser", "TLexer", 79 "a", "zz", debug); 80 assertEquals("zz\n", found); 81 } 82 testParserNotTokenWithLabel()83 @Test public void testParserNotTokenWithLabel() throws Exception { 84 String grammar = 85 "grammar T;\n" + 86 "a : t=~'x' 'z' {System.out.println($t.text);} ;\n"; 87 String found = execParser("T.g", grammar, "TParser", "TLexer", 88 "a", "zz", debug); 89 assertEquals("z\n", found); 90 } 91 testRuleAsSet()92 @Test public void testRuleAsSet() throws Exception { 93 String grammar = 94 "grammar T;\n" + 95 "a @after {System.out.println(input);} : 'a' | 'b' |'c' ;\n"; 96 String found = execParser("T.g", grammar, "TParser", "TLexer", 97 "a", "b", debug); 98 assertEquals("b\n", found); 99 } 100 testRuleAsSetAST()101 @Test public void testRuleAsSetAST() throws Exception { 102 String grammar = 103 "grammar T;\n" + 104 "options {output=AST;}\n" + 105 "a : 'a' | 'b' |'c' ;\n"; 106 String found = execParser("T.g", grammar, "TParser", "TLexer", 107 "a", "b", debug); 108 assertEquals("b\n", found); 109 } 110 testNotChar()111 @Test public void testNotChar() throws Exception { 112 String grammar = 113 "grammar T;\n" + 114 "a : A {System.out.println($A.text);} ;\n" + 115 "A : ~'b' ;\n"; 116 String found = execParser("T.g", grammar, "TParser", "TLexer", 117 "a", "x", debug); 118 assertEquals("x\n", found); 119 } 120 testOptionalSingleElement()121 @Test public void testOptionalSingleElement() throws Exception { 122 String grammar = 123 "grammar T;\n" + 124 "a : A? 'c' {System.out.println(input);} ;\n" + 125 "A : 'b' ;\n"; 126 String found = execParser("T.g", grammar, "TParser", "TLexer", 127 "a", "bc", debug); 128 assertEquals("bc\n", found); 129 } 130 testOptionalLexerSingleElement()131 @Test public void testOptionalLexerSingleElement() throws Exception { 132 String grammar = 133 "grammar T;\n" + 134 "a : A {System.out.println(input);} ;\n" + 135 "A : 'b'? 'c' ;\n"; 136 String found = execParser("T.g", grammar, "TParser", "TLexer", 137 "a", "bc", debug); 138 assertEquals("bc\n", found); 139 } 140 testStarLexerSingleElement()141 @Test public void testStarLexerSingleElement() throws Exception { 142 String grammar = 143 "grammar T;\n" + 144 "a : A {System.out.println(input);} ;\n" + 145 "A : 'b'* 'c' ;\n"; 146 String found = execParser("T.g", grammar, "TParser", "TLexer", 147 "a", "bbbbc", debug); 148 assertEquals("bbbbc\n", found); 149 found = execParser("T.g", grammar, "TParser", "TLexer", 150 "a", "c", debug); 151 assertEquals("c\n", found); 152 } 153 testPlusLexerSingleElement()154 @Test public void testPlusLexerSingleElement() throws Exception { 155 String grammar = 156 "grammar T;\n" + 157 "a : A {System.out.println(input);} ;\n" + 158 "A : 'b'+ 'c' ;\n"; 159 String found = execParser("T.g", grammar, "TParser", "TLexer", 160 "a", "bbbbc", debug); 161 assertEquals("bbbbc\n", found); 162 } 163 testOptionalSet()164 @Test public void testOptionalSet() throws Exception { 165 String grammar = 166 "grammar T;\n" + 167 "a : ('a'|'b')? 'c' {System.out.println(input);} ;\n"; 168 String found = execParser("T.g", grammar, "TParser", "TLexer", 169 "a", "ac", debug); 170 assertEquals("ac\n", found); 171 } 172 testStarSet()173 @Test public void testStarSet() throws Exception { 174 String grammar = 175 "grammar T;\n" + 176 "a : ('a'|'b')* 'c' {System.out.println(input);} ;\n"; 177 String found = execParser("T.g", grammar, "TParser", "TLexer", 178 "a", "abaac", debug); 179 assertEquals("abaac\n", found); 180 } 181 testPlusSet()182 @Test public void testPlusSet() throws Exception { 183 String grammar = 184 "grammar T;\n" + 185 "a : ('a'|'b')+ 'c' {System.out.println(input);} ;\n"; 186 String found = execParser("T.g", grammar, "TParser", "TLexer", 187 "a", "abaac", debug); 188 assertEquals("abaac\n", found); 189 } 190 testLexerOptionalSet()191 @Test public void testLexerOptionalSet() throws Exception { 192 String grammar = 193 "grammar T;\n" + 194 "a : A {System.out.println(input);} ;\n" + 195 "A : ('a'|'b')? 'c' ;\n"; 196 String found = execParser("T.g", grammar, "TParser", "TLexer", 197 "a", "ac", debug); 198 assertEquals("ac\n", found); 199 } 200 testLexerStarSet()201 @Test public void testLexerStarSet() throws Exception { 202 String grammar = 203 "grammar T;\n" + 204 "a : A {System.out.println(input);} ;\n" + 205 "A : ('a'|'b')* 'c' ;\n"; 206 String found = execParser("T.g", grammar, "TParser", "TLexer", 207 "a", "abaac", debug); 208 assertEquals("abaac\n", found); 209 } 210 testLexerPlusSet()211 @Test public void testLexerPlusSet() throws Exception { 212 String grammar = 213 "grammar T;\n" + 214 "a : A {System.out.println(input);} ;\n" + 215 "A : ('a'|'b')+ 'c' ;\n"; 216 String found = execParser("T.g", grammar, "TParser", "TLexer", 217 "a", "abaac", debug); 218 assertEquals("abaac\n", found); 219 } 220 testNotCharSet()221 @Test public void testNotCharSet() throws Exception { 222 String grammar = 223 "grammar T;\n" + 224 "a : A {System.out.println($A.text);} ;\n" + 225 "A : ~('b'|'c') ;\n"; 226 String found = execParser("T.g", grammar, "TParser", "TLexer", 227 "a", "x", debug); 228 assertEquals("x\n", found); 229 } 230 testNotCharSetWithLabel()231 @Test public void testNotCharSetWithLabel() throws Exception { 232 // This doesn't work in lexer yet. 233 // Generates: h=input.LA(1); but h is defined as a Token 234 String grammar = 235 "grammar T;\n" + 236 "a : A {System.out.println($A.text);} ;\n" + 237 "A : h=~('b'|'c') ;\n"; 238 String found = execParser("T.g", grammar, "TParser", "TLexer", 239 "a", "x", debug); 240 assertEquals("x\n", found); 241 } 242 testNotCharSetWithRuleRef()243 @Test public void testNotCharSetWithRuleRef() throws Exception { 244 String grammar = 245 "grammar T;\n" + 246 "a : A {System.out.println($A.text);} ;\n" + 247 "A : ~('a'|B) ;\n" + 248 "B : 'b' ;\n"; 249 String found = execParser("T.g", grammar, "TParser", "TLexer", 250 "a", "x", debug); 251 assertEquals("x\n", found); 252 } 253 testNotCharSetWithRuleRef2()254 @Test public void testNotCharSetWithRuleRef2() throws Exception { 255 String grammar = 256 "grammar T;\n" + 257 "a : A {System.out.println($A.text);} ;\n" + 258 "A : ~('a'|B) ;\n" + 259 "B : 'b'|'c' ;\n"; 260 String found = execParser("T.g", grammar, "TParser", "TLexer", 261 "a", "x", debug); 262 assertEquals("x\n", found); 263 } 264 testNotCharSetWithRuleRef3()265 @Test public void testNotCharSetWithRuleRef3() throws Exception { 266 String grammar = 267 "grammar T;\n" + 268 "a : A {System.out.println($A.text);} ;\n" + 269 "A : ('a'|B) ;\n" + 270 "fragment\n" + 271 "B : ~('a'|'c') ;\n"; 272 String found = execParser("T.g", grammar, "TParser", "TLexer", 273 "a", "x", debug); 274 assertEquals("x\n", found); 275 } 276 testNotCharSetWithRuleRef4()277 @Test public void testNotCharSetWithRuleRef4() throws Exception { 278 String grammar = 279 "grammar T;\n" + 280 "a : A {System.out.println($A.text);} ;\n" + 281 "A : ('a'|B) ;\n" + 282 "fragment\n" + 283 "B : ~('a'|C) ;\n" + 284 "fragment\n" + 285 "C : 'c'|'d' ;\n "; 286 String found = execParser("T.g", grammar, "TParser", "TLexer", 287 "a", "x", debug); 288 assertEquals("x\n", found); 289 } 290 291 } 292