Home | History | Annotate | Download | only in test
      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.antlr.tool.Grammar;
     31 import org.junit.Test;
     32 
     33 public class TestASTConstruction extends BaseTest {
     34 
     35     /** Public default constructor used by TestRig */
     36     public TestASTConstruction() {
     37     }
     38 
     39 	@Test public void testA() throws Exception {
     40 		Grammar g = new Grammar(
     41 				"parser grammar P;\n"+
     42 				"a : A;");
     43 		String expecting =
     44 			"(rule a ARG RET scope (BLOCK (ALT A <end-of-alt>) <end-of-block>) <end-of-rule>)";
     45 		String found = g.getRule("a").tree.toStringTree();
     46 		assertEquals(expecting, found);
     47 	}
     48 
     49 	@Test public void testEmptyAlt() throws Exception {
     50 		Grammar g = new Grammar(
     51 				"parser grammar P;\n"+
     52 				"a : ;");
     53 		String expecting =
     54 			"(rule a ARG RET scope (BLOCK (ALT epsilon <end-of-alt>) <end-of-block>) <end-of-rule>)";
     55 		String found = g.getRule("a").tree.toStringTree();
     56 		assertEquals(expecting, found);
     57 	}
     58 
     59 	@Test public void testNakeRulePlusInLexer() throws Exception {
     60 		Grammar g = new Grammar(
     61 				"lexer grammar P;\n"+
     62 				"A : B+;\n" +
     63 				"B : 'a';");
     64 		String expecting =
     65 			"(rule A ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT B <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     66 		String found = g.getRule("A").tree.toStringTree();
     67 		assertEquals(expecting, found);
     68 	}
     69 
     70 	@Test public void testRulePlus() throws Exception {
     71 		Grammar g = new Grammar(
     72 				"parser grammar P;\n"+
     73 				"a : (b)+;\n" +
     74 				"b : B;");
     75 		String expecting =
     76 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     77 		String found = g.getRule("a").tree.toStringTree();
     78 		assertEquals(expecting, found);
     79 	}
     80 
     81 	@Test public void testNakedRulePlus() throws Exception {
     82 		Grammar g = new Grammar(
     83 				"parser grammar P;\n"+
     84 				"a : b+;\n" +
     85 				"b : B;");
     86 		String expecting =
     87 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     88 		String found = g.getRule("a").tree.toStringTree();
     89 		assertEquals(expecting, found);
     90 	}
     91 
     92 	@Test public void testRuleOptional() throws Exception {
     93 		Grammar g = new Grammar(
     94 				"parser grammar P;\n"+
     95 				"a : (b)?;\n" +
     96 				"b : B;");
     97 		String expecting =
     98 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     99 		String found = g.getRule("a").tree.toStringTree();
    100 		assertEquals(expecting, found);
    101 	}
    102 
    103 	@Test public void testNakedRuleOptional() throws Exception {
    104 		Grammar g = new Grammar(
    105 				"parser grammar P;\n"+
    106 				"a : b?;\n" +
    107 				"b : B;");
    108 		String expecting =
    109 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    110 		String found = g.getRule("a").tree.toStringTree();
    111 		assertEquals(expecting, found);
    112 	}
    113 
    114 	@Test public void testRuleStar() throws Exception {
    115 		Grammar g = new Grammar(
    116 				"parser grammar P;\n"+
    117 				"a : (b)*;\n" +
    118 				"b : B;");
    119 		String expecting =
    120 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    121 		String found = g.getRule("a").tree.toStringTree();
    122 		assertEquals(expecting, found);
    123 	}
    124 
    125 	@Test public void testNakedRuleStar() throws Exception {
    126 		Grammar g = new Grammar(
    127 				"parser grammar P;\n"+
    128 				"a : b*;\n" +
    129 				"b : B;");
    130 		String expecting =
    131 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    132 		String found = g.getRule("a").tree.toStringTree();
    133 		assertEquals(expecting, found);
    134 	}
    135 
    136 	@Test public void testCharStar() throws Exception {
    137 		Grammar g = new Grammar(
    138 				"grammar P;\n"+
    139 				"a : 'a'*;");
    140 		String expecting =
    141 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'a' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    142 		String found = g.getRule("a").tree.toStringTree();
    143 		assertEquals(expecting, found);
    144 	}
    145 
    146 	@Test public void testCharStarInLexer() throws Exception {
    147 		Grammar g = new Grammar(
    148 				"lexer grammar P;\n"+
    149 				"B : 'b'*;");
    150 		String expecting =
    151 			"(rule B ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'b' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    152 		String found = g.getRule("B").tree.toStringTree();
    153 		assertEquals(expecting, found);
    154 	}
    155 
    156 	@Test public void testStringStar() throws Exception {
    157 		Grammar g = new Grammar(
    158 				"grammar P;\n"+
    159 				"a : 'while'*;");
    160 		String expecting =
    161 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'while' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    162 		String found = g.getRule("a").tree.toStringTree();
    163 		assertEquals(expecting, found);
    164 	}
    165 
    166 	@Test public void testStringStarInLexer() throws Exception {
    167 		Grammar g = new Grammar(
    168 				"lexer grammar P;\n"+
    169 				"B : 'while'*;");
    170 		String expecting =
    171 			"(rule B ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'while' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    172 		String found = g.getRule("B").tree.toStringTree();
    173 		assertEquals(expecting, found);
    174 	}
    175 
    176 	@Test public void testCharPlus() throws Exception {
    177 		Grammar g = new Grammar(
    178 				"grammar P;\n"+
    179 				"a : 'a'+;");
    180 		String expecting =
    181 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT 'a' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    182 		String found = g.getRule("a").tree.toStringTree();
    183 		assertEquals(expecting, found);
    184 	}
    185 
    186 	@Test public void testCharPlusInLexer() throws Exception {
    187 		Grammar g = new Grammar(
    188 				"lexer grammar P;\n"+
    189 				"B : 'b'+;");
    190 		String expecting =
    191 			"(rule B ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT 'b' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    192 		String found = g.getRule("B").tree.toStringTree();
    193 		assertEquals(expecting, found);
    194 	}
    195 
    196 	@Test public void testCharOptional() throws Exception {
    197 		Grammar g = new Grammar(
    198 				"grammar P;\n"+
    199 				"a : 'a'?;");
    200 		String expecting =
    201 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT 'a' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    202 		String found = g.getRule("a").tree.toStringTree();
    203 		assertEquals(expecting, found);
    204 	}
    205 
    206 	@Test public void testCharOptionalInLexer() throws Exception {
    207 		Grammar g = new Grammar(
    208 				"lexer grammar P;\n"+
    209 				"B : 'b'?;");
    210 		String expecting =
    211 			"(rule B ARG RET scope (BLOCK (ALT (? (BLOCK (ALT 'b' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    212 		String found = g.getRule("B").tree.toStringTree();
    213 		assertEquals(expecting, found);
    214 	}
    215 
    216 	@Test public void testCharRangePlus() throws Exception {
    217 		Grammar g = new Grammar(
    218 				"lexer grammar P;\n"+
    219 				"ID : 'a'..'z'+;");
    220 		String expecting =
    221 			"(rule ID ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (.. 'a' 'z') <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    222 		String found = g.getRule("ID").tree.toStringTree();
    223 		assertEquals(expecting, found);
    224 	}
    225 
    226 	@Test public void testLabel() throws Exception {
    227 		Grammar g = new Grammar(
    228 				"grammar P;\n"+
    229 				"a : x=ID;");
    230 		String expecting =
    231 			"(rule a ARG RET scope (BLOCK (ALT (= x ID) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    232 		String found = g.getRule("a").tree.toStringTree();
    233 		assertEquals(expecting, found);
    234 	}
    235 
    236 	@Test public void testLabelOfOptional() throws Exception {
    237 		Grammar g = new Grammar(
    238 				"grammar P;\n"+
    239 				"a : x=ID?;");
    240 		String expecting =
    241 			"(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT (= x ID) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    242 		String found = g.getRule("a").tree.toStringTree();
    243 		assertEquals(expecting, found);
    244 	}
    245 
    246 	@Test public void testLabelOfClosure() throws Exception {
    247 		Grammar g = new Grammar(
    248 				"grammar P;\n"+
    249 				"a : x=ID*;");
    250 		String expecting =
    251 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT (= x ID) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    252 		String found = g.getRule("a").tree.toStringTree();
    253 		assertEquals(expecting, found);
    254 	}
    255 
    256 	@Test public void testRuleLabel() throws Exception {
    257 		Grammar g = new Grammar(
    258 				"grammar P;\n"+
    259 				"a : x=b;\n" +
    260 				"b : ID;\n");
    261 		String expecting =
    262 			"(rule a ARG RET scope (BLOCK (ALT (= x b) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    263 		String found = g.getRule("a").tree.toStringTree();
    264 		assertEquals(expecting, found);
    265 	}
    266 
    267 	@Test public void testSetLabel() throws Exception {
    268 		Grammar g = new Grammar(
    269 				"grammar P;\n"+
    270 				"a : x=(A|B);\n");
    271 		String expecting =
    272 			"(rule a ARG RET scope (BLOCK (ALT (= x (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    273 		String found = g.getRule("a").tree.toStringTree();
    274 		assertEquals(expecting, found);
    275 	}
    276 
    277 	@Test public void testNotSetLabel() throws Exception {
    278 		Grammar g = new Grammar(
    279 				"grammar P;\n"+
    280 				"a : x=~(A|B);\n");
    281 		String expecting =
    282 			"(rule a ARG RET scope (BLOCK (ALT (= x (~ (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>))) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    283 		String found = g.getRule("a").tree.toStringTree();
    284 		assertEquals(expecting, found);
    285 	}
    286 
    287 	@Test public void testNotSetListLabel() throws Exception {
    288 		Grammar g = new Grammar(
    289 				"grammar P;\n"+
    290 				"a : x+=~(A|B);\n");
    291 		String expecting =
    292 			"(rule a ARG RET scope (BLOCK (ALT (+= x (~ (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>))) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    293 		String found = g.getRule("a").tree.toStringTree();
    294 		assertEquals(expecting, found);
    295 	}
    296 
    297 	@Test public void testNotSetListLabelInLoop() throws Exception {
    298 		Grammar g = new Grammar(
    299 				"grammar P;\n"+
    300 				"a : x+=~(A|B)+;\n");
    301 		String expecting =
    302 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (+= x (~ (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>))) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    303 		String found = g.getRule("a").tree.toStringTree();
    304 		assertEquals(expecting, found);
    305 	}
    306 
    307 	@Test public void testRuleLabelOfPositiveClosure() throws Exception {
    308 		Grammar g = new Grammar(
    309 				"grammar P;\n"+
    310 				"a : x=b+;\n" +
    311 				"b : ID;\n");
    312 		String expecting =
    313 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (= x b) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    314 		String found = g.getRule("a").tree.toStringTree();
    315 		assertEquals(expecting, found);
    316 	}
    317 
    318 	@Test public void testListLabelOfClosure() throws Exception {
    319 		Grammar g = new Grammar(
    320 				"grammar P;\n"+
    321 				"a : x+=ID*;");
    322 		String expecting =
    323 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT (+= x ID) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    324 		String found = g.getRule("a").tree.toStringTree();
    325 		assertEquals(expecting, found);
    326 	}
    327 
    328 	@Test public void testListLabelOfClosure2() throws Exception {
    329 		Grammar g = new Grammar(
    330 				"grammar P;\n"+
    331 				"a : x+='int'*;");
    332 		String expecting =
    333 			"(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT (+= x 'int') <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    334 		String found = g.getRule("a").tree.toStringTree();
    335 		assertEquals(expecting, found);
    336 	}
    337 
    338 	@Test public void testRuleListLabelOfPositiveClosure() throws Exception {
    339 		Grammar g = new Grammar(
    340 				"grammar P;\n" +
    341 				"options {output=AST;}\n"+
    342 				"a : x+=b+;\n" +
    343 				"b : ID;\n");
    344 		String expecting =
    345 			"(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (+= x b) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
    346 		String found = g.getRule("a").tree.toStringTree();
    347 		assertEquals(expecting, found);
    348 	}
    349 
    350 	@Test public void testRootTokenInStarLoop() throws Exception {
    351 		Grammar g = new Grammar(
    352 				"grammar Expr;\n" +
    353 				"options { output=AST; backtrack=true; }\n" +
    354 				"a : ('*'^)* ;\n");  // bug: the synpred had nothing in it
    355 		String expecting =
    356 			"(rule synpred1_Expr ARG RET scope (BLOCK (ALT '*' <end-of-alt>) <end-of-block>) <end-of-rule>)";
    357 		String found = g.getRule("synpred1_Expr").tree.toStringTree();
    358 		assertEquals(expecting, found);
    359 	}
    360 
    361 	@Test public void testActionInStarLoop() throws Exception {
    362 		Grammar g = new Grammar(
    363 				"grammar Expr;\n" +
    364 				"options { backtrack=true; }\n" +
    365 				"a : ({blort} 'x')* ;\n");  // bug: the synpred had nothing in it
    366 		String expecting =
    367 			"(rule synpred1_Expr ARG RET scope (BLOCK (ALT blort 'x' <end-of-alt>) <end-of-block>) <end-of-rule>)";
    368 		String found = g.getRule("synpred1_Expr").tree.toStringTree();
    369 		assertEquals(expecting, found);
    370 	}
    371 
    372 }
    373