Search in sources :

Example 61 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class OutputModelController method buildRuleFunction.

/**
 * Create RuleFunction per rule and update sempreds,actions of parser
 *  output object with stuff found in r.
 */
public void buildRuleFunction(Parser parser, Rule r) {
    RuleFunction function = rule(r);
    parser.funcs.add(function);
    pushCurrentRule(function);
    function.fillNamedActions(delegate, r);
    if (r instanceof LeftRecursiveRule) {
        buildLeftRecursiveRuleFunction((LeftRecursiveRule) r, (LeftRecursiveRuleFunction) function);
    } else {
        buildNormalRuleFunction(r, function);
    }
    Grammar g = getGrammar();
    for (ActionAST a : r.actions) {
        if (a instanceof PredAST) {
            PredAST p = (PredAST) a;
            RuleSempredFunction rsf = parser.sempredFuncs.get(r);
            if (rsf == null) {
                rsf = new RuleSempredFunction(delegate, r, function.ctxType);
                parser.sempredFuncs.put(r, rsf);
            }
            rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
        }
    }
    popCurrentRule();
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Action(org.antlr.v4.codegen.model.Action) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) PredAST(org.antlr.v4.tool.ast.PredAST) RuleSempredFunction(org.antlr.v4.codegen.model.RuleSempredFunction) Grammar(org.antlr.v4.tool.Grammar) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 62 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class ParserFactory method ruleRef.

@Override
public List<SrcOp> ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) {
    InvokeRule invokeOp = new InvokeRule(this, ID, label);
    // If no manual label and action refs as token/rule not label, we need to define implicit label
    if (controller.needsImplicitLabel(ID, invokeOp))
        defineImplicitLabel(ID, invokeOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(invokeOp, label);
    return list(invokeOp, listLabelOp);
}
Also used : InvokeRule(org.antlr.v4.codegen.model.InvokeRule) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 63 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class RuleDependencyChecker method getRuleVersions.

private static int[] getRuleVersions(Class<? extends Recognizer<?, ?>> recognizerClass, String[] ruleNames) {
    int[] versions = new int[ruleNames.length];
    Field[] fields = recognizerClass.getFields();
    for (Field field : fields) {
        boolean isStatic = (field.getModifiers() & Modifier.STATIC) != 0;
        boolean isInteger = field.getType() == Integer.TYPE;
        if (isStatic && isInteger && field.getName().startsWith("RULE_")) {
            try {
                String name = field.getName().substring("RULE_".length());
                if (name.isEmpty() || !Character.isLowerCase(name.charAt(0))) {
                    continue;
                }
                int index = field.getInt(null);
                if (index < 0 || index >= versions.length) {
                    Object[] params = { index, field.getName(), recognizerClass.getSimpleName() };
                    LOGGER.log(Level.WARNING, "Rule index {0} for rule ''{1}'' out of bounds for recognizer {2}.", params);
                    continue;
                }
                Method ruleMethod = getRuleMethod(recognizerClass, name);
                if (ruleMethod == null) {
                    Object[] params = { name, recognizerClass.getSimpleName() };
                    LOGGER.log(Level.WARNING, "Could not find rule method for rule ''{0}'' in recognizer {1}.", params);
                    continue;
                }
                RuleVersion ruleVersion = ruleMethod.getAnnotation(RuleVersion.class);
                int version = ruleVersion != null ? ruleVersion.value() : 0;
                versions[index] = version;
            } catch (IllegalArgumentException ex) {
                LOGGER.log(Level.WARNING, null, ex);
            } catch (IllegalAccessException ex) {
                LOGGER.log(Level.WARNING, null, ex);
            }
        }
    }
    return versions;
}
Also used : Field(java.lang.reflect.Field) RuleVersion(org.antlr.v4.runtime.RuleVersion) Method(java.lang.reflect.Method)

Example 64 with Rule

use of org.antlr.v4.tool.Rule 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 65 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class XPath method split.

// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
    XPathLexer lexer = new XPathLexer(CharStreams.fromString(path)) {

        @Override
        public void recover(LexerNoViableAltException e) {
            throw e;
        }
    };
    lexer.removeErrorListeners();
    lexer.addErrorListener(new XPathLexerErrorListener());
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException e) {
        int pos = lexer.getCharPositionInLine();
        String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
        throw new IllegalArgumentException(msg, e);
    }
    List<Token> tokens = tokenStream.getTokens();
    // System.out.println("path="+path+"=>"+tokens);
    List<XPathElement> elements = new ArrayList<XPathElement>();
    int n = tokens.size();
    int i = 0;
    loop: while (i < n) {
        Token el = tokens.get(i);
        Token next = null;
        switch(el.getType()) {
            case XPathLexer.ROOT:
            case XPathLexer.ANYWHERE:
                boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
                i++;
                next = tokens.get(i);
                boolean invert = next.getType() == XPathLexer.BANG;
                if (invert) {
                    i++;
                    next = tokens.get(i);
                }
                XPathElement pathElement = getXPathElement(next, anywhere);
                pathElement.invert = invert;
                elements.add(pathElement);
                i++;
                break;
            case XPathLexer.TOKEN_REF:
            case XPathLexer.RULE_REF:
            case XPathLexer.WILDCARD:
                elements.add(getXPathElement(el, false));
                i++;
                break;
            case Token.EOF:
                break loop;
            default:
                throw new IllegalArgumentException("Unknowth path element " + el);
        }
    }
    return elements.toArray(new XPathElement[0]);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)100 Rule (org.antlr.v4.tool.Rule)95 Test (org.junit.Test)94 ATN (org.antlr.v4.runtime.atn.ATN)87 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)69 ArrayList (java.util.ArrayList)59 Grammar (org.antlr.v4.tool.Grammar)58 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)51 ATNState (org.antlr.v4.runtime.atn.ATNState)42 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)37 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)36 Token (org.antlr.v4.runtime.Token)21 ActionAST (org.antlr.v4.tool.ast.ActionAST)21 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)20 ParseTree (org.antlr.v4.runtime.tree.ParseTree)19 RuleAST (org.antlr.v4.tool.ast.RuleAST)19 HashMap (java.util.HashMap)18 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)16 AltAST (org.antlr.v4.tool.ast.AltAST)16 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)14