Search in sources :

Example 26 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class BasicSemanticChecks method checkElementIsOuterMostInSingleAlt.

/**
	 Make sure that action is last element in outer alt; here action,
	 a2, z, and zz are bad, but a3 is ok:
	 (RULE A (BLOCK (ALT {action} 'a')))
	 (RULE B (BLOCK (ALT (BLOCK (ALT {a2} 'x') (ALT 'y')) {a3})))
	 (RULE C (BLOCK (ALT 'd' {z}) (ALT 'e' {zz})))
	 */
protected void checkElementIsOuterMostInSingleAlt(GrammarAST tree) {
    CommonTree alt = tree.parent;
    CommonTree blk = alt.parent;
    boolean outerMostAlt = blk.parent.getType() == RULE;
    Tree rule = tree.getAncestor(RULE);
    String fileName = tree.getToken().getInputStream().getSourceName();
    if (!outerMostAlt || blk.getChildCount() > 1) {
        ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE;
        g.tool.errMgr.grammarError(e, fileName, tree.getToken(), rule.getChild(0).getText());
    }
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree)

Example 27 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class BasicSemanticChecks method finishRule.

@Override
public void finishRule(RuleAST rule, GrammarAST ID, GrammarAST block) {
    if (rule.isLexerRule())
        return;
    BlockAST blk = (BlockAST) rule.getFirstChildWithType(BLOCK);
    int nalts = blk.getChildCount();
    GrammarAST idAST = (GrammarAST) rule.getChild(0);
    for (int i = 0; i < nalts; i++) {
        AltAST altAST = (AltAST) blk.getChild(i);
        if (altAST.altLabel != null) {
            String altLabel = altAST.altLabel.getText();
            // first check that label doesn't conflict with a rule
            // label X or x can't be rule x.
            Rule r = ruleCollector.rules.get(Utils.decapitalize(altLabel));
            if (r != null) {
                g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_CONFLICTS_WITH_RULE, g.fileName, altAST.altLabel.token, altLabel, r.name);
            }
            // Now verify that label X or x doesn't conflict with label
            // in another rule. altLabelToRuleName has both X and x mapped.
            String prevRuleForLabel = ruleCollector.altLabelToRuleName.get(altLabel);
            if (prevRuleForLabel != null && !prevRuleForLabel.equals(rule.getRuleName())) {
                g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_REDEF, g.fileName, altAST.altLabel.token, altLabel, rule.getRuleName(), prevRuleForLabel);
            }
        }
    }
    List<GrammarAST> altLabels = ruleCollector.ruleToAltLabels.get(rule.getRuleName());
    int numAltLabels = 0;
    if (altLabels != null)
        numAltLabels = altLabels.size();
    if (numAltLabels > 0 && nalts != numAltLabels) {
        g.tool.errMgr.grammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS, g.fileName, idAST.token, rule.getRuleName());
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) BlockAST(org.antlr.v4.tool.ast.BlockAST) AltAST(org.antlr.v4.tool.ast.AltAST) Rule(org.antlr.v4.tool.Rule)

Example 28 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class Tool method sortGrammarByTokenVocab.

public List<GrammarRootAST> sortGrammarByTokenVocab(List<String> fileNames) {
    //		System.out.println(fileNames);
    Graph<String> g = new Graph<String>();
    List<GrammarRootAST> roots = new ArrayList<GrammarRootAST>();
    for (String fileName : fileNames) {
        GrammarAST t = parseGrammar(fileName);
        // came back as error node
        if (t == null || t instanceof GrammarASTErrorNode)
            continue;
        if (((GrammarRootAST) t).hasErrors)
            continue;
        GrammarRootAST root = (GrammarRootAST) t;
        roots.add(root);
        root.fileName = fileName;
        String grammarName = root.getChild(0).getText();
        GrammarAST tokenVocabNode = findOptionValueAST(root, "tokenVocab");
        // Make grammars depend on any tokenVocab options
        if (tokenVocabNode != null) {
            String vocabName = tokenVocabNode.getText();
            // Strip quote characters if any
            int len = vocabName.length();
            int firstChar = vocabName.charAt(0);
            int lastChar = vocabName.charAt(len - 1);
            if (len >= 2 && firstChar == '\'' && lastChar == '\'') {
                vocabName = vocabName.substring(1, len - 1);
            }
            // If the name contains a path delimited by forward slashes,
            // use only the part after the last slash as the name
            int lastSlash = vocabName.lastIndexOf('/');
            if (lastSlash >= 0) {
                vocabName = vocabName.substring(lastSlash + 1);
            }
            g.addEdge(grammarName, vocabName);
        }
        // add cycle to graph so we always process a grammar if no error
        // even if no dependency
        g.addEdge(grammarName, grammarName);
    }
    List<String> sortedGrammarNames = g.sort();
    //		System.out.println("sortedGrammarNames="+sortedGrammarNames);
    List<GrammarRootAST> sortedRoots = new ArrayList<GrammarRootAST>();
    for (String grammarName : sortedGrammarNames) {
        for (GrammarRootAST root : roots) {
            if (root.getGrammarName().equals(grammarName)) {
                sortedRoots.add(root);
                break;
            }
        }
    }
    return sortedRoots;
}
Also used : Graph(org.antlr.v4.misc.Graph) GrammarASTErrorNode(org.antlr.v4.tool.ast.GrammarASTErrorNode) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 29 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class ParserATNFactory method newState.

public <T extends ATNState> T newState(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)

Example 30 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class ParserATNFactory method rule.

/* start->ruleblock->end */
@Override
public Handle rule(GrammarAST ruleAST, String name, Handle blk) {
    Rule r = g.getRule(name);
    RuleStartState start = atn.ruleToStartState[r.index];
    epsilon(start, blk.left);
    RuleStopState stop = atn.ruleToStopState[r.index];
    epsilon(blk.right, stop);
    Handle h = new Handle(start, stop);
    //		ATNPrinter ser = new ATNPrinter(g, h.left);
    //		System.out.println(ruleAST.toStringTree()+":\n"+ser.asString());
    ruleAST.atnState = start;
    return h;
}
Also used : RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)55 Rule (org.antlr.v4.tool.Rule)20 ATNState (org.antlr.v4.runtime.atn.ATNState)15 ArrayList (java.util.ArrayList)12 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)12 Grammar (org.antlr.v4.tool.Grammar)8 Decl (org.antlr.v4.codegen.model.decl.Decl)7 ActionAST (org.antlr.v4.tool.ast.ActionAST)7 AltAST (org.antlr.v4.tool.ast.AltAST)7 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)7 LinkedHashMap (java.util.LinkedHashMap)6 Token (org.antlr.runtime.Token)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 HashMap (java.util.HashMap)5 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)5 Pair (org.antlr.v4.runtime.misc.Pair)5 LexerGrammar (org.antlr.v4.tool.LexerGrammar)5 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5