本文整理汇总了C#中Antlr3.Grammars.ActionTranslator.Translate方法的典型用法代码示例。如果您正苦于以下问题:C# ActionTranslator.Translate方法的具体用法?C# ActionTranslator.Translate怎么用?C# ActionTranslator.Translate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Antlr3.Grammars.ActionTranslator
的用法示例。
在下文中一共展示了ActionTranslator.Translate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestMessageStringificationIsConsistent
public void TestMessageStringificationIsConsistent()
{
string action = "$other.tree = null;";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar a;\n" +
"options { output = AST;}" +
"otherrule\n" +
" : 'y' ;" +
"rule\n" +
" : other=otherrule {" + action + "}\n" +
" ;" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator,
"rule",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string rawTranslation =
translator.Translate();
int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
object expectedArg = "other";
object expectedArg2 = "tree";
GrammarSemanticsMessage expectedMessage =
new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
string expectedMessageString = expectedMessage.ToString();
Assert.AreEqual( expectedMessageString, expectedMessage.ToString() );
}
示例2: TestReturnWithMultipleRuleRefs
public void TestReturnWithMultipleRuleRefs()
{
string action1 = "$obj = $rule2.obj;";
string action2 = "$obj = $rule3.obj;";
string expecting1 = "obj = rule21;";
string expecting2 = "obj = rule32;";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar t;\n" +
"rule1 returns [ Object obj ]\n" +
": rule2 { " + action1 + " }\n" +
"| rule3 { " + action2 + " }\n" +
";\n" +
"rule2 returns [ Object obj ]\n" +
": foo='foo' { $obj = $foo.text; }\n" +
";\n" +
"rule3 returns [ Object obj ]\n" +
": bar='bar' { $obj = $bar.text; }\n" +
";" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
int i = 0;
string action = action1;
string expecting = expecting1;
do
{
ActionTranslator translator = new ActionTranslator( generator, "rule1",
new CommonToken( ANTLRParser.ACTION, action ), i + 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
action = action2;
expecting = expecting2;
} while ( i++ < 1 );
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例3: TestTemplateConstructorNoArgs
public void TestTemplateConstructorNoArgs()
{
string action = "x = %foo();";
string expecting = "x = templateLib.getInstanceOf(\"foo\");";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar t;\n" +
"options {\n" +
" output=template;\n" +
"}\n" +
"\n" +
"a : ID {" + action + "}\n" +
" ;\n" +
"\n" +
"ID : 'a';\n" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator =
new ActionTranslator( generator,
"a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string rawTranslation =
translator.Translate();
StringTemplateGroup templates =
new StringTemplateGroup();
StringTemplate actionST = new StringTemplate( templates, rawTranslation );
string found = actionST.Render();
assertNoErrors( equeue );
Assert.AreEqual( expecting, found );
}
示例4: TestFullyQualifiedRefToCurrentRuleParameter
public void TestFullyQualifiedRefToCurrentRuleParameter()
{
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" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator, "a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例5: TestEscapedLessThanInAction
public void TestEscapedLessThanInAction()
{
Grammar g = new Grammar();
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
string action = "i<3; '<xmltag>'";
ActionTranslator translator = new ActionTranslator( generator, "a",
new CommonToken( ANTLRParser.ACTION, action ), 0 );
string expecting = action;
string rawTranslation =
translator.Translate();
StringTemplateGroup templates =
new StringTemplateGroup();
StringTemplate actionST = new StringTemplate( templates, "<action>" );
actionST.SetAttribute( "action", rawTranslation );
string found = actionST.Render();
Assert.AreEqual( expecting, found );
}
示例6: TestDynamicRuleScopeRefInSubrule
public void TestDynamicRuleScopeRefInSubrule()
{
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" +
" float n;\n" +
"} : b ;\n" +
"b : {" + action + "}\n" +
" ;\n" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator, "b",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例7: Test0IndexedGlobalScope
public void Test0IndexedGlobalScope()
{
string action = "$Symbols[0]::names.add($id.text);";
string expecting =
"((Symbols_scope)Symbols_stack.elementAt(0)).names.add((id!=null?id.getText():null));";
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" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator, "a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例8: TestComplicatedSingleArgParsing
public void TestComplicatedSingleArgParsing()
{
string action = "(*a).foo(21,33,\",\")";
string expecting = "(*a).foo(21,33,\",\")";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
// now check in actual grammar.
Grammar g = new Grammar(
"parser grammar t;\n" +
"a[User u, int i]\n" +
" : A a[" + action + "] B\n" +
" ;" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator, "a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string rawTranslation = translator.Translate();
Assert.AreEqual( expecting, rawTranslation );
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例9: TestSimplePlusEqualLabel
public void TestSimplePlusEqualLabel()
{
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" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator =
new ActionTranslator( generator,
"a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例10: TestSharedGlobalScope
public void TestSharedGlobalScope()
{
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" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator, "a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例11: TestSettingLexerRulePropertyRefs
public void TestSettingLexerRulePropertyRefs()
{
string action = "$text $type=1 $line=1 $pos=1 $channel=1 $index";
string expecting = "getText() _type=1 state.tokenStartLine=1 state.tokenStartCharPositionInLine=1 _channel=1 -1";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"lexer grammar t;\n" +
"R : 'r' {" + action + "};\n" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator =
new ActionTranslator( generator,
"R",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例12: TestScopeAndAttributeWithUnderscore
public void TestScopeAndAttributeWithUnderscore()
{
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" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator, "a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual( expecting, found );
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例13: TestRuleScopeOutsideRule
public void TestRuleScopeOutsideRule()
{
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 { String name; }\n" +
" : {foo();}\n" +
" ;\n" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator,
null,
new CommonToken( ANTLRParser.ACTION, action ), 0 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例14: TestRuleRefWithDynamicScope
public void TestRuleRefWithDynamicScope()
{
string action = "$field::x = $field.st;";
string expecting = "((field_scope)field_stack.peek()).x = retval.st;";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar a;\n" +
"field\n" +
"scope { StringTemplate x; }\n" +
" : 'y' {" + action + "}\n" +
" ;\n" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator,
"field",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
示例15: TestReuseExistingListLabelWithImplicitTokenLabel
public void TestReuseExistingListLabelWithImplicitTokenLabel()
{
string action = "$ID.text;";
string expecting = "(x!=null?x.getText():null);";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar t;\n" +
"a : x+=ID {" + action + "} ;" +
"ID : 'a';\n" );
AntlrTool antlr = newTool();
antlr.SetOutputDirectory( null ); // write to /dev/null
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer();
ActionTranslator translator = new ActionTranslator( generator, "a",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string found = translator.Translate();
Assert.AreEqual(expecting, found);
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}