package org.antlr.v4.test;

import org.junit.Test;

public class TestParseTrees extends BaseTest {
	@Test public void testToken() throws Exception {
		String grammar =
			"grammar T;\n" +
			"s\n" +
			"@init {setBuildParseTrees(true);}\n" +
			"@after {System.out.println($r.toStringTree(this));}\n" +
			"  :r=a ;\n" +
			"a : 'x' ;\n";
		String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "x", false);
		String expecting = "(a x)\n";
		assertEquals(expecting, result);
	}

	@Test public void testToken2() throws Exception {
		String grammar =
			"grammar T;\n" +
			"s\n" +
			"@init {setBuildParseTrees(true);}\n" +
			"@after {System.out.println($r.toStringTree(this));}\n" +
			"  :r=a ;\n" +
			"a : 'x' 'y'\n" +
			"  ;\n";
		String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xy", false);
		String expecting = "(a x y)\n";
		assertEquals(expecting, result);
	}

	@Test public void test2Alts() throws Exception {
		String grammar =
			"grammar T;\n" +
			"s\n" +
			"@init {setBuildParseTrees(true);}\n" +
			"@after {System.out.println($r.toStringTree(this));}\n" +
			"  :r=a ;\n" +
			"a : 'x' | 'y'\n" +
			"  ;\n";
		String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "y", false);
		String expecting = "(a y)\n";
		assertEquals(expecting, result);
	}

	@Test public void test2AltLoop() throws Exception {
		String grammar =
			"grammar T;\n" +
			"s\n" +
			"@init {setBuildParseTrees(true);}\n" +
			"@after {System.out.println($r.toStringTree(this));}\n" +
			"  :r=a ;\n" +
			"a : ('x' | 'y')* 'z'\n" +
			"  ;\n";
		String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xyyxyxz", false);
		String expecting = "(a x y y x y x z)\n";
		assertEquals(expecting, result);
	}

	@Test public void testRuleRef() throws Exception {
		String grammar =
			"grammar T;\n" +
			"s\n" +
			"@init {setBuildParseTrees(true);}\n" +
			"@after {System.out.println($r.toStringTree(this));}\n" +
			"  : r=a ;\n" +
			"a : b 'x'\n" +
			"  ;\n" +
			"b : 'y' ;\n";
		String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "yx", false);
		String expecting = "(a (b y) x)\n";
		assertEquals(expecting, result);
	}

	// ERRORS

	@Test public void testExtraToken() throws Exception {
		String grammar =
			"grammar T;\n" +
			"s\n" +
			"@init {setBuildParseTrees(true);}\n" +
			"@after {System.out.println($r.toStringTree(this));}\n" +
			"  : r=a ;\n" +
			"a : 'x' 'y'\n" +
			"  ;\n" +
			"Z : 'z'; \n";
		String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xzy", false);
		String expecting = "(a x <ERROR:z> y)\n";
		assertEquals(expecting, result);
	}

	@Test public void testNoViableAlt() throws Exception {
		String grammar =
			"grammar T;\n" +
			"s\n" +
			"@init {setBuildParseTrees(true);}\n" +
			"@after {System.out.println($r.toStringTree(this));}\n" +
			"  : r=a ;\n" +
			"a : 'x' | 'y'\n" +
			"  ;\n" +
			"Z : 'z'; \n";
		String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "z", false);
		String expecting = "(a <ERROR:z>)\n";
		assertEquals(expecting, result);
	}
}

@@ -3095,11 +3095,465 @@
 ult);%0A%09%7D
+%0A%0A%09@Test public void testSync() throws Exception %7B%0A%09%09String grammar =%0A%09%09%09%22grammar T;%5Cn%22 +%0A%09%09%09%22s%5Cn%22 +%0A%09%09%09%22@init %7BsetBuildParseTrees(true);%7D%5Cn%22 +%0A%09%09%09%22@after %7BSystem.out.println($r.toStringTree(this));%7D%5Cn%22 +%0A%09%09%09%22  : r=a ;%5Cn%22 +%0A%09%09%09%22a : 'x' 'y'* '!'%5Cn%22 +%0A%09%09%09%22  ;%5Cn%22 +%0A%09%09%09%22Z : 'z'; %5Cn%22;%0A%09%09String result = execParser(%22T.g%22, grammar, %22TParser%22, %22TLexer%22, %22s%22, %22xzyy!%22, false);%0A%09%09String expecting = %22(a x %3CERROR:z%3E y y !)%5Cn%22;%0A%09%09assertEquals(expecting, result);%0A%09%7D
 %0A%7D%0A

tweak tests