Search in sources :

Example 1 with ScopeModel

use of com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel in project dsl-devkit by dsldevkit.

the class ScopeJavaValidator method getAllInheritedScopeDefinitions.

/**
 * Returns a set comprising all inherited scope definitions (excluding local scope definitions).
 *
 * @param context
 *          scope model to retrieve inherited scope definitions for
 * @return all inherited (recursively) scope definitions
 */
private Set<ScopeDefinition> getAllInheritedScopeDefinitions(final ScopeModel context) {
    final Set<ScopeDefinition> defs = Sets.newLinkedHashSet();
    for (ScopeModel include : context.getIncludedScopes()) {
        defs.addAll(include.getScopes());
        defs.addAll(getAllInheritedScopeDefinitions(include));
    }
    return defs;
}
Also used : ScopeDefinition(com.avaloq.tools.ddk.xtext.scope.scope.ScopeDefinition) ScopeModel(com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel)

Example 2 with ScopeModel

use of com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel in project dsl-devkit by dsldevkit.

the class ScopeLinkingService method getIncludedModel.

/**
 * Returns the {@link ScopeModel#getIncludes() included scope model} corresponding to the given node from the node model.
 *
 * @param context
 *          context object
 * @param node
 *          parser node corresponding to include reference
 * @return referenced {@link ScopeModel} to include
 */
private List<EObject> getIncludedModel(final ScopeModel context, final INode node) {
    // based on XtextLinkingService#getUsedGrammar()
    try {
        String scopeModelName = (String) valueConverterService.toValue("", grammarAccess.getDottedIDRule().getName(), node);
        if (scopeModelName != null) {
            final ResourceSet resourceSet = context.eResource().getResourceSet();
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource && ((XtextResource) resource).getLanguageName().equals(languageName) && !resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof ScopeModel) {
                    ScopeModel otherModel = (ScopeModel) rootElement;
                    if (scopeModelName.equals(otherModel.getName())) {
                        return Collections.<EObject>singletonList(otherModel);
                    }
                }
            }
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + scopeModelName.replace('.', '/') + "." + fileExtensionProvider.getPrimaryFileExtension());
            final Resource resource = resourceSet.getResource(classpathURI, true);
            if (!resource.getContents().isEmpty()) {
                final ScopeModel otherModel = (ScopeModel) resource.getContents().get(0);
                if (scopeModelName.equals(otherModel.getName())) {
                    return Collections.<EObject>singletonList(otherModel);
                }
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        LOG.warn("Cannot load included scope.", e);
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        LOG.warn("Cannot convert included scope name.", e);
        return Collections.emptyList();
    }
}
Also used : ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ScopeModel(com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

Example 3 with ScopeModel

use of com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel in project dsl-devkit by dsldevkit.

the class ScopeResourceDescriptionStrategy method createEObjectDescriptions.

@Override
public boolean createEObjectDescriptions(final EObject eObject, final IAcceptor<IEObjectDescription> acceptor) {
    if (getQualifiedNameProvider() == null || !(eObject instanceof ScopeModel)) {
        return false;
    }
    ScopeModel model = (ScopeModel) eObject;
    try {
        QualifiedName qualifiedName = getQualifiedNameProvider().getFullyQualifiedName(model);
        if (qualifiedName != null) {
            Hasher hasher = Hashing.murmur3_32().newHasher(HASHER_CAPACITY);
            hasher.putUnencodedChars(getSourceText(model));
            for (ScopeModel include : model.getIncludedScopes()) {
                hasher.putUnencodedChars(getSourceText(include));
            }
            acceptor.accept(EObjectDescription.create(qualifiedName, model, Collections.singletonMap("fingerprint", hasher.hash().toString())));
        }
    // CHECKSTYLE:CHECK-OFF IllegalCatch
    } catch (RuntimeException e) {
        // CHECKSTYLE:CHECK-ON
        LOG.error(e.getMessage(), e);
    }
    return false;
}
Also used : Hasher(com.google.common.hash.Hasher) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ScopeModel(com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel)

Example 4 with ScopeModel

use of com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel in project dsl-devkit by dsldevkit.

the class ScopeJavaValidator method checkNameFunctionExists.

/**
 * Verifies that a matching default name function exists for scope expressions without explicit name functions.
 *
 * @param expr
 *          scope expression to check
 */
@Check
public void checkNameFunctionExists(final SimpleScopeExpression expr) {
    if (expr.getNaming() != null && !expr.getNaming().getNames().isEmpty()) {
        return;
    }
    ScopeDefinition def = EObjectUtil.eContainer(expr, ScopeDefinition.class);
    ScopeModel model = EObjectUtil.eContainer(expr, ScopeModel.class);
    if (def != null && model != null) {
        EClass scopeType = getType(def);
        NamingSection namingSection = model.getNaming();
        if (namingSection != null) {
            for (NamingDefinition naming : namingSection.getNamings()) {
                if (naming.getType() != null && isLeftMostSuperType(naming.getType(), scopeType)) {
                    return;
                }
            }
        }
        error(NLS.bind(Messages.missingNameFunction, scopeType.getName()), ScopePackage.Literals.SIMPLE_SCOPE_EXPRESSION__EXPR);
    }
}
Also used : ScopeDefinition(com.avaloq.tools.ddk.xtext.scope.scope.ScopeDefinition) EClass(org.eclipse.emf.ecore.EClass) ScopeModel(com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel) NamingSection(com.avaloq.tools.ddk.xtext.scope.scope.NamingSection) NamingDefinition(com.avaloq.tools.ddk.xtext.scope.scope.NamingDefinition) Check(org.eclipse.xtext.validation.Check)

Aggregations

ScopeModel (com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel)4 ScopeDefinition (com.avaloq.tools.ddk.xtext.scope.scope.ScopeDefinition)2 NamingDefinition (com.avaloq.tools.ddk.xtext.scope.scope.NamingDefinition)1 NamingSection (com.avaloq.tools.ddk.xtext.scope.scope.NamingSection)1 Hasher (com.google.common.hash.Hasher)1 URI (org.eclipse.emf.common.util.URI)1 EClass (org.eclipse.emf.ecore.EClass)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 Check (org.eclipse.xtext.validation.Check)1