Example usage for org.antlr.v4.runtime.atn PredictionContextCache PredictionContextCache

List of usage examples for org.antlr.v4.runtime.atn PredictionContextCache PredictionContextCache

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.atn PredictionContextCache PredictionContextCache.

Prototype

PredictionContextCache

Source Link

Usage

From source file:com.github.robozonky.strategy.natural.SideEffectFreeParser.java

License:Apache License

private static void modifyInterpreter(final NaturalLanguageStrategyParser p) {
    final int originalSize = p.getInterpreter().decisionToDFA.length;
    final DFA[] emptyDFA = new DFA[originalSize]; // give our own array so the static one isn't used
    final ParserATNSimulator newInterpreter = new ParserATNSimulator(p, p.getATN(), emptyDFA,
            new PredictionContextCache());
    newInterpreter.clearDFA(); // initialize our array so that the parser functions properly
    p.setInterpreter(newInterpreter); // replace the interpreter to bypass all static caches
}

From source file:com.github.robozonky.strategy.natural.SideEffectFreeParser.java

License:Apache License

private static void modifyInterpreter(final NaturalLanguageStrategyLexer l) {
    final int originalSize = l.getInterpreter().decisionToDFA.length;
    final DFA[] emptyDFA = new DFA[originalSize]; // give our own array so the static one isn't used
    final LexerATNSimulator newInterpreter = new LexerATNSimulator(l, l.getATN(), emptyDFA,
            new PredictionContextCache());
    newInterpreter.clearDFA(); // initialize our array so that the lexer functions properly
    l.setInterpreter(newInterpreter); // replace the interpreter to bypass all static caches
}

From source file:org.eclipse.titan.designer.parsers.ttcn3parser.TTCN3Analyzer.java

License:Open Source License

/**
 * Parse TTCN-3 file using ANTLR v4//from   w w w.  j a  va2 s  .  c o  m
 * @param aReader file to parse (cannot be null, closes aReader)
 * @param aFileLength file length
 * @param aEclipseFile Eclipse dependent resource file
 */
private void parse(final Reader aReader, final int aFileLength, IFile aEclipseFile) {
    CharStream charStream = new UnbufferedCharStream(aReader);
    Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);

    lexer.setCommentTodo(true);
    lexer.setTokenFactory(new CommonTokenFactory(true));
    lexer.initRootInterval(aFileLength);

    TitanListener lexerListener = new TitanListener();
    // remove ConsoleErrorListener
    lexer.removeErrorListeners();
    lexer.addErrorListener(lexerListener);

    // 1. Previously it was UnbufferedTokenStream(lexer), but it was changed to BufferedTokenStream, because UnbufferedTokenStream seems to be unusable. It is an ANTLR 4 bug.
    // Read this: https://groups.google.com/forum/#!topic/antlr-discussion/gsAu-6d3pKU
    // pr_PatternChunk[StringBuilder builder, boolean[] uni]:
    //   $builder.append($v.text); <-- exception is thrown here: java.lang.UnsupportedOperationException: interval 85..85 not in token buffer window: 86..341
    // 2. Changed from BufferedTokenStream to CommonTokenStream, otherwise tokens with "-> channel(HIDDEN)" are not filtered out in lexer.
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);

    Ttcn3Parser parser = new Ttcn3Parser(tokenStream);
    parser.setBuildParseTree(false);
    PreprocessedTokenStream preprocessor = null;

    if (aEclipseFile != null && GlobalParser.TTCNPP_EXTENSION.equals(aEclipseFile.getFileExtension())) {
        lexer.setTTCNPP();
        preprocessor = new PreprocessedTokenStream(lexer);
        preprocessor.setActualFile(aEclipseFile);
        if (aEclipseFile.getProject() != null) {
            preprocessor.setMacros(
                    PreprocessorSymbolsOptionsData.getTTCN3PreprocessorDefines(aEclipseFile.getProject()));
        }
        parser = new Ttcn3Parser(preprocessor);
        preprocessor.setActualLexer(lexer);
        preprocessor.setParser(parser);
    }

    if (aEclipseFile != null) {
        lexer.setActualFile(aEclipseFile);
        parser.setActualFile(aEclipseFile);
        parser.setProject(aEclipseFile.getProject());
    }

    parser.setLexer(lexer);
    // remove ConsoleErrorListener
    parser.removeErrorListeners();
    TitanListener parserListener = new TitanListener();
    parser.addErrorListener(parserListener);

    // This is added because of the following ANTLR 4 bug:
    // Memory Leak in PredictionContextCache #499
    // https://github.com/antlr/antlr4/issues/499
    DFA[] decisionToDFA = parser.getInterpreter().decisionToDFA;
    parser.setInterpreter(
            new ParserATNSimulator(parser, parser.getATN(), decisionToDFA, new PredictionContextCache()));

    //try SLL mode
    try {
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        parser.pr_TTCN3File();
        warnings = parser.getWarnings();
        mErrorsStored = lexerListener.getErrorsStored();
        mErrorsStored.addAll(parserListener.getErrorsStored());
    } catch (RecognitionException e) {
        // quit
    }

    if (!warnings.isEmpty() || !mErrorsStored.isEmpty()) {
        //SLL mode might have failed, try LL mode
        try {
            CharStream charStream2 = new UnbufferedCharStream(aReader);
            lexer.setInputStream(charStream2);
            //lexer.reset();
            parser.reset();
            parserListener.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            parser.pr_TTCN3File();
            warnings = parser.getWarnings();
            mErrorsStored = lexerListener.getErrorsStored();
            mErrorsStored.addAll(parserListener.getErrorsStored());
        } catch (RecognitionException e) {

        }
    }

    unsupportedConstructs = parser.getUnsupportedConstructs();
    rootInterval = lexer.getRootInterval();
    actualTtc3Module = parser.getModule();
    if (preprocessor != null) {
        // if the file was preprocessed
        mErrorsStored.addAll(preprocessor.getErrorStorage());
        warnings.addAll(preprocessor.getWarnings());
        unsupportedConstructs.addAll(preprocessor.getUnsupportedConstructs());
        if (actualTtc3Module != null) {
            actualTtc3Module.setIncludedFiles(preprocessor.getIncludedFiles());
            actualTtc3Module.setInactiveCodeLocations(preprocessor.getInactiveCodeLocations());
        }
    }
    //TODO: empty mErrorsStored not to store errors from the previous parse round in case of exception

    try {
        aReader.close();
    } catch (IOException e) {
    }
}