📄 testattributes.java
字号:
String found = actionST.toString(); assertEquals(expecting, found); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testFullyQualifiedRefToCurrentRuleParameter() throws Exception { String action = "$a.i;"; String expecting = "i;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a[int i]: {"+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 testFullyQualifiedRefToCurrentRuleRetVal() throws Exception { String action = "$a.i;"; String expecting = "retval.i;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a returns [int i, int j]: {"+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 testSetFullyQualifiedRefToCurrentRuleRetVal() throws Exception { String action = "$a.i = 1;"; String expecting = "retval.i = 1;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a returns [int i, int j]: {"+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 testIsolatedRefToCurrentRule() throws Exception { String action = "$a;"; String expecting = ""; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : 'a' {"+action+"}\n" + " ;\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.setCodeGenerator(generator); generator.genRecognizer(); // forces load of templates int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE; Object expectedArg = "a"; Object expectedArg2 = null; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testIsolatedRefToRule() throws Exception { String action = "$x;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : x=b {"+action+"}\n" + " ;\n" + "b : 'b' ;\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.setCodeGenerator(generator); generator.genRecognizer(); // forces load of templates int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE; Object expectedArg = "x"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg); checkError(equeue, expectedMessage); } /* I think these have to be errors $a.x makes no sense. public void testFullyQualifiedRefToLabelInCurrentRule() throws Exception { String action = "$a.x;"; String expecting = "x;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : x='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 testFullyQualifiedRefToListLabelInCurrentRule() throws Exception { String action = "$a.x;"; // must be qualified String expecting = "list_x;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a : x+='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 testFullyQualifiedRefToTemplateAttributeInCurrentRule() throws Exception { String action = "$a.st;"; // can be qualified String expecting = "retval.st;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n" + "options {output=template;}\n"+ "a : (A->{$A.text}) {"+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 testRuleRefWhenRuleHasScope() throws Exception { String action = "$b.start;"; String expecting = "((Token)b1.start);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n" + "a : b {###"+action+"!!!} ;\n" + "b\n" + "scope {\n" + " int n;\n" + "} : 'b' \n" + " ;\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.setCodeGenerator(generator); generator.genRecognizer(); // forces load of templates 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 testDynamicScopeRefOkEvenThoughRuleRefExists() throws Exception { String action = "$b::n;"; String expecting = "((b_scope)b_stack.peek()).n;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n" + "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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -