Search in sources :

Example 6 with FormatConfiguration

use of com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration 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)

Example 7 with FormatConfiguration

use of com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration in project dsl-devkit by dsldevkit.

the class FormatJavaValidator method checkExtendedGrammarCompatible.

/**
 * Check that extended configuration's grammar is compatible.
 *
 * @param model
 *          the model
 */
@Check
public void checkExtendedGrammarCompatible(final FormatConfiguration model) {
    FormatConfiguration extendedModel = model.getExtendedFormatConfiguration();
    if (extendedModel == null || extendedModel.eIsProxy()) {
        return;
    }
    if (!extendedModel.getTargetGrammar().eIsProxy()) {
        List<Grammar> grammars = Lists.newArrayList(model.getTargetGrammar());
        grammars.addAll(model.getTargetGrammar().getUsedGrammars());
        for (Grammar grammar : grammars) {
            if (extendedModel.getTargetGrammar().getName().equals(grammar.getName())) {
                return;
            }
        }
    }
    error("Extended format configuration has incompatible grammar", FormatPackage.Literals.FORMAT_CONFIGURATION__EXTENDED_FORMAT_CONFIGURATION, EXTENDED_GRAMMAR_INCOMPATIBLE_CODE);
}
Also used : FormatConfiguration(com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration) Grammar(org.eclipse.xtext.Grammar) Check(org.eclipse.xtext.validation.Check)

Example 8 with FormatConfiguration

use of com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration in project dsl-devkit by dsldevkit.

the class FormatJavaValidator method checkIllegalOverride.

/**
 * Checks that no rule declares override when there is no corresponding inherited rule.
 *
 * @param model
 *          the model
 */
@Check
public void checkIllegalOverride(final FormatConfiguration model) {
    Iterable<Rule> overrideRules = Iterables.filter(model.getRules(), IS_OVERRIDE);
    Iterable<Rule> overrideableRules = Lists.newArrayList();
    FormatConfiguration extendedModel = model.getExtendedFormatConfiguration();
    if (extendedModel != null && !extendedModel.eIsProxy()) {
        overrideableRules = collectRules(extendedModel);
    }
    Map<AbstractRule, GrammarRule> overrideableAbstractRuleMap = Maps.newHashMap();
    for (GrammarRule rule : Iterables.filter(overrideableRules, GrammarRule.class)) {
        overrideableAbstractRuleMap.put(TARGET_RULE.apply(rule), rule);
    }
    // Check GrammarRules
    for (GrammarRule overrideRule : Iterables.filter(overrideRules, GrammarRule.class)) {
        if (!overrideableAbstractRuleMap.containsKey(TARGET_RULE.apply(overrideRule))) {
            error(OVERRIDE_ILLEGAL_MESSAGE, overrideRule, FormatPackage.Literals.GRAMMAR_RULE__TARGET_RULE, OVERRIDE_ILLEGAL_CODE);
        }
    }
    // Check WildcardRule
    if (!Iterables.isEmpty(Iterables.filter(overrideRules, WildcardRule.class)) && Iterables.isEmpty(Iterables.filter(overrideableRules, WildcardRule.class))) {
        error(OVERRIDE_ILLEGAL_MESSAGE, Iterables.filter(overrideRules, WildcardRule.class).iterator().next(), null, OVERRIDE_ILLEGAL_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 9 with FormatConfiguration

use of com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration in project dsl-devkit by dsldevkit.

the class FormatLinkingService method getConstant.

/**
 * Tries to find {@link Constant} that my be defined in the same formatter or in any formatter that is extended by the current formatter.
 * An appropriate constant should be matched by comparing its name with the desired one.
 *
 * @param resourceSet
 *          to be used for loading
 * @param node
 *          parse subtree for the reference
 * @return A singleton list containing the desired constant, or an empty list if not found.
 */
private List<EObject> getConstant(final ResourceSet resourceSet, final INode node) {
    String constantFullyQualifiedName = NodeModelUtils.getTokenText(node);
    String formatName = URI.createURI(constantFullyQualifiedName).trimFileExtension().toString();
    if (formatName != null) {
        FormatConfiguration result = loadExtendedFormatConfiguration(formatName, resourceSet);
        if (result != null) {
            EList<Constant> constants = result.getConstants();
            for (Constant constant : constants) {
                if (constantFullyQualifiedName.equals(formatName + "." + constant.getName())) {
                    return Collections.<EObject>singletonList(constant);
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : FormatConfiguration(com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration) Constant(com.avaloq.tools.ddk.xtext.format.format.Constant) EObject(org.eclipse.emf.ecore.EObject)

Example 10 with FormatConfiguration

use of com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration in project dsl-devkit by dsldevkit.

the class FormatHyperlinkHelper method getExtendedRules.

/**
 * Gets all extended rules.
 *
 * @param rule
 *          the rule
 * @return list with all extended rules
 */
private List<Rule> getExtendedRules(final Rule rule) {
    FormatConfiguration model = EObjectUtil.eContainer(rule, FormatConfiguration.class);
    List<Rule> result = Lists.newArrayList();
    for (FormatConfiguration extendedModel : getExtendedModels(model)) {
        for (Rule candidate : extendedModel.getRules()) {
            if (// 
            rule instanceof GrammarRule && candidate instanceof GrammarRule && ((GrammarRule) rule).getTargetRule().equals(((GrammarRule) candidate).getTargetRule())) {
                result.add(candidate);
            } else if (rule instanceof WildcardRule && candidate instanceof WildcardRule) {
                result.add(candidate);
            }
        }
    }
    return result;
}
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) 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)

Aggregations

FormatConfiguration (com.avaloq.tools.ddk.xtext.format.format.FormatConfiguration)11 GrammarRule (com.avaloq.tools.ddk.xtext.format.format.GrammarRule)5 Rule (com.avaloq.tools.ddk.xtext.format.format.Rule)4 WildcardRule (com.avaloq.tools.ddk.xtext.format.format.WildcardRule)4 AbstractRule (org.eclipse.xtext.AbstractRule)4 Check (org.eclipse.xtext.validation.Check)4 EnumRule (org.eclipse.xtext.EnumRule)3 ParserRule (org.eclipse.xtext.ParserRule)3 TerminalRule (org.eclipse.xtext.TerminalRule)3 AbstractValidationTest (com.avaloq.tools.ddk.xtext.test.validation.AbstractValidationTest)2 Test (org.junit.Test)2 Constant (com.avaloq.tools.ddk.xtext.format.format.Constant)1 Function (com.google.common.base.Function)1 IOException (java.io.IOException)1 EObject (org.eclipse.emf.ecore.EObject)1 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)1 Grammar (org.eclipse.xtext.Grammar)1