Search in sources :

Example 21 with NotNull

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

the class ParseTreePatternMatcher method match.

/**
 * Compare {@code pattern} matched against {@code tree} and return a
 * {@link ParseTreeMatch} object that contains the matched elements, or the
 * node at which the match failed. Pass in a compiled pattern instead of a
 * string representation of a tree pattern.
 */
@NotNull
public ParseTreeMatch match(@NotNull ParseTree tree, @NotNull ParseTreePattern pattern) {
    MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>();
    ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels);
    return new ParseTreeMatch(tree, pattern, labels, mismatchedNode);
}
Also used : MultiMap(org.antlr.v4.runtime.misc.MultiMap) ParseTree(org.antlr.v4.runtime.tree.ParseTree) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 22 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project SpinalTap by airbnb.

the class MysqlSchemaDatabase method addSourcePrefix.

@VisibleForTesting
String addSourcePrefix(@NotNull final String ddl) {
    CharStream charStream = CharStreams.fromString(ddl);
    MySQLLexer lexer = new MySQLLexer(charStream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    MySQLParser parser = new MySQLParser(tokens);
    ParseTree tree = parser.root();
    ParseTreeWalker walker = new ParseTreeWalker();
    MySQLDBNamePrefixAdder prefixAdder = new MySQLDBNamePrefixAdder(tokens);
    walker.walk(prefixAdder, tree);
    return prefixAdder.rewriter.getText();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 23 with NotNull

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

the class SemanticPipeline method hasTypeOrMoreCommand.

boolean hasTypeOrMoreCommand(@NotNull Rule r) {
    GrammarAST ast = r.ast;
    if (ast == null) {
        return false;
    }
    GrammarAST altActionAst = (GrammarAST) ast.getFirstDescendantWithType(ANTLRParser.LEXER_ALT_ACTION);
    if (altActionAst == null) {
        // the rule isn't followed by any commands
        return false;
    }
    // first child is the alt itself, subsequent are the actions
    for (int i = 1; i < altActionAst.getChildCount(); i++) {
        GrammarAST node = (GrammarAST) altActionAst.getChild(i);
        if (node.getType() == ANTLRParser.LEXER_ACTION_CALL) {
            if ("type".equals(node.getChild(0).getText())) {
                return true;
            }
        } else if ("more".equals(node.getText())) {
            return true;
        }
    }
    return false;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Example 24 with NotNull

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

the class TreeViewer method open.

@NotNull
public Future<JDialog> open() {
    final TreeViewer viewer = this;
    viewer.setScale(1.5);
    Callable<JDialog> callable = new Callable<JDialog>() {

        JDialog result;

        @Override
        public JDialog call() throws Exception {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    result = showInDialog(viewer);
                }
            });
            return result;
        }
    };
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        return executor.submit(callable);
    } finally {
        executor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) JDialog(javax.swing.JDialog) Callable(java.util.concurrent.Callable) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 25 with NotNull

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

the class ScopeParser method parseAttributeDef.

/**
 * For decls like "String foo" or "char *foo32[]" compute the ID
 * and type declarations.  Also handle "int x=3" and 'T t = new T("foo")'
 * but if the separator is ',' you cannot use ',' in the initvalue
 * unless you escape use "\," escape.
 */
public static Attribute parseAttributeDef(@Nullable ActionAST action, @NotNull Tuple2<String, Integer> decl, Grammar g) {
    if (decl.getItem1() == null)
        return null;
    Attribute attr = new Attribute();
    int rightEdgeOfDeclarator = decl.getItem1().length() - 1;
    int equalsIndex = decl.getItem1().indexOf('=');
    if (equalsIndex > 0) {
        // everything after the '=' is the init value
        attr.initValue = decl.getItem1().substring(equalsIndex + 1, decl.getItem1().length()).trim();
        rightEdgeOfDeclarator = equalsIndex - 1;
    }
    String declarator = decl.getItem1().substring(0, rightEdgeOfDeclarator + 1);
    Tuple2<Integer, Integer> p;
    String text = decl.getItem1();
    text = text.replaceAll("::", "");
    if (text.contains(":")) {
        // declarator has type appearing after the name like "x:T"
        p = _parsePostfixDecl(attr, declarator, action, g);
    } else {
        // declarator has type appearing before the name like "T x"
        p = _parsePrefixDecl(attr, declarator, action, g);
    }
    int idStart = p.getItem1();
    int idStop = p.getItem2();
    attr.decl = decl.getItem1();
    if (action != null) {
        String actionText = action.getText();
        int[] lines = new int[actionText.length()];
        int[] charPositionInLines = new int[actionText.length()];
        for (int i = 0, line = 0, col = 0; i < actionText.length(); i++, col++) {
            lines[i] = line;
            charPositionInLines[i] = col;
            if (actionText.charAt(i) == '\n') {
                line++;
                col = -1;
            }
        }
        int[] charIndexes = new int[actionText.length()];
        for (int i = 0, j = 0; i < actionText.length(); i++, j++) {
            charIndexes[j] = i;
            // skip comments
            if (i < actionText.length() - 1 && actionText.charAt(i) == '/' && actionText.charAt(i + 1) == '/') {
                while (i < actionText.length() && actionText.charAt(i) != '\n') {
                    i++;
                }
            }
        }
        int declOffset = charIndexes[decl.getItem2()];
        int declLine = lines[declOffset + idStart];
        int line = action.getToken().getLine() + declLine;
        int charPositionInLine = charPositionInLines[declOffset + idStart];
        if (declLine == 0) {
            /* offset for the start position of the ARG_ACTION token, plus 1
				 * since the ARG_ACTION text had the leading '[' stripped before
				 * reaching the scope parser.
				 */
            charPositionInLine += action.getToken().getCharPositionInLine() + 1;
        }
        int offset = ((CommonToken) action.getToken()).getStartIndex();
        attr.token = new CommonToken(action.getToken().getInputStream(), ANTLRParser.ID, BaseRecognizer.DEFAULT_TOKEN_CHANNEL, offset + declOffset + idStart + 1, offset + declOffset + idStop);
        attr.token.setLine(line);
        attr.token.setCharPositionInLine(charPositionInLine);
        assert attr.name.equals(attr.token.getText()) : "Attribute text should match the pseudo-token text at this point.";
    }
    return attr;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) CommonToken(org.antlr.runtime.CommonToken)

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