Search in sources :

Example 1 with GrammarRule

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);
    }
}
Also used : FormatConfiguration(com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) EnumRule(org.eclipse.xtext.EnumRule) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) TerminalRule(org.eclipse.xtext.TerminalRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) AbstractRule(org.eclipse.xtext.AbstractRule) Check(org.eclipse.xtext.validation.Check)

Example 2 with GrammarRule

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;
}
Also used : GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 3 with GrammarRule

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;
}
Also used : GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) EnumRule(org.eclipse.xtext.EnumRule) WildcardRule(com.avaloq.tools.ddk.xtext.format.format.WildcardRule) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) TerminalRule(org.eclipse.xtext.TerminalRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) Rule(com.avaloq.tools.ddk.xtext.format.format.Rule) Predicate(com.google.common.base.Predicate)

Example 4 with GrammarRule

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;
}
Also used : GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 5 with GrammarRule

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());
        }
    }
}
Also used : Function(com.google.common.base.Function) FormatConfiguration(com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration) GrammarRule(com.avaloq.tools.ddk.xtext.format.format.GrammarRule) AbstractRule(org.eclipse.xtext.AbstractRule) Check(org.eclipse.xtext.validation.Check)

Aggregations

GrammarRule (com.avaloq.tools.ddk.xtext.format.format.GrammarRule)9 AbstractRule (org.eclipse.xtext.AbstractRule)7 WildcardRule (com.avaloq.tools.ddk.xtext.format.format.WildcardRule)6 FormatConfiguration (com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration)5 Rule (com.avaloq.tools.ddk.xtext.format.format.Rule)5 ParserRule (org.eclipse.xtext.ParserRule)5 EnumRule (org.eclipse.xtext.EnumRule)4 TerminalRule (org.eclipse.xtext.TerminalRule)4 Check (org.eclipse.xtext.validation.Check)3 Predicate (com.google.common.base.Predicate)2 ColumnLocator (com.avaloq.tools.ddk.xtext.format.format.ColumnLocator)1 Constant (com.avaloq.tools.ddk.xtext.format.format.Constant)1 ContextFreeDirective (com.avaloq.tools.ddk.xtext.format.format.ContextFreeDirective)1 GrammarElementLookup (com.avaloq.tools.ddk.xtext.format.format.GrammarElementLookup)1 GrammarElementReference (com.avaloq.tools.ddk.xtext.format.format.GrammarElementReference)1 GroupBlock (com.avaloq.tools.ddk.xtext.format.format.GroupBlock)1 IndentLocator (com.avaloq.tools.ddk.xtext.format.format.IndentLocator)1 IntValue (com.avaloq.tools.ddk.xtext.format.format.IntValue)1 KeywordPair (com.avaloq.tools.ddk.xtext.format.format.KeywordPair)1 LinewrapLocator (com.avaloq.tools.ddk.xtext.format.format.LinewrapLocator)1