Search in sources :

Example 1 with Dependents

use of org.antlr.v4.runtime.Dependents in project antlr4 by antlr.

the class Tool method processGrammarsOnCommandLine.

public void processGrammarsOnCommandLine() {
    List<GrammarRootAST> sortedGrammars = sortGrammarByTokenVocab(grammarFiles);
    for (GrammarRootAST t : sortedGrammars) {
        final Grammar g = createGrammar(t);
        g.fileName = t.fileName;
        if (gen_dependencies) {
            BuildDependencyGenerator dep = new BuildDependencyGenerator(this, g);
            /*
					List outputFiles = dep.getGeneratedFileList();
					List dependents = dep.getDependenciesFileList();
					System.out.println("output: "+outputFiles);
					System.out.println("dependents: "+dependents);
					 */
            System.out.println(dep.getDependencies().render());
        } else if (errMgr.getNumErrors() == 0) {
            process(g, true);
        }
    }
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) BuildDependencyGenerator(org.antlr.v4.tool.BuildDependencyGenerator) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Example 2 with Dependents

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

the class Tool method processGrammarsOnCommandLine.

public void processGrammarsOnCommandLine() {
    List<GrammarRootAST> sortedGrammars = sortGrammarByTokenVocab(grammarFiles);
    for (GrammarRootAST t : sortedGrammars) {
        final Grammar g = createGrammar(t);
        g.fileName = t.fileName;
        if (gen_dependencies) {
            BuildDependencyGenerator dep = new BuildDependencyGenerator(this, g);
            /*
					List outputFiles = dep.getGeneratedFileList();
					List dependents = dep.getDependenciesFileList();
					System.out.println("output: "+outputFiles);
					System.out.println("dependents: "+dependents);
					 */
            System.out.println(dep.getDependencies().render());
        } else if (errMgr.getNumErrors() == 0) {
            process(g, true);
        }
    }
}
Also used : GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) BuildDependencyGenerator(org.antlr.v4.tool.BuildDependencyGenerator) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Example 3 with Dependents

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

the class RuleDependencyProcessor method checkDependencies.

private void checkDependencies(List<Tuple2<RuleDependency, Element>> dependencies, TypeMirror recognizerType) {
    String[] ruleNames = getRuleNames(recognizerType);
    int[] ruleVersions = getRuleVersions(recognizerType, ruleNames);
    RuleRelations relations = extractRuleRelations(recognizerType);
    for (Tuple2<RuleDependency, Element> dependency : dependencies) {
        try {
            if (!processingEnv.getTypeUtils().isAssignable(getRecognizerType(dependency.getItem1()), recognizerType)) {
                continue;
            }
            // this is the rule in the dependency set with the highest version number
            int effectiveRule = dependency.getItem1().rule();
            if (effectiveRule < 0 || effectiveRule >= ruleVersions.length) {
                Tuple2<AnnotationMirror, AnnotationValue> ruleReferenceElement = findRuleDependencyProperty(dependency, RuleDependencyProperty.RULE);
                String message = String.format("Rule dependency on unknown rule %d@%d in %s", dependency.getItem1().rule(), dependency.getItem1().version(), getRecognizerType(dependency.getItem1()).toString());
                if (ruleReferenceElement != null) {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, dependency.getItem2(), ruleReferenceElement.getItem1(), ruleReferenceElement.getItem2());
                } else {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, dependency.getItem2());
                }
                continue;
            }
            EnumSet<Dependents> dependents = EnumSet.of(Dependents.SELF, dependency.getItem1().dependents());
            reportUnimplementedDependents(dependency, dependents);
            BitSet checked = new BitSet();
            int highestRequiredDependency = checkDependencyVersion(dependency, ruleNames, ruleVersions, effectiveRule, null);
            if (dependents.contains(Dependents.PARENTS)) {
                BitSet parents = relations.parents[dependency.getItem1().rule()];
                for (int parent = parents.nextSetBit(0); parent >= 0; parent = parents.nextSetBit(parent + 1)) {
                    if (parent < 0 || parent >= ruleVersions.length || checked.get(parent)) {
                        continue;
                    }
                    checked.set(parent);
                    int required = checkDependencyVersion(dependency, ruleNames, ruleVersions, parent, "parent");
                    highestRequiredDependency = Math.max(highestRequiredDependency, required);
                }
            }
            if (dependents.contains(Dependents.CHILDREN)) {
                BitSet children = relations.children[dependency.getItem1().rule()];
                for (int child = children.nextSetBit(0); child >= 0; child = children.nextSetBit(child + 1)) {
                    if (child < 0 || child >= ruleVersions.length || checked.get(child)) {
                        continue;
                    }
                    checked.set(child);
                    int required = checkDependencyVersion(dependency, ruleNames, ruleVersions, child, "child");
                    highestRequiredDependency = Math.max(highestRequiredDependency, required);
                }
            }
            if (dependents.contains(Dependents.ANCESTORS)) {
                BitSet ancestors = relations.getAncestors(dependency.getItem1().rule());
                for (int ancestor = ancestors.nextSetBit(0); ancestor >= 0; ancestor = ancestors.nextSetBit(ancestor + 1)) {
                    if (ancestor < 0 || ancestor >= ruleVersions.length || checked.get(ancestor)) {
                        continue;
                    }
                    checked.set(ancestor);
                    int required = checkDependencyVersion(dependency, ruleNames, ruleVersions, ancestor, "ancestor");
                    highestRequiredDependency = Math.max(highestRequiredDependency, required);
                }
            }
            if (dependents.contains(Dependents.DESCENDANTS)) {
                BitSet descendants = relations.getDescendants(dependency.getItem1().rule());
                for (int descendant = descendants.nextSetBit(0); descendant >= 0; descendant = descendants.nextSetBit(descendant + 1)) {
                    if (descendant < 0 || descendant >= ruleVersions.length || checked.get(descendant)) {
                        continue;
                    }
                    checked.set(descendant);
                    int required = checkDependencyVersion(dependency, ruleNames, ruleVersions, descendant, "descendant");
                    highestRequiredDependency = Math.max(highestRequiredDependency, required);
                }
            }
            int declaredVersion = dependency.getItem1().version();
            if (declaredVersion > highestRequiredDependency) {
                Tuple2<AnnotationMirror, AnnotationValue> versionElement = findRuleDependencyProperty(dependency, RuleDependencyProperty.VERSION);
                String message = String.format("Rule dependency version mismatch: %s has maximum dependency version %d (expected %d) in %s", ruleNames[dependency.getItem1().rule()], highestRequiredDependency, declaredVersion, getRecognizerType(dependency.getItem1()).toString());
                if (versionElement != null) {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, dependency.getItem2(), versionElement.getItem1(), versionElement.getItem2());
                } else {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, dependency.getItem2());
                }
            }
        } catch (AnnotationTypeMismatchException ex) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, String.format("Could not validate rule dependencies for element %s", dependency.getItem2().toString()), dependency.getItem2());
        }
    }
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) BitSet(java.util.BitSet) AnnotationTypeMismatchException(java.lang.annotation.AnnotationTypeMismatchException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Dependents(org.antlr.v4.runtime.Dependents) AnnotationValue(javax.lang.model.element.AnnotationValue) RuleDependency(org.antlr.v4.runtime.RuleDependency)

Aggregations

BuildDependencyGenerator (org.antlr.v4.tool.BuildDependencyGenerator)2 Grammar (org.antlr.v4.tool.Grammar)2 LexerGrammar (org.antlr.v4.tool.LexerGrammar)2 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)2 AnnotationTypeMismatchException (java.lang.annotation.AnnotationTypeMismatchException)1 BitSet (java.util.BitSet)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 Dependents (org.antlr.v4.runtime.Dependents)1 RuleDependency (org.antlr.v4.runtime.RuleDependency)1