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

The following examples show how to use org.antlr.runtime.Token#getTokenIndex() . 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: InternalHighlightingParser.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void announce(Token start, Token stop, AbstractElement element) {
	if (start != null && start != Token.EOF_TOKEN) {
		if (start == stop) {
			announce(start, element);
		} else {
			CommonToken castedStart = (CommonToken) start;
			if (stop == null) { // possible error condition
				if (start.getTokenIndex() == state.lastErrorIndex) {
					return;
				}
			}
			CommonToken castedEnd = (CommonToken) stop;
			Integer newType = rewriter.rewrite(castedStart, element);
			if (newType != null && castedEnd != null && castedEnd != Token.EOF_TOKEN) {
				LazyTokenStream castedInput = (LazyTokenStream) this.input;
				for (int i = castedStart.getTokenIndex() + 1; i < castedEnd.getTokenIndex(); i++) {
					Token token = castedInput.get(i);
					if (token.getChannel() != Token.HIDDEN_CHANNEL)
						token.setType(newType);
				}
				castedEnd.setType(newType);
			}
		}
	}
}
 
Example 2
Source File: SemicolonInjectionHelper.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns true if there was an unexpected EOL.
 */
public static boolean hasDisallowedEOL(Callback callback) {
	TokenStream input = callback.getInput();
	Token lt = input.LT(1);

	// Start on the position before the current token and scan backwards off channel tokens until the previous on
	// channel token.
	for (int ix = lt.getTokenIndex() - 1; ix > 0; ix--) {
		lt = input.get(ix);
		if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
			// On channel token found: stop scanning.
			break;
		} else if (isSemicolonEquivalent(lt)) {
			return true;
		}
	}
	return false;
}
 
Example 3
Source File: LazyTokenStream.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public int LA(int i) {
	Token lookaheadToken = LT(i);
	if (markerCount > 0) { // predicting with a marker
		if (Token.EOF_TOKEN == lookaheadToken) { // predicated past EOF
			if (indexOfLookAhead != size()) {
				indexOfLookAhead = size();
				myLookAhead++;
			}
		} else {
			int laTokenIndex = lookaheadToken.getTokenIndex();
			if (indexOfLookAhead < laTokenIndex) {
				indexOfLookAhead = laTokenIndex;
				myLookAhead++;
			}
		}
	} else {
		myLookAhead = Math.max(i, myLookAhead);
	}
	// return super.LA(i); // inlined
	return lookaheadToken.getType();
}
 
Example 4
Source File: SemicolonInjectionHelper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * <p>
 * Promotes EOL which may lead to an automatically inserted semicolon. This is probably the most important method
 * for automatic semicolon insertion, as it is only possible to insert a semicolon in case of line breaks (even if
 * they are hidden in a multi-line comment!).
 * </p>
 */
public static void promoteEOL(Callback callback) {
	RecognizerSharedState state = callback.getState();
	TokenStream input = callback.getInput();
	// Don't promote EOL if there was a syntax error at EOF
	if (state.lastErrorIndex == input.size()) {
		return;
	}
	// Get current token and its type (the possibly offending token).
	Token prev = input.LT(-1);
	Token next = input.LT(1);
	int la = next.getType();
	if (la == InternalN4JSParser.Semicolon) {
		return;
	}

	// Promoting an EOL means switching it from off channel to on channel.
	// A ML_COMMENT gets promoted when it contains an EOL.
	for (int idx = prev == null ? 0 : prev.getTokenIndex() + 1, max = la == Token.EOF ? input.size()
			: next.getTokenIndex(); idx < max; idx++) {
		Token lt = input.get(idx);
		if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
			// On channel token found: stop scanning (previously promoted)
			break;
		} else if (isSemicolonEquivalent(lt)) {
			// We found our EOL: promote the token to on channel, position the input on it and reset the rule
			// start.
			lt.setChannel(Token.DEFAULT_CHANNEL);
			input.seek(idx);
			break;
		}
	}
}
 
Example 5
Source File: ProgressingTokenStream.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void limitReadCalls(Token t) throws RuntimeException {
    int index = t.getTokenIndex();
    if (index > highestReachedIndex) {
        highestReachedIndex = index;
        readCalls = 0;
    }
    readCalls++;
    if (readCalls > maxReadCalls) {
        throw new ProgressingFailedException("Excessive read calls");
    }
}
 
Example 6
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendAllTokens() {
	for (int x = lastConsumedIndex + 1; input.size() > x; input.consume(), x++) {
		Token hidden = input.get(x);
		createLeafNode(hidden, null);
		lastConsumedIndex = hidden.getTokenIndex();
	}
	if (currentError != null) {
		appendError(getLastLeafNode());
	}
}
 
Example 7
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendTrailingHiddenTokens() {
	Token tokenBefore = input.LT(-1);
	int size = input.size();
	if (tokenBefore != null && tokenBefore.getTokenIndex() < size) {
		for (int x = tokenBefore.getTokenIndex() + 1; x < size; x++) {
			Token hidden = input.get(x);
			createLeafNode(hidden, null);
			lastConsumedIndex = hidden.getTokenIndex();
		}
	}
}
 
Example 8
Source File: AbstractInternalAntlrParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void newLeafNode(Token token, EObject grammarElement) {
	if (token == null)
		return;

	final int tokenIndex = token.getTokenIndex();
	if (tokenIndex > lastConsumedIndex) {
		for (int x = lastConsumedIndex + 1; x < tokenIndex; x++) {
			Token hidden = input.get(x);
			createLeafNode(hidden, null);
		}
		lastConsumedIndex = tokenIndex;
		lastConsumedNode = createLeafNode(token, grammarElement);
	}
}
 
Example 9
Source File: XtextTokenStream.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.22
 */
protected int getTokenIndex(Token tok) {
	if (tok == Token.EOF_TOKEN) {
		return size();
	}
	return tok.getTokenIndex();
}
 
Example 10
Source File: DRL6StrictParser.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private boolean parseEvalExpression(EvalDescrBuilder<?> eval) throws RecognitionException {
    match(input,
            DRL6Lexer.LEFT_PAREN,
            null,
            null,
            DroolsEditorType.SYMBOL);
    if (state.failed)
        return false;

    if (state.backtracking == 0) {
        helper.emit(Location.LOCATION_LHS_INSIDE_EVAL);
    }

    int idx = input.index();
    final String expr;
    try {
        expr = conditionalExpression();
    } catch (RecognitionException e) {
        final Token tempToken = helper.getLastTokenOnList(helper.getEditorInterface().getLast().getContent());
        if (tempToken != null) {
            for (int i = tempToken.getTokenIndex() + 1; i < input.size(); i++) {
                final Token token = input.get(i);
                if (token.getType() == DRL6Lexer.EOF) {
                    break;
                }
                helper.emit(token, DroolsEditorType.CODE_CHUNK);
            }
        }

        throw e;
    }

    if (state.backtracking == 0) {
        eval.constraint(expr);
    }

    match(input,
            DRL6Lexer.RIGHT_PAREN,
            null,
            null,
            DroolsEditorType.SYMBOL);
    if (state.failed)
        return false;

    helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
    return true;
}
 
Example 11
Source File: DRL6Parser.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private boolean parseEvalExpression(EvalDescrBuilder<?> eval) throws RecognitionException {
    match(input,
            DRL6Lexer.LEFT_PAREN,
            null,
            null,
            DroolsEditorType.SYMBOL);
    if (state.failed)
        return false;

    if (state.backtracking == 0) {
        helper.emit(Location.LOCATION_LHS_INSIDE_EVAL);
    }

    int idx = input.index();
    final String expr;
    try {
        expr = conditionalExpression();
    } catch (RecognitionException e) {
        final Token tempToken = helper.getLastTokenOnList(helper.getEditorInterface().getLast().getContent());
        if (tempToken != null) {
            for (int i = tempToken.getTokenIndex() + 1; i < input.size(); i++) {
                final Token token = input.get(i);
                if (token.getType() == DRL6Lexer.EOF) {
                    break;
                }
                helper.emit(token, DroolsEditorType.CODE_CHUNK);
            }
        }

        throw e;
    }

    if (state.backtracking == 0) {
        eval.constraint(expr);
    }

    match(input,
            DRL6Lexer.RIGHT_PAREN,
            null,
            null,
            DroolsEditorType.SYMBOL);
    if (state.failed)
        return false;

    helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
    return true;
}
 
Example 12
Source File: DRL5Parser.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private boolean parseEvalExpression(EvalDescrBuilder<?> eval) throws RecognitionException {
    match( input,
           DRL5Lexer.LEFT_PAREN,
           null,
           null,
           DroolsEditorType.SYMBOL );
    if ( state.failed ) return false;

    if ( state.backtracking == 0 ) {
        helper.emit( Location.LOCATION_LHS_INSIDE_EVAL );
    }

    int idx = input.index();
    final String expr;
    try{
        expr = conditionalExpression();
    } catch (RecognitionException e){
        final Token tempToken = helper.getLastTokenOnList(helper.getEditorInterface().getLast().getContent());
        if (tempToken != null){
            for (int i = tempToken.getTokenIndex() + 1; i < input.size(); i++) {
                final Token token = input.get(i);
                if (token.getType() == DRL5Lexer.EOF){
                    break;
                }
                helper.emit(token, DroolsEditorType.CODE_CHUNK);
            }
        }

        throw e;
    }


    if ( state.backtracking == 0 ) {
        eval.constraint( expr );
    }

    match( input,
           DRL5Lexer.RIGHT_PAREN,
           null,
           null,
           DroolsEditorType.SYMBOL );
    if ( state.failed ) return false;

    helper.emit( Location.LOCATION_LHS_BEGIN_OF_CONDITION );
    return true;
}