Java Code Examples for org.antlr.v4.runtime.ParserRuleContext#getChild()
The following examples show how to use
org.antlr.v4.runtime.ParserRuleContext#getChild() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Trainer.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** Walk upwards from node until we find a child of p at t's char position. * Don't see alignment with self, t, or element *after* us. * return null if there is no such ancestor p. */ public static Pair<ParserRuleContext,Integer> earliestAncestorWithChildStartingAtCharPos(ParserRuleContext node, Token t, int charpos) { ParserRuleContext p = node; while ( p!=null ) { // check all children of p to see if one of them starts at charpos for (int i = 0; i<p.getChildCount(); i++) { ParseTree child = p.getChild(i); Token start; if ( child instanceof ParserRuleContext ) { start = ((ParserRuleContext) child).getStart(); } else { // must be token start = ((TerminalNode)child).getSymbol(); } // check that we don't see alignment with self or element *after* us if ( start.getTokenIndex()<t.getTokenIndex() && start.getCharPositionInLine()==charpos ) { return new Pair<>(p,i); } } p = p.getParent(); } return null; }
Example 2
Source File: RootDocInfo.java From bookish with MIT License | 5 votes |
public RootDocInfo(Artifact artifact, Parser parser, ParserRuleContext tree) { super(artifact, parser, tree); this.parser = parser; this.tree = tree; ParseTree rootTag = tree.getChild(0); if ( rootTag instanceof BookishParser.BookContext ) { attributes = ((BookishParser.BookContext)rootTag).attrs().attributes; } else { if ( ((BookishParser.ArticleContext)rootTag).attrs()!=null ) { attributes = ((BookishParser.ArticleContext) rootTag).attrs().attributes; } } }
Example 3
Source File: ProgramParser.java From yql-plus with Apache License 2.0 | 5 votes |
private String assignAlias(String alias, ParserRuleContext node, Scope scope) { if (alias == null) { alias = "source"; } if (node != null && node instanceof yqlplusParser.Alias_defContext) { //alias_def : (AS? ID); ParseTree idChild = node; if (node.getChildCount() > 1) { idChild = node.getChild(1); } alias = idChild.getText(); if (scope.isCursor(alias)) { throw new ProgramCompileException(toLocation(scope, idChild), "Source alias '%s' is already used", alias); } scope.defineDataSource(toLocation(scope, idChild), alias, true); return alias; } else { String candidate = alias; int c = 0; while (scope.isCursor(candidate)) { candidate = alias + (++c); } scope.defineDataSource(null, candidate); return alias; } }
Example 4
Source File: VisitSiblingLists.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
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); } } } }
Example 5
Source File: Formatter.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public void align(int alignOrIndent, TerminalNode node) { int[] deltaChild = Trainer.triple(alignOrIndent); int deltaFromAncestor = deltaChild[0]; int childIndex = deltaChild[1]; ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken(node); ParserRuleContext ancestor = Trainer.getAncestor(earliestLeftAncestor, deltaFromAncestor); Token start = null; if ( ancestor==null ) { // System.err.println("Whoops. No ancestor at that delta"); } else { ParseTree child = ancestor.getChild(childIndex); if (child instanceof ParserRuleContext) { start = ((ParserRuleContext) child).getStart(); } else if (child instanceof TerminalNode) { start = ((TerminalNode) child).getSymbol(); } else { // uh oh. // System.err.println("Whoops. Tried to access invalid child"); } } if ( start!=null ) { int indentCol = start.getCharPositionInLine(); charPosInLine = indentCol; output.append(Tool.spaces(indentCol)); } }
Example 6
Source File: BuffUtils.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public static int indexOf(ParserRuleContext parent, ParseTree child) { for (int i = 0; i<parent.getChildCount(); i++) { if ( parent.getChild(i)==child ) { return i; } } return -1; }
Example 7
Source File: ProgramParser.java From vespa with Apache License 2.0 | 5 votes |
private String assignAlias(String alias, ParserRuleContext node, Scope scope) { if (alias == null) { alias = "source"; } if (node != null && node instanceof yqlplusParser.Alias_defContext) { //alias_def : (AS? ID); ParseTree idChild = node; if (node.getChildCount() > 1) { idChild = node.getChild(1); } alias = idChild.getText(); if (scope.isCursor(alias)) { throw new ProgramCompileException(toLocation(scope, idChild), "Source alias '%s' is already used", alias); } scope.defineDataSource(toLocation(scope, idChild), alias); return alias; } else { String candidate = alias; int c = 0; while (scope.isCursor(candidate)) { candidate = alias + (++c); } scope.defineDataSource(null, candidate); return alias; } }
Example 8
Source File: CSSParserVisitorImpl.java From jStyleParser with GNU Lesser General Public License v3.0 | 5 votes |
/** * check if rule context contains error node * * @param ctx rule context * @return contains context error node */ private boolean ctxHasErrorNode(ParserRuleContext ctx) { for (int i = 0; i < ctx.children.size(); i++) { if (ctx.getChild(i) instanceof ErrorNode) { return true; } } return false; }
Example 9
Source File: Formatter.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
public void indent(int indentCat, TerminalNode node) { int tokenIndexInStream = node.getSymbol().getTokenIndex(); List<Token> tokensOnPreviousLine = getTokensOnPreviousLine(testDoc.tokens, tokenIndexInStream, line); Token firstTokenOnPrevLine = null; if ( tokensOnPreviousLine.size()>0 ) { firstTokenOnPrevLine = tokensOnPreviousLine.get(0); } if ( indentCat==CAT_INDENT ) { if ( firstTokenOnPrevLine!=null ) { // if not on first line, we cannot indent int indentedCol = firstTokenOnPrevLine.getCharPositionInLine()+indentSize; charPosInLine = indentedCol; output.append(Tool.spaces(indentedCol)); } else { // no prev token? ok, just indent from left edge charPosInLine = indentSize; output.append(Tool.spaces(indentSize)); } return; } int[] deltaChild = Trainer.unindentcat(indentCat); int deltaFromAncestor = deltaChild[0]; int childIndex = deltaChild[1]; ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken(node); ParserRuleContext ancestor = Trainer.getAncestor(earliestLeftAncestor, deltaFromAncestor); Token start = null; if ( ancestor==null ) { // System.err.println("Whoops. No ancestor at that delta"); } else { ParseTree child = ancestor.getChild(childIndex); if ( child instanceof ParserRuleContext ) { start = ((ParserRuleContext) child).getStart(); } else if ( child instanceof TerminalNode ) { start = ((TerminalNode) child).getSymbol(); } else { // uh oh. // System.err.println("Whoops. Tried to access invalid child"); } } if ( start!=null ) { int indentCol = start.getCharPositionInLine()+indentSize; charPosInLine = indentCol; output.append(Tool.spaces(indentCol)); } }