gt: 			"ID : 'a'..'z'+ ;\n" +
context: /*
   * [The "BSD license"]
   *  Copyright (c) 2010 Terence Parr
   *  All rights reserved.
   *
   *  Redistribution and use in source and binary forms, with or without
   *  modification, are permitted provided that the following conditions
   *  are met:
   *  1. Redistributions of source code must retain the above copyright
   *      notice, this list of conditions and the following disclaimer.
   *  2. Redistributions in binary form must reproduce the above copyright
   *      notice, this list of conditions and the following disclaimer in the
   *      documentation and/or other materials provided with the distribution.
   *  3. The name of the author may not be used to endorse or promote products
   *      derived from this software without specific prior written permission.
   *
   *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */
  package org.antlr.test;
  
  import org.junit.Ignore;
  import org.junit.Test;
  
  public class TestAutoAST extends BaseTest {
  	protected boolean debug = false;
  
  	@Test public void testTokenList() throws Exception {
  		String grammar =
  			"grammar foo;\n" +
  			"options {output=AST;}\n" +
  			"a : ID INT ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",
  								  "a", "abc 34", debug);
  		assertEquals("abc 34\n", found);
  	}
  
  	@Test public void testTokenListInSingleAltBlock() throws Exception {
  		String grammar =
  			"grammar foo;\n" +
  			"options {output=AST;}\n" +
  			"a : (ID INT) ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",
  								  "a", "abc 34", debug);
  		assertEquals("abc 34\n", found);
  	}
  
  	@Test public void testSimpleRootAtOuterLevel() throws Exception {
  		String grammar =
  			"grammar foo;\n" +
  			"options {output=AST;}\n" +
  			"a : ID^ INT ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",
  								  "a", "abc 34", debug);
  		assertEquals("(abc 34)\n", found);
  	}
  
  	@Test public void testSimpleRootAtOuterLevelReverse() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : INT ID^ ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "34 abc", debug);
  		assertEquals("(abc 34)\n", found);
  	}
  
  	@Test public void testBang() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID INT! ID! INT ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "abc 34 dag 4532", debug);
  		assertEquals("abc 4532\n", found);
  	}
  
  	@Test public void testOptionalThenRoot() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ( ID INT )? ID^ ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "a 1 b", debug);
  		assertEquals("(b a 1)\n", found);
  	}
  
  	@Test public void testLabeledStringRoot() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : v='void'^ ID ';' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "void foo;", debug);
  		assertEquals("(void foo ;)\n", found);
  	}
  
  	@Test public void testWildcard() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : v='void'^ . ';' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "void foo;", debug);
  		assertEquals("(void foo ;)\n", found);
  	}
  
  	@Test public void testWildcardRoot() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : v='void' .^ ';' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "void foo;", debug);
  		assertEquals("(foo void ;)\n", found);
  	}
  
  	@Test public void testWildcardRootWithLabel() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : v='void' x=.^ ';' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "void foo;", debug);
  		assertEquals("(foo void ;)\n", found);
  	}
  
      @Test public void testWildcardRootWithListLabel() throws Exception {
          String grammar =
              "grammar T;\n" +
              "options {output=AST;}\n" +
              "a : v='void' x=.^ ';' ;\n" +
              "ID : 'a'..'z'+ ;\n" +
              "INT : '0'..'9'+;\n" +
              "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
          String found = execParser("T.g", grammar, "TParser", "TLexer",
                                    "a", "void foo;", debug);
          assertEquals("(foo void ;)\n", found);
      }
  
      @Test public void testWildcardBangWithListLabel() throws Exception {
          String grammar =
              "grammar T;\n" +
              "options {output=AST;}\n" +
              "a : v='void' x=.! ';' ;\n" +
              "ID : 'a'..'z'+ ;\n" +
              "INT : '0'..'9'+;\n" +
              "WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
          String found = execParser("T.g", grammar, "TParser", "TLexer",
                                    "a", "void foo;", debug);
          assertEquals("void ;\n", found);
      }
  
  	@Test public void testRootRoot() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID^ INT^ ID ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "a 34 c", debug);
  		assertEquals("(34 a c)\n", found);
  	}
  
  	@Test public void testRootRoot2() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID INT^ ID^ ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "a 34 c", debug);
  		assertEquals("(c (34 a))\n", found);
  	}
  
  	@Test public void testRootThenRootInLoop() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID^ (INT '*'^ ID)+ ;\n" +
  			"ID  : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "a 34 * b 9 * c", debug);
  		assertEquals("(* (* (a 34) b 9) c)\n", found);
  	}
  
  	@Test public void testNestedSubrule() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : 'void' (({;}ID|INT) ID | 'null' ) ';' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "void a b;", debug);
  		assertEquals("void a b ;\n", found);
  	}
  
  	@Test public void testInvokeRule() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a  : type ID ;\n" +
  			"type : {;}'int' | 'float' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "int a", debug);
  		assertEquals("int a\n", found);
  	}
  
  	@Test public void testInvokeRuleAsRoot() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a  : type^ ID ;\n" +
  			"type : {;}'int' | 'float' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "int a", debug);
  		assertEquals("(int a)\n", found);
  	}
  
  	@Test public void testInvokeRuleAsRootWithLabel() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a  : x=type^ ID ;\n" +
  			"type : {;}'int' | 'float' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "int a", debug);
  		assertEquals("(int a)\n", found);
  	}
  
  	@Test public void testInvokeRuleAsRootWithListLabel() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a  : x+=type^ ID ;\n" +
  			"type : {;}'int' | 'float' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "int a", debug);
  		assertEquals("(int a)\n", found);
  	}
  
  	@Test public void testRuleRootInLoop() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID ('+'^ ID)* ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "a+b+c+d", debug);
  		assertEquals("(+ (+ (+ a b) c) d)\n", found);
  	}
  
  	@Test public void testRuleInvocationRuleRootInLoop() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID (op^ ID)* ;\n" +
  			"op : {;}'+' | '-' ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "a+b+c-d", debug);
  		assertEquals("(- (+ (+ a b) c) d)\n", found);
  	}
  
  	@Test public void testTailRecursion() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"s : a ;\n" +
  			"a : atom ('exp'^ a)? ;\n" +
  			"atom : INT ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "s", "3 exp 4 exp 5", debug);
  		assertEquals("(exp 3 (exp 4 5))\n", found);
  	}
  
  	@Test public void testSet() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID|INT ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "abc", debug);
  		assertEquals("abc\n", found);
  	}
  
  	@Test public void testSetRoot() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ('+' | '-')^ ID ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "+abc", debug);
  		assertEquals("(+ abc)\n", found);
  	}
  
  	@Ignore
      // TODO: FAILS until I rebuild the antlr.g in v3
      //
      public void testSetRootWithLabel() throws Exception {
  		
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : x=('+' | '-')^ ID ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "+abc", debug);
  		assertEquals("(+ abc)\n", found);
  	}
  
  	@Test public void testSetAsRuleRootInLoop() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ID (('+'|'-')^ ID)* ;\n" +
  			"ID : 'a'..'z'+ ;\n" +
  			"INT : '0'..'9'+;\n" +
  			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
  		String found = execParser("T.g", grammar, "TParser", "TLexer",
  								  "a", "a+b-c", debug);
  		assertEquals("(- (+ a b) c)\n", found);
  	}
  
  	@Test public void testNotSet() throws Exception {
  		String grammar =
  			"grammar T;\n" +
  			"options {output=AST;}\n" +
  			"a : ~ID '+' INT ;\n" +
  