use of com.avaloq.tools.ddk.xtext.format.format.GrammarRule in project dsl-devkit by dsldevkit.
the class FormatJavaValidator method checkOverrideMissing.
/**
* Checks that rules declare overrides when there is a corresponding inherited rule.
*
* @param model
* the model
*/
@Check
public void checkOverrideMissing(final FormatConfiguration model) {
FormatConfiguration extendedModel = model.getExtendedFormatConfiguration();
if (extendedModel == null || extendedModel.eIsProxy()) {
return;
}
Iterable<Rule> nonOverrideRules = Iterables.filter(model.getRules(), Predicates.not(IS_OVERRIDE));
Iterable<Rule> overriddenRules = collectRules(extendedModel);
Map<AbstractRule, GrammarRule> localAbstractRuleMap = Maps.newHashMap();
for (GrammarRule rule : Iterables.filter(nonOverrideRules, GrammarRule.class)) {
localAbstractRuleMap.put(TARGET_RULE.apply(rule), rule);
}
// Check GrammarRules
for (GrammarRule overriddenRule : Iterables.filter(overriddenRules, GrammarRule.class)) {
if (localAbstractRuleMap.containsKey(TARGET_RULE.apply(overriddenRule))) {
GrammarRule localRule = localAbstractRuleMap.get(TARGET_RULE.apply(overriddenRule));
error(OVERRIDE_MISSING_MESSAGE, localRule, FormatPackage.Literals.GRAMMAR_RULE__TARGET_RULE, OVERRIDE_MISSING_CODE);
}
}
// Check WildcardRule
if (!Iterables.isEmpty(Iterables.filter(nonOverrideRules, WildcardRule.class)) && !Iterables.isEmpty(Iterables.filter(overriddenRules, WildcardRule.class))) {
error(OVERRIDE_MISSING_MESSAGE, Iterables.filter(nonOverrideRules, WildcardRule.class).iterator().next(), null, OVERRIDE_MISSING_CODE);
}
}
use of com.avaloq.tools.ddk.xtext.format.format.GrammarRule in project dsl-devkit by dsldevkit.
the class FormatExtensions method getAllUniqueGrammarRules.
/**
* Gets the all GrammarRules in the inheritance hierarchy excluding overridden rules.
*
* @param model
* the model
* @param targetClass
* the target class
* @return all unique grammar rules
*/
private static List<GrammarRule> getAllUniqueGrammarRules(final FormatConfiguration model, final Class<?> targetClass) {
final Set<AbstractRule> grammarElements = Sets.newHashSet();
// ensure that identically named rules in a parent grammar are not returned
final Set<String> grammarElementNames = Sets.newHashSet();
List<GrammarRule> allRules = getAllGrammarRules(model);
List<GrammarRule> result = Lists.newArrayList();
for (int i = allRules.size() - 1; i > -1; i--) {
GrammarRule rule = allRules.get(i);
if (grammarElements.add(rule.getTargetRule()) && targetClass.isInstance(rule.getTargetRule()) && grammarElementNames.add(rule.getTargetRule().getName())) {
result.add(0, rule);
}
}
return result;
}
use of com.avaloq.tools.ddk.xtext.format.format.GrammarRule in project dsl-devkit by dsldevkit.
the class FormatExtensions method getAllGrammarRules.
/**
* Gets all GrammarRules in the inheritance hierarchy, including overridden rules.
*
* @param model
* the model
* @return the all grammar rules
*/
private static List<GrammarRule> getAllGrammarRules(final FormatConfiguration model) {
List<GrammarRule> allRules = Lists.newArrayList();
if (model.getExtendedFormatConfiguration() != null && !model.getExtendedFormatConfiguration().eIsProxy()) {
allRules.addAll(getAllGrammarRules(model.getExtendedFormatConfiguration()));
}
Predicate<Rule> resolvedGrammarRules = new Predicate<Rule>() {
@Override
public boolean apply(final Rule input) {
return input instanceof GrammarRule && ((GrammarRule) input).getTargetRule() != null && !((GrammarRule) input).getTargetRule().eIsProxy();
}
};
for (Rule rule : Iterables.filter(model.getRules(), resolvedGrammarRules)) {
allRules.add((GrammarRule) rule);
}
return allRules;
}
use of com.avaloq.tools.ddk.xtext.format.format.GrammarRule in project dsl-devkit by dsldevkit.
the class FormatGeneratorUtil method getAllUniqueGrammarRules.
/**
* Gets the all GrammarRules in the inheritance hierarchy excluding overridden rules.
*
* @param model
* the model
* @param targetClass
* the target class
* @return all unique grammar rules
*/
private static List<GrammarRule> getAllUniqueGrammarRules(final FormatConfiguration model, final Class<?> targetClass) {
final Set<AbstractRule> grammarElements = Sets.newHashSet();
// ensure that identically named rules in a parent grammar are not returned
final Set<String> grammarElementNames = Sets.newHashSet();
List<GrammarRule> allRules = getAllGrammarRules(model);
List<GrammarRule> result = Lists.newArrayList();
for (int i = allRules.size() - 1; i > -1; i--) {
GrammarRule rule = allRules.get(i);
if (grammarElements.add(rule.getTargetRule()) && targetClass.isInstance(rule.getTargetRule()) && grammarElementNames.add(rule.getTargetRule().getName())) {
result.add(0, rule);
}
}
return result;
}
use of com.avaloq.tools.ddk.xtext.format.format.GrammarRule in project dsl-devkit by dsldevkit.
the class FormatJavaValidator method checkRequiredRulesImplemented.
/**
* Check that identically named parent grammar rules are implemented in the model if the parent configuration implements the rule.
* If both grammars have an identically named rule then the extending FormatConfiguration must declare formatting if the parent FormatConfiguration does.
*
* @param model
* the model
*/
@Check
public void checkRequiredRulesImplemented(final FormatConfiguration model) {
FormatConfiguration extendedModel = model.getExtendedFormatConfiguration();
if (extendedModel == null || extendedModel.eIsProxy() || model.getTargetGrammar() == extendedModel.getTargetGrammar()) {
// NOPMD
return;
}
Iterable<GrammarRule> inheritedRules = Iterables.filter(collectRules(extendedModel), GrammarRule.class);
Set<String> ruleNames = Sets.newHashSet(Iterables.transform(inheritedRules, new Function<GrammarRule, String>() {
@Override
public String apply(final GrammarRule from) {
return from.getTargetRule().getName();
}
}));
for (GrammarRule rule : Iterables.filter(model.getRules(), GrammarRule.class)) {
if (rule.getTargetRule() != null && !rule.getTargetRule().eIsProxy()) {
ruleNames.remove(rule.getTargetRule().getName());
}
}
for (AbstractRule rule : model.getTargetGrammar().getRules()) {
if (ruleNames.contains(rule.getName())) {
error(NLS.bind("Required formatting for rule \"{0}\" missing", rule.getName()), FormatPackage.Literals.FORMAT_CONFIGURATION__TARGET_GRAMMAR, GRAMMAR_RULE_MISSING_CODE, rule.getName());
}
}
}
Aggregations