Java org.antlr.v4.runtime.ParserRuleContext 代码实例

・13 分钟阅读

以下是展示如何使用org.antlr.v4.runtime.ParserRuleContext的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


private Location formatLocation(ParserRuleContext ctx){
    Token tokenStart = ctx.getStart();
    Token tokenEnd = ctx.getStop();
    return new Location(file.getName(),
        tokenStart.getStartIndex(),
        tokenStart.getStopIndex() - tokenStart.getStartIndex(),
        tokenStart.getLine(),
        tokenStart.getCharPositionInLine(),
        tokenEnd.getLine(),
        tokenEnd.getCharPositionInLine());
}
 

实例 2


@CheckForNull
protected Token getStopToken(@NonNull ParserRuleContext child, @NonNull DocumentSnapshot snapshot) {
    Token result = child.getStop();
    if (result != null) {
        return result;
    }
    return ParseTrees.getStopSymbol(child);
}
 

实例 3


private void exitAnchor(ParserRuleContext ctx, int anchorId) {
    int start = anchorPositions.pop();
    Interval sourceInterval = ParseTrees.getSourceInterval(ctx);
    int stop = sourceInterval.b + 1;
    TrackingPositionRegion.Bias trackingMode = ctx.getStop() != null ? TrackingPositionRegion.Bias.Exclusive : TrackingPositionRegion.Bias.Forward;
    if (stop >= start) {
        anchors.add(createAnchor(ctx, start, stop, trackingMode, anchorId));
    }
}
 

实例 4


/**
 * If there are no statements within a block, we need a special method to grab any comments that
 * might exist between braces.
 *
 * @param node
 * @param parserRuleContext
 * @param adapterParameters
 */
public static void setInternalComments(Node node, ParserRuleContext parserRuleContext, AdapterParameters adapterParameters) {
    BufferedTokenStream tokens = adapterParameters.getTokens();
    if (node == null || parserRuleContext == null || tokens == null) {
        throw new IllegalArgumentException("Parameters must not be null");
    }
    Token startToken = parserRuleContext.getStart();
    Token stopToken = parserRuleContext.getStop();
    List<Token> commentTokens;
    List<Comment> internalCommentList = new LinkedList<Comment>();
    // Checking to the right of the start token will check inside the statement
    commentTokens = tokens.getHiddenTokensToRight(startToken.getTokenIndex(), Java7Lexer.COMMENTS);
    if (commentTokens != null) {
        for (Token commentToken : commentTokens) {
            // Skip already claimed comments (prevents comment repeats)
            if (adapterParameters.isCommentTokenClaimed(commentToken.getTokenIndex())) {
                continue;
            } else {
                // Claim it
                adapterParameters.claimCommentToken(commentToken.getTokenIndex());
            }
            if (commentToken.getText().startsWith("/**")) {
                JavadocComment javadocComment = new JavadocComment(commentToken.getText());
                internalCommentList.add(javadocComment);
            } else if (commentToken.getText().startsWith("/*")) {
                BlockComment blockComment = new BlockComment(commentToken.getText());
                internalCommentList.add(blockComment);
            } else if (commentToken.getText().startsWith("//")) {
                LineComment lineComment = new LineComment(commentToken.getText());
                internalCommentList.add(lineComment);
            }
        }
    }
    if (internalCommentList.size() > 0) {
        if (node.getInternalComments() != null) {
            node.getInternalComments().addAll(internalCommentList);
        } else {
            node.setInternalComments(internalCommentList);
        }
    }
}
 

实例 5


private InitFinalAST buildImplModuleInitFinal(ParserRuleContext ctx,
        List<ResolveParser.VariableDeclGroupContext> v, ParseTree requires,
        ParseTree ensures, Type t) {
    sanityCheckInitFinal(ctx);
    List<VariableAST> variables = new ArrayList<VariableAST>();
    for (ResolveParser.VariableDeclGroupContext grp : v) {
        variables.addAll(getAll(VariableAST.class, grp.IDENTIFIER()));
    }
    return new InitFinalAST(ctx.getStart(), ctx.getStop(), get(
            ExprAST.class, requires), get(ExprAST.class, ensures),
            variables, new ArrayList<StmtAST>(), t);
}
 

实例 6


private Iterable<Integer> getSpan(final ParserRuleContext ctx) {
  final Token startToken = ctx.getStart();
  final Token stopToken = ctx.getStop();
  if (stopToken.getLine() == startToken.getLine()) {
    return Arrays.asList(startToken.getLine() - 1, startToken.getCharPositionInLine(),
        getEndPositionInLine(stopToken));
  } else {
    return Arrays.asList(startToken.getLine() - 1, startToken.getCharPositionInLine(),
        stopToken.getLine() - 1, getEndPositionInLine(stopToken));
  }
}
 

实例 7


private String getTrailingComments(final ParserRuleContext ctx) {
  final Token stopToken = ctx.getStop();
  final List<Token> comments =
      tokens.getHiddenTokensToRight(stopToken.getTokenIndex(), Token.HIDDEN_CHANNEL);
  if (comments == null) {
    return "";
  }
  final Token nextToken =
      tokens.nextToken(comments.get(comments.size() - 1).getTokenIndex(), Token.DEFAULT_CHANNEL);
  int toIndex;
  if (nextToken == null || isCommentSkipper(nextToken)) {
    toIndex = comments.size();
  } else {
    toIndex = setLeadingComments(stopToken, nextToken, comments);
  }
  int currentLine = stopToken.getLine();
  final StringBuilder builder = new StringBuilder();
  // FIXME deal with in beetwen comments intervals and endings!
  for (final Token token : comments.subList(0, toIndex)) {
    if (token.getLine() - currentLine > 1) {
      break;
    }
    final String text = token.getText();
    if (token.getType() == LINE_COMMENT) {
      builder.append(text, 2, text.length()).append(NEW_LINE);
      currentLine = token.getLine();
    } else { // this must be COMMENT!
      builder.append(getTrimmedBlockCommentContent(text));
      currentLine = token.getLine() + getLineCount(text);
    }
  }
  return builder.toString();
}
 

实例 8


public static boolean isInContexts(@NonNull ParserRuleContext context, boolean allowGaps, @NonNull int... stack) {
    Parameters.notNull("context", context);
    Parameters.notNull("stack", stack);
    if (allowGaps) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }
    ParserRuleContext currentContext = context;
    for (int element : stack) {
        if (currentContext.getRuleIndex() != element) {
            return false;
        }
        currentContext = currentContext.getParent();
        if (currentContext == null) {
            return false;
        }
    }
    return true;
}
 

实例 9


public static boolean isInContexts(@NonNull ParserRuleContext context, boolean allowGaps, @NonNull int... stack) {
    Parameters.notNull("context", context);
    Parameters.notNull("stack", stack);
    if (allowGaps) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }
    ParserRuleContext currentContext = context;
    for (int element : stack) {
        if (currentContext.getRuleIndex() != element) {
            return false;
        }
        currentContext = currentContext.getParent();
        if (currentContext == null) {
            return false;
        }
    }
    return true;
}
 

实例 10


private void validateInsideOfDirectiveFor(ParserRuleContext ctx, String name) {
    ParserRuleContext p = ctx.getParent();
    while (p != null) {
        if (p instanceof Directive_forContext) {
            return;
        }
        if (p instanceof Directive_elseContext) {
            p = p.getParent();
        }
        p = p.getParent();
    }
    throw new SyntaxException(Errors.DIRECTIVE_OUTSIDE_OF_FOR, name).set(pos(ctx));
}
 

实例 11


private Expression toAst(TurinParser.PlaceholderUsageContext ctx) {
    ParserRuleContext parent = ctx.getParent();
    String fieldName = null;
    while (parent != null && fieldName == null) {
        if (parent instanceof TurinParser.InTypePropertyDeclarationContext) {
            fieldName = idText(((TurinParser.InTypePropertyDeclarationContext)parent).name);
        } else {
            parent = parent.getParent();
        }
    }
    if (fieldName == null) {
        return new SemanticError("A placeholder should be used only inside in-type property declarations", getPosition(ctx));
    } else {
        Placeholder placeholder = new Placeholder();
        getPositionFrom(placeholder, ctx);
        return placeholder;
    }
}
 

实例 12


private void assert_inside_of_for_directive(ParserRuleContext ctx, String name) {
    // ?????????? forStack ????????
    ParserRuleContext p = ctx.getParent();
    while (p != null) {
        if (p instanceof For_directiveContext) {
            return;
        }
        if (p instanceof Else_directiveContext) {
            // ?????  for-else ???, ??????
            // ?????? if-else ???, ??????? #if?????
            p = p.getParent();
        }
        p = p.getParent();
    }
    throw reportError(name + " cannot be used outside of a #for directive", ctx);
}
 

实例 13


private ParserRuleContext parent(ParserRuleContext ctx, int level) {
    for (int i = 0; i < level; i++) {
        if (ctx != null) {
            ctx = ctx.getParent();
        }
    }
    return ctx;
}
 

实例 14


protected static <T extends ParserRuleContext> T replace(ParserRuleContext ctx1, T ctx2) {
    ParserRuleContext parent = ctx1.getParent();
    int index = parent.children.indexOf(ctx1);
    ctx2.parent = parent;
    parent.children.set(index, ctx2);
    return ctx2;
}
 

实例 15


/**
 * Return parent `nval` levels 上面 ctx.
 *
 * @param ctx Child node
 * @param nval 'n' value, number of levels to go up the tree
 * @return Parent node or null if parent does not exist
 */
public static ParserRuleContext getNthParent(ParserRuleContext ctx, int nval) {
    if (ctx == null) {
        return null;
    }
    while (nval != 0) {
        nval--;
        ctx = ctx.getParent();
        if (ctx == null) {
            return null;
        }
    }
    return ctx;
}
 
讨论
淘淘あ西西 profile image