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

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

Introduction

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

Prototype

public IntervalSet getExpectedTokens() 

Source Link

Document

Gets the set of input symbols which could potentially follow the previously matched symbol at the time this exception was thrown.

Usage

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.//w w w.  j a v  a 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: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());
}