Java Code Examples for org.antlr.v4.runtime.ParserRuleContext#getStop()

The following examples show how to use org.antlr.v4.runtime.ParserRuleContext#getStop() . 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: BNFListener.java    From openCypher with Apache License 2.0 6 votes vote down vote up
private String findHiddenTextAfter(ParserRuleContext ctx)
{
    Token endCtx = ctx.getStop();
    int i = endCtx.getTokenIndex();
    List<Token> normalTextChannel =
                tokens.getHiddenTokensToRight(i, BNFLexer.HIDDEN);
    if (normalTextChannel != null) {
        // the quasi-comment (description) may be the end of a rule or start of the next. separation is on
        // a blank line
        int nextLine = endCtx.getLine() + 1;
        List<String> content = new ArrayList<>();
        for (Token lineToken : normalTextChannel) {
            if (lineToken.getLine() == nextLine) {
                content.add(lineToken.getText().replaceFirst("// ?", ""));
                nextLine++;
            } else {
                break;
            }
        }
        return content.stream().collect(Collectors.joining("\n"));
    }
    return "";
}
 
Example 2
Source File: AbstractProtoParserListener.java    From protostuff-compiler with Apache License 2.0 6 votes vote down vote up
protected void attachComments(ParserRuleContext ctx, AbstractElement element, boolean addTrailingComment) {
    List<String> comments = new ArrayList<>();
    Token stop = ctx.getStop();
    Token start = ctx.getStart();
    List<Token> tokensBefore = tokens.getHiddenTokensToLeft(start.getTokenIndex(), HIDDEN);
    if (tokensBefore != null) {
        for (Token token : tokensBefore) {
            if (usedComments.get(token.getLine())) {
                continue;
            }
            if (token.getType() == LINE_COMMENT) {
                addCommentToList(token, comments);
            }
        }
    }
    if (addTrailingComment) {
        List<Token> tokensAfter = tokens.getHiddenTokensToRight(stop.getTokenIndex(), HIDDEN);
        findTrailingComment(tokensAfter)
                .ifPresent(token -> addCommentToList(token, comments));
    }
    List<String> trimComments = trim(comments);
    for (String comment : trimComments) {
        element.addComment(comment);
    }
}
 
Example 3
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Get start/stop of an entire rule including semi and then clean up
 *  WS at end.
 */
public static String getRuleText(CommonTokenStream tokens, ParserRuleContext ruleDefNode) {
	Token stop = ruleDefNode.getStop();
	Token semi = stop;
	TerminalNode colonNode = ruleDefNode.getToken(ANTLRv4Parser.COLON, 0);
	Token colon = colonNode.getSymbol();
	Token beforeSemi = tokens.get(stop.getTokenIndex()-1);
	Token afterColon = tokens.get(colon.getTokenIndex()+1);

	// trim whitespace/comments before / after rule text
	List<Token> ignoreBefore = tokens.getHiddenTokensToRight(colon.getTokenIndex());
	List<Token> ignoreAfter = tokens.getHiddenTokensToLeft(semi.getTokenIndex());
	Token textStart = afterColon;
	Token textStop = beforeSemi;
	if ( ignoreBefore!=null ) {
		Token lastWSAfterColon = ignoreBefore.get(ignoreBefore.size()-1);
		textStart = tokens.get(lastWSAfterColon.getTokenIndex()+1);
	}
	if ( ignoreAfter!=null ) {
		int firstWSAtEndOfRule = ignoreAfter.get(0).getTokenIndex()-1;
		textStop = tokens.get(firstWSAtEndOfRule); // stop before 1st ignore token at end
	}

	return tokens.getText(textStart, textStop);
}
 
Example 4
Source File: Rule.java    From gyro with Apache License 2.0 5 votes vote down vote up
public Rule(ParserRuleContext context) {
    if (context != null) {
        start = context.getStart();
        stop = context.getStop();

    } else {
        start = null;
        stop = null;
    }
}
 
Example 5
Source File: Location.java    From rockscript with Apache License 2.0 5 votes vote down vote up
public Location(ParserRuleContext parserRuleContext) {
  Token start = parserRuleContext.getStart();
  this.start = start.getStartIndex();
  Token stop = parserRuleContext.getStop();
  end = stop.getStopIndex();
  line = start.getLine();
}
 
Example 6
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Walk upwards from node while p.stop == token; return immediate parent
 *  if there is no ancestor stopping at token. This is the earliest
 *  right ancestor. E.g, for '}' of a block, return parent up the chain from
 *  block stopping with '}'. For '{' of block, return just block as nothing
 *  stops with '{'. (block starts with it).
 */
public static ParserRuleContext earliestAncestorEndingWithToken(TerminalNode node) {
	Token token = node.getSymbol();
	ParserRuleContext p = (ParserRuleContext)node.getParent();
	ParserRuleContext prev = null;
	while (p!=null && p.getStop()==token) {
		prev = p;
		p = p.getParent();
	}
	if ( prev==null ) {
		return (ParserRuleContext)node.getParent();
	}
	return prev;
}
 
Example 7
Source File: G4Listener.java    From openCypher with Apache License 2.0 5 votes vote down vote up
private FreeTextItem findHiddenText(ParserRuleContext ctx)
{
    // to suppress lexing, !! normal english text is a special comment //!! -> hidden
    // not sure i need to do that
    Token endAlt = ctx.getStop();
    int i = endAlt.getTokenIndex();
    List<Token> normalTextChannel = tokens.getHiddenTokensToRight(i, Gee4Lexer.HIDDEN);
    if (normalTextChannel != null) {
        // there should be only one line now
        String content = normalTextChannel.stream().map(tk -> tk.getText().replaceFirst("//!!\\s*", ""))
                .collect(Collectors.joining());
        return new FreeTextItem(content);
    }
    return null;
}
 
Example 8
Source File: HierarchyViewer.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean inBounds(ParserRuleContext ctx, int offset) {
	Token start = ctx.getStart();
	Token stop = ctx.getStop();
	if ( start!=null && stop!=null ) {
		return start.getStartIndex()<=offset && stop.getStopIndex()>=offset;
	}
	return false;
}
 
Example 9
Source File: OffsetRangeHelper.java    From kalang with MIT License 4 votes vote down vote up
public static OffsetRange getOffsetRange(ParserRuleContext tree) {
    Token start = tree.getStart();
    Token stop = tree.getStop();
    if(start==null || stop==null) return OffsetRange.NONE;
    return getOffsetRange(start, stop);
}