📄 testattributes.java
字号:
String expecting = "ID1.getText();"; String expecting2 = "INT2.getText();"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : ID {"+action+"}\n" + " | INT {"+action2+"}\n" + " ;\n" + "ID : 'a';\n" + "INT : '0';\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()); translator = new ActionTranslatorLexer(generator, "a", new antlr.CommonToken(ANTLRParser.ACTION,action2),2); rawTranslation = translator.translate(); templates = new StringTemplateGroup(".", AngleBracketTemplateLexer.class); actionST = new StringTemplate(templates, rawTranslation); found = actionST.toString(); assertEquals(expecting2, found); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testRuleLabelFromMultipleAlts() throws Exception { String action = "$b.text;"; // must be qualified String action2 = "$c.text;"; // must be qualified String expecting = "input.toString(b1.start,b1.stop);"; String expecting2 = "input.toString(c2.start,c2.stop);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : b {"+action+"}\n" + " | c {"+action2+"}\n" + " ;\n" + "b : 'a';\n" + "c : '0';\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()); translator = new ActionTranslatorLexer(generator, "a", new antlr.CommonToken(ANTLRParser.ACTION,action2),2); rawTranslation = translator.translate(); templates = new StringTemplateGroup(".", AngleBracketTemplateLexer.class); actionST = new StringTemplate(templates, rawTranslation); found = actionST.toString(); assertEquals(expecting2, found); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testUnknownDynamicAttribute() throws Exception { String action = "$a::x"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a\n" + "scope {\n" + " int n;\n" + "} : {"+action+"}\n" + " ;\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); int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE; Object expectedArg = "a"; Object expectedArg2 = "x"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testUnknownGlobalDynamicAttribute() throws Exception { String action = "$Symbols::x"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "scope Symbols {\n" + " int n;\n" + "}\n" + "a : {'+action+'}\n" + " ;\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); int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE; Object expectedArg = "Symbols"; Object expectedArg2 = "x"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testUnqualifiedRuleScopeAttribute() throws Exception { String action = "$n;"; // must be qualified String expecting = "$n;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a\n" + "scope {\n" + " int n;\n" + "} : b\n" + " ;\n" + "b : {'+action+'}\n" + " ;\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); 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); int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE; Object expectedArg = "n"; Object expectedArg2 = null; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testRuleAndTokenLabelTypeMismatch() throws Exception { ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : id='foo' id=b\n" + " ;\n" + "b : ;\n"); int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT; Object expectedArg = "id"; Object expectedArg2 = "rule!=token"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testListAndTokenLabelTypeMismatch() throws Exception { ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : ids+='a' ids='b'\n" + " ;\n" + "b : ;\n"); int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT; Object expectedArg = "ids"; Object expectedArg2 = "token!=token-list"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testListAndRuleLabelTypeMismatch() throws Exception { ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n" + "options {output=AST;}\n"+ "a : bs+=b bs=b\n" + " ;\n" + "b : 'b';\n"); int expectedMsgID = ErrorManager.MSG_LABEL_TYPE_CONFLICT; Object expectedArg = "bs"; Object expectedArg2 = "rule!=rule-list"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testArgReturnValueMismatch() throws Exception { ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a[int i] returns [int x, int i]\n" + " : \n" + " ;\n" + "b : ;\n"); int expectedMsgID = ErrorManager.MSG_ARG_RETVAL_CONFLICT; Object expectedArg = "i"; Object expectedArg2 = "a"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testSimplePlusEqualLabel() throws Exception { String action = "$ids.size();"; // must be qualified String expecting = "list_ids.size();"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "a : ids+=ID ( COMMA ids+=ID {"+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()); } public void testPlusEqualStringLabel() throws Exception { String action = "$ids.size();"; // must be qualified String expecting = "list_ids.size();"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : ids+='if' ( ',' ids+=ID {"+action+"})* ;" + "ID : 'a';\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 testPlusEqualSetLabel() throws Exception { String action = "$ids.size();"; // must be qualified String expecting = "list_ids.size();"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : ids+=('a'|'b') ( ',' ids+=ID {"+action+"})* ;" + "ID : 'a';\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 testPlusEqualWildcardLabel() throws Exception { String action = "$ids.size();"; // must be qualified String expecting = "list_ids.size();"; ErrorQueue equeue = new ErrorQueue(); ErrorManage
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -