📄 testattributes.java
字号:
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); 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 testAbsoluteIndexedGlobalScope() throws Exception { String action = "$Symbols[3]::names.add($id.text);"; String expecting = "((Symbols_scope)Symbols_stack.elementAt(3)).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 testScopeAndAttributeWithUnderscore() throws Exception { String action = "$foo_bar::a_b;"; String expecting = "((foo_bar_scope)foo_bar_stack.peek()).a_b;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "scope foo_bar {\n" + " int a_b;\n" + "}\n" + "a scope foo_bar; : (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 testSharedGlobalScope() throws Exception { String action = "$Symbols::x;"; String expecting = "((Symbols_scope)Symbols_stack.peek()).x;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "scope Symbols {\n" + " String x;\n" + "}\n" + "a\n"+ "scope { int y; }\n"+ "scope Symbols;\n" + " : b {"+action+"}\n" + " ;\n" + "b : ID {$Symbols::x=$ID.text} ;\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 testGlobalScopeOutsideRule() throws Exception { String action = "public void foo() {$Symbols::names.add('foo');}"; String expecting = "public void foo() {((Symbols_scope)Symbols_stack.peek()).names.add('foo');}"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "scope Symbols {\n" + " int n;\n" + " List names;\n" + "}\n" + "@members {'+action+'}\n" + "a : \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 testRuleScopeOutsideRule() throws Exception { String action = "public void foo() {$a::name;}"; String expecting = "public void foo() {((a_scope)a_stack.peek()).name;}"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "@members {"+action+"}\n" + "a\n" + "scope { int name; }\n" + " : {foo();}\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, 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); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testBasicRuleScope() throws Exception { String action = "$a::n;"; String expecting = "((a_scope)a_stack.peek()).n;"; 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); assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size()); } public void testUnqualifiedRuleScopeAccessInsideRule() throws Exception { String action = "$n;"; 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 int expectedMsgID = ErrorManager.MSG_ISOLATED_RULE_ATTRIBUTE; Object expectedArg = "n"; Object expectedArg2 = null; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g, null, expectedArg, expectedArg2); checkError(equeue, expectedMessage); } public void testIsolatedDynamicRuleScopeRef() throws Exception { String action = "$a;"; // refers to stack not top of stack String expecting = "a_stack;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n"+ "a\n" + "scope {\n" + " int n;\n" + "} : b ;\n" + "b : {"+action+"}\n" + " ;\n"); Tool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.setCodeGenerator(generator);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -