Search in sources :

Example 6 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project beetl2.0 by javamonkey.

the class BeetlAntlrErrorStrategy method reportNoViableAlternative.

protected void reportNoViableAlternative(@NotNull Parser recognizer, @NotNull NoViableAltException e) {
    TokenStream tokens = recognizer.getInputStream();
    String input;
    if (tokens instanceof TokenStream) {
        if (e.getStartToken().getType() == Token.EOF)
            input = "<文件尾>";
        else
            input = tokens.getText(e.getStartToken(), e.getOffendingToken());
    } else {
        input = "<未知输入>";
    }
    BeetlException exception = null;
    if (keys.contains(e.getOffendingToken().getText())) {
        exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR, "不允许" + e.getOffendingToken().getText() + "关键出现在这里" + ":" + escapeWSAndQuote(input), e);
    } else {
        exception = new BeetlParserException(BeetlException.PARSER_VIABLE_ERROR, escapeWSAndQuote(input), e);
    }
    // String msg = "no viable alternative at input " + escapeWSAndQuote(input);
    exception.pushToken(this.getGrammarToken(e.getOffendingToken()));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) TokenStream(org.antlr.v4.runtime.TokenStream) BeetlParserException(org.beetl.core.exception.BeetlParserException)

Example 7 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project ihmc-pub-sub by ihmcrobotics.

the class DefaultErrorStrategy method reportUnwantedToken.

@Override
protected void reportUnwantedToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = getTokenErrorDisplay(t);
    String msg = "Unexpected input " + ColorMessage.bold(tokenName);
    recognizer.notifyErrorListeners(t, msg, null);
}
Also used : Token(org.antlr.v4.runtime.Token)

Example 8 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method _createATN.

protected void _createATN(@NotNull Collection<Rule> rules) {
    createRuleStartAndStopATNStates();
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
    for (Rule r : rules) {
        // find rule's block
        GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
        ATNBuilder b = new ATNBuilder(nodes, this);
        try {
            setCurrentRuleName(r.name);
            Handle h = b.ruleBlock(null);
            rule(r.ast, r.name, h);
        } catch (RecognitionException re) {
            ErrorManager.fatalInternalError("bad grammar AST structure", re);
        }
    }
}
Also used : ATNBuilder(org.antlr.v4.parse.ATNBuilder) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) RecognitionException(org.antlr.runtime.RecognitionException) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 9 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method set.

/**
 * From set build single edge graph {@code o->o-set->o}.  To conform to
 *  what an alt block looks like, must have extra state on left.
 *  This also handles {@code ~A}, converted to {@code ~{A}} set.
 */
@NotNull
@Override
public Handle set(@NotNull GrammarAST associatedAST, @NotNull List<GrammarAST> terminals, boolean invert) {
    ATNState left = newState(associatedAST);
    ATNState right = newState(associatedAST);
    IntervalSet set = new IntervalSet();
    for (GrammarAST t : terminals) {
        int ttype = g.getTokenType(t.getText());
        set.add(ttype);
    }
    if (invert) {
        left.addTransition(new NotSetTransition(right, set));
    } else {
        left.addTransition(new SetTransition(right, set));
    }
    associatedAST.atnState = left;
    return new Handle(left, right);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 10 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method newState.

@NotNull
public ATNState newState(@Nullable GrammarAST node) {
    ATNState n = new BasicState();
    n.setRuleIndex(currentRule.index);
    atn.addState(n);
    return n;
}
Also used : BasicState(org.antlr.v4.runtime.atn.BasicState) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Aggregations

NotNull (org.antlr.v4.runtime.misc.NotNull)40 ATNState (org.antlr.v4.runtime.atn.ATNState)14 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)14 DFAState (org.antlr.v4.runtime.dfa.DFAState)12 ParseTree (org.antlr.v4.runtime.tree.ParseTree)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)8 ArrayList (java.util.ArrayList)7 Token (org.antlr.v4.runtime.Token)7 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)4 DFA (org.antlr.v4.runtime.dfa.DFA)4 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)4 List (java.util.List)3 ATN (org.antlr.v4.runtime.atn.ATN)3 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)3 Transition (org.antlr.v4.runtime.atn.Transition)3 WildcardTransition (org.antlr.v4.runtime.atn.WildcardTransition)3 Interval (org.antlr.v4.runtime.misc.Interval)3 Nullable (org.antlr.v4.runtime.misc.Nullable)3