本文整理汇总了C#中Antlr3.Tool.GrammarAST类的典型用法代码示例。如果您正苦于以下问题:C# GrammarAST类的具体用法?C# GrammarAST怎么用?C# GrammarAST使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
GrammarAST类属于Antlr3.Tool命名空间,在下文中一共展示了GrammarAST类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: ActionAnalysisLexer
public ActionAnalysisLexer(Grammar grammar, string ruleName, GrammarAST actionAST)
: this(new ANTLRStringStream(actionAST.Token.Text))
{
this.grammar = grammar;
this.enclosingRule = grammar.GetLocallyDefinedRule(ruleName);
this.actionToken = actionAST.Token;
this.outerAltNum = actionAST.outerAltNum;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:8,代码来源:ActionAnalysisLexerHelper.cs
示例2: TrackInlineAction
protected void TrackInlineAction( GrammarAST actionAST )
{
Rule r = grammar.GetRule( currentRuleName );
if ( r != null )
{
r.TrackInlineAction( actionAST );
}
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:8,代码来源:DefineGrammarItemsWalkerHelper.cs
示例3: CountAltsForBlock
public int CountAltsForBlock(GrammarAST t)
{
int n = 0;
for ( int i = 0; i < t.ChildCount; i++ )
{
if ( t.GetChild( i ).Type == ALT )
n++;
}
return n;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:11,代码来源:ANTLRTreePrinterHelper.cs
示例4: BlockHasSynPred
public static bool BlockHasSynPred(GrammarAST blockAST)
{
GrammarAST c1 = blockAST.FindFirstType(ANTLRParser.SYN_SEMPRED);
GrammarAST c2 = blockAST.FindFirstType(ANTLRParser.BACKTRACK_SEMPRED);
if (c1 != null || c2 != null)
return true;
// System.out.println(blockAST.enclosingRuleName+
// " "+blockAST.getLine()+":"+blockAST.getColumn()+" no preds AST="+blockAST.toStringTree());
return false;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:11,代码来源:GrammarReport.cs
示例5: TernaryAlt
/** Convert e ? e : e -> ? e : e_[nextPrec] */
public override void TernaryAlt(GrammarAST altTree, GrammarAST rewriteTree, int alt)
{
altTree = GrammarAST.DupTree(altTree);
rewriteTree = GrammarAST.DupTree(rewriteTree);
StripSynPred(altTree);
StripLeftRecursion(altTree);
int nextPrec = NextPrecedence(alt);
StringTemplate refST = recRuleTemplates.GetInstanceOf("recRuleRef");
refST.SetAttribute("ruleName", ruleName);
refST.SetAttribute("arg", nextPrec);
altTree = ReplaceLastRuleRef(altTree, refST.Render());
string altText = Text(altTree);
altText = altText.Trim();
altText += "{}"; // add empty alt to prevent pred hoisting
StringTemplate nameST = recRuleTemplates.GetInstanceOf("recRuleName");
nameST.SetAttribute("ruleName", ruleName);
rewriteTree = ReplaceRuleRefs(rewriteTree, "$" + nameST.Render());
string rewriteText = Text(rewriteTree);
ternaryAlts.Add(alt, altText + (rewriteText != null ? " " + rewriteText : ""));
//System.out.println("ternaryAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:25,代码来源:LeftRecursiveRuleAnalyzer.cs
示例6: StripSynPred
public void StripSynPred(GrammarAST altAST)
{
GrammarAST t = (GrammarAST)altAST.GetChild(0);
if (t.Type == ANTLRParser.BACKTRACK_SEMPRED ||
t.Type == ANTLRParser.SYNPRED ||
t.Type == ANTLRParser.SYN_SEMPRED)
{
altAST.DeleteChild(0);
}
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:10,代码来源:LeftRecursiveRuleAnalyzer.cs
示例7: SetTokenPrec
public override void SetTokenPrec(GrammarAST t, int alt)
{
int ttype = g.GetTokenType(t.Text);
//tokenToPrec.Add(ttype, alt);
tokenToPrec[ttype] = alt;
ASSOC assoc = ASSOC.left;
if (t.terminalOptions != null)
{
object o;
t.terminalOptions.TryGetValue("assoc", out o);
string a = o as string;
if (a != null)
{
if (a.Equals(ASSOC.right.ToString()))
{
assoc = ASSOC.right;
}
else
{
ErrorManager.Error(ErrorManager.MSG_ILLEGAL_OPTION_VALUE, "assoc", assoc);
}
}
}
ASSOC currentAssociativity;
if (altAssociativity.TryGetValue(alt, out currentAssociativity))
{
if (currentAssociativity != assoc)
ErrorManager.Error(ErrorManager.MSG_ALL_OPS_NEED_SAME_ASSOC, alt);
}
else
{
altAssociativity.Add(alt, assoc);
}
//System.out.println("op " + alt + ": " + t.getText()+", assoc="+assoc);
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:38,代码来源:LeftRecursiveRuleAnalyzer.cs
示例8: ReplaceLastRuleRef
public GrammarAST ReplaceLastRuleRef(GrammarAST t, string name)
{
if (t == null)
return null;
GrammarAST last = null;
foreach (GrammarAST rref in t.FindAllType(RULE_REF))
{
last = rref;
}
if (last != null && last.Text.Equals(ruleName))
last.Text = name;
return t;
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:13,代码来源:LeftRecursiveRuleAnalyzer.cs
示例9: Alias
protected override void Alias( GrammarAST t, GrammarAST s )
{
string tokenID = t.Text;
string literal = s.Text;
string prevAliasLiteralID;
_aliasesReverseIndex.TryGetValue(literal, out prevAliasLiteralID);
if ( prevAliasLiteralID != null )
{ // we've seen this literal before
if ( tokenID.Equals( prevAliasLiteralID ) )
{
// duplicate but identical alias; might be tokens {A='a'} and
// lexer rule A : 'a' ; Is ok, just return
return;
}
// give error unless both are rules (ok if one is in tokens section)
if ( !( tokenRuleDefs.Contains( tokenID ) && tokenRuleDefs.Contains( prevAliasLiteralID ) ) )
{
// don't allow alias if A='a' in tokens section and B : 'a'; is rule.
// Allow if both are rules. Will get DFA nondeterminism error later.
ErrorManager.GrammarError( ErrorManager.MSG_TOKEN_ALIAS_CONFLICT,
grammar,
t.Token,
tokenID + "=" + literal,
prevAliasLiteralID );
}
return; // don't do the alias
}
int existingLiteralType = grammar.GetTokenType( literal );
if ( existingLiteralType != Label.INVALID )
{
// we've seen this before from a tokenVocab most likely
// don't assign a new token type; use existingLiteralType.
_tokens[tokenID] = existingLiteralType;
}
string prevAliasTokenID;
_aliases.TryGetValue(tokenID, out prevAliasTokenID);
if ( prevAliasTokenID != null )
{
ErrorManager.GrammarError( ErrorManager.MSG_TOKEN_ALIAS_REASSIGNMENT,
grammar,
t.Token,
tokenID + "=" + literal,
prevAliasTokenID );
return; // don't do the alias
}
_aliases[tokenID] = literal;
_aliasesReverseIndex[literal] = tokenID;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:49,代码来源:AssignTokenTypesBehavior.cs
示例10: TrackToken
protected override void TrackToken( GrammarAST t )
{
// imported token names might exist, only add if new
// Might have ';'=4 in vocab import and SEMI=';'. Avoid
// setting to UNASSIGNED if we have loaded ';'/SEMI
if ( grammar.GetTokenType( t.Text ) == Label.INVALID &&
!_tokens.ContainsKey( t.Text ) )
{
_tokens[t.Text] = Unassigned;
}
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:11,代码来源:AssignTokenTypesBehavior.cs
示例11: BuildSemanticPredicate
/** Build what amounts to an epsilon transition with a semantic
* predicate action. The pred is a pointer into the AST of
* the SEMPRED token.
*/
public virtual StateCluster BuildSemanticPredicate( GrammarAST pred )
{
// don't count syn preds
if ( !pred.Text.StartsWith( Grammar.SynpredRulePrefix, StringComparison.OrdinalIgnoreCase ) )
{
_nfa.Grammar.numberOfSemanticPredicates++;
}
NFAState left = NewState();
NFAState right = NewState();
Transition e = new Transition( new PredicateLabel( pred ), right );
left.AddTransition( e );
StateCluster g = new StateCluster( left, right );
return g;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:18,代码来源:NFAFactory.cs
示例12: BuildCharLiteralAtom
/** From char 'c' build StateCluster o-intValue(c)->o
*/
public virtual StateCluster BuildCharLiteralAtom( GrammarAST charLiteralAST )
{
int c = Grammar.GetCharValueFromGrammarCharLiteral( charLiteralAST.Text );
return BuildAtom( c, charLiteralAST );
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:7,代码来源:NFAFactory.cs
示例13: BuildAtom
public virtual StateCluster BuildAtom( GrammarAST atomAST )
{
int tokenType = _nfa.Grammar.GetTokenType( atomAST.Text );
return BuildAtom( tokenType, atomAST );
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:5,代码来源:NFAFactory.cs
示例14: BuildAction
/** Build what amounts to an epsilon transition with an action.
* The action goes into NFA though it is ignored during analysis.
* It slows things down a bit, but I must ignore predicates after
* having seen an action (5-5-2008).
*/
public virtual StateCluster BuildAction( GrammarAST action )
{
NFAState left = NewState();
NFAState right = NewState();
Transition e = new Transition( new ActionLabel( action ), right );
left.AddTransition( e );
return new StateCluster( left, right );
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:13,代码来源:NFAFactory.cs
示例15: TernaryAlt
public virtual void TernaryAlt(GrammarAST altTree, GrammarAST rewriteTree, int alt)
{
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:3,代码来源:LeftRecursiveRuleWalker.g3.cs
示例16: TrackString
/** Track string literals (could be in tokens{} section) */
protected override void TrackString( GrammarAST t )
{
// if lexer, don't allow aliasing in tokens section
if ( currentRuleName == null && grammar.type == GrammarType.Lexer )
{
ErrorManager.GrammarError( ErrorManager.MSG_CANNOT_ALIAS_TOKENS_IN_LEXER,
grammar,
t.Token,
t.Text );
return;
}
// in a plain parser grammar rule, cannot reference literals
// (unless defined previously via tokenVocab option)
// don't warn until we hit root grammar as may be defined there.
if ( grammar.IsRoot &&
grammar.type == GrammarType.Parser &&
grammar.GetTokenType( t.Text ) == Label.INVALID )
{
ErrorManager.GrammarError( ErrorManager.MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE,
grammar,
t.Token,
t.Text );
}
// Don't record literals for lexers, they are things to match not tokens
if ( grammar.type == GrammarType.Lexer )
{
return;
}
// otherwise add literal to token types if referenced from parser rule
// or in the tokens{} section
if ( ( currentRuleName == null ||
Rule.GetRuleType(currentRuleName) == RuleType.Parser) &&
grammar.GetTokenType( t.Text ) == Label.INVALID )
{
_stringLiterals[t.Text] = UnassignedInParserRule;
}
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:38,代码来源:AssignTokenTypesBehavior.cs
示例17: BuildSet
/** From set build single edge graph o->o-set->o. To conform to
* what an alt block looks like, must have extra state on left.
*/
public virtual StateCluster BuildSet( IIntSet set, GrammarAST associatedAST )
{
NFAState left = NewState();
NFAState right = NewState();
left.associatedASTNode = associatedAST;
right.associatedASTNode = associatedAST;
Label label = new Label( set );
Transition e = new Transition( label, right );
left.AddTransition( e );
StateCluster g = new StateCluster( left, right );
return g;
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:15,代码来源:NFAFactory.cs
示例18: TrackTokenRule
protected override void TrackTokenRule( GrammarAST t,
GrammarAST modifier,
GrammarAST block )
{
// imported token names might exist, only add if new
if ( grammar.type == GrammarType.Lexer || grammar.type == GrammarType.Combined )
{
if (Rule.GetRuleType(t.Text) == RuleType.Parser)
{
return;
}
if ( t.Text.Equals( Grammar.ArtificialTokensRuleName ) )
{
// don't add Tokens rule
return;
}
// track all lexer rules so we can look for token refs w/o
// associated lexer rules.
grammar.composite.LexerRules.Add( t.Text );
int existing = grammar.GetTokenType( t.Text );
if ( existing == Label.INVALID )
{
_tokens[t.Text] = Unassigned;
}
// look for "<TOKEN> : <literal> ;" pattern
// (can have optional action last)
if ( block.HasSameTreeStructure( charAlias ) ||
block.HasSameTreeStructure( stringAlias ) ||
block.HasSameTreeStructure( charAlias2 ) ||
block.HasSameTreeStructure( stringAlias2 ) )
{
tokenRuleDefs.Add( t.Text );
/*
Grammar parent = grammar.composite.getDelegator(grammar);
boolean importedByParserOrCombined =
parent!=null &&
(parent.type==GrammarType.Lexer||parent.type==GrammarType.Parser);
*/
if ( grammar.type == GrammarType.Combined || grammar.type == GrammarType.Lexer )
{
// only call this rule an alias if combined or lexer
Alias( t, (GrammarAST)block.GetChild( 0 ).GetChild( 0 ) );
}
}
}
// else error
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:49,代码来源:AssignTokenTypesBehavior.cs
示例19: BuildStringLiteralAtom
/** For a non-lexer, just build a simple token reference atom.
* For a lexer, a string is a sequence of char to match. That is,
* "fog" is treated as 'f' 'o' 'g' not as a single transition in
* the DFA. Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states
* for n characters.
*/
public virtual StateCluster BuildStringLiteralAtom( GrammarAST stringLiteralAST )
{
if ( _nfa.Grammar.type == GrammarType.Lexer )
{
StringBuilder chars =
Grammar.GetUnescapedStringFromGrammarStringLiteral( stringLiteralAST.Text );
NFAState first = NewState();
NFAState last = null;
NFAState prev = first;
for ( int i = 0; i < chars.Length; i++ )
{
int c = chars[i];
NFAState next = NewState();
TransitionBetweenStates( prev, next, c );
prev = last = next;
}
return new StateCluster( first, last );
}
// a simple token reference in non-Lexers
int tokenType = _nfa.Grammar.GetTokenType( stringLiteralAST.Text );
return BuildAtom( tokenType, stringLiteralAST );
}
开发者ID:mahanteshck,项目名称:antlrcs,代码行数:29,代码来源:NFAFactory.cs
示例20: OtherAlt
public override void OtherAlt(GrammarAST altTree, GrammarAST rewriteTree, int alt)
{
altTree = GrammarAST.DupTree(altTree);
rewriteTree = GrammarAST.DupTree(rewriteTree);
StripSynPred(altTree);
StripLeftRecursion(altTree);
string altText = Text(altTree);
string rewriteText = Text(rewriteTree);
otherAlts.Add(altText + (rewriteText != null ? " " + rewriteText : ""));
//System.out.println("otherAlt " + alt + ": " + altText + ", rewrite=" + rewriteText);
}
开发者ID:JSchofield,项目名称:antlrcs,代码行数:12,代码来源:LeftRecursiveRuleAnalyzer.cs
注:本文中的Antlr3.Tool.GrammarAST类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论