Java Code Examples for org.antlr.v4.runtime.Token#getStartIndex()

The following examples show how to use org.antlr.v4.runtime.Token#getStartIndex() . 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: SqlParser.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void exitNonReserved(SqlBaseParser.NonReservedContext context)
{
    // we can't modify the tree during rule enter/exit event handling unless we're dealing with a terminal.
    // Otherwise, ANTLR gets confused an fires spurious notifications.
    if (!(context.getChild(0) instanceof TerminalNode)) {
        int rule = ((ParserRuleContext) context.getChild(0)).getRuleIndex();
        throw new AssertionError("nonReserved can only contain tokens. Found nested rule: " + ruleNames.get(rule));
    }

    // replace nonReserved words with IDENT tokens
    context.getParent().removeLastChild();

    Token token = (Token) context.getChild(0).getPayload();
    Token newToken = new CommonToken(
            new Pair<>(token.getTokenSource(), token.getInputStream()),
            SqlBaseLexer.IDENTIFIER,
            token.getChannel(),
            token.getStartIndex(),
            token.getStopIndex());

    context.getParent().addChild(parser.createTerminalNode(context.getParent(), newToken));
}
 
Example 2
Source File: CumulusConcatenatedControlPlaneExtractor.java    From batfish with Apache License 2.0 6 votes vote down vote up
private void parsePortsFile() {
  int end = _text.indexOf(START_OF_FRR_FILE);
  String text = end > 0 ? _text.substring(0, end) : _text;

  CumulusPortsCombinedParser parser =
      new CumulusPortsCombinedParser(text, _grammarSettings, _line, _offset);
  Cumulus_ports_configurationContext ctxt = parser.parse();
  checkErrors(parser);
  ParseTreeWalker walker = new BatfishParseTreeWalker(parser);
  CumulusPortsConfigurationBuilder cb =
      new CumulusPortsConfigurationBuilder(_configuration, parser, _w);
  walker.walk(cb, ctxt);
  mergeParseTree(ctxt, parser);

  Token startOfInterfacesFile = ctxt.getStop();
  _line = startOfInterfacesFile.getLine();
  _offset = startOfInterfacesFile.getStartIndex();
}
 
Example 3
Source File: ANTLRAssistBehavior.java    From onedev with MIT License 6 votes vote down vote up
@Override
protected int getAnchor(String content) {
	List<Token> tokens = codeAssist.getGrammar().lex(content);
	if (tokens.isEmpty()) {
		return 0;
	} else {
		Token lastToken = tokens.get(tokens.size()-1);
		String contentAfterLastToken = content.substring(lastToken.getStopIndex()+1);
		if (contentAfterLastToken.length() > 0) {
			contentAfterLastToken = StringUtils.trimStart(contentAfterLastToken);
			return content.length() - contentAfterLastToken.length();
		} else {
			return lastToken.getStartIndex();
		}
	}
}
 
Example 4
Source File: SyntaxError.java    From gyro with Apache License 2.0 6 votes vote down vote up
public SyntaxError(GyroCharStream stream, String message, Token token) {
    this.stream = stream;
    this.message = message;
    this.line = token.getLine() - 1;
    this.startColumn = token.getCharPositionInLine();

    int start = token.getStartIndex();
    int stop = token.getStopIndex();

    if (start >= 0 && stop >= 0 && stop > start) {
        this.stopColumn = this.startColumn + stop - start;

    } else {
        this.stopColumn = this.startColumn;
    }
}
 
Example 5
Source File: HashemNodeFactory.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
/**
 * Returns an {@link HashemTaNode} for the given parameters.
 *
 * @param whileToken The token containing the while node's info
 * @param conditionNode The conditional node for this while loop
 * @param bodyNode The body of the while loop
 * @return A SLWhileNode built using the given parameters. null if either conditionNode or
 *         bodyNode is null.
 */
public HashemStatementNode createWhile(Token whileToken, HashemExpressionNode conditionNode, HashemStatementNode bodyNode) {
    if (conditionNode == null || bodyNode == null) {
        return null;
    }

    conditionNode.addStatementTag();
    final int start = whileToken.getStartIndex();
    final int end = bodyNode.getSourceEndIndex();
    final HashemTaNode whileNode = new HashemTaNode(conditionNode, bodyNode);
    whileNode.setSourceSection(start, end - start);
    return whileNode;
}
 
Example 6
Source File: RefactorUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Token getTokenForCharIndex(TokenStream tokens, int charIndex) {
	for (int i=0; i<tokens.size(); i++) {
		Token t = tokens.get(i);
		if ( charIndex>=t.getStartIndex() && charIndex<=t.getStopIndex() ) {
			return t;
		}
	}
	return null;
}
 
Example 7
Source File: ErrorStrategyAdaptor.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** By default ANTLR makes the start/stop -1/-1 for invalid tokens
 *  which is reasonable but here we want to highlight the
 *  current position indicating that is where we lack a token.
 *  if no input, highlight at position 0.
 */
protected Token getMissingSymbol(Parser recognizer) {
	Token missingSymbol = super.getMissingSymbol(recognizer);
	// alter the default missing symbol.
	if ( missingSymbol instanceof CommonToken) {
		int start, stop;
		Token current = recognizer.getCurrentToken();
		start = current.getStartIndex();
		stop = current.getStopIndex();
		((CommonToken) missingSymbol).setStartIndex(start);
		((CommonToken) missingSymbol).setStopIndex(stop);
	}
	return missingSymbol;
}
 
Example 8
Source File: MySQLDDLVisitor.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private ModifyColumnDefinitionSegment extractModifyColumnDefinition(final Token start, final Token stop, 
                                                                    final ColumnDefinitionContext columnDefinition, final FirstOrAfterColumnContext firstOrAfterColumn) {
    ModifyColumnDefinitionSegment result = new ModifyColumnDefinitionSegment(start.getStartIndex(), stop.getStopIndex(),
            (ColumnDefinitionSegment) visit(columnDefinition));
    if (null != firstOrAfterColumn) {
        result.setColumnPosition(getColumnPositionSegment(result.getColumnDefinition(), (ColumnPositionSegment) visit(firstOrAfterColumn)));
    }
    return result;
}
 
Example 9
Source File: ExprGenerator.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static byte[] getBytes(TerminalNode rule) {
    Token token = rule.getSymbol();
    byte[] bytes = new byte[token.getStopIndex() - token.getStartIndex() - 1];
    CharStream cs = token.getInputStream();
    int pos = cs.index();
    cs.seek(token.getStartIndex() + 1);
    int j = 0;
    for (int i = 0; i < bytes.length; i++) {
        int ch = cs.LA(i + 1);
        if (ch == '\\') {
            i++;
            ch = cs.LA(i + 1);
            if (ch == '0') {
                ch = 0;
            }
            else if (ch == 'n') {
                ch = '\n';
            }
            else if (ch == 'r') {
                ch = '\r';
            }
            else if (ch == 'Z') {
                ch = '\032';
            }
        }
        bytes[j] = (byte) ch;
        j++;
    }
    cs.seek(pos);
    if (j != bytes.length) {
        // esacpe characters
        byte[] old = bytes;
        bytes = new byte[j];
        System.arraycopy(old, 0, bytes, 0, j);
    }
    return bytes;
}
 
Example 10
Source File: ExprGenerator.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
static String getString(TerminalNode rule, Decoder decoder) {
    Token token = rule.getSymbol();
    CharStream cs = token.getInputStream();
    int pos = cs.index();
    cs.seek(token.getStartIndex() + 1);
    int len = token.getStopIndex() - token.getStartIndex() - 1;
    IntSupplier supplier = new IntSupplier() {
        int i = 0;
        
        @Override
        public int getAsInt() {
            if (i >= len) {
                return -1;
            }
            int ch = cs.LA(i + 1);
            if (ch == '\\') {
                i++;
                ch = cs.LA(i + 1);
                if (ch == '0') {
                    ch = 0;
                }
                else if (ch == 'n') {
                    ch = '\n';
                }
                else if (ch == 'r') {
                    ch = '\r';
                }
                else if (ch == 'Z') {
                    ch = '\032';
                }
            }
            i++;
            return ch;
        }
    };
    String result = decoder.toString(supplier);
    cs.seek(pos);
    return result;
}
 
Example 11
Source File: OffsetRangeHelper.java    From kalang with MIT License 5 votes vote down vote up
public static OffsetRange getOffsetRange(Token start, Token stop) {
    OffsetRange offset = new OffsetRange();
    offset.startOffset = start.getStartIndex();
    offset.stopOffset = stop.getStopIndex();
    offset.startLine = start.getLine();
    offset.startLineColumn = start.getCharPositionInLine();
    offset.stopLine = stop.getLine();
    offset.stopLineColumn = stop.getCharPositionInLine();
    return offset;
}
 
Example 12
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 13
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 14
Source File: HashemNodeFactory.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
/**
 * Returns an {@link HashemInvokeNode} for the given parameters.
 *
 * @param functionNode The function being called
 * @param parameterNodes The parameters of the function call
 * @param finalToken A token used to determine the end of the sourceSelection for this call
 * @return An SLInvokeNode for the given parameters. null if functionNode or any of the
 *         parameterNodes are null.
 */
public HashemExpressionNode createCall(HashemExpressionNode functionNode, List<HashemExpressionNode> parameterNodes, Token finalToken) {
    if (functionNode == null || containsNull(parameterNodes)) {
        return null;
    }

    final HashemExpressionNode result = new HashemInvokeNode(functionNode, parameterNodes.toArray(new HashemExpressionNode[parameterNodes.size()]));

    final int startPos = functionNode.getSourceCharIndex();
    final int endPos = finalToken.getStartIndex() + finalToken.getText().length();
    result.setSourceSection(startPos, endPos - startPos);
    result.addExpressionTag();

    return result;
}
 
Example 15
Source File: HashemNodeFactory.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
/**
 * Returns an {@link HashemBedeNode} for the given parameters.
 *
 * @param t The token containing the return node's info
 * @param valueNode The value of the return (null if not returning a value)
 * @return An SLReturnNode for the given parameters.
 */
public HashemStatementNode createReturn(Token t, HashemExpressionNode valueNode) {
    final int start = t.getStartIndex();
    final int length = valueNode == null ? t.getText().length() : valueNode.getSourceEndIndex() - start;
    final HashemBedeNode returnNode = new HashemBedeNode(valueNode);
    returnNode.setSourceSection(start, length);
    return returnNode;
}
 
Example 16
Source File: HashemNodeFactory.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
/**
 * Returns an {@link HashemAgeNode} for the given parameters.
 *
 * @param ifToken The token containing the if node's info
 * @param conditionNode The condition node of this if statement
 * @param thenPartNode The then part of the if
 * @param elsePartNode The else part of the if (null if no else part)
 * @return An SLIfNode for the given parameters. null if either conditionNode or thenPartNode is
 *         null.
 */
public HashemStatementNode createIf(Token ifToken, HashemExpressionNode conditionNode, HashemStatementNode thenPartNode, HashemStatementNode elsePartNode) {
    if (conditionNode == null || thenPartNode == null) {
        return null;
    }

    conditionNode.addStatementTag();
    final int start = ifToken.getStartIndex();
    final int end = elsePartNode == null ? thenPartNode.getSourceEndIndex() : elsePartNode.getSourceEndIndex();
    final HashemAgeNode ifNode = new HashemAgeNode(conditionNode, thenPartNode, elsePartNode);
    ifNode.setSourceSection(start, end - start);
    return ifNode;
}
 
Example 17
Source File: MatchingHighlighter.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void highlightMatched(int offset, String p) {
    List<Token> latestTokenList = smartDocumentFilter.getLatestTokenList();
    Tuple3<Integer, Integer, Boolean> tokenTypeTuple = PAREN_MAP.get(p);
    int triggerTokenType = tokenTypeTuple.getV1();
    int matchedTokenType = tokenTypeTuple.getV2();
    boolean normalOrder = tokenTypeTuple.getV3();
    Deque<Tuple2<Token, Boolean>> stack = new ArrayDeque<>();

    Token triggerToken = null;
    Token matchedToken = null;

    for (ListIterator<Token> iterator = latestTokenList.listIterator(normalOrder ? 0 : latestTokenList.size());
         normalOrder ? iterator.hasNext() : iterator.hasPrevious(); ) {
        Token token = normalOrder ? iterator.next() : iterator.previous();

        int tokenType = token.getType();
        if (tokenType == triggerTokenType) {
            Boolean triggerFlag = offset == token.getStartIndex();

            stack.push(tuple(token, triggerFlag));
        } else if (tokenType == matchedTokenType) {
            if (!stack.isEmpty()) {
                Tuple2<Token, Boolean> tokenAndTriggerFlagTuple = stack.pop();
                if (tokenAndTriggerFlagTuple.getV2()) {
                    triggerToken = tokenAndTriggerFlagTuple.getV1();
                    matchedToken = token;
                    break;
                }
            }
        }
    }

    if (null != triggerToken && null != matchedToken) {
        highlightToken(p, triggerToken);
        highlightToken(p, matchedToken);
        try {
            highlightedTokenInfoList = Arrays.asList(
                    tuple(triggerToken.getType(), doc.createPosition(triggerToken.getStartIndex())),
                    tuple(matchedToken.getType(), doc.createPosition(matchedToken.getStartIndex()))
            );
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}
 
Example 18
Source File: XmlTerminal.java    From manifold with Apache License 2.0 4 votes vote down vote up
XmlTerminal( Token t, XmlPart parent )
{
  super( parent, t.getStartIndex(), t.getStopIndex() - t.getStartIndex() + 1, t.getLine() );
  _text = t.getText();
}
 
Example 19
Source File: AbstractLexerTest.java    From jdmn with Apache License 2.0 4 votes vote down vote up
public int getBeginOffset(Token token) {
    return token.getStartIndex();
}
 
Example 20
Source File: REPLShell.java    From Concurnas with MIT License 4 votes vote down vote up
@Override
public AttributedString highlight(LineReader reader, String buffer) {
	
	LexParseErrorCapturer errs = new LexParseErrorCapturer("");
	
	CharStream input = CharStreams.fromString(buffer, "");
	ConcurnasLexer lexer = new ConcurnasLexer(input);
	lexer.removeErrorListeners();
	lexer.addErrorListener(errs);
	
	List<? extends Token> toks = lexer.getAllTokens();
	if(toks.isEmpty() || !errs.errors.isEmpty()) {
		return AttributedString.fromAnsi(buffer);//do nothing
	}
	
	int cursor = 0;
	
	StringBuilder sb = new StringBuilder();
	for(Token tok : toks) {
		int startIdx = tok.getStartIndex();
		while(cursor++ < startIdx-1) {
			sb.append(" ");//add whitespace padding
		}
		
		String tokenName = ConcurnasLexer.VOCABULARY.getDisplayName(tok.getType());
		
		int color = Color_Default;
		if(null != tokenName && tokenToColor.containsKey(tokenName)) {
			color = tokenToColor.get(tokenName);
		}

		boolean escaped = buffer.charAt(startIdx) == '\\';
		
		sb.append(String.format("\u001b[38;5;%sm%s", color, (escaped?"\\" : "") + tok.getText()));
		cursor = tok.getStopIndex();
	}
	
	int len = buffer.length();
	while(cursor++ < len-1) {
		sb.append(" ");//trailing spaces
	}
	return AttributedString.fromAnsi(sb.toString());
}