Search in sources :

Example 6 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class XtextLinkingService method getPackage.

private List<EObject> getPackage(ReferencedMetamodel context, ILeafNode text) {
    String nsUri = getMetamodelNsURI(text);
    if (nsUri == null)
        return Collections.emptyList();
    Grammar grammar = GrammarUtil.getGrammar(context);
    Set<Grammar> visitedGrammars = new HashSet<Grammar>();
    for (Grammar usedGrammar : grammar.getUsedGrammars()) {
        List<EObject> result = getPackage(nsUri, usedGrammar, visitedGrammars);
        if (result != null)
            return result;
    }
    QualifiedName packageNsURI = QualifiedName.create(nsUri);
    EPackage pack = findPackageInScope(context, packageNsURI);
    if (pack == null) {
        pack = findPackageInAllDescriptions(context, packageNsURI);
        if (pack == null) {
            pack = loadEPackage(nsUri, context.eResource().getResourceSet());
        }
    }
    if (pack != null)
        return Collections.<EObject>singletonList(pack);
    return Collections.emptyList();
}
Also used : EObject(org.eclipse.emf.ecore.EObject) QualifiedName(org.eclipse.xtext.naming.QualifiedName) Grammar(org.eclipse.xtext.Grammar) HashSet(java.util.HashSet) EPackage(org.eclipse.emf.ecore.EPackage)

Example 7 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class XtextLinkingService method getUsedGrammar.

private List<EObject> getUsedGrammar(Grammar grammar, INode node) {
    try {
        String grammarName = (String) valueConverterService.toValue("", "GrammarID", node);
        if (grammarName != null) {
            final ResourceSet resourceSet = grammar.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) {
                    IParseResult parseResult = ((XtextResource) resource).getParseResult();
                    if (parseResult != null)
                        rootElement = parseResult.getRootASTElement();
                } else if (!resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof Grammar) {
                    Grammar otherGrammar = (Grammar) rootElement;
                    if (grammarName.equals(otherGrammar.getName())) {
                        if (resource instanceof DerivedStateAwareResource)
                            resource.getContents();
                        return Collections.<EObject>singletonList(otherGrammar);
                    }
                }
            }
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + "." + fileExtension);
            URI normalizedURI = null;
            if (resourceSet instanceof XtextResourceSet) {
                XtextResourceSet set = (XtextResourceSet) resourceSet;
                normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
            } else {
                normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
            }
            final Resource resource = resourceSet.getResource(normalizedURI, true);
            if (!resource.getContents().isEmpty()) {
                final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
                if (grammarName.equals(usedGrammar.getName()))
                    return Collections.<EObject>singletonList(usedGrammar);
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Grammar(org.eclipse.xtext.Grammar) URI(org.eclipse.emf.common.util.URI) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 8 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class XtextResourceDescriptionStrategy method createEObjectDescriptions.

@Override
public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
    if (eObject instanceof Grammar) {
        String grammarName = ((Grammar) eObject).getName();
        if (!Strings.isEmpty(grammarName)) {
            QualifiedName qualifiedName = defaultQualifiedNameConverter.toQualifiedName(grammarName);
            acceptor.accept(EObjectDescription.create(qualifiedName, eObject));
        }
    } else if (eObject instanceof AbstractMetamodelDeclaration || eObject instanceof AbstractRule)
        return super.createEObjectDescriptions(eObject, acceptor);
    return eObject instanceof Grammar;
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 9 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class XtextScopeProvider method createEPackageScope.

protected IScope createEPackageScope(final Grammar grammar) {
    final List<Grammar> allGrammars = getAllGrammars(grammar);
    IScope current = new SimpleScope(IScope.NULLSCOPE, Iterables.transform(EPackage.Registry.INSTANCE.keySet(), new Function<String, IEObjectDescription>() {

        @Override
        public IEObjectDescription apply(String from) {
            InternalEObject proxyPackage = (InternalEObject) EcoreFactory.eINSTANCE.createEPackage();
            proxyPackage.eSetProxyURI(URI.createURI(from));
            return EObjectDescription.create(QualifiedName.create(from), proxyPackage, Collections.singletonMap("nsURI", "true"));
        }
    }));
    for (int i = allGrammars.size() - 1; i >= 0; i--) {
        current = createEPackageScope(allGrammars.get(i), current);
    }
    return current;
}
Also used : Function(com.google.common.base.Function) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) IScope(org.eclipse.xtext.scoping.IScope) Grammar(org.eclipse.xtext.Grammar) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 10 with Grammar

use of org.eclipse.xtext.Grammar in project xtext-core by eclipse.

the class XtextValidator method checkMetamodelUris.

@Check
public void checkMetamodelUris(final AbstractMetamodelDeclaration declaration) {
    if (!(declaration.getEPackage() != null && declaration.getEPackage().getNsURI() != null))
        return;
    Grammar grammar = GrammarUtil.getGrammar(declaration);
    Iterable<String> nsUris = Iterables.transform(grammar.getMetamodelDeclarations(), new Function<AbstractMetamodelDeclaration, String>() {

        @Override
        public String apply(AbstractMetamodelDeclaration param) {
            if (param.getEPackage() != null)
                return param.getEPackage().getNsURI();
            return null;
        }
    });
    int count = Iterables.size(Iterables.filter(nsUris, new Predicate<String>() {

        @Override
        public boolean apply(String param) {
            return declaration.getEPackage().getNsURI().equals(param);
        }
    }));
    if (count != 1) {
        error("EPackage with ns-uri '" + declaration.getEPackage().getNsURI() + "' is used twice.", XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE);
    }
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Predicate(com.google.common.base.Predicate) Check(org.eclipse.xtext.validation.Check)

Aggregations

Grammar (org.eclipse.xtext.Grammar)356 Test (org.junit.Test)273 ParserRule (org.eclipse.xtext.ParserRule)151 AbstractRule (org.eclipse.xtext.AbstractRule)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)101 EObject (org.eclipse.emf.ecore.EObject)70 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 XtextResource (org.eclipse.xtext.resource.XtextResource)33 RuleCall (org.eclipse.xtext.RuleCall)28 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)23 TypeReference (org.eclipse.xtext.xtext.generator.model.TypeReference)23 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)21 Assignment (org.eclipse.xtext.Assignment)21 EPackage (org.eclipse.emf.ecore.EPackage)20 AbstractElement (org.eclipse.xtext.AbstractElement)17 TerminalRule (org.eclipse.xtext.TerminalRule)17 Resource (org.eclipse.emf.ecore.resource.Resource)16 EClassifier (org.eclipse.emf.ecore.EClassifier)13 InternalEObject (org.eclipse.emf.ecore.InternalEObject)13 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)12