本文整理汇总了C#中Antlr4.Tool.Ast.GrammarAST.GetNodesWithType方法的典型用法代码示例。如果您正苦于以下问题:C# GrammarAST.GetNodesWithType方法的具体用法?C# GrammarAST.GetNodesWithType怎么用?C# GrammarAST.GetNodesWithType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Antlr4.Tool.Ast.GrammarAST
的用法示例。
在下文中一共展示了GrammarAST.GetNodesWithType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AugmentTokensWithOriginalPosition
public static void AugmentTokensWithOriginalPosition(Grammar g, GrammarAST tree)
{
if (tree == null)
return;
IList<GrammarAST> optionsSubTrees = tree.GetNodesWithType(ANTLRParser.ELEMENT_OPTIONS);
for (int i = 0; i < optionsSubTrees.Count; i++)
{
GrammarAST t = optionsSubTrees[i];
CommonTree elWithOpt = (CommonTree)t.Parent;
if (elWithOpt is GrammarASTWithOptions)
{
IDictionary<string, GrammarAST> options = ((GrammarASTWithOptions)elWithOpt).GetOptions();
if (options.ContainsKey(LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME))
{
GrammarToken newTok = new GrammarToken(g, elWithOpt.Token);
newTok.originalTokenIndex = int.Parse(options[LeftRecursiveRuleTransformer.TOKENINDEX_OPTION_NAME].Text);
elWithOpt.Token = newTok;
GrammarAST originalNode = g.ast.GetNodeWithTokenIndex(newTok.TokenIndex);
if (originalNode != null)
{
// update the AST node start/stop index to match the values
// of the corresponding node in the original parse tree.
elWithOpt.TokenStartIndex = originalNode.TokenStartIndex;
elWithOpt.TokenStopIndex = originalNode.TokenStopIndex;
}
else
{
// the original AST node could not be located by index;
// make sure to assign valid values for the start/stop
// index so toTokenString will not throw exceptions.
elWithOpt.TokenStartIndex = newTok.TokenIndex;
elWithOpt.TokenStopIndex = newTok.TokenIndex;
}
}
}
}
}
示例2: Text
public virtual string Text(GrammarAST t)
{
if (t == null)
return "";
int tokenStartIndex = t.TokenStartIndex;
int tokenStopIndex = t.TokenStopIndex;
// ignore tokens from existing option subtrees like:
// (ELEMENT_OPTIONS (= assoc right))
//
// element options are added back according to the values in the map
// returned by getOptions().
IntervalSet ignore = new IntervalSet();
IList<GrammarAST> optionsSubTrees = t.GetNodesWithType(ELEMENT_OPTIONS);
foreach (GrammarAST sub in optionsSubTrees)
{
ignore.Add(sub.TokenStartIndex, sub.TokenStopIndex);
}
// Individual labels appear as RULE_REF or TOKEN_REF tokens in the tree,
// but do not support the ELEMENT_OPTIONS syntax. Make sure to not try
// and add the tokenIndex option when writing these tokens.
IntervalSet noOptions = new IntervalSet();
IList<GrammarAST> labeledSubTrees = t.GetNodesWithType(new IntervalSet(ASSIGN, PLUS_ASSIGN));
foreach (GrammarAST sub in labeledSubTrees)
{
noOptions.Add(sub.GetChild(0).TokenStartIndex);
}
StringBuilder buf = new StringBuilder();
int i = tokenStartIndex;
while (i <= tokenStopIndex)
{
if (ignore.Contains(i))
{
i++;
continue;
}
IToken tok = tokenStream.Get(i);
// Compute/hold any element options
StringBuilder elementOptions = new StringBuilder();
if (!noOptions.Contains(i))
{
GrammarAST node = t.GetNodeWithTokenIndex(tok.TokenIndex);
if (node != null &&
(tok.Type == TOKEN_REF ||
tok.Type == STRING_LITERAL ||
tok.Type == RULE_REF))
{
elementOptions.Append("tokenIndex=").Append(tok.TokenIndex);
}
if (node is GrammarASTWithOptions)
{
GrammarASTWithOptions o = (GrammarASTWithOptions)node;
foreach (KeyValuePair<string, GrammarAST> entry in o.GetOptions())
{
if (elementOptions.Length > 0)
{
elementOptions.Append(',');
}
elementOptions.Append(entry.Key);
elementOptions.Append('=');
elementOptions.Append(entry.Value.Text);
}
}
}
buf.Append(tok.Text); // add actual text of the current token to the rewritten alternative
i++; // move to the next token
// Are there args on a rule?
if (tok.Type == RULE_REF && i <= tokenStopIndex && tokenStream.Get(i).Type == ARG_ACTION)
{
buf.Append('[' + tokenStream.Get(i).Text + ']');
i++;
}
// now that we have the actual element, we can add the options.
if (elementOptions.Length > 0)
{
buf.Append('<').Append(elementOptions).Append('>');
}
}
return buf.ToString();
}