Search in sources :

Example 6 with GrammarAST

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

the class Rule method getAltLabels.

/**
	 * Get {@code #} labels. The keys of the map are the labels applied to outer
	 * alternatives of a lexer rule, and the values are collections of pairs
	 * (alternative number and {@link AltAST}) identifying the alternatives with
	 * this label. Unlabeled alternatives are not included in the result.
	 */
public Map<String, List<Pair<Integer, AltAST>>> getAltLabels() {
    Map<String, List<Pair<Integer, AltAST>>> labels = new LinkedHashMap<String, List<Pair<Integer, AltAST>>>();
    for (int i = 1; i <= numberOfAlts; i++) {
        GrammarAST altLabel = alt[i].ast.altLabel;
        if (altLabel != null) {
            List<Pair<Integer, AltAST>> list = labels.get(altLabel.getText());
            if (list == null) {
                list = new ArrayList<Pair<Integer, AltAST>>();
                labels.put(altLabel.getText(), list);
            }
            list.add(new Pair<Integer, AltAST>(i, alt[i].ast));
        }
    }
    if (labels.isEmpty())
        return null;
    return labels;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) List(java.util.List) AltAST(org.antlr.v4.tool.ast.AltAST) LinkedHashMap(java.util.LinkedHashMap) Pair(org.antlr.v4.runtime.misc.Pair)

Example 7 with GrammarAST

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

the class GrammarAST method dupTree.

public GrammarAST dupTree() {
    GrammarAST t = this;
    CharStream input = this.token.getInputStream();
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
    return (GrammarAST) adaptor.dupTree(t);
}
Also used : GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CharStream(org.antlr.runtime.CharStream)

Example 8 with GrammarAST

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

the class GrammarDependencies method analyse.

private void analyse(File grammarFile, Collection<File> grammarFiles, Tool tool) {
    GrammarRootAST grammar = tool.parseGrammar(grammarFile.getAbsolutePath());
    if (grammar == null)
        return;
    for (GrammarAST importDecl : grammar.getAllChildrenWithType(ANTLRParser.IMPORT)) {
        Tree id = importDecl.getFirstChildWithType(ANTLRParser.ID);
        // being reported by the ANTLR tool
        if (id != null) {
            String grammarPath = getRelativePath(grammarFile);
            graph.addEdge(id.getText() + ".g4", grammarPath);
        }
    }
    for (GrammarAST options : grammar.getAllChildrenWithType(ANTLRParser.OPTIONS)) {
        for (int i = 0, count = options.getChildCount(); i < count; i++) {
            Tree option = options.getChild(i);
            if (option.getType() == ANTLRParser.ASSIGN) {
                String key = option.getChild(0).getText();
                String value = option.getChild(1).getText();
                if ("tokenVocab".equals(key)) {
                    String name = stripQuotes(value);
                    // the grammar name may be qualified, but we resolve the path anyway
                    String grammarName = stripPath(name);
                    String grammarPath = MojoUtils.findSourceSubdir(sourceDirectory, grammarFile);
                    File depGrammarFile = resolve(grammarName, grammarPath);
                    // (files probably reside in the root directory anyway with such a configuration )
                    if (packageName != null)
                        grammarPath = packageName;
                    graph.addEdge(getRelativePath(depGrammarFile), grammarPath + grammarFile.getName());
                }
            }
        }
    }
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Tree(org.antlr.runtime.tree.Tree) File(java.io.File)

Example 9 with GrammarAST

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

the class GrammarASTAdaptor method create.

@Override
public /** Make sure even imaginary nodes know the input stream */
Object create(int tokenType, String text) {
    GrammarAST t;
    if (tokenType == ANTLRParser.RULE) {
        // needed by TreeWizard to make RULE tree
        t = new RuleAST(new CommonToken(tokenType, text));
    } else if (tokenType == ANTLRParser.STRING_LITERAL) {
        // implicit lexer construction done with wizard; needs this node type
        // whereas grammar ANTLRParser.g can use token option to spec node type
        t = new TerminalAST(new CommonToken(tokenType, text));
    } else {
        t = (GrammarAST) super.create(tokenType, text);
    }
    t.token.setInputStream(input);
    return t;
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CommonToken(org.antlr.runtime.CommonToken) TerminalAST(org.antlr.v4.tool.ast.TerminalAST)

Example 10 with GrammarAST

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

the class RuleFunction method getDeclsForAllElements.

/** for all alts, find which ref X or r needs List
	   Must see across alts.  If any alt needs X or r as list, then
	   define as list.
	 */
public Set<Decl> getDeclsForAllElements(List<AltAST> altASTs) {
    Set<String> needsList = new HashSet<String>();
    Set<String> nonOptional = new HashSet<String>();
    List<GrammarAST> allRefs = new ArrayList<GrammarAST>();
    boolean firstAlt = true;
    for (AltAST ast : altASTs) {
        IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF);
        List<GrammarAST> refs = ast.getNodesWithType(reftypes);
        allRefs.addAll(refs);
        Pair<FrequencySet<String>, FrequencySet<String>> minAndAltFreq = getElementFrequenciesForAlt(ast);
        FrequencySet<String> minFreq = minAndAltFreq.a;
        FrequencySet<String> altFreq = minAndAltFreq.b;
        for (GrammarAST t : refs) {
            String refLabelName = t.getText();
            if (altFreq.count(refLabelName) > 1) {
                needsList.add(refLabelName);
            }
            if (firstAlt && minFreq.count(refLabelName) != 0) {
                nonOptional.add(refLabelName);
            }
        }
        for (String ref : nonOptional.toArray(new String[nonOptional.size()])) {
            if (minFreq.count(ref) == 0) {
                nonOptional.remove(ref);
            }
        }
        firstAlt = false;
    }
    Set<Decl> decls = new LinkedHashSet<Decl>();
    for (GrammarAST t : allRefs) {
        String refLabelName = t.getText();
        List<Decl> d = getDeclForAltElement(t, refLabelName, needsList.contains(refLabelName), !nonOptional.contains(refLabelName));
        decls.addAll(d);
    }
    return decls;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) ContextRuleListGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) StructDecl(org.antlr.v4.codegen.model.decl.StructDecl) AttributeDecl(org.antlr.v4.codegen.model.decl.AttributeDecl) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) ContextTokenListGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl) ContextRuleListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl) ContextRuleGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl) ContextTokenListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl) ContextTokenGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl) AltAST(org.antlr.v4.tool.ast.AltAST) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) FrequencySet(org.antlr.v4.misc.FrequencySet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) OrderedHashSet(org.antlr.v4.runtime.misc.OrderedHashSet)

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