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); } } } }
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); }
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; }
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; }
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); } } } } } }
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); } } } } }
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; }
@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); }
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(); }
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; }
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; }
@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()); }
@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(); }
@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(); }
@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(); }
@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(); }
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()); }
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; }
@Override protected List<EPackage> getEPackages() { List<EPackage> pkg = new ArrayList<EPackage>(); for (AbstractMetamodelDeclaration a : GrammarUtil.allMetamodelDeclarations(grammar.getGrammar())) pkg.add(a.getEPackage()); return pkg; }
TypeRef getTypeRef(EClassifier classifier) { TypeRef result = XtextFactory.eINSTANCE.createTypeRef(); result.setClassifier(classifier); EPackage pack = classifier.getEPackage(); for(AbstractMetamodelDeclaration decl: GrammarUtil.allMetamodelDeclarations(grammar)) { if (pack.equals(decl.getEPackage())) { result.setMetamodel(decl); return result; } } return result; }
public AbstractMetamodelDeclaration findMetamodel(Grammar grammar, String alias, String containedClassifier) { final List<AbstractMetamodelDeclaration> declarations = grammar.getMetamodelDeclarations(); AbstractMetamodelDeclaration result = null; for (AbstractMetamodelDeclaration decl : declarations) { if (isSameAlias(decl.getAlias(), alias)) { EPackage pack = decl.getEPackage(); if (pack != null && pack.getEClassifier(containedClassifier) != null) { if (result != null) return null; result = decl; } } } return result; }
@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; }
@Override protected EStructuralFeature getIdentifierFeature(EObject obj) { if (obj instanceof AbstractMetamodelDeclaration) { AbstractMetamodelDeclaration decl = (AbstractMetamodelDeclaration) obj; if (decl.getAlias() != null) return XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__ALIAS; if (decl instanceof GeneratedMetamodel && ((GeneratedMetamodel) decl).getName() != null) return XtextPackage.Literals.GENERATED_METAMODEL__NAME; return XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE; } return super.getIdentifierFeature(obj); }
private static List<EObject> findSingleElementInCollections(final String alias, final List<AbstractMetamodelDeclaration> candidates) { final List<AbstractMetamodelDeclaration> exactMatches = new ArrayList<AbstractMetamodelDeclaration>(); filterExactMatches(alias, candidates, exactMatches); if (exactMatches.size() == 1) return Collections.singletonList((EObject) exactMatches.get(0)); if (candidates.size() == 1) return Collections.singletonList((EObject) candidates.get(0)); return null; }
private static void filterExactMatches(final String alias, final List<AbstractMetamodelDeclaration> importedMetamodels, final List<AbstractMetamodelDeclaration> exactMatches) { Iterables.addAll(exactMatches, Iterables.filter(importedMetamodels, new Predicate<AbstractMetamodelDeclaration>() { @Override public boolean apply(AbstractMetamodelDeclaration param) { return alias.equals(param.getAlias()); } })); }
private List<EObject> getPackage(String nsUri, Grammar grammar, Set<Grammar> visitedGrammars) { if (!visitedGrammars.add(grammar)) return null; for(AbstractMetamodelDeclaration declaration: grammar.getMetamodelDeclarations()) { EPackage pack = declaration.getEPackage(); if (pack != null && nsUri.equals(pack.getNsURI())) return Collections.<EObject>singletonList(pack); } for (Grammar usedGrammar: grammar.getUsedGrammars()) { List<EObject> result = getPackage(nsUri, usedGrammar, visitedGrammars); if (result != null) return result; } return null; }
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; }
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); }
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()); } })); }
/** * <!-- 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)); }
/** * <!-- 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); }
/** * <!-- 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); }
/** * <!-- 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; }
/** * <!-- 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); }
protected void validateAllImports(final Grammar grammar) { List<AbstractMetamodelDeclaration> _allMetamodelDeclarations = GrammarUtil.allMetamodelDeclarations(grammar); for (final AbstractMetamodelDeclaration amd : _allMetamodelDeclarations) { if ((amd instanceof ReferencedMetamodel)) { this.validateReferencedMetamodel(((ReferencedMetamodel)amd)); } } }
@Test public void testDuplicateFeatures_01() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: Parent1 | Parent2 | NoParent;\n" + "NoParent: foo=ID;" + "Parent1: Sub1 | Sub2;\n" + "Parent2: Sub2 | Sub3;\n" + "Sub1: x=ID;\n" + "Sub2: x=ID;\n" + "Sub3: x=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); messageAcceptor.expectedContext( grammar.getRules().get(2).getType(), grammar.getRules().get(3).getType(), grammar.getRules().get(5).getType(), grammar.getRules().get(4).getAlternatives(), grammar.getRules().get(5).getAlternatives(), grammar.getRules().get(6).getAlternatives() ); validator.setMessageAcceptor(messageAcceptor); validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP); messageAcceptor.validate(); }
@Test public void testDuplicateFeatures_02() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A : (b+=B)*;\n" + "B : C | D;\n" + "C : 'c' name=ID ('e' e+=E)+;\n" + "E : name=ID;\n" + "F : C | E;\n" + "D : 'd' name=ID 'ref' ref=[F];"; 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(1).getType(), grammar.getRules().get(2).getType(), grammar.getRules().get(4).getType(), ((CompoundElement) grammar.getRules().get(2).getAlternatives()).getElements().get(1), grammar.getRules().get(3).getAlternatives(), ((CompoundElement) grammar.getRules().get(5).getAlternatives()).getElements().get(1) ); validator.setMessageAcceptor(messageAcceptor); validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP); messageAcceptor.validate(); }
@Test public void testGeneratedPackageNotEmpty() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Foo: 'a';\n"; Grammar grammar = (Grammar) getModel(grammarAsText); AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(metamodelDeclaration, true, false); validator.setMessageAcceptor(messageAcceptor); validator.checkGeneratedPackageNotEmpty((GeneratedMetamodel) metamodelDeclaration); messageAcceptor.validate(); }
@Test public void testConcreteLanguageToMetamodel1() throws Exception { XtextResource r = getResource("classpath:/" + ConcreteTestLanguage.class.getName().replace('.', '/') + ".xtext"); EObject element = r.getContents().get(0); Grammar g = (Grammar) element; List<AbstractMetamodelDeclaration> mms = Lists.<AbstractMetamodelDeclaration>newArrayList( Iterables.filter(g.getMetamodelDeclarations(), GeneratedMetamodel.class)); assertNotNull(mms); assertEquals(1, mms.size()); }
@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")); }