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();
}
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();
}
}
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;
}
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;
}
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);
}
}
Aggregations