本文整理汇总了C#中Antlr3.Codegen.CodeGenerator类的典型用法代码示例。如果您正苦于以下问题:C# Antlr3.Codegen.CodeGenerator类的具体用法?C# Antlr3.Codegen.CodeGenerator怎么用?C# Antlr3.Codegen.CodeGenerator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Antlr3.Codegen.CodeGenerator类属于命名空间,在下文中一共展示了Antlr3.Codegen.CodeGenerator类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: GetTargetCharLiteralFromANTLRCharLiteral
public override string GetTargetCharLiteralFromANTLRCharLiteral(
CodeGenerator generator,
string literal )
{
int c = Grammar.GetCharValueFromGrammarCharLiteral( literal );
return ( (char)c ).ToString();
}
开发者ID:bszafko,项目名称:antlrcs,代码行数:7,代码来源:PythonTarget.cs
示例2: TestCannotHaveSpaceAfterDot
public void TestCannotHaveSpaceAfterDot()
{
string action = "%x. y = z;";
//String expecting = null;
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
int expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
object expectedArg = "%x.";
GrammarSemanticsMessage expectedMessage =
new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
checkError( equeue, expectedMessage );
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:28,代码来源:TestTemplates.cs
示例3: 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() );
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:31,代码来源:TestMessages.cs
示例4: GetTargetStringLiteralFromANTLRStringLiteral
/** Convert from an ANTLR string literal found in a grammar file to
* an equivalent string literal in the target language. For Java, this
* is the translation 'a\n"' -> "a\n\"". Expect single quotes
* around the incoming literal. Just flip the quotes and replace
* double quotes with \"
*/
public override string GetTargetStringLiteralFromANTLRStringLiteral( CodeGenerator generator,
string literal )
{
literal = literal.Replace( "\"", "\\\"" );
StringBuilder buf = new StringBuilder( literal );
buf[0] = '"';
buf[literal.Length - 1] = '"';
buf.Insert( 0, '@' );
return buf.ToString();
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:16,代码来源:ObjCTarget.cs
示例5: GetTokenTypeAsTargetLabel
/** If we have a label, prefix it with the recognizer's name */
public override string GetTokenTypeAsTargetLabel( CodeGenerator generator, int ttype )
{
string name = generator.grammar.GetTokenDisplayName( ttype );
// If name is a literal, return the token type instead
if ( name[0] == '\'' )
{
return ttype.ToString();
}
return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[(int)generator.grammar.type] + "_" + name;
//return super.getTokenTypeAsTargetLabel(generator, ttype);
//return this.getTokenTextAndTypeAsTargetLabel(generator, null, ttype);
}
开发者ID:bszafko,项目名称:antlrcs,代码行数:13,代码来源:ObjCTarget.cs
示例6: PerformGrammarAnalysis
protected override void PerformGrammarAnalysis(CodeGenerator generator, Grammar grammar)
{
base.PerformGrammarAnalysis(generator, grammar);
foreach (Rule rule in grammar.Rules)
rule.ThrowsSpec.Add("RecognitionException");
IEnumerable<Rule> delegatedRules = grammar.GetDelegatedRules();
if (delegatedRules != null)
{
foreach (Rule rule in delegatedRules)
rule.ThrowsSpec.Add("RecognitionException");
}
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:14,代码来源:JavaTarget.cs
示例7: GetTargetCharLiteralFromANTLRCharLiteral
public override string GetTargetCharLiteralFromANTLRCharLiteral( CodeGenerator generator,
string literal )
{
StringBuilder buf = new StringBuilder( 10 );
int c = Grammar.GetCharValueFromGrammarCharLiteral( literal );
if ( c < Label.MIN_CHAR_VALUE )
{
buf.Append( "\\x{0000}" );
}
else if ( c < targetCharValueEscape.Length &&
targetCharValueEscape[c] != null )
{
buf.Append( targetCharValueEscape[c] );
}
else if ( ( c < 0x7F ) && !char.IsControl( (char)c ) )
{
// normal char
buf.Append( (char)c );
}
else
{
// must be something unprintable...use \\uXXXX
// turn on the bit above max "\\uFFFF" value so that we pad with zeros
// then only take last 4 digits
string hex = c.ToString( "X4" );
buf.Append( "\\x{" );
buf.Append( hex );
buf.Append( "}" );
}
if ( buf.ToString().IndexOf( '\\' ) == -1 )
{
// no need for interpolation, use single quotes
buf.Insert( 0, '\'' );
buf.Append( '\'' );
}
else
{
// need string interpolation
buf.Insert( 0, '\"' );
buf.Append( '\"' );
}
return buf.ToString();
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:46,代码来源:Perl5Target.cs
示例8: GetTargetCharLiteralFromANTLRCharLiteral
public override string GetTargetCharLiteralFromANTLRCharLiteral( CodeGenerator generator,
string literal )
{
if ( literal.StartsWith( "'\\u" ) )
{
literal = "0x" + literal.Substring( 3, 4 );
}
else
{
int c = literal[1]; // TJP
if ( c < 32 || c > 127 )
{
literal = "0x" + c.ToString( "x" );
}
}
return literal;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:18,代码来源:ObjCTarget.cs
示例9: GetTokenTypeAsTargetLabel
/** Target must be able to override the labels used for token types */
public override string GetTokenTypeAsTargetLabel( CodeGenerator generator,
int ttype )
{
// use ints for predefined types;
// <invalid> <EOR> <DOWN> <UP>
if ( ttype >= 0 && ttype <= 3 )
{
return ttype.ToString();
}
string name = generator.grammar.GetTokenDisplayName( ttype );
// If name is a literal, return the token type instead
if ( name[0] == '\'' )
{
return ttype.ToString();
}
return name;
}
开发者ID:bszafko,项目名称:antlrcs,代码行数:21,代码来源:PythonTarget.cs
示例10: TestSetAttrOfExprInMembers
public void TestSetAttrOfExprInMembers()
{
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar t;\n" +
"options {\n" +
" output=template;\n" +
"}\n" +
"@members {\n" +
"%code.instr = o;" + // must not get null ptr!
"}\n" +
"a : ID\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
assertNoErrors( equeue );
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:23,代码来源:TestTemplates.cs
示例11: TestSetAttrOfExpr
public void TestSetAttrOfExpr()
{
string action = "%{foo($ID.text).getST()}.y = z;";
string expecting = "(foo((ID1!=null?ID1.getText():null)).getST()).setAttribute(\"y\", z);";
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 );
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:35,代码来源:TestTemplates.cs
示例12: TestRewriteRuleAndRewriteModeRefRule
public void TestRewriteRuleAndRewriteModeRefRule()
{
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"tree grammar TP;\n" +
"options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
"a : b+ -> {ick}\n" +
" | b b A -> {ick}\n" +
" ;\n" +
"b : B ;\n"
);
AntlrTool antlr = newTool();
antlr.SetOutputDirectory( null ); // write to /dev/null
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer();
Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:20,代码来源:TestRewriteTemplates.cs
示例13: 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 );
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:18,代码来源:TestAttributes.cs
示例14: TestForwardRefRuleLabels
public void TestForwardRefRuleLabels()
{
string action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.tree;";
string expecting = "(r!=null?r.x:0); (r!=null?((Token)r.start):null); (r!=null?((Token)r.stop):null); (r!=null?((Object)r.tree):null); (r!=null?r.x:0); (r!=null?((Object)r.tree):null);";
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" );
AntlrTool antlr = newTool();
antlr.SetOutputDirectory( null ); // write to /dev/null
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // codegen phase sets some vars we need
StringTemplate codeST = generator.RecognizerST;
string code = codeST.Render();
int startIndex = code.IndexOf("###") + 3;
int endIndex = code.IndexOf("!!!");
string found = code.Substring(startIndex, endIndex - startIndex);
Assert.AreEqual( expecting, found );
Assert.AreEqual(0, equeue.errors.Count, "unexpected errors: " + equeue);
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:28,代码来源:TestAttributes.cs
示例15: TestRewriteRuleAndRewriteModeIgnoreActionsPredicates
public void TestRewriteRuleAndRewriteModeIgnoreActionsPredicates()
{
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"tree grammar TP;\n" +
"options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
"a: {action} {action2} x=A -> {ick}\n" +
" | {pred1}? y+=B -> {ick}\n" +
" | C {action} -> {ick}\n" +
" | {pred2}?=> z+=D -> {ick}\n" +
" | (E)=> ^(F G) -> {ick}\n" +
" ;\n"
);
AntlrTool antlr = newTool();
antlr.SetOutputDirectory( null ); // write to /dev/null
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer();
Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:22,代码来源:TestRewriteTemplates.cs
示例16: CreateStateTables
public virtual void CreateStateTables( CodeGenerator generator )
{
//[email protected]("createTables:\n"+this);
this._generator = generator;
Description = NFADecisionStartState.Description;
Description = generator.Target.GetTargetStringLiteralFromString( Description );
// create all the tables
//special = new List<int>( this.NumberOfStates ); // Vector<short>
//special.setSize( this.NumberOfStates );
_special = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
_specialStates = new List<DFAState>();
_specialStateSTs = new List<StringTemplate>();
//accept = new List<int>( this.NumberOfStates ); // Vector<int>
//accept.setSize( this.NumberOfStates );
_accept = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
//eot = new List<int>( this.NumberOfStates ); // Vector<int>
//eot.setSize( this.NumberOfStates );
_eot = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
//eof = new List<int>( this.NumberOfStates ); // Vector<int>
//eof.setSize( this.NumberOfStates );
_eof = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
//min = new List<int>( this.NumberOfStates ); // Vector<int>
//min.setSize( this.NumberOfStates );
_min = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
//max = new List<int>( this.NumberOfStates ); // Vector<int>
//max.setSize( this.NumberOfStates );
_max = Enumerable.Repeat( EmptyValue, NumberOfStates ).ToArray();
_transition = new int[NumberOfStates][]; // Vector<Vector<int>>
//transition.setSize( this.NumberOfStates );
_transitionEdgeTables = new List<int?>( this.NumberOfStates ); // Vector<Vector<int>>
_transitionEdgeTables.Resize( this.NumberOfStates );
// for each state in the DFA, fill relevant tables.
IEnumerable<DFAState> it = null;
if ( UserMaxLookahead > 0 )
{
it = _states;
}
else
{
it = UniqueStates.Values;
}
foreach ( DFAState s in it )
{
if ( s == null )
{
// ignore null states; some acylic DFA see this condition
// when inlining DFA (due to lacking of exit branch pruning?)
continue;
}
if ( s.IsAcceptState )
{
// can't compute min,max,special,transition on accepts
_accept[s.StateNumber] = s.GetUniquelyPredictedAlt();
}
else
{
CreateMinMaxTables( s );
CreateTransitionTableEntryForState( s );
CreateSpecialTable( s );
CreateEOTAndEOFTables( s );
}
}
// now that we have computed list of specialStates, gen code for 'em
for ( int i = 0; i < _specialStates.Count; i++ )
{
DFAState ss = _specialStates[i];
StringTemplate stateST = generator.GenerateSpecialState( ss );
_specialStateSTs.Add( stateST );
}
// check that the tables are not messed up by encode/decode
/*
testEncodeDecode(min);
testEncodeDecode(max);
testEncodeDecode(accept);
testEncodeDecode(special);
[email protected]("min="+min);
[email protected]("max="+max);
[email protected]("eot="+eot);
[email protected]("eof="+eof);
[email protected]("accept="+accept);
[email protected]("special="+special);
[email protected]("transition="+transition);
*/
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:90,代码来源:DFA.cs
示例17: checkDecision
//throws Exception
protected void checkDecision( Grammar g,
int decision,
string expecting,
int[] expectingUnreachableAlts )
{
Antlr3.AntlrTool tool = new Antlr3.AntlrTool();
// mimic actions of org.antlr.Tool first time for grammar g
if ( g.CodeGenerator == null )
{
CodeGenerator generator = new CodeGenerator( tool, g, "Java" );
g.CodeGenerator = generator;
g.BuildNFA();
g.CreateLookaheadDFAs( false );
}
DFA dfa = g.GetLookaheadDFA( decision );
Assert.IsNotNull(dfa, "unknown decision #" + decision);
FASerializer serializer = new FASerializer( g );
string result = serializer.Serialize( dfa.StartState );
//System.out.print(result);
var nonDetAlts = dfa.UnreachableAlts;
//System.out.println("alts w/o predict state="+nonDetAlts);
// first make sure nondeterministic alts are as expected
if ( expectingUnreachableAlts == null )
{
if ( nonDetAlts != null && nonDetAlts.Count != 0 )
{
Console.Error.WriteLine( "nondeterministic alts (should be empty): " + ( (IList)nonDetAlts ).ToElementString() );
}
Assert.AreEqual(0, nonDetAlts != null ? nonDetAlts.Count : 0, "unreachable alts mismatch");
}
else
{
for ( int i = 0; i < expectingUnreachableAlts.Length; i++ )
{
Assert.IsTrue(nonDetAlts != null ? nonDetAlts.Contains(expectingUnreachableAlts[i]) : false, "unreachable alts mismatch");
}
}
Assert.AreEqual( expecting, result );
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:42,代码来源:TestCharDFAConversion.cs
示例18: TestUnknownToken
public void TestUnknownToken()
{
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
string grammar =
"grammar T;\n" +
"options {output=AST;}\n" +
"a : INT -> ICK ;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+;\n" +
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
Grammar g = new Grammar( grammar );
AntlrTool antlr = newTool();
antlr.SetOutputDirectory( null ); // write to /dev/null
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer();
int expectedMsgID = ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE;
object expectedArg = "ICK";
object expectedArg2 = null;
GrammarSemanticsMessage expectedMessage =
new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
checkError( equeue, expectedMessage );
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:28,代码来源:TestRewriteAST.cs
示例19: TestWeirdRuleRef
public void TestWeirdRuleRef()
{
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
string grammar =
"grammar T;\n" +
"options {output=AST;}\n" +
"a : ID a -> $a | INT ;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT: '0'..'9'+ ;\n" +
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
Grammar g = new Grammar( grammar );
AntlrTool antlr = newTool();
antlr.SetOutputDirectory( null ); // write to /dev/null
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer();
// $a is ambig; is it previous root or ref to a ref in alt?
Assert.AreEqual(1, equeue.errors.Count, "unexpected errors: " + equeue);
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:22,代码来源:TestRewriteAST.cs
示例20: StringRenderer
public StringRenderer(CodeGenerator generator, CSharp2Target target)
{
_generator = generator;
_target = target;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:5,代码来源:CSharp2Target.cs
注:本文中的Antlr3.Codegen.CodeGenerator类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论