Example usage for org.antlr.v4.runtime Parser getInputStream

List of usage examples for org.antlr.v4.runtime Parser getInputStream

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Parser getInputStream.

Prototype

@Override
    public TokenStream getInputStream() 

Source Link

Usage

From source file:com.huawei.streaming.cql.semanticanalyzer.parser.CQLErrorStrategy.java

License:Apache License

/**
 * {@inheritDoc}/*from w  ww.  ja v  a 2 s .  c  om*/
 */
@Override
public void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input;
    if (tokens instanceof TokenStream) {
        if (e.getStartToken().getType() == Token.EOF)
            input = "<EOF>";
        else
            input = getText(tokens, e.getStartToken(), e.getOffendingToken());
    } else {
        input = "<unknown input>";
    }
    String msg = "no viable alternative at input " + escapeWSAndQuote(input);
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * {@inheritDoc}//from  w w w.  ja v  a  2 s. c  o  m
 *
 * <p>
 * The default implementation resynchronizes the parser by consuming tokens
 * until we find one in the resynchronization set--loosely the set of tokens
 * that can follow the current rule.
 * </p>
 */
@Override
public void recover(Parser recognizer, RecognitionException e) {
    // System.out.println("recover in "+recognizer.getRuleInvocationStack()+
    // " index="+recognizer.getInputStream().index()+
    // ", lastErrorIndex="+
    // lastErrorIndex+
    // ", states="+lastErrorStates);
    if (lastErrorIndex == recognizer.getInputStream().index() && lastErrorStates != null
            && lastErrorStates.contains(recognizer.getState())) {
        // uh oh, another error at same token index and previously-visited
        // state in ATN; must be a case where LT(1) is in the recovery
        // token set so nothing got consumed. Consume a single token
        // at least to prevent an infinite loop; this is a failsafe.
        // System.err.println("seen error condition before index="+
        // lastErrorIndex+", states="+lastErrorStates);
        // System.err.println("FAILSAFE consumes "+recognizer.getTokenNames()[recognizer.getInputStream().LA(1)]);
        recognizer.consume();
    }
    lastErrorIndex = recognizer.getInputStream().index();
    if (lastErrorStates == null)
        lastErrorStates = new IntervalSet();
    lastErrorStates.add(recognizer.getState());
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * The default implementation of {@link ANTLRErrorStrategy#sync} makes sure
 * that the current lookahead symbol is consistent with what were expecting
 * at this point in the ATN. You can call this anytime but ANTLR only
 * generates code to check before subrules/loops and each iteration.
 *
 * <p>//from   ww  w . j a v a  2 s.  co  m
 * Implements Jim Idle's magic sync mechanism in closures and optional
 * subrules. E.g.,
 * </p>
 *
 * <pre>
 * a : sync ( stuff sync )* ;
 * sync : {consume to what can follow sync} ;
 * </pre>
 *
 * At the start of a sub rule upon error, {@link #sync} performs single
 * token deletion, if possible. If it can't do that, it bails on the current
 * rule and uses the default error recovery, which consumes until the
 * resynchronization set of the current rule.
 *
 * <p>
 * If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block
 * with an empty alternative), then the expected set includes what follows
 * the subrule.
 * </p>
 *
 * <p>
 * During loop iteration, it consumes until it sees a token that can start a
 * sub rule or what follows loop. Yes, that is pretty aggressive. We opt to
 * stay in the loop as long as possible.
 * </p>
 *
 * <p>
 * <strong>ORIGINS</strong>
 * </p>
 *
 * <p>
 * Previous versions of ANTLR did a poor job of their recovery within loops.
 * A single mismatch token or missing token would force the parser to bail
 * out of the entire rules surrounding the loop. So, for rule
 * </p>
 *
 * <pre>
 * classDef : 'class' ID '{' member* '}'
 * </pre>
 *
 * input with an extra token between members would force the parser to
 * consume until it found the next class definition rather than the next
 * member definition of the current class.
 *
 * <p>
 * This functionality cost a little bit of effort because the parser has to
 * compare token set at the start of the loop and at each iteration. If for
 * some reason speed is suffering for you, you can turn off this
 * functionality by simply overriding this method as a blank { }.
 * </p>
 */
@Override
public void sync(Parser recognizer) throws RecognitionException {
    ATNState s = recognizer.getInterpreter().atn.states.get(recognizer.getState());
    // System.err.println("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName());
    // If already recovering, don't try to sync
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }

    TokenStream tokens = recognizer.getInputStream();
    int la = tokens.LA(1);

    // try cheaper subset first; might get lucky. seems to shave a wee bit
    // off
    if (recognizer.getATN().nextTokens(s).contains(la) || la == Token.EOF)
        return;

    // Return but don't end recovery. only do that upon valid token match
    if (recognizer.isExpectedToken(la)) {
        return;
    }

    switch (s.getStateType()) {
    case ATNState.BLOCK_START:
    case ATNState.STAR_BLOCK_START:
    case ATNState.PLUS_BLOCK_START:
    case ATNState.STAR_LOOP_ENTRY:
        // report error and recover if possible
        if (singleTokenDeletion(recognizer) != null) {
            return;
        }

        throw new InputMismatchException(recognizer);

    case ATNState.PLUS_LOOP_BACK:
    case ATNState.STAR_LOOP_BACK:
        // System.err.println("at loop back: "+s.getClass().getSimpleName());
        reportUnwantedToken(recognizer);
        IntervalSet expecting = recognizer.getExpectedTokens();
        IntervalSet whatFollowsLoopIterationOrRule = expecting.or(getErrorRecoverySet(recognizer));
        consumeUntil(recognizer, whatFollowsLoopIterationOrRule);
        break;

    default:
        // do nothing if we can't identify the exact kind of ATN state
        break;
    }
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This is called by {@link #reportError} when the exception is a
 * {@link NoViableAltException}./*from  w  w w .  ja  v a2s .  c o  m*/
 *
 * @see #reportError
 *
 * @param recognizer
 *            the parser instance
 * @param e
 *            the recognition exception
 */
protected void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input;
    if (tokens != null) {
        if (e.getStartToken().getType() == Token.EOF)
            input = "<EOF>";
        else
            input = tokens.getText(e.getStartToken(), e.getOffendingToken());
    } else {
        input = "<unknown input>";
    }
    String msg = "no viable alternative at input " + escapeWSAndQuote(input);
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This method implements the single-token insertion inline error recovery
 * strategy. It is called by {@link #recoverInline} if the single-token
 * deletion strategy fails to recover from the mismatched input. If this
 * method returns {@code true}, {@code recognizer} will be in error recovery
 * mode./*from ww w.ja v  a  2  s  .co  m*/
 *
 * <p>
 * This method determines whether or not single-token insertion is viable by
 * checking if the {@code LA(1)} input symbol could be successfully matched
 * if it were instead the {@code LA(2)} symbol. If this method returns
 * {@code true}, the caller is responsible for creating and inserting a
 * token with the correct type to produce this behavior.
 * </p>
 *
 * @param recognizer
 *            the parser instance
 * @return {@code true} if single-token insertion is a viable recovery
 *         strategy for the current mismatched input, otherwise
 *         {@code false}
 */
protected boolean singleTokenInsertion(@NotNull Parser recognizer) {
    int currentSymbolType = recognizer.getInputStream().LA(1);
    // if current token is consistent with what could come after current
    // ATN state, then we know we're missing a token; error recovery
    // is free to conjure up and insert the missing token
    ATNState currentState = recognizer.getInterpreter().atn.states.get(recognizer.getState());
    ATNState next = currentState.transition(0).target;
    ATN atn = recognizer.getInterpreter().atn;
    IntervalSet expectingAtLL2 = atn.nextTokens(next, recognizer.getContext());
    // System.out.println("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
    if (expectingAtLL2.contains(currentSymbolType)) {
        reportMissingToken(recognizer);
        return true;
    }
    return false;
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This method implements the single-token deletion inline error recovery
 * strategy. It is called by {@link #recoverInline} to attempt to recover
 * from mismatched input. If this method returns null, the parser and error
 * handler state will not have changed. If this method returns non-null,
 * {@code recognizer} will <em>not</em> be in error recovery mode since the
 * returned token was a successful match.
 *
 * <p>/*from w w w .  jav a2 s .co  m*/
 * If the single-token deletion is successful, this method calls
 * {@link #reportUnwantedToken} to report the error, followed by
 * {@link Parser#consume} to actually "delete" the extraneous token. Then,
 * before returning {@link #reportMatch} is called to signal a successful
 * match.
 * </p>
 *
 * @param recognizer
 *            the parser instance
 * @return the successfully matched {@link Token} instance if single-token
 *         deletion successfully recovers from the mismatched input,
 *         otherwise {@code null}
 */
@Nullable
protected Token singleTokenDeletion(@NotNull Parser recognizer) {
    int nextTokenType = recognizer.getInputStream().LA(2);
    IntervalSet expecting = getExpectedTokens(recognizer);
    if (expecting.contains(nextTokenType)) {
        reportUnwantedToken(recognizer);
        /*
         * System.err.println("recoverFromMismatchedToken deleting "+
         * ((TokenStream)recognizer.getInputStream()).LT(1)+
         * " since "+((TokenStream)recognizer.getInputStream()).LT(2)+
         * " is what we want");
         */
        recognizer.consume(); // simply delete extra token
        // we want to return the token we're actually matching
        Token matchedSymbol = recognizer.getCurrentToken();
        reportMatch(recognizer); // we know current token is correct
        return matchedSymbol;
    }
    return null;
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * Conjure up a missing token during error recovery.
 *
 * The recognizer attempts to recover from single missing symbols. But,
 * actions might refer to that missing symbol. For example, x=ID {f($x);}.
 * The action clearly assumes that there has been an identifier matched
 * previously and that $x points at that token. If that token is missing,
 * but the next token in the stream is what we want we assume that this
 * token is missing and we keep going. Because we have to return some token
 * to replace the missing token, we have to conjure one up. This method
 * gives the user control over the tokens returned for missing tokens.
 * Mostly, you will want to create something special for identifier tokens.
 * For literals such as '{' and ',', the default action in the parser or
 * tree parser works. It simply creates a CommonToken of the appropriate
 * type. The text will be the token. If you change what tokens must be
 * created by the lexer, override this method to create the appropriate
 * tokens.//from  ww  w .  ja  v  a 2 s . c o  m
 */
@NotNull
protected Token getMissingSymbol(@NotNull Parser recognizer) {
    Token currentSymbol = recognizer.getCurrentToken();
    IntervalSet expecting = getExpectedTokens(recognizer);
    int expectedTokenType = expecting.getMinElement(); // get any element
    String tokenText;
    if (expectedTokenType == Token.EOF)
        tokenText = "<missing EOF>";
    else
        tokenText = "<missing " + recognizer.getTokenNames()[expectedTokenType] + ">";
    Token current = currentSymbol;
    Token lookback = recognizer.getInputStream().LT(-1);
    if (current.getType() == Token.EOF && lookback != null) {
        current = lookback;
    }
    return recognizer.getTokenFactory()
            .create(new Pair<TokenSource, CharStream>(current.getTokenSource(),
                    current.getTokenSource().getInputStream()), expectedTokenType, tokenText,
                    Token.DEFAULT_CHANNEL, -1, -1, current.getLine(), current.getCharPositionInLine());
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/** Consume tokens until one matches the given token set. */
protected void consumeUntil(@NotNull Parser recognizer, @NotNull IntervalSet set) {
    // System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF && !set.contains(ttype)) {
        // System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
        // recognizer.getInputStream().consume();
        recognizer.consume();/*ww w. ja  v a  2s  .co m*/
        ttype = recognizer.getInputStream().LA(1);
    }
}

From source file:de.up.ling.irtg.codec.ExceptionErrorStrategy.java

@Override
public void reportInputMismatch(Parser recognizer, InputMismatchException e) throws RecognitionException {
    String msg = getTokenPosition(e.getOffendingToken()) + ": mismatched input "
            + getTokenErrorDisplay(e.getOffendingToken());
    msg += " expecting one of " + e.getExpectedTokens().toString(recognizer.getTokenNames());
    RecognitionException ex = new RecognitionException(msg, recognizer, recognizer.getInputStream(),
            recognizer.getContext());//from   ww w .  j  av a 2s.co m
    ex.initCause(e);
    throw ex;
}

From source file:de.up.ling.irtg.codec.ExceptionErrorStrategy.java

@Override
public void reportMissingToken(Parser recognizer) {
    beginErrorCondition(recognizer);/*from ww  w  .  j  a  v  a  2s.  c o m*/
    Token t = recognizer.getCurrentToken();
    IntervalSet expecting = getExpectedTokens(recognizer);
    String msg = getTokenPosition(t) + ": missing " + expecting.toString(recognizer.getTokenNames()) + " at "
            + getTokenErrorDisplay(t);
    throw new RecognitionException(msg, recognizer, recognizer.getInputStream(), recognizer.getContext());
}