Example usage for org.antlr.v4.runtime CommonTokenStream reset

List of usage examples for org.antlr.v4.runtime CommonTokenStream reset

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CommonTokenStream reset.

Prototype

@Deprecated
public void reset() 

Source Link

Document

This method resets the token stream back to the first token in the buffer.

Usage

From source file:org.teamtwo.r.parser.NbParser.java

@Override
public void parse(Snapshot snapshot, Task task, SourceModificationEvent event) throws ParseException {
    System.out.println("I am in the parser");
    this.snapshot = snapshot;
    //System.out.println(snapshot.getText().toString());
    //org.antlr.v4.runtime.ANTLRInputStream input = new ANTLRInputStream("G");
    org.antlr.v4.runtime.ANTLRInputStream input = new ANTLRInputStream(
            snapshot.getText().toString().replace("\\", "\\\\"));
    // org.antlr.runtime.ANTLRStringStream input  =  new ANTLRStringStream(snapshot.getText().toString());
    //  System.out.println("Input is " + input.size() + " and the letter is " + input.substring(0, input.size() - 1));
    //input = input.replace(' ', '*');
    //System.out.println(input);
    RLexer lexer = new RLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    RFilter filter = new RFilter(tokens);
    filter.stream(); // call start rule: stream
    tokens.reset();

    rparser = new RParser(tokens);
    try {//from  w  w  w .ja v a2 s. com
        rparser.prog();
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:org.tvl.goworks.editor.go.parser.CompiledModelParser.java

License:Open Source License

protected CompiledModel parseImpl(@NonNull ParserTaskManager taskManager, ParseContext context,
        DocumentSnapshot snapshot) throws InterruptedException, ExecutionException {

    Parameters.notNull("snapshot", snapshot);

    synchronized (lock) {
        if (snapshot.equals(lastSnapshot)) {
            if (lastException != null) {
                throw new ExecutionException("An unexpected error occurred.", lastException);
            }//w  w w  .j  av  a 2  s . c  o m

            return new CompiledModel(snapshot, lastResult);
        }

        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.log(Level.FINE, "Reparsing snapshot {0}", snapshot);
        }

        try {
            Future<ParserData<Tagger<TokenTag<Token>>>> futureTokensData = taskManager.getData(snapshot,
                    GoParserDataDefinitions.LEXER_TOKENS);
            Tagger<TokenTag<Token>> tagger = futureTokensData != null ? futureTokensData.get().getData() : null;
            TaggerTokenSource tokenSource = new TaggerTokenSource(tagger, snapshot);
            CommonTokenStream tokenStream = new CommonTokenStream(tokenSource);
            GoParser parser = GoParserFactory.DEFAULT.getParser(tokenStream, ParserConfiguration.FASTEST);
            try {
                SyntaxErrorListener syntaxErrorListener = new SyntaxErrorListener(snapshot);
                SourceFileContext sourceFileContext;
                try {
                    try {
                        sourceFileContext = parser.sourceFile();
                    } catch (ParseCancellationException ex) {
                        if (ex.getCause() instanceof RecognitionException) {
                            // retry with hybrid parser
                            tokenStream.reset();
                            parser = GoParserFactory.DEFAULT.getParser(tokenStream, ParserConfiguration.HYBRID);
                            sourceFileContext = parser.sourceFile();
                        } else {
                            throw ex;
                        }
                    }
                } catch (ParseCancellationException ex) {
                    if (ex.getCause() instanceof RecognitionException) {
                        // retry with precise parser and default error handler
                        tokenStream.reset();
                        parser = GoParserFactory.DEFAULT.getParser(tokenStream, ParserConfiguration.PRECISE);
                        parser.removeErrorListeners();
                        parser.addErrorListener(syntaxErrorListener);
                        sourceFileContext = parser.sourceFile();
                    } else {
                        throw ex;
                    }
                }

                FileObject fileObject = snapshot.getVersionedDocument().getFileObject();
                Token[] groupTokens = tokenStream.getTokens().toArray(new Token[0]);
                lastSnapshot = snapshot;
                lastResult = new CompiledFileModel(sourceFileContext, syntaxErrorListener.getSyntaxErrors(),
                        fileObject, groupTokens);
                lastException = null;
                return new CompiledModel(snapshot, lastResult);
            } catch (RecognitionException ex) {
                if (LOGGER.isLoggable(Level.FINE)) {
                    LOGGER.log(Level.FINE, "A recognition exception occurred while parsing.", ex);
                }

                lastSnapshot = snapshot;
                lastResult = null;
                lastException = null;
                return null;
            }
        } catch (InterruptedException | ExecutionException | ParseCancellationException ex) {
            lastSnapshot = snapshot;
            lastResult = null;
            lastException = ex;
            throw new ExecutionException("An unexpected error occurred.", ex);
        }
    }
}

From source file:uk.ac.open.crc.jim.queue.FileReader.java

License:Apache License

private boolean parseAsJava17(File sourceFile) {
    ANTLRInputStream input;/*from  ww w.  ja  v  a 2 s  .  c  om*/
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile)))) {
        input = new ANTLRInputStream(reader);

        JavaLexer javaLexer = new JavaLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(javaLexer);

        JavaParser javaParser = new JavaParser(tokens);
        javaParser.removeErrorListeners();
        javaParser.addErrorListener(new LogListener());
        javaParser.setErrorHandler(new BailErrorStrategy());
        javaParser.getInterpreter().setPredictionMode(PredictionMode.SLL);

        ParseTree parseTree;

        try {
            parseTree = javaParser.compilationUnit();
        } catch (RecognitionException e) {
            // in here when the SLL(*) parser fails.
            // log it
            LOGGER.warn("Syntax error encountered parsing file \"{}\" " + "using SLL(*), switching to LL(*)",
                    sourceFile.getAbsolutePath());
            tokens.reset();
            javaParser.reset();
            javaParser.getInterpreter().setPredictionMode(PredictionMode.LL);
            try {
                parseTree = javaParser.compilationUnit();
            } catch (RecognitionException ex) {
                // log it
                LOGGER.warn("Syntax error encountered parsing file \"{}\" " + "using LL(*).\n\"{}\"",
                        sourceFile.getAbsolutePath(), ex.getMessage());
                return false;
            } catch (RuntimeException ex) {
                // log it
                LOGGER.warn(
                        "ANTLR threw a runtime exception while parsing " + "file \"{}\" using LL(*).\n\"{}\"",
                        sourceFile.getAbsolutePath(), ex.getMessage());
                return false;
            }
        } catch (RuntimeException e) {
            // this is a catch-all for ANTLR to retain control
            // and can hand off to other parsers.
            // log it
            LOGGER.warn("ANTLR threw a runtime exception while parsing " + "file \"{}\" using SLL(*)\n\"{}\"",
                    sourceFile.getAbsolutePath(), e.getMessage());
            return false;
        }

        Java17VisitorImplementation javaVisitor = new Java17VisitorImplementation(sourceFile.getName(),
                EntityStoreSingleton.getInstance());
        javaVisitor.visit(parseTree);
    } catch (IOException ioEx) {
        LOGGER.error("File \"{}\" passed to ANTLR parser " + "either cannot be found or opened.\n\"{}\"",
                sourceFile.getAbsolutePath(), ioEx.getMessage());
        return false; // bail on the exception
    }

    return true;
}