📄 testattributes.java
字号:
"s : b ;\n"+ "b\n" + "scope {\n" + " int n;\n" + "} : '(' b ')' {"+action+"}\n" + // refers to current invocation's 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, "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 testRefToTemplateAttributeForCurrentRule() throws Exception { String action = "$st=null;"; String expecting = "retval.st =null;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n" + "options {output=template;}\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); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testRefToTextAttributeForCurrentRule() throws Exception { String action = "$text"; String expecting = "input.toString(retval.start,input.LT(-1))"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n" + "options {output=template;}\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); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testRefToStartAttributeForCurrentRule() throws Exception { String action = "$start;"; String expecting = "((Token)retval.start);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\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); StringTemplate codeST = generator.getRecognizerST(); String code = codeST.toString(); String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!")); assertEquals(expecting, found); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testTokenLabelFromMultipleAlts() throws Exception { String action = "$ID.text;"; // must be qualified String action2 = "$INT.text;"; // must be qualified 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 { ErrorQ
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -