Search in sources :

Example 6 with ATNConfigSet

use of org.antlr.v4.runtime.atn.ATNConfigSet in project antlr4 by antlr.

the class ParserATNSimulator method adaptivePredict.

public int adaptivePredict(TokenStream input, int decision, ParserRuleContext outerContext) {
    if (debug || debug_list_atn_decisions) {
        System.out.println("adaptivePredict decision " + decision + " exec LA(1)==" + getLookaheadName(input) + " line " + input.LT(1).getLine() + ":" + input.LT(1).getCharPositionInLine());
    }
    _input = input;
    _startIndex = input.index();
    _outerContext = outerContext;
    DFA dfa = decisionToDFA[decision];
    _dfa = dfa;
    int m = input.mark();
    int index = _startIndex;
    // But, do we still need an initial state?
    try {
        DFAState s0;
        if (dfa.isPrecedenceDfa()) {
            // the start state for a precedence DFA depends on the current
            // parser precedence, and is provided by a DFA method.
            s0 = dfa.getPrecedenceStartState(parser.getPrecedence());
        } else {
            // the start state for a "regular" DFA is just s0
            s0 = dfa.s0;
        }
        if (s0 == null) {
            if (outerContext == null)
                outerContext = ParserRuleContext.EMPTY;
            if (debug || debug_list_atn_decisions) {
                System.out.println("predictATN decision " + dfa.decision + " exec LA(1)==" + getLookaheadName(input) + ", outerContext=" + outerContext.toString(parser));
            }
            boolean fullCtx = false;
            ATNConfigSet s0_closure = computeStartState(dfa.atnStartState, ParserRuleContext.EMPTY, fullCtx);
            if (dfa.isPrecedenceDfa()) {
                /* If this is a precedence DFA, we use applyPrecedenceFilter
					 * to convert the computed start state to a precedence start
					 * state. We then use DFA.setPrecedenceStartState to set the
					 * appropriate start state for the precedence level rather
					 * than simply setting DFA.s0.
					 */
                // not used for prediction but useful to know start configs anyway
                dfa.s0.configs = s0_closure;
                s0_closure = applyPrecedenceFilter(s0_closure);
                s0 = addDFAState(dfa, new DFAState(s0_closure));
                dfa.setPrecedenceStartState(parser.getPrecedence(), s0);
            } else {
                s0 = addDFAState(dfa, new DFAState(s0_closure));
                dfa.s0 = s0;
            }
        }
        int alt = execATN(dfa, s0, input, index, outerContext);
        if (debug)
            System.out.println("DFA after predictATN: " + dfa.toString(parser.getVocabulary()));
        return alt;
    } finally {
        // wack cache after each prediction
        mergeCache = null;
        _dfa = null;
        input.seek(index);
        input.release(m);
    }
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 7 with ATNConfigSet

use of org.antlr.v4.runtime.atn.ATNConfigSet in project antlr4 by antlr.

the class ParserATNSimulator method reportAttemptingFullContext.

protected void reportAttemptingFullContext(DFA dfa, BitSet conflictingAlts, ATNConfigSet configs, int startIndex, int stopIndex) {
    if (debug || retry_debug) {
        Interval interval = Interval.of(startIndex, stopIndex);
        System.out.println("reportAttemptingFullContext decision=" + dfa.decision + ":" + configs + ", input=" + parser.getTokenStream().getText(interval));
    }
    if (parser != null)
        parser.getErrorListenerDispatch().reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, conflictingAlts, configs);
}
Also used : Interval(org.antlr.v4.runtime.misc.Interval)

Example 8 with ATNConfigSet

use of org.antlr.v4.runtime.atn.ATNConfigSet in project kripton by xcesco.

the class ContentUriChecker method prepareUri.

private Pair<ParserRuleContext, CommonTokenStream> prepareUri(final String input) {
    UriLexer lexer = new UriLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    UriParser parser = new UriParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new ContentUriBaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            AssertKripton.assertTrue(false, "unespected char at pos %s of URI '%s'", charPositionInLine, input);
        }

        @Override
        public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "ambiguity syntax at pos %s of URI '%s'", startIndex, input);
        }

        @Override
        public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "error at pos %s of URI '%s'", startIndex, input);
        }

        @Override
        public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
            AssertKripton.assertTrue(false, "context eror at pos %s of URI '%s'", startIndex, input);
        }
    });
    ParserRuleContext context = parser.uri();
    return new Pair<>(context, tokens);
}
Also used : ATNConfigSet(org.antlr.v4.runtime.atn.ATNConfigSet) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BitSet(java.util.BitSet) Parser(org.antlr.v4.runtime.Parser) RecognitionException(org.antlr.v4.runtime.RecognitionException) DFA(org.antlr.v4.runtime.dfa.DFA) Pair(com.abubusoft.kripton.common.Pair)

Example 9 with ATNConfigSet

use of org.antlr.v4.runtime.atn.ATNConfigSet 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 10 with ATNConfigSet

use of org.antlr.v4.runtime.atn.ATNConfigSet in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method computeStartState.

@NotNull
protected SimulatorState computeStartState(DFA dfa, ParserRuleContext globalContext, boolean useContext) {
    DFAState s0 = dfa.isPrecedenceDfa() ? dfa.getPrecedenceStartState(parser.getPrecedence(), useContext) : useContext ? dfa.s0full.get() : dfa.s0.get();
    if (s0 != null) {
        if (!useContext) {
            return new SimulatorState(globalContext, s0, useContext, globalContext);
        }
        s0.setContextSensitive(atn);
    }
    final int decision = dfa.decision;
    @NotNull final ATNState p = dfa.atnStartState;
    int previousContext = 0;
    ParserRuleContext remainingGlobalContext = globalContext;
    // always at least the implicit call to start rule
    PredictionContext initialContext = useContext ? PredictionContext.EMPTY_FULL : PredictionContext.EMPTY_LOCAL;
    PredictionContextCache contextCache = new PredictionContextCache();
    if (useContext) {
        if (!enable_global_context_dfa) {
            while (remainingGlobalContext != null) {
                if (remainingGlobalContext.isEmpty()) {
                    previousContext = PredictionContext.EMPTY_FULL_STATE_KEY;
                    remainingGlobalContext = null;
                } else {
                    previousContext = getReturnState(remainingGlobalContext);
                    initialContext = initialContext.appendContext(previousContext, contextCache);
                    remainingGlobalContext = remainingGlobalContext.getParent();
                }
            }
        }
        while (s0 != null && s0.isContextSensitive() && remainingGlobalContext != null) {
            DFAState next;
            remainingGlobalContext = skipTailCalls(remainingGlobalContext);
            if (remainingGlobalContext.isEmpty()) {
                next = s0.getContextTarget(PredictionContext.EMPTY_FULL_STATE_KEY);
                previousContext = PredictionContext.EMPTY_FULL_STATE_KEY;
                remainingGlobalContext = null;
            } else {
                previousContext = getReturnState(remainingGlobalContext);
                next = s0.getContextTarget(previousContext);
                initialContext = initialContext.appendContext(previousContext, contextCache);
                remainingGlobalContext = remainingGlobalContext.getParent();
            }
            if (next == null) {
                break;
            }
            s0 = next;
        }
    }
    if (s0 != null && !s0.isContextSensitive()) {
        return new SimulatorState(globalContext, s0, useContext, remainingGlobalContext);
    }
    ATNConfigSet configs = new ATNConfigSet();
    while (true) {
        ATNConfigSet reachIntermediate = new ATNConfigSet();
        int n = p.getNumberOfTransitions();
        for (int ti = 0; ti < n; ti++) {
            // for each transition
            ATNState target = p.transition(ti).target;
            reachIntermediate.add(ATNConfig.create(target, ti + 1, initialContext));
        }
        boolean hasMoreContext = remainingGlobalContext != null;
        if (!hasMoreContext) {
            configs.setOutermostConfigSet(true);
        }
        final boolean collectPredicates = true;
        closure(reachIntermediate, configs, collectPredicates, hasMoreContext, contextCache, false);
        boolean stepIntoGlobal = configs.getDipsIntoOuterContext();
        DFAState next;
        if (useContext && !enable_global_context_dfa) {
            s0 = addDFAState(dfa, configs, contextCache);
            break;
        } else if (s0 == null) {
            if (!dfa.isPrecedenceDfa()) {
                AtomicReference<DFAState> reference = useContext ? dfa.s0full : dfa.s0;
                next = addDFAState(dfa, configs, contextCache);
                if (!reference.compareAndSet(null, next)) {
                    next = reference.get();
                }
            } else {
                /* If this is a precedence DFA, we use applyPrecedenceFilter
					 * to convert the computed start state to a precedence start
					 * state. We then use DFA.setPrecedenceStartState to set the
					 * appropriate start state for the precedence level rather
					 * than simply setting DFA.s0.
					 */
                configs = applyPrecedenceFilter(configs, globalContext, contextCache);
                next = addDFAState(dfa, configs, contextCache);
                dfa.setPrecedenceStartState(parser.getPrecedence(), useContext, next);
            }
        } else {
            if (dfa.isPrecedenceDfa()) {
                configs = applyPrecedenceFilter(configs, globalContext, contextCache);
            }
            next = addDFAState(dfa, configs, contextCache);
            s0.setContextTarget(previousContext, next);
        }
        s0 = next;
        if (!useContext || !stepIntoGlobal) {
            break;
        }
        // TODO: make sure it distinguishes empty stack states
        next.setContextSensitive(atn);
        configs.clear();
        remainingGlobalContext = skipTailCalls(remainingGlobalContext);
        int nextContextElement = getReturnState(remainingGlobalContext);
        if (remainingGlobalContext.isEmpty()) {
            remainingGlobalContext = null;
        } else {
            remainingGlobalContext = remainingGlobalContext.getParent();
        }
        if (nextContextElement != PredictionContext.EMPTY_FULL_STATE_KEY) {
            initialContext = initialContext.appendContext(nextContextElement, contextCache);
        }
        previousContext = nextContextElement;
    }
    return new SimulatorState(globalContext, s0, useContext, remainingGlobalContext);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DFAState(org.antlr.v4.runtime.dfa.DFAState) AtomicReference(java.util.concurrent.atomic.AtomicReference) NotNull(org.antlr.v4.runtime.misc.NotNull) NotNull(org.antlr.v4.runtime.misc.NotNull)

Aggregations

DFAState (org.antlr.v4.runtime.dfa.DFAState)13 NotNull (org.antlr.v4.runtime.misc.NotNull)6 BitSet (java.util.BitSet)5 DFA (org.antlr.v4.runtime.dfa.DFA)4 Interval (org.antlr.v4.runtime.misc.Interval)4 ArrayList (java.util.ArrayList)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 ATNConfigSet (org.antlr.v4.runtime.atn.ATNConfigSet)2 AcceptStateInfo (org.antlr.v4.runtime.dfa.AcceptStateInfo)2 Pair (com.abubusoft.kripton.common.Pair)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ANTLRErrorListener (org.antlr.v4.runtime.ANTLRErrorListener)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 Parser (org.antlr.v4.runtime.Parser)1 IntegerList (org.antlr.v4.runtime.misc.IntegerList)1 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)1 CommentLineContext (org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.CommentLineContext)1