Search in sources :

Example 41 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project sts4 by spring-projects.

the class AntlrParser method parse.

@Override
public ParseResults parse(String text) {
    ArrayList<Problem> syntaxErrors = new ArrayList<>();
    ArrayList<Problem> problems = new ArrayList<>();
    ArrayList<PropertiesAst.Node> astNodes = new ArrayList<>();
    JavaPropertiesLexer lexer = new JavaPropertiesLexer(new ANTLRInputStream(text.toCharArray(), text.length()));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaPropertiesParser parser = new JavaPropertiesParser(tokens);
    // To avoid printing parse errors in the console
    parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
    // Add listener to collect various parser errors
    parser.addErrorListener(new ANTLRErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            syntaxErrors.add(createProblem(msg, ProblemCodes.PROPERTIES_SYNTAX_ERROR, (Token) offendingSymbol));
        }

        @Override
        public void reportAmbiguity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
            problems.add(createProblem("Ambiguity detected!", ProblemCodes.PROPERTIES_AMBIGUITY_ERROR, recognizer.getCurrentToken()));
        }

        @Override
        public void reportAttemptingFullContext(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
            problems.add(createProblem("Full-Context attempt detected!", ProblemCodes.PROPERTIES_FULL_CONTEXT_ERROR, recognizer.getCurrentToken()));
        }

        @Override
        public void reportContextSensitivity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
            problems.add(createProblem("Context sensitivity detected!", ProblemCodes.PROPERTIES_CONTEXT_SENSITIVITY_ERROR, recognizer.getCurrentToken()));
        }
    });
    // Add listener to the parse tree to collect AST nodes
    parser.addParseListener(new JavaPropertiesBaseListener() {

        private Key key = null;

        private Value value = null;

        @Override
        public void exitPropertyLine(PropertyLineContext ctx) {
            KeyValuePair pair = new KeyValuePair(ctx, key, value);
            key.parent = value.parent = pair;
            astNodes.add(pair);
            key = null;
            value = null;
        }

        @Override
        public void exitCommentLine(CommentLineContext ctx) {
            astNodes.add(new Comment(ctx));
        }

        @Override
        public void exitKey(KeyContext ctx) {
            key = new Key(ctx);
        }

        @Override
        public void exitSeparatorAndValue(SeparatorAndValueContext ctx) {
            value = new Value(ctx);
        }

        @Override
        public void exitEmptyLine(EmptyLineContext ctx) {
            astNodes.add(new EmptyLine(ctx));
        }
    });
    parser.parse();
    // Collect and return parse results
    return new ParseResults(new PropertiesAst(ImmutableList.copyOf(astNodes)), ImmutableList.copyOf(syntaxErrors), ImmutableList.copyOf(problems));
}
Also used : ATNConfigSet(org.antlr.v4.runtime.atn.ATNConfigSet) CommentLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.CommentLineContext) ArrayList(java.util.ArrayList) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) PropertiesAst(org.springframework.ide.vscode.java.properties.parser.PropertiesAst) KeyContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.KeyContext) SeparatorAndValueContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.SeparatorAndValueContext) EmptyLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.EmptyLineContext) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BitSet(java.util.BitSet) PropertyLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.PropertyLineContext) Problem(org.springframework.ide.vscode.java.properties.parser.Problem) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) RecognitionException(org.antlr.v4.runtime.RecognitionException) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 42 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project elasticsearch by elastic.

the class ParserErrorStrategy method recover.

@Override
public void recover(final Parser recognizer, final RecognitionException re) {
    final Token token = re.getOffendingToken();
    String message;
    if (token == null) {
        message = "no parse token found.";
    } else if (re instanceof InputMismatchException) {
        message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == PainlessParser.EOF) {
            message = "unexpected end of script.";
        } else {
            message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
        }
    } else {
        message = "unexpected token near [" + getTokenErrorDisplay(token) + "].";
    }
    Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex());
    throw location.createError(new IllegalArgumentException(message, re));
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Token(org.antlr.v4.runtime.Token) InputMismatchException(org.antlr.v4.runtime.InputMismatchException) Location(org.elasticsearch.painless.Location)

Example 43 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project elasticsearch by elastic.

the class ParserErrorStrategy method recoverInline.

@Override
public Token recoverInline(final Parser recognizer) throws RecognitionException {
    final Token token = recognizer.getCurrentToken();
    final String message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    Location location = new Location(sourceName, token.getStartIndex());
    throw location.createError(new IllegalArgumentException(message));
}
Also used : Token(org.antlr.v4.runtime.Token) Location(org.elasticsearch.painless.Location)

Example 44 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project elasticsearch by elastic.

the class Walker method setupPicky.

private void setupPicky(PainlessParser parser) {
    // Diagnostic listener invokes syntaxError on other listeners for ambiguity issues,
    parser.addErrorListener(new DiagnosticErrorListener(true));
    // a second listener to fail the test when the above happens.
    parser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
            throw new AssertionError("line: " + line + ", offset: " + charPositionInLine + ", symbol:" + offendingSymbol + " " + msg);
        }
    });
    // Enable exact ambiguity detection (costly). we enable exact since its the default for
    // DiagnosticErrorListener, life is too short to think about what 'inexact ambiguity' might mean.
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
Also used : DiagnosticErrorListener(org.antlr.v4.runtime.DiagnosticErrorListener) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) EString(org.elasticsearch.painless.node.EString) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 45 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project antlr4 by antlr.

the class BaseSwiftTest method execTest.

private String execTest() {
    try {
        String exec = tmpdir + "/" + EXEC_NAME;
        String[] args = //new File(tmpdir, "input").getAbsolutePath()
        new String[] { exec, "input" };
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.directory(new File(tmpdir));
        Process p = pb.start();
        StreamVacuum stdoutVacuum = new StreamVacuum(p.getInputStream());
        StreamVacuum stderrVacuum = new StreamVacuum(p.getErrorStream());
        stdoutVacuum.start();
        stderrVacuum.start();
        p.waitFor();
        stdoutVacuum.join();
        stderrVacuum.join();
        String output = stdoutVacuum.toString();
        if (output.length() == 0) {
            output = null;
        }
        if (stderrVacuum.toString().length() > 0) {
            this.stderrDuringParse = stderrVacuum.toString();
        }
        return output;
    } catch (Exception e) {
        System.err.println("can't exec recognizer");
        e.printStackTrace(System.err);
    }
    return null;
}
Also used : StreamVacuum(org.antlr.v4.test.runtime.StreamVacuum) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) IOException(java.io.IOException)

Aggregations

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)24 Token (org.antlr.v4.runtime.Token)22 RecognitionException (org.antlr.v4.runtime.RecognitionException)19 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)15 File (java.io.File)11 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 ATNState (org.antlr.v4.runtime.atn.ATNState)9 IOException (java.io.IOException)8 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)8 Parser (org.antlr.v4.runtime.Parser)8 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)8 ArrayList (java.util.ArrayList)7 ATN (org.antlr.v4.runtime.atn.ATN)6 Pair (com.abubusoft.kripton.common.Pair)5 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)5 TokenStream (org.antlr.v4.runtime.TokenStream)5 BeetlException (org.beetl.core.exception.BeetlException)5 STGroupString (org.stringtemplate.v4.STGroupString)5 CommonToken (org.antlr.v4.runtime.CommonToken)4