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

📄 testattributes.java

📁 antlr最新版本V3源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* [The "BSD licence"] Copyright (c) 2005-2006 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.antlr.Tool;import org.antlr.codegen.CodeGenerator;import org.antlr.codegen.ActionTranslatorLexer;import org.antlr.stringtemplate.StringTemplate;import org.antlr.stringtemplate.StringTemplateGroup;import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;import org.antlr.tool.*;import java.io.StringReader;import java.util.List;import java.util.ArrayList;/** Check the $x, $x.y attributes.  For checking the actual *  translation, assume the Java target.  This is still a great test *  for the semantics of the $x.y stuff regardless of the target. */public class TestAttributes extends BaseTest {	/** Public default constructor used by TestRig */	public TestAttributes() {	}	public void testEscapedLessThanInAction() throws Exception {		Grammar g = new Grammar();		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		String action = "i<3; '<xmltag>'";		ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"a",																	 new antlr.CommonToken(ANTLRParser.ACTION,action),0);		String expecting = action;		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, "<action>");		actionST.setAttribute("action", rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);	}	public void testEscaped$InAction() throws Exception {		String action = "int \\$n; \"\\$in string\\$\"";		String expecting = "int $n; \"$in string$\"";		Grammar g = new Grammar(			"parser grammar t;\n"+				"@members {"+action+"}\n"+				"a[User u, int i]\n" +				"        : {"+action+"}\n" +				"        ;");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		ActionTranslatorLexer translator =			new ActionTranslatorLexer(generator,									  "a",									  new antlr.CommonToken(ANTLRParser.ACTION,action),0);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);	}	public void testArguments() throws Exception {		String action = "$i; $i.x; $u; $u.x";		String expecting = "i; i.x; u; u.x";		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"parser grammar t;\n"+				"a[User u, int i]\n" +				"        : {"+action+"}\n" +				"        ;");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"a",																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());	}	/** $x.start refs are checked during translation not before so ANTLR misses	 the fact that rule r has refs to predefined attributes if the ref is after	 the def of the method or self-referential.  Actually would be ok if I didn't	 convert actions to strings; keep as templates.	 June 9, 2006: made action translation leave templates not strings	 */	public void testRefToReturnValueBeforeRefToPredefinedAttr() throws Exception {		String action = "$x.foo";		String expecting = "x.foo";		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"parser grammar t;\n"+			"a : x=b {"+action+"} ;\n" +			"b returns [int foo] : B {$b.start} ;\n");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"a",																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());	}	public void testRuleLabelBeforeRefToPredefinedAttr() throws Exception {		// As of Mar 2007, I'm removing unused labels.  Unfortunately,		// the action is not seen until code gen.  Can't see $x.text		// before stripping unused labels.  We really need to translate		// actions first so code gen logic can use info.		String action = "$x.text";		String expecting = "input.toString(x.start,x.stop)";		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"parser grammar t;\n"+			"a : x=b {"+action+"} ;\n" +			"b : B ;\n");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"a",																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());	}	public void testInvalidArguments() throws Exception {		String action = "$x";		String expecting = action;		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"parser grammar t;\n"+				"a[User u, int i]\n" +				"        : {"+action+"}\n" +				"        ;");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,																	 "a",																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);		int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE;		Object expectedArg = "x";		GrammarSemanticsMessage expectedMessage =			new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg);		checkError(equeue, expectedMessage);	}	public void testReturnValue() throws Exception {		String action = "$x.i";		String expecting = "x";		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"grammar t;\n"+				"a returns [int i]\n" +				"        : 'a'\n" +				"        ;\n" +				"b : x=a {"+action+"} ;\n");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		ActionTranslatorLexer translator =			new ActionTranslatorLexer(generator,									  "b",									  new antlr.CommonToken(ANTLRParser.ACTION,action),1);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());	}	public void testReturnValueWithNumber() throws Exception {		String action = "$x.i1";		String expecting = "x";		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"grammar t;\n"+				"a returns [int i1]\n" +				"        : 'a'\n" +				"        ;\n" +				"b : x=a {"+action+"} ;\n");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		ActionTranslatorLexer translator =			new ActionTranslatorLexer(generator,									  "b",									  new antlr.CommonToken(ANTLRParser.ACTION,action),1);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());	}	public void testReturnValues() throws Exception {		String action = "$i; $i.x; $u; $u.x";		String expecting = "retval.i; retval.i.x; retval.u; retval.u.x";		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"parser grammar t;\n"+				"a returns [User u, int i]\n" +				"        : {"+action+"}\n" +				"        ;");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"a",																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);		String rawTranslation =			translator.translate();		StringTemplateGroup templates =			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);		StringTemplate actionST = new StringTemplate(templates, rawTranslation);		String found = actionST.toString();		assertEquals(expecting, found);		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());	}	/* regression test for ANTLR-46 */	public void testReturnWithMultipleRuleRefs() throws Exception {		String action1 = "$obj = $rule2.obj;";		String action2 = "$obj = $rule3.obj;";		String expecting1 = "obj = rule21;";		String expecting2 = "obj = rule32;";		ErrorQueue equeue = new ErrorQueue();		ErrorManager.setErrorListener(equeue);		Grammar g = new Grammar(			"grammar t;\n" +			"rule1 returns [ Object obj ]\n" +			":	rule2 { "+action1+" }\n" +			"|	rule3 { "+action2+" }\n" +			";\n"+			"rule2 returns [ Object obj ]\n"+			":	foo='foo' { $obj = $foo.text; }\n"+			";\n"+			"rule3 returns [ Object obj ]\n"+			":	bar='bar' { $obj = $bar.text; }\n"+			";");		Tool antlr = newTool();		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");		g.setCodeGenerator(generator);		generator.genRecognizer(); // forces load of templates		int i = 0;		String action = action1;		String expecting = expecting1;		do {			ActionTranslatorLexer translator = new ActionTranslatorLexer(generator,"rule1",																		 new antlr.CommonToken(ANTLRParser.ACTION,action),i+1);			String rawTranslation =					translator.translate();			StringTemplateGroup templates =					new StringTemplateGroup(".", AngleBracketTemplateLexer.class);			StringTemplate actionST = new StringTemplate(templates, rawTranslation);			String found = actionST.toString();			assertEquals(expecting, found);			action = action2;			expecting = expecting2;		} while (i++ < 1);		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());	}	public void testInvalidReturnValues() throws Exception {		String action = "$x";		String expecting = action;		ErrorQueue equeue = new ErrorQueue();

⌨️ 快捷键说明

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