Search in sources :

Example 1 with RuleVersion

use of org.antlr.v4.runtime.RuleVersion 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 2 with RuleVersion

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

the class RuleDependencyProcessor method getRuleVersions.

private int[] getRuleVersions(TypeMirror recognizerClass, String[] ruleNames) {
    int[] versions = new int[ruleNames.length];
    List<? extends Element> elements = processingEnv.getElementUtils().getAllMembers((TypeElement) processingEnv.getTypeUtils().asElement(recognizerClass));
    for (Element element : elements) {
        if (element.getKind() != ElementKind.FIELD) {
            continue;
        }
        VariableElement field = (VariableElement) element;
        boolean isStatic = element.getModifiers().contains(Modifier.STATIC);
        Object constantValue = field.getConstantValue();
        boolean isInteger = constantValue instanceof Integer;
        String name = field.getSimpleName().toString();
        if (isStatic && isInteger && name.startsWith("RULE_")) {
            try {
                name = name.substring("RULE_".length());
                if (name.isEmpty() || !Character.isLowerCase(name.charAt(0))) {
                    continue;
                }
                int index = (Integer) constantValue;
                if (index < 0 || index >= versions.length) {
                    String message = String.format("Rule index %d for rule '%s' out of bounds for recognizer %s.", index, name, recognizerClass.toString());
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
                    continue;
                }
                if (name.indexOf(ATNSimulator.RULE_VARIANT_DELIMITER) >= 0) {
                    // ignore left-factored pseudo-rules
                    continue;
                }
                ExecutableElement ruleMethod = getRuleMethod(recognizerClass, name);
                if (ruleMethod == null) {
                    String message = String.format("Could not find rule method for rule '%s' in recognizer %s.", name, recognizerClass.toString());
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
                    continue;
                }
                RuleVersion ruleVersion = ruleMethod.getAnnotation(RuleVersion.class);
                int version = ruleVersion != null ? ruleVersion.value() : 0;
                versions[index] = version;
            } catch (IllegalArgumentException ex) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Exception occurred while validating rule dependencies.", element);
            }
        }
    }
    return versions;
}
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) ExecutableElement(javax.lang.model.element.ExecutableElement) RuleVersion(org.antlr.v4.runtime.RuleVersion) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

RuleVersion (org.antlr.v4.runtime.RuleVersion)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)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