📄 testattributes.java
字号:
assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testForwardRefRuleLabels() throws Exception { String action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.tree;"; String expecting = "r.x; ((Token)r.start); ((Token)r.stop); ((Object)r.tree); r.x; ((Object)r.tree);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "b : r=a {###"+action+"!!!}\n" + " ;\n" + "a returns [int x]\n" + " : ;\n"); Tool antlr = newTool(); antlr.setOutputDirectory(null); // write to /dev/null CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.setCodeGenerator(generator); generator.genRecognizer(); // codegen phase sets some vars we need 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 testInvalidRuleLabelAccessesParameter() throws Exception { String action = "$r.z"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "a[int z] returns [int x]\n" + " :\n" + " ;\n"+ "b : r=a[3] {"+action+"}\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_INVALID_RULE_PARAMETER_REF; Object expectedArg = "a"; Object expectedArg2 = "z"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testInvalidRuleLabelAccessesScopeAttribute() throws Exception { String action = "$r.n"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "a\n" + "scope { int n; }\n" + " :\n" + " ;\n"+ "b : r=a[3] {"+action+"}\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_INVALID_RULE_SCOPE_ATTRIBUTE_REF; Object expectedArg = "a"; Object expectedArg2 = "n"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testInvalidRuleAttribute() throws Exception { String action = "$r.blort"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "a[int z] returns [int x]\n" + " :\n" + " ;\n"+ "b : r=a[3] {"+action+"}\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_RULE_ATTRIBUTE; Object expectedArg = "a"; Object expectedArg2 = "blort"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testMissingRuleAttribute() throws Exception { String action = "$r"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "a[int z] returns [int x]\n" + " :\n" + " ;\n"+ "b : r=a[3] {"+action+"}\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(); int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE; Object expectedArg = "r"; Object expectedArg2 = null; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testMissingUnlabeledRuleAttribute() throws Exception { String action = "$a"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "a returns [int x]:\n" + " ;\n"+ "b : a {"+action+"}\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(); int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_SCOPE; Object expectedArg = "a"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg); checkError(equeue, expectedMessage); } public void testNonDynamicAttributeOutsideRule() throws Exception { String action = "public void foo() { $x; }"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "@members {'+action+'}\n" + "a : ;\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); ActionTranslatorLexer translator = new ActionTranslatorLexer(generator, null, 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); int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE; Object expectedArg = "x"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg); checkError(equeue, expectedMessage); } public void testNonDynamicAttributeOutsideRule2() throws Exception { String action = "public void foo() { $x.y; }"; String expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "parser grammar t;\n"+ "@members {'+action+'}\n" + "a : ;\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); ActionTranslatorLexer translator = new ActionTranslatorLexer(generator, null, 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); int expectedMsgID = ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE; Object expectedArg = "x"; Object expectedArg2 = "y"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } // D Y N A M I C A L L Y S C O P E D A T T R I B U T E S public void testBasicGlobalScope() throws Exception { String action = "$Symbols::names.add($id.text);"; String expecting = "((Symbols_scope)Symbols_stack.peek()).names.add(id.getText());"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "scope Symbols {\n" + " int n;\n" + " List names;\n" + "}\n" + "a scope Symbols; : (id=ID ';' {"+action+"} )+\n" + " ;\n" + "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 testUnknownGlobalScope() throws Exception { String action = "$Symbols::names.add($id.text);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a scope Symbols; : (id=ID ';' {"+action+"} )+\n" + " ;\n" + "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); assertEquals("unexpected errors: "+equeue, 2, equeue.errors.size()); int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE; Object expectedArg = "Symbols"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg); checkError(equeue, expectedMessage); } public void testIndexedGlobalScope() throws Exception { String action = "$Symbols[-1]::names.add($id.text);"; String expecting = "((Symbols_scope)Symbols_stack.elementAt(Symbols_stack.size()-1-1)).names.add(id.getText());"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "scope Symbols {\n" + " int n;\n" + " List names;\n" + "}\n" + "a scope Symbols; : (id=ID ';' {"+action+"} )+\n" + " ;\n" + "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 test0IndexedGlobalScope() throws Exception { String action = "$Symbols[0]::names.add($id.text);"; String expecting = "((Symbols_scope)Symbols_stack.elementAt(0)).names.add(id.getText());"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "scope Symbols {\n" + " int n;\n" + " List names;\n" + "}\n" + "a scope Symbols; : (id=ID ';' {"+action+"} )+\n" + " ;\n" + "ID : 'a';\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.setCodeGenerator(generator);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -