Example usage for org.antlr.v4.runtime NoViableAltException getOffendingToken

List of usage examples for org.antlr.v4.runtime NoViableAltException getOffendingToken

Introduction

In this page you can find the example usage for org.antlr.v4.runtime NoViableAltException getOffendingToken.

Prototype

public Token getOffendingToken() 

Source Link

Usage

From source file:com.github.jknack.handlebars.internal.HbsErrorStrategy.java

License:Apache License

@Override
public void reportNoViableAlternative(final Parser recognizer, final NoViableAltException e) {
    HbsParser parser = (HbsParser) recognizer;
    TokenStream tokens = parser.getTokenStream();
    HbsLexer lexer = (HbsLexer) tokens.getTokenSource();
    String msg = new ErrorStrategyVisitor(lexer.start, lexer.end).visit(e.getCtx());
    if (msg != null) {
        recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
    } else {//from  www .  jav  a2 s  .c om
        super.reportNoViableAlternative(recognizer, e);
    }
}

From source file:com.globalforge.infix.FixRulesErrorStrategy.java

License:Open Source License

/**
 * @see DefaultErrorStrategy#reportNoViableAlternative
 *///  w  w w .  j ava  2 s. c o  m
@Override
protected void reportNoViableAlternative(Parser parser, NoViableAltException e) {
    String msg = "can't choose between alternatives"; // nonstandard msg
    parser.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

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

License:Apache License

/**
 * {@inheritDoc}/*from w w w  .  j a v a  2 s . c  o  m*/
 */
@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

/**
 * This is called by {@link #reportError} when the exception is a
 * {@link NoViableAltException}.//from  w w w .  jav a  2  s .co 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.xiaomi.linden.bql.BQLCompiler.java

License:Apache License

protected String getErrorMessage(NoViableAltException error) {
    return String.format("[line:%d, col:%d] No viable alternative (token=%s)",
            error.getOffendingToken().getLine(), error.getOffendingToken().getCharPositionInLine(),
            error.getOffendingToken().getText());
}

From source file:cx2x.translator.language.base.ClawLanguage.java

License:BSD License

/**
 * Analyze a raw string input and match it with the CLAW language definition.
 *
 * @param rawPragma A raw pragma statement to be analyzed against the CLAW
 *                  language.//from  ww  w  .ja va 2  s  .  c o m
 * @param lineno    Line number of the pragma statement.
 * @param generator Accelerator directive generator.
 * @param target    Target that influences the code transformation.
 * @return A ClawLanguage object with the corresponding extracted information.
 * @throws IllegalDirectiveException If directive does not follow the CLAW
 *                                   language specification.
 */
private static ClawLanguage analyze(String rawPragma, int lineno, AcceleratorGenerator generator, Target target)
        throws IllegalDirectiveException {
    // Remove additional claw keyword
    rawPragma = nakenize(rawPragma);

    // Discard the ignored code after the claw ignore directive
    if (rawPragma.toLowerCase().contains(IGNORE)) {
        rawPragma = rawPragma.substring(0, rawPragma.toLowerCase().indexOf(IGNORE) + IGNORE.length());
    }

    // Instantiate the lexer with the raw string input
    ClawLexer lexer = new ClawLexer(CharStreams.fromString(rawPragma));

    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Pass the tokens to the parser
    ClawParser parser = new ClawParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();

    try {
        // Start the parser analysis from the "analyze" entry point
        ClawParser.AnalyzeContext ctx = parser.analyze();
        // Get the ClawLanguage object return by the parser after analysis.
        ctx.l.setAcceleratorGenerator(generator);
        ctx.l.setTarget(target);
        return ctx.l;
    } catch (ParseCancellationException pcex) {
        if (pcex.getCause() instanceof InputMismatchException) {
            InputMismatchException imex = (InputMismatchException) pcex.getCause();
            throw new IllegalDirectiveException(getTokens(imex.getExpectedTokens(), parser), lineno,
                    imex.getOffendingToken().getCharPositionInLine());
        } else if (pcex.getCause() instanceof NoViableAltException) {
            NoViableAltException nvex = (NoViableAltException) pcex.getCause();
            throw new IllegalDirectiveException(nvex.getOffendingToken(),
                    getTokens(nvex.getExpectedTokens(), parser), lineno,
                    nvex.getOffendingToken().getCharPositionInLine());
        }
        throw new IllegalDirectiveException(rawPragma, "Unsupported construct", lineno, 0);
    }
}

From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RErrorStrategy.java

License:Open Source License

/**
 * This is called by {@link #reportError} when the exception is a
 * {@link NoViableAltException}./*from   w  w  w .j a v a 2  s. 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 alternative available at input " + escapeWSAndQuote(input);
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

From source file:org.ballerinalang.composer.service.workspace.suggetions.CapturePossibleTokenStrategy.java

License:Open Source License

@Override
public void reportNoViableAlternative(Parser parser, NoViableAltException e) {
    fetchPossibleTokens(parser, e.getOffendingToken(), e.getExpectedTokens());
}

From source file:org.ballerinalang.langserver.completions.BallerinaCustomErrorStrategy.java

License:Open Source License

@Override
public void reportNoViableAlternative(Parser parser, NoViableAltException e) {
    fillContext(parser, e.getOffendingToken());
}

From source file:org.beetl.core.parser.BeetlAntlrErrorStrategy.java

License:BSD License

protected void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input;//w  w w.  ja  v a  2  s. co  m
    if (tokens instanceof TokenStream) {
        if (e.getStartToken().getType() == Token.EOF)
            input = "<>";
        else
            input = tokens.getText(e.getStartToken(), e.getOffendingToken());
    } else {
        input = "<>";
    }
    //      String msg = "no viable alternative at input " + escapeWSAndQuote(input);
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR,
            escapeWSAndQuote(input), e);
    exception.token = this.getGrammarToken(e.getOffendingToken());
    throw exception;
}