org.eclipse.xtext.AbstractMetamodelDeclaration Java Examples

The following examples show how to use org.eclipse.xtext.AbstractMetamodelDeclaration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNameClash_03() throws Exception {
	String grammarAsText =
			"grammar test with org.eclipse.xtext.common.Terminals\n" +
			"generate test 'http://test'\n" +
			"Foo: myVars=ID my_vars=ID;\n";
	
	Grammar grammar = (Grammar) getModel(grammarAsText);
	AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
	
	XtextValidator validator = get(XtextValidator.class);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
	CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives();
	messageAcceptor.expectedContext(
			grammar.getRules().get(0).getType(),
			element.getElements().get(0),
			element.getElements().get(1)
	);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
	messageAcceptor.validate();
}
 
Example #2
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public List<EPackage> getGeneratedPackages() {
	final List<EPackage> result = new ArrayList<EPackage>();
	final ResourceSet resourceSet = grammar.eResource().getResourceSet();
	if (resourceSet == null)
		throw new NullPointerException("resourceSet may not be null");
	Iterables.addAll(result, Iterables.filter(Iterables.transform(
			Iterables.filter(grammar.getMetamodelDeclarations(), GeneratedMetamodel.class),
			new Function<AbstractMetamodelDeclaration, EPackage>() {
				@Override
				public EPackage apply(AbstractMetamodelDeclaration param) {
					EPackage pack = (EPackage) param.eGet(XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, false);
					if (pack != null && !pack.eIsProxy()) {
						return pack;
					}
					return null;
				}
			}), Predicates.notNull()));
	return getPackagesSortedByName(result);
}
 
Example #3
Source File: XtextRenameStrategyProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected IRenameStrategy createRenameStrategy(final EObject targetElement, final IRenameElementContext context) {
	return new XtextSwitch<IRenameStrategy>() {

		@Override
		public IRenameStrategy caseAbstractMetamodelDeclaration(AbstractMetamodelDeclaration metamodelDeclaration) {
			return metamodelDeclarationProvider.get();
		}

		@Override
		public IRenameStrategy caseAbstractRule(AbstractRule rule) {
			return ruleProvider.get();
		}

		@Override
		public IRenameStrategy caseGrammar(Grammar grammar) {
			return XtextRenameStrategyProvider.super.createRenameStrategy(targetElement, context);
		}
	}.doSwitch(targetElement);
}
 
Example #4
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private TypeRef getOrComputeReturnType(AbstractRule rule) {
	TypeRef result = rule.getType();
	if (result == null) {
		EClassifier classifier = getClassifierFor(rule);
		if (classifier == null) {
			if (rule.getName() == null)
				return null;
			result = getTypeRef(rule.getName());
		} else
			result = getTypeRef(classifier);
		if (result.getMetamodel() == null) {
			AbstractMetamodelDeclaration bestMatch = null;
			for (AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
				if (decl instanceof GeneratedMetamodel && Strings.isEmpty(decl.getAlias())) {
					bestMatch = decl;
					break;
				}
			}
			if (result.getMetamodel() == null)
				result.setMetamodel(bestMatch);
		}
		rule.setType(result);
	}
	return result;
}
 
Example #5
Source File: AbstractTemplateVariableResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected EClassifier getEClassifierForGrammar(String fqnClassName,
		Grammar grammar) {
	int dotIndex = fqnClassName.indexOf('.');
	String packageName = null;
	String className = fqnClassName;
	if (dotIndex > 0) {
		packageName = fqnClassName.substring(0, dotIndex);
		className = fqnClassName.substring(dotIndex + 1);
	}
	List<AbstractMetamodelDeclaration> allMetamodelDeclarations = GrammarUtil
			.allMetamodelDeclarations(grammar);
	for (AbstractMetamodelDeclaration decl : allMetamodelDeclarations) {
		EPackage pack = decl.getEPackage();
		if (packageName == null || pack.getName().equals(packageName)) {
			EClassifier eClassifier = pack.getEClassifier(className);
			if (eClassifier != null) {
				return eClassifier;
			}
		}
	}
	return null;
}
 
Example #6
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private EClassifierInfos createClassifierInfosFor(Grammar grammar, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar))
		return null;
	final EClassifierInfos result = new EClassifierInfos(grammar);
	for(AbstractMetamodelDeclaration declaration: grammar.getMetamodelDeclarations()) {
		final EPackage referencedEPackage = declaration.getEPackage();
		if (referencedEPackage != null) {
			collectClassInfosOf(result, referencedEPackage, declaration, false);
		}
	}
	for(Grammar usedGrammar: grammar.getUsedGrammars()) {
		EClassifierInfos parent = createClassifierInfosFor(usedGrammar, visitedGrammars);
		if (parent != null)
			result.addParent(parent);
	}
	return result;
}
 
Example #7
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void checkPackageRemovalAfterGrammarChange(final boolean isRemoved, final String originalGrammar, final int offset, final int length, final String replacement) throws Exception {
  final XtextResource resource = this.getResourceFromStringAndExpect(originalGrammar, 1);
  EObject _get = resource.getContents().get(0);
  Grammar grammar = ((Grammar) _get);
  AbstractMetamodelDeclaration generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
  EPackage ePackage = generatedMetamodel.getEPackage();
  Assert.assertEquals(ePackage.eResource().getResourceSet(), resource.getResourceSet());
  resource.update(offset, length, replacement);
  if (isRemoved) {
    Assert.assertNull(ePackage.eResource().getResourceSet());
  } else {
    Assert.assertEquals(ePackage.eResource().getResourceSet(), resource.getResourceSet());
  }
  EObject _get_1 = resource.getContents().get(0);
  grammar = ((Grammar) _get_1);
  generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
  ePackage = generatedMetamodel.getEPackage();
  Assert.assertEquals(resource.getResourceSet(), ePackage.eResource().getResourceSet());
}
 
Example #8
Source File: XtextLinker.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
void discardGeneratedPackages(EObject root) {
	if (root instanceof Grammar) {
		// unload generated metamodels as they will be recreated during linking 
		for (AbstractMetamodelDeclaration metamodelDeclaration : ((Grammar) root).getMetamodelDeclarations()) {
			if (metamodelDeclaration instanceof GeneratedMetamodel) {
				EPackage ePackage = (EPackage) metamodelDeclaration.eGet(XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, false);
				if (ePackage != null && !ePackage.eIsProxy()) {
					Resource resource = ePackage.eResource();
					if (resource != null && resource.getResourceSet() != null) {
						if (unloader != null) {
							for (EObject content : resource.getContents()) {
								unloader.unloadRoot(content);
							}
						}
						resource.getResourceSet().getResources().remove(resource);
					}
				}
			}
		}
	}
}
 
Example #9
Source File: XtextLinker.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void processMetamodelDeclarations(Collection<AbstractMetamodelDeclaration> declarations, ResourceSet resourceSet,
		Collection<Resource> resourcesToRemove, Collection<Resource> resourcesToUnload,
		Collection<Resource> referencedResources) {
	for (AbstractMetamodelDeclaration declaration : declarations) {
		EPackage pack = (EPackage) declaration.eGet(XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, false);
		if (pack != null && !pack.eIsProxy()) {
			Resource packResource = pack.eResource();
			if (packResource != null) {
				resourcesToRemove.add(packResource);
				if (declaration instanceof ReferencedMetamodel) {
						resourcesToUnload.add(packResource);
				}
				if (isPackageReferenced(resourceSet, pack, declarations)) {
					referencedResources.add(packResource);
				}
			}
		}
	}
}
 
Example #10
Source File: XtextLinker.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public boolean isPackageReferenced(ResourceSet set, EPackage pack,
		Collection<AbstractMetamodelDeclaration> knownReferences) {
	for (int i = 0; i < set.getResources().size(); i++) { 
		// cannot use foreach since we may get
		// a CME due to proxy resolution and transparent
		// loading of resources
		Resource resource = set.getResources().get(i);
		if (resource != null) {
			for (EObject content : resource.getContents()) {
				if (content instanceof Grammar) {
					for (AbstractMetamodelDeclaration decl : ((Grammar) content).getMetamodelDeclarations()) {
						if (pack.equals(decl.getEPackage()) && !knownReferences.contains(decl))
							return true;
					}
				}
			}
		}
	}
	return false;
}
 
Example #11
Source File: XtextMetamodelReferenceHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
static List<EObject> findBestMetamodelForType(TypeRef context, final String alias, String typeName, IScope scope) {
	final List<AbstractMetamodelDeclaration> generatedMetamodels = new ArrayList<AbstractMetamodelDeclaration>();
	final List<AbstractMetamodelDeclaration> importedMetamodels = new ArrayList<AbstractMetamodelDeclaration>();
	filterMetamodelsInScope(alias, scope, generatedMetamodels, importedMetamodels);
	final List<AbstractMetamodelDeclaration> exactMatches = new ArrayList<AbstractMetamodelDeclaration>();
	filterExactMatches(alias, importedMetamodels, exactMatches);
	List<EObject> result = findReferencedMetamodelWithType(typeName, exactMatches);
	if (result != null)
		return result;
	result = findReferencedMetamodelWithType(typeName, importedMetamodels);
	if (result != null)
		return result;
	result = findSingleElementInCollections(alias, generatedMetamodels);
	if (result != null)
		return result;
	result = findSingleElementInCollections(alias, importedMetamodels);
	if (result != null)
		return result;
	return Collections.emptyList();
}
 
Example #12
Source File: XtextMetamodelReferenceHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private static List<EObject> findReferencedMetamodelWithType(String typeName, List<AbstractMetamodelDeclaration> candidates) {
	AbstractMetamodelDeclaration result = null;
	for (AbstractMetamodelDeclaration metamodel : candidates) {
		if (metamodel instanceof ReferencedMetamodel) {
			EPackage pack = metamodel.getEPackage();
			if (pack != null) {
				final EClassifier classifier = pack.getEClassifier(typeName);
				if (classifier != null) {
					if (result == null)
						result = metamodel;
					else
						return Collections.emptyList();
				}
			}
		}
	}
	if (result != null)
		return Collections.<EObject>singletonList(result);
	return null;
}
 
Example #13
Source File: Xtext2EcoreTransformerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Deprecated
@Test
public void testPostProcessorHook() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals import \'http://www.eclipse.org/emf/2002/Ecore\' as ecore  generate test \'http://test\' MyRule: myFeature=INT;");
  final String xtextGrammar = _builder.toString();
  EObject _model = this.getModel(xtextGrammar);
  final Grammar grammar = ((Grammar) _model);
  final Xtext2EcoreTransformer transformer = new Xtext2EcoreTransformer(grammar);
  AbstractMetamodelDeclaration _get = grammar.getMetamodelDeclarations().get(1);
  GeneratedMetamodel testMetamodel = ((GeneratedMetamodel) _get);
  final Xtext2EcoreTransformerTest.MockedXtext2EcorePostProcessor postProcessor = new Xtext2EcoreTransformerTest.MockedXtext2EcorePostProcessor(testMetamodel);
  transformer.setPostProcessor(postProcessor);
  transformer.transform();
  Assert.assertEquals("process mthde called once", 1, postProcessor.proccessMethCalled());
}
 
Example #14
Source File: XtextScopeProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected IScope createScope(final Grammar grammar, EClass type, IScope current) {
	if (EcorePackage.Literals.EPACKAGE == type) {
		return createEPackageScope(grammar);
	} else if (AbstractMetamodelDeclaration.class.isAssignableFrom(type.getInstanceClass())) {
		return new SimpleScope(IScope.NULLSCOPE,Iterables.transform(grammar.getMetamodelDeclarations(),
						new Function<AbstractMetamodelDeclaration,IEObjectDescription>(){
							@Override
							public IEObjectDescription apply(AbstractMetamodelDeclaration from) {
								String name = from.getAlias() != null ? from.getAlias() : "";
								return EObjectDescription.create(QualifiedName.create(name), from);
							}
						}));
	}
	final List<Grammar> allGrammars = getAllGrammars(grammar);
	for (int i = allGrammars.size() - 1; i >= 0; i--) {
		current = doCreateScope(allGrammars.get(i), type, current);
	}
	return current;
}
 
Example #15
Source File: NodeModelUtilsTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testFindNodesForFeature() throws Exception {
	Grammar grammar = (Grammar) getModel("grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model : name=ID;");
	List<INode> nodes = NodeModelUtils.findNodesForFeature(grammar, XtextPackage.eINSTANCE.getGrammar_Name());
	assertEquals(1, nodes.size());
	assertEquals("foo.Bar", nodes.get(0).getText().trim());
	
	nodes = NodeModelUtils.findNodesForFeature(grammar, XtextPackage.eINSTANCE.getGrammar_Rules());
	assertEquals(1, nodes.size());
	assertEquals("Model : name=ID;", nodes.get(0).getText().trim());
	
	AbstractMetamodelDeclaration declaration = grammar.getMetamodelDeclarations().get(0);
	nodes = NodeModelUtils.findNodesForFeature(declaration, XtextPackage.eINSTANCE.getGeneratedMetamodel_Name());
	assertEquals(1, nodes.size());
	assertEquals("foo", nodes.get(0).getText().trim());
	
	nodes = NodeModelUtils.findNodesForFeature(declaration, null);
	assertEquals(0, nodes.size());
}
 
Example #16
Source File: XtextCrossReferenceSerializer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected String getUnconvertedLinkText(EObject object, EReference reference, EObject context) {
	if (reference == XtextPackage.eINSTANCE.getGrammar_UsedGrammars())
		return ((Grammar) object).getName();
	if (reference == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
		AbstractMetamodelDeclaration casted = (AbstractMetamodelDeclaration) object;
		return casted.getAlias();
	}
	if (reference == XtextPackage.eINSTANCE.getAbstractMetamodelDeclaration_EPackage())
		return ((EPackage) object).getNsURI();
	if (object instanceof AbstractRule) {
		if (reference == XtextPackage.eINSTANCE.getRuleCall_Rule()) {
			if (((RuleCall)context).isExplicitlyCalled()) {
				return super.getUnconvertedLinkText(object, reference, context);
			}
		}
		return ((AbstractRule) object).getName();
	}
	return super.getUnconvertedLinkText(object, reference, context);
}
 
Example #17
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testCycleInTypeHierarchy() throws Exception {
	String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals" +
			" generate test 'http://test'";
	grammarAsText += " RuleA: RuleB;";
	grammarAsText += " RuleB: RuleC;";
	grammarAsText += " RuleC: RuleA;";
	grammarAsText += " RuleD: RuleA;";

	Grammar grammar = (Grammar) getModel(grammarAsText);
	AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
	
	XtextValidator validator = get(XtextValidator.class);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(grammar.getRules().get(0).getType(), true, false);
	messageAcceptor.expectedContext(
			grammar.getRules().get(1).getType(),
			grammar.getRules().get(2).getType()
	);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
	messageAcceptor.validate();
}
 
Example #18
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNameClash_01() throws Exception {
	String grammarAsText =
			"grammar test with org.eclipse.xtext.common.Terminals\n" +
			"generate test 'http://test'\n" +
			"PRINTF: vars=PRINTF_Vars;\n" +
			"PRINTF_Vars: arg1=ID;";
	
	Grammar grammar = (Grammar) getModel(grammarAsText);
	AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
	
	XtextValidator validator = get(XtextValidator.class);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
	messageAcceptor.expectedContext(
			grammar.getRules().get(0).getAlternatives(),
			grammar.getRules().get(1).getType()
	);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkGeneratedPackageForNameClashes((GeneratedMetamodel) metamodelDeclaration);
	messageAcceptor.validate();
}
 
Example #19
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNameClash_02() throws Exception {
	String grammarAsText =
			"grammar test with org.eclipse.xtext.common.Terminals\n" +
			"generate test 'http://test'\n" +
			"Class returns Class: {Class_} name=ID;\n";
	
	Grammar grammar = (Grammar) getModel(grammarAsText);
	AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
	
	XtextValidator validator = get(XtextValidator.class);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
	CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives();
	messageAcceptor.expectedContext(
			grammar.getRules().get(0).getType(),
			((Action) element.getElements().get(0)).getType()
	);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
	messageAcceptor.validate();
}
 
Example #20
Source File: GrammarAccessFragment.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void replaceResourceURIsWithNsURIs(Grammar grammar, ResourceSet set) {
	for (AbstractMetamodelDeclaration metamodelDecl : grammar.getMetamodelDeclarations()) {
		EPackage pack = metamodelDecl.getEPackage();
		Resource packResource = pack.eResource();
		if (!packResource.getURI().toString().equals(pack.getNsURI())) {
			ResourceSet packResourceSet = packResource.getResourceSet();
			if (packResourceSet != null) {
				EPackage topMost = pack;
				// we need to be aware of empty subpackages
				while (topMost.getESuperPackage() != null
						&& topMost.getESuperPackage().eResource() == topMost.eResource())
					topMost = topMost.getESuperPackage();
				if (packResource.getContents().contains(topMost) && packResource.getContents().size() == 1) {
					if (!topMost.getEClassifiers().isEmpty())
						packResource.setURI(URI.createURI(topMost.getNsURI()));
					else
						moveSubpackagesToNewResource(topMost, set);
				}
				if (!topMost.eResource().getURI().toString().equals(topMost.getNsURI())) 
					movePackageToNewResource(topMost, set);
			}
		}
	}
}
 
Example #21
Source File: GrammarImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@SuppressWarnings("unchecked")
@Override
public void eSet(int featureID, Object newValue) {
	switch (featureID) {
		case XtextPackage.GRAMMAR__NAME:
			setName((String)newValue);
			return;
		case XtextPackage.GRAMMAR__USED_GRAMMARS:
			getUsedGrammars().clear();
			getUsedGrammars().addAll((Collection<? extends Grammar>)newValue);
			return;
		case XtextPackage.GRAMMAR__DEFINES_HIDDEN_TOKENS:
			setDefinesHiddenTokens((Boolean)newValue);
			return;
		case XtextPackage.GRAMMAR__HIDDEN_TOKENS:
			getHiddenTokens().clear();
			getHiddenTokens().addAll((Collection<? extends AbstractRule>)newValue);
			return;
		case XtextPackage.GRAMMAR__METAMODEL_DECLARATIONS:
			getMetamodelDeclarations().clear();
			getMetamodelDeclarations().addAll((Collection<? extends AbstractMetamodelDeclaration>)newValue);
			return;
		case XtextPackage.GRAMMAR__RULES:
			getRules().clear();
			getRules().addAll((Collection<? extends AbstractRule>)newValue);
			return;
	}
	super.eSet(featureID, newValue);
}
 
Example #22
Source File: XtextScopeProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createEPackageScope(final Grammar grammar, IScope parent) {
	return new SimpleScope(parent,Iterables.transform(Iterables.filter(grammar.getMetamodelDeclarations(),
			new Predicate<AbstractMetamodelDeclaration>() {
				@Override
				public boolean apply(AbstractMetamodelDeclaration input) {
					return input.getEPackage() != null;
				}
			}), new Function<AbstractMetamodelDeclaration, IEObjectDescription>() {
		@Override
		public IEObjectDescription apply(AbstractMetamodelDeclaration from) {
			return EObjectDescription.create(QualifiedName.create(from.getEPackage().getNsURI()), from.getEPackage());
		}
	}));
}
 
Example #23
Source File: TypeRefImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setMetamodel(AbstractMetamodelDeclaration newMetamodel) {
	AbstractMetamodelDeclaration oldMetamodel = metamodel;
	metamodel = newMetamodel;
	if (eNotificationRequired())
		eNotify(new ENotificationImpl(this, Notification.SET, XtextPackage.TYPE_REF__METAMODEL, oldMetamodel, metamodel));
}
 
Example #24
Source File: XtextScopeProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createReferencedPackagesScope(Grammar g) {
	final Collection<EClassifier> allClassifiers = new ArrayList<EClassifier>();
	for(AbstractMetamodelDeclaration decl: g.getMetamodelDeclarations()) {
		if (decl.getEPackage() != null)
			allClassifiers.addAll(decl.getEPackage().getEClassifiers());
	}
	return createClassifierScope(allClassifiers);
}
 
Example #25
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isReferencedByUsedGrammar(Grammar grammar, String nsURI, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar)) 
		return false;
	for(AbstractMetamodelDeclaration decl: grammar.getMetamodelDeclarations()) {
		EPackage pack = decl.getEPackage();
		if (pack != null && nsURI.equals(pack.getNsURI())) {
			return true;
		}
	}
	for (Grammar usedGrammar: grammar.getUsedGrammars()) {
		if (isReferencedByUsedGrammar(usedGrammar, nsURI, visitedGrammars))
			return true;
	}
	return false;
}
 
Example #26
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void completeParserRule(EObject model, final ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	final Grammar grammar = GrammarUtil.getGrammar(model);
	for (AbstractMetamodelDeclaration metamodelDeclaration : grammar.getMetamodelDeclarations()) {
		if (metamodelDeclaration instanceof ReferencedMetamodel) {
			ReferencedMetamodel referencedMetamodel = (ReferencedMetamodel) metamodelDeclaration;
			EPackage ePackage = referencedMetamodel.getEPackage();
			if (ePackage != null) {
				for (EClassifier eClassifier : ePackage.getEClassifiers()) {
					if (isProposeParserRule(eClassifier, grammar)) {
						String proposal = eClassifier.getName();
						String metamodelAlias = referencedMetamodel.getAlias();
						if (metamodelAlias != null) {
							proposal = proposal + " returns " + metamodelAlias + "::" + eClassifier.getName();
						}
						proposal = proposal + ": \n;\n";
						ConfigurableCompletionProposal completionProposal = (ConfigurableCompletionProposal) createCompletionProposal(
								proposal, eClassifier.getName() + " - parser rule",
								getImage(XtextFactory.eINSTANCE.createParserRule()), context);
						if (completionProposal != null) {
							completionProposal.setCursorPosition(proposal.length() - 3);
							acceptor.accept(completionProposal);
						}
					}
				}
			}
		}
	}
}
 
Example #27
Source File: TypeRefImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eSet(int featureID, Object newValue) {
	switch (featureID) {
		case XtextPackage.TYPE_REF__METAMODEL:
			setMetamodel((AbstractMetamodelDeclaration)newValue);
			return;
		case XtextPackage.TYPE_REF__CLASSIFIER:
			setClassifier((EClassifier)newValue);
			return;
	}
	super.eSet(featureID, newValue);
}
 
Example #28
Source File: TypeRefImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eUnset(int featureID) {
	switch (featureID) {
		case XtextPackage.TYPE_REF__METAMODEL:
			setMetamodel((AbstractMetamodelDeclaration)null);
			return;
		case XtextPackage.TYPE_REF__CLASSIFIER:
			setClassifier((EClassifier)null);
			return;
	}
	super.eUnset(featureID);
}
 
Example #29
Source File: GrammarImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public EList<AbstractMetamodelDeclaration> getMetamodelDeclarations() {
	if (metamodelDeclarations == null) {
		metamodelDeclarations = new EObjectContainmentEList<AbstractMetamodelDeclaration>(AbstractMetamodelDeclaration.class, this, XtextPackage.GRAMMAR__METAMODEL_DECLARATIONS);
	}
	return metamodelDeclarations;
}
 
Example #30
Source File: Xtext2EcoreTransformerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testTypesOfImplicitSuperGrammar() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate test \'http://test\'");
  _builder.newLine();
  _builder.append("MyRule: myFeature=INT;");
  _builder.newLine();
  final String xtextGrammar = _builder.toString();
  EObject _model = this.getModel(xtextGrammar);
  final Grammar grammar = ((Grammar) _model);
  final Xtext2EcoreTransformer transformer = new Xtext2EcoreTransformer(grammar);
  transformer.removeGeneratedPackages();
  transformer.transform();
  final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
  TypeRef type = rule.getType();
  Assert.assertNotNull(type);
  Assert.assertNotNull(transformer.getEClassifierInfos().getInfo(type));
  AbstractMetamodelDeclaration _get = GrammarUtil.allMetamodelDeclarations(grammar).get(1);
  final ReferencedMetamodel referenced = ((ReferencedMetamodel) _get);
  Assert.assertNotNull(referenced);
  Assert.assertEquals("ecore", referenced.getAlias());
  Assert.assertNull(transformer.getEClassifierInfos().getInfo(referenced, "EString"));
  Assert.assertNull(transformer.getEClassifierInfos().getInfo(referenced, "EInt"));
  EClassifierInfos parentInfos = IterableExtensions.<EClassifierInfos>head(transformer.getEClassifierInfos().getParents());
  Assert.assertNotNull(parentInfos.getInfo(referenced, "EString"));
  Assert.assertNotNull(parentInfos.getInfo(referenced, "EInt"));
}