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;
}
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;
}
Aggregations