⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testsyntacticpredicateevaluation.java

📁 ANTLR(ANother Tool for Language Recognition)它是这样的一种工具
💻 JAVA
字号:
/*[The "BSD licence"]Copyright (c) 2005-2006 Terence ParrAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.3. The name of the author may not be used to endorse or promote productsderived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIESOF 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, BUTNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OFTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.antlr.test;public class TestSyntacticPredicateEvaluation extends BaseTest {	public void testTwoPredsWithNakedAlt() throws Exception {		String grammar =			"grammar t;\n" +			"s : (a ';')+ ;\n" +			"a\n" +			"options {\n" +			"  k=1;\n" +			"}\n" +			"  : (b '.')=> b '.' {System.out.println(\"alt 1\");}\n" +			"  | (b)=> b {System.out.println(\"alt 2\");}\n" +			"  | c       {System.out.println(\"alt 3\");}\n" +			"  ;\n" +			"b\n" +			"@init {System.out.println(\"enter b\");}\n" +			"   : '(' 'x' ')' ;\n" +			"c\n" +			"@init {System.out.println(\"enter c\");}\n" +			"   : '(' c ')' | 'x' ;\n" +			"WS : (' '|'\\n')+ {channel=99;}\n" +			"   ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "a", "(x) ;", false);		String expecting =			"enter b\n" +			"enter b\n" +			"enter b\n" +			"alt 2\n";		assertEquals(expecting, found);		found = execParser("t.g", grammar, "tParser", "tLexer",			    "a", "(x). ;", false);		expecting =			"enter b\n" +			"enter b\n" +			"alt 1\n";		assertEquals(expecting, found);		found = execParser("t.g", grammar, "tParser", "tLexer",			    "a", "((x)) ;", false);		expecting =			"enter b\n" +			"enter b\n" +			"enter c\n" +			"enter c\n" +			"enter c\n" +			"alt 3\n";		assertEquals(expecting, found);	}	public void testTwoPredsWithNakedAltNotLast() throws Exception {		String grammar =			"grammar t;\n" +			"s : (a ';')+ ;\n" +			"a\n" +			"options {\n" +			"  k=1;\n" +			"}\n" +			"  : (b '.')=> b '.' {System.out.println(\"alt 1\");}\n" +			"  | c       {System.out.println(\"alt 2\");}\n" +			"  | (b)=> b {System.out.println(\"alt 3\");}\n" +			"  ;\n" +			"b\n" +			"@init {System.out.println(\"enter b\");}\n" +			"   : '(' 'x' ')' ;\n" +			"c\n" +			"@init {System.out.println(\"enter c\");}\n" +			"   : '(' c ')' | 'x' ;\n" +			"WS : (' '|'\\n')+ {channel=99;}\n" +			"   ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "a", "(x) ;", false);		String expecting =			"enter b\n" +			"enter c\n" +			"enter c\n" +			"alt 2\n";		assertEquals(expecting, found);		found = execParser("t.g", grammar, "tParser", "tLexer",			    "a", "(x). ;", false);		expecting =			"enter b\n" +			"enter b\n" +			"alt 1\n";		assertEquals(expecting, found);		found = execParser("t.g", grammar, "tParser", "tLexer",			    "a", "((x)) ;", false);		expecting =			"enter b\n" +			"enter c\n" +			"enter c\n" +			"enter c\n" +			"alt 2\n";		assertEquals(expecting, found);	}	public void testLexerPred() throws Exception {		String grammar =			"grammar t;\n" +			"s : A ;\n" +			"A options {k=1;}\n" + // force backtracking			"  : (B '.')=>B '.' {System.out.println(\"alt1\");}\n" +			"  | B {System.out.println(\"alt2\");}" +			"  ;\n" +			"fragment\n" +			"B : 'x'+ ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "s", "xxx", false);		assertEquals("alt2\n", found);		found = execParser("t.g", grammar, "tParser", "tLexer",			    "s", "xxx.", false);		assertEquals("alt1\n", found);	}	public void testLexerPredCyclicPrediction() throws Exception {		String grammar =			"grammar t;\n" +			"s : A ;\n" +			"A : (B)=>(B|'y'+) {System.out.println(\"alt1\");}\n" +			"  | B {System.out.println(\"alt2\");}\n" +			"  | 'y'+ ';'" +			"  ;\n" +			"fragment\n" +			"B : 'x'+ ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "s", "xxx", false);		assertEquals("alt1\n", found);	}	public void testLexerPredCyclicPrediction2() throws Exception {		String grammar =			"grammar t;\n" +			"s : A ;\n" +			"A : (B '.')=>(B|'y'+) {System.out.println(\"alt1\");}\n" +			"  | B {System.out.println(\"alt2\");}\n" +			"  | 'y'+ ';'" +			"  ;\n" +			"fragment\n" +			"B : 'x'+ ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "s", "xxx", false);		assertEquals("alt2\n", found);	}	public void testSimpleNestedPred() throws Exception {		String grammar =			"grammar t;\n" +			"s : (expr ';')+ ;\n" +			"expr\n" +			"options {\n" +			"  k=1;\n" +			"}\n" +			"@init {System.out.println(\"enter expr \"+input.LT(1).getText());}\n" +			"  : (atom 'x') => atom 'x'\n" +			"  | atom\n" +			";\n" +			"atom\n" +			"@init {System.out.println(\"enter atom \"+input.LT(1).getText());}\n" +			"   : '(' expr ')'\n" +			"   | INT\n" +			"   ;\n" +			"INT: '0'..'9'+ ;\n" +			"WS : (' '|'\\n')+ {channel=99;}\n" +			"   ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "s", "(34)x;", false);		String expecting =			"enter expr (\n" +			"enter atom (\n" +			"enter expr 34\n" +			"enter atom 34\n" +			"enter atom 34\n" +			"enter atom (\n" +			"enter expr 34\n" +			"enter atom 34\n" +			"enter atom 34\n";		assertEquals(expecting, found);	}	public void testTripleNestedPredInLexer() throws Exception {		String grammar =			"grammar t;\n" +			"s : (.)+ {System.out.println(\"done\");} ;\n" +			"EXPR\n" +			"options {\n" +			"  k=1;\n" +			"}\n" +			"@init {System.out.println(\"enter expr \"+(char)input.LT(1));}\n" +			"  : (ATOM 'x') => ATOM 'x' {System.out.println(\"ATOM x\");}\n" +			"  | ATOM {System.out.println(\"ATOM \"+$ATOM.text);}\n" +			";\n" +			"fragment ATOM\n" +			"@init {System.out.println(\"enter atom \"+(char)input.LT(1));}\n" +			"   : '(' EXPR ')'\n" +			"   | INT\n" +			"   ;\n" +			"fragment INT: '0'..'9'+ ;\n" +			"fragment WS : (' '|'\\n')+ \n" +			"   ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "s", "((34)x)x", false);		String expecting = // has no memoization			"enter expr (\n" +			"enter atom (\n" +			"enter expr (\n" +			"enter atom (\n" +			"enter expr 3\n" +			"enter atom 3\n" +			"enter atom 3\n" +			"enter atom (\n" +			"enter expr 3\n" +			"enter atom 3\n" +			"enter atom 3\n" +			"enter atom (\n" +			"enter expr (\n" +			"enter atom (\n" +			"enter expr 3\n" +			"enter atom 3\n" +			"enter atom 3\n" +			"enter atom (\n" +			"enter expr 3\n" +			"enter atom 3\n" +			"enter atom 3\n" +			"ATOM 34\n" +			"ATOM x\n" +			"ATOM x\n" +			"done\n";		assertEquals(expecting, found);	}	public void testSynPredWithOutputTemplate() throws Exception {		// really just seeing if it will compile		String grammar =			"grammar t;\n" +			"options {output=template;}\n" +			"a\n" +			"options {\n" +			"  k=1;\n" +			"}\n" +			"  : ('x'+ 'y')=> 'x'+ 'y' -> template(a={$text}) <<1:<a>;>>\n" +			"  | 'x'+ 'z' -> template(a={$text}) <<2:<a>;>>\n"+			"  ;\n" +			"WS : (' '|'\\n')+ {channel=99;}\n" +			"   ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "a", "xxxy", false);		assertEquals("1:xxxy;\n", found);	}	public void testSynPredWithOutputAST() throws Exception {		// really just seeing if it will compile		String grammar =			"grammar t;\n" +			"options {output=AST;}\n" +			"a\n" +			"options {\n" +			"  k=1;\n" +			"}\n" +			"  : ('x'+ 'y')=> 'x'+ 'y'\n" +			"  | 'x'+ 'z'\n"+			"  ;\n" +			"WS : (' '|'\\n')+ {channel=99;}\n" +			"   ;\n" ;		String found = execParser("t.g", grammar, "tParser", "tLexer",				    "a", "xxxy", false);		assertEquals("x x x y\n", found);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -