Search in sources :

Example 16 with NotNull

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

the class ParserATNFactory method optional.

/**
 * From {@code (A)?} build either:
 *
 * <pre>
 *  o--A-&gt;o
 *  |     ^
 *  o----&gt;|
 * </pre>
 *
 * or, if {@code A} is a block, just add an empty alt to the end of the
 * block
 */
@NotNull
@Override
public Handle optional(@NotNull GrammarAST optAST, @NotNull Handle blk) {
    BlockStartState blkStart = (BlockStartState) blk.left;
    ATNState blkEnd = blk.right;
    preventEpsilonOptionalBlocks.add(Tuple.create(currentRule, blkStart, blkEnd));
    boolean greedy = ((QuantifierAST) optAST).isGreedy();
    // no way to express SLL restriction
    blkStart.sll = false;
    blkStart.nonGreedy = !greedy;
    epsilon(blkStart, blk.right, !greedy);
    optAST.atnState = blk.left;
    return blk;
}
Also used : QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) StarBlockStartState(org.antlr.v4.runtime.atn.StarBlockStartState) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 17 with NotNull

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

the class ParserATNFactory method newState.

@NotNull
public <T extends ATNState> T newState(@NotNull Class<T> nodeType, GrammarAST node) {
    Exception cause;
    try {
        Constructor<T> ctor = nodeType.getConstructor();
        T s = ctor.newInstance();
        if (currentRule == null)
            s.setRuleIndex(-1);
        else
            s.setRuleIndex(currentRule.index);
        atn.addState(s);
        return s;
    } catch (InstantiationException ex) {
        cause = ex;
    } catch (IllegalAccessException ex) {
        cause = ex;
    } catch (IllegalArgumentException ex) {
        cause = ex;
    } catch (InvocationTargetException ex) {
        cause = ex;
    } catch (NoSuchMethodException ex) {
        cause = ex;
    } catch (SecurityException ex) {
        cause = ex;
    }
    String message = String.format("Could not create %s of type %s.", ATNState.class.getName(), nodeType.getName());
    throw new UnsupportedOperationException(message, cause);
}
Also used : QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) TerminalAST(org.antlr.v4.tool.ast.TerminalAST) BlockAST(org.antlr.v4.tool.ast.BlockAST) AltAST(org.antlr.v4.tool.ast.AltAST) PredAST(org.antlr.v4.tool.ast.PredAST) RecognitionException(org.antlr.runtime.RecognitionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 18 with NotNull

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

the class Target method loadTemplates.

@NotNull
protected STGroup loadTemplates() {
    STGroup result = new STGroupFile(CodeGenerator.TEMPLATE_ROOT + "/" + getLanguage() + "/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION);
    result.registerRenderer(Integer.class, new NumberRenderer());
    result.registerRenderer(String.class, new StringRenderer());
    result.setListener(new STErrorListener() {

        @Override
        public void compileTimeError(STMessage msg) {
            reportError(msg);
        }

        @Override
        public void runTimeError(STMessage msg) {
            reportError(msg);
        }

        @Override
        public void IOError(STMessage msg) {
            reportError(msg);
        }

        @Override
        public void internalError(STMessage msg) {
            reportError(msg);
        }

        private void reportError(STMessage msg) {
            getCodeGenerator().tool.errMgr.toolError(ErrorType.STRING_TEMPLATE_WARNING, msg.cause, msg.toString());
        }
    });
    return result;
}
Also used : STMessage(org.stringtemplate.v4.misc.STMessage) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) STErrorListener(org.stringtemplate.v4.STErrorListener) NumberRenderer(org.stringtemplate.v4.NumberRenderer) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 19 with NotNull

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

the class Trees method getNodeText.

public static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames) {
    if (ruleNames != null) {
        if (t instanceof RuleNode) {
            RuleContext ruleContext = ((RuleNode) t).getRuleContext();
            int ruleIndex = ruleContext.getRuleIndex();
            String ruleName = ruleNames.get(ruleIndex);
            int altNumber = ruleContext.getAltNumber();
            if (altNumber != ATN.INVALID_ALT_NUMBER) {
                return ruleName + ":" + altNumber;
            }
            return ruleName;
        } else if (t instanceof ErrorNode) {
            return t.toString();
        } else if (t instanceof TerminalNode) {
            Token symbol = ((TerminalNode) t).getSymbol();
            if (symbol != null) {
                String s = symbol.getText();
                return s;
            }
        }
    }
    // no recog for rule names
    Object payload = t.getPayload();
    if (payload instanceof Token) {
        return ((Token) payload).getText();
    }
    return t.getPayload().toString();
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token)

Example 20 with NotNull

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

the class ParseTreePattern method findAll.

/**
 * Find all nodes using XPath and then try to match those subtrees against
 * this tree pattern.
 *
 * @param tree The {@link ParseTree} to match against this pattern.
 * @param xpath An expression matching the nodes
 *
 * @return A collection of {@link ParseTreeMatch} objects describing the
 * successful matches. Unsuccessful matches are omitted from the result,
 * regardless of the reason for the failure.
 */
@NotNull
public List<ParseTreeMatch> findAll(@NotNull ParseTree tree, @NotNull String xpath) {
    Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser());
    List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>();
    for (ParseTree t : subtrees) {
        ParseTreeMatch match = match(t);
        if (match.succeeded()) {
            matches.add(match);
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) ParseTree(org.antlr.v4.runtime.tree.ParseTree) 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