public static void _getAllLeaves(Tree t, List<? super Tree> leaves,
final int startIndex,
final int stopIndex)
{
int n = t.getChildCount();
if ( n==0 ) { // must be leaf
Token tok = ((TerminalNode)t).getSymbol();
int i = tok.getTokenIndex();
if ( i>=startIndex && i<=stopIndex && tok.getType() != Token.INVALID_TYPE ) {
leaves.add(t);
}
return;
}
for (int i = 0 ; i < n ; i++){
_getAllLeaves(t.getChild(i), leaves, startIndex, stopIndex);
}
}
java类org.antlr.v4.runtime.tree.Tree的实例源码
ParsingUtils.java 文件源码
项目:intellij-plugin-v4
阅读 26
收藏 0
点赞 0
评论 0
VisitSiblingLists.java 文件源码
项目:codebuff
阅读 25
收藏 0
点赞 0
评论 0
public void enterEveryRule(ParserRuleContext ctx) {
// Find sibling lists that are children of this parent node
Set<Class> completed = new HashSet<>(); // only count sibling list for each subtree type once
for (int i = 0; i<ctx.getChildCount(); i++) {
ParseTree child = ctx.getChild(i);
if ( completed.contains(child.getClass()) ) continue; // avoid counting repeatedly
completed.add(child.getClass());
if ( child instanceof TerminalNode ) continue; // tokens are separators at most not siblings
// found subtree child
List<? extends ParserRuleContext> siblings =
ctx.getRuleContexts(((ParserRuleContext) child).getClass());
if ( siblings.size()>1 ) { // we found a list
// check for separator by looking between first two siblings (assume all are same)
ParserRuleContext first = siblings.get(0);
ParserRuleContext second = siblings.get(1);
List<Tree> children = Trees.getChildren(ctx);
int firstIndex = children.indexOf(first);
int secondIndex = children.indexOf(second);
if ( firstIndex+1 == secondIndex ) continue; // nothing between first and second so no separator
ParseTree between = ctx.getChild(firstIndex+1);
if ( between instanceof TerminalNode ) { // is it a token?
Token separator = ((TerminalNode) between).getSymbol();
visitNonSingletonWithSeparator(ctx, siblings, separator);
}
}
}
}
VisitSiblingLists.java 文件源码
项目:codebuff
阅读 23
收藏 0
点赞 0
评论 0
public static List<Tree> getSeparators(ParserRuleContext ctx, List<? extends ParserRuleContext> siblings) {
ParserRuleContext first = siblings.get(0);
ParserRuleContext last = siblings.get(siblings.size()-1);
int start = BuffUtils.indexOf(ctx, first);
int end = BuffUtils.indexOf(ctx, last);
List<Tree> elements = Trees.getChildren(ctx).subList(start, end+1);
return BuffUtils.filter(elements, c -> c instanceof TerminalNode);
}
Trees.java 文件源码
项目:codebuff
阅读 20
收藏 0
点赞 0
评论 0
/** Save this tree in a postscript file */
public static void save(Tree t, Parser parser, String fileName)
throws IOException, PrintException
{
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
save(t, ruleNames, fileName);
}
Trees.java 文件源码
项目:codebuff
阅读 25
收藏 0
点赞 0
评论 0
/** Save this tree in a postscript file using a particular font name and size */
public static void save(Tree t, Parser parser, String fileName,
String fontName, int fontSize)
throws IOException
{
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
save(t, ruleNames, fileName, fontName, fontSize);
}
Trees.java 文件源码
项目:codebuff
阅读 26
收藏 0
点赞 0
评论 0
/** Save this tree in a postscript file using a particular font name and size */
public static void save(Tree t,
List<String> ruleNames, String fileName,
String fontName, int fontSize)
throws IOException
{
writePS(t, ruleNames, fileName, fontName, fontSize);
}
Trees.java 文件源码
项目:codebuff
阅读 25
收藏 0
点赞 0
评论 0
public static String getPS(Tree t, List<String> ruleNames,
String fontName, int fontSize)
{
TreePostScriptGenerator psgen =
new TreePostScriptGenerator(ruleNames, t, fontName, fontSize);
return psgen.getPS();
}
Trees.java 文件源码
项目:codebuff
阅读 25
收藏 0
点赞 0
评论 0
public static void writePS(Tree t, List<String> ruleNames,
String fileName,
String fontName, int fontSize)
throws IOException
{
String ps = getPS(t, ruleNames, fontName, fontSize);
FileWriter f = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(f);
try {
bw.write(ps);
}
finally {
bw.close();
}
}
TreePostScriptGenerator.java 文件源码
项目:codebuff
阅读 26
收藏 0
点赞 0
评论 0
@Override
public double getHeight(Tree tree) {
String s = getText(tree);
double h =
doc.getLineHeight() + nodeHeightPaddingAbove + nodeHeightPaddingBelow;
String[] lines = s.split("\n");
return h * lines.length;
}
TreePostScriptGenerator.java 文件源码
项目:codebuff
阅读 26
收藏 0
点赞 0
评论 0
public TreePostScriptGenerator(List<String> ruleNames, Tree root,
String fontName, int fontSize)
{
this.root = root;
setTreeTextProvider(new TreeViewer.DefaultTreeTextProvider(ruleNames));
doc = new PostScriptDocument(fontName, fontSize);
boolean compareNodeIdentities = true;
this.treeLayout =
new TreeLayout<Tree>(getTreeLayoutAdaptor(root),
new VariableExtentProvide(),
new DefaultConfiguration<Tree>(gapBetweenLevels,
gapBetweenNodes,
Configuration.Location.Bottom),
compareNodeIdentities);
}