org.eclipse.xtext.GeneratedMetamodel Java Examples
The following examples show how to use
org.eclipse.xtext.GeneratedMetamodel.
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: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Check public void checkReferencedMetamodel(ReferencedMetamodel metamodel) throws ValueConverterException { if (metamodel.getEPackage() == null) return; String nsURI = metamodel.getEPackage().getNsURI(); List<GeneratedMetamodel> allGeneratedMetamodels = getInheritedGeneratedMetamodels(metamodel); String text = getUsedUri(metamodel); for (GeneratedMetamodel generatedMetamodel : allGeneratedMetamodels) { EPackage generatedPackage = generatedMetamodel.getEPackage(); if (generatedPackage != null && nsURI.equals((generatedPackage.getNsURI()))) { if (!text.equals(nsURI)) { addIssue("Metamodels that have been generated by a super grammar must be referenced by nsURI: " + nsURI, metamodel, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_INHERITED, nsURI); return; } return; } } checkExternalPackage(metamodel, text); }
Example #2
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
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: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Check public void checkGeneratedEnumIsValid(EnumLiteralDeclaration decl) { EnumRule rule = GrammarUtil.containingEnumRule(decl); if (!(rule.getType().getMetamodel() instanceof GeneratedMetamodel)) return; if (!(rule.getType().getClassifier() instanceof EEnum)) return; EEnum eEnum = (EEnum) rule.getType().getClassifier(); List<EnumLiteralDeclaration> declarations = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class); if (declarations.size() == eEnum.getELiterals().size()) return; for (EnumLiteralDeclaration otherDecl : declarations) { if (decl == otherDecl) { return; } if (otherDecl.getEnumLiteral() == decl.getEnumLiteral()) { if (!decl.getEnumLiteral().getLiteral().equals(decl.getLiteral().getValue())) addIssue("Enum literal '" + decl.getEnumLiteral().getName() + "' has already been defined with literal '" + decl.getEnumLiteral().getLiteral() + "'.", decl, XtextPackage.Literals.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, DUPLICATE_ENUM_LITERAL); return; } } }
Example #4
Source File: XtendXtext2EcorePostProcessor.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override public void process(GeneratedMetamodel metamodel) { Resource xtendFile = loadXtendFile(metamodel); if (xtendFile != null) { logger.warn("You are using an old xtend(1)-based IXtext2EcorePostProcessor. This features is deprecated and will be dropped in a future release of Xtext."); ExecutionContext ctx = getExecutionContext(metamodel); ctx = ctx.cloneWithResource(xtendFile); ResourceLoader currentThreadResourceLoader = null; try { currentThreadResourceLoader = getCurrentThreadResourceLoader(); setCurrentThreadResourceLoader(getResourceLoader(metamodel)); final Object[] params = new Object[] { metamodel }; Extension extension = ctx.getExtension("process", params); if (extension != null) { extension.evaluate(params, ctx); } } catch (EvaluationException e) { logger.error("Error encountered processing metamodel " + metamodel.getName() + " with " + xtendFile.getFullyQualifiedName(), e); } finally { setCurrentThreadResourceLoader(currentThreadResourceLoader); } } }
Example #5
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
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 #6
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@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 #7
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
private void doPropagateValidationResult(Diagnostic diagnostic, GeneratedMetamodel metamodel, ValidationMessageAcceptor acceptor) { boolean foundEObject = false; Object firstData = null; for(Object data: diagnostic.getData()) { if (firstData == null) { firstData = diagnostic.getData().get(0); } if (data instanceof EObject) { if (createMessageForSource(diagnostic, (EObject) data, acceptor)) { foundEObject = true; } if (data instanceof EStructuralFeature && ((EStructuralFeature) data).getEContainingClass() != firstData) { EClass containingClass = ((EStructuralFeature) data).getEContainingClass(); createMessageForSource(diagnostic, containingClass, acceptor); } } } if (!foundEObject) { doCreateMessage( diagnostic, metamodel, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, acceptor); } }
Example #8
Source File: XtextProposalProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
/** * Not a full featured solution for the computation of available structural features, but it is sufficient for some * interesting 85%. */ @Override public void completeAssignment_Feature(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { AbstractRule rule = EcoreUtil2.getContainerOfType(model, AbstractRule.class); TypeRef typeRef = rule.getType(); if (typeRef != null && typeRef.getClassifier() instanceof EClass) { Iterable<EStructuralFeature> features = ((EClass) typeRef.getClassifier()).getEAllStructuralFeatures(); Function<IEObjectDescription, ICompletionProposal> factory = getProposalFactory(grammarAccess.getValidIDRule().getName(), context); Iterable<String> processedFeatures = completeStructuralFeatures(context, factory, acceptor, features); if(rule.getType().getMetamodel() instanceof GeneratedMetamodel) { if(notNull(rule.getName()).toLowerCase().startsWith("import")) { completeSpecialAttributeAssignment("importedNamespace", 2, processedFeatures, factory, context, acceptor); completeSpecialAttributeAssignment("importURI", 1, processedFeatures, factory, context, acceptor); } else { completeSpecialAttributeAssignment("name", 3, processedFeatures, factory, context, acceptor); } } } super.completeAssignment_Feature(model, assignment, context, acceptor); }
Example #9
Source File: XtextLinkingService.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
private List<EObject> createPackage(GeneratedMetamodel generatedMetamodel, ILeafNode text) { final String nsURI = getMetamodelNsURI(text); final URI uri = URI.createURI(nsURI); if (uri == null || isReferencedByUsedGrammar(generatedMetamodel, nsURI)) return Collections.emptyList(); EPackage pack = ((GeneratedMetamodelImpl)generatedMetamodel).basicGetEPackage(); if (pack != null && !pack.eIsProxy()) return singletonList((EObject)pack); final EPackage generatedEPackage = EcoreFactory.eINSTANCE.createEPackage(); generatedEPackage.setName(generatedMetamodel.getName()); generatedEPackage.setNsPrefix(generatedMetamodel.getName()); generatedEPackage.setNsURI(nsURI); final Resource generatedPackageResource = new EcoreResourceFactoryImpl().createResource(uri); XtextResourceSet resourceSet = (XtextResourceSet) generatedMetamodel.eResource().getResourceSet(); if (!resourceSet.getURIResourceMap().containsKey(generatedPackageResource.getURI())) { generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource); } else { generatedPackageResource.setURI(URI.createURI(nsURI+"_"+generatedMetamodel.hashCode())); generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource); } generatedPackageResource.getContents().add(generatedEPackage); return Collections.<EObject>singletonList(generatedEPackage); }
Example #10
Source File: Xtext2EcoreTransformerTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@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 #11
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@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 #12
Source File: ProjectAwareXtendXtext2EcorePostProcessor.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Override protected synchronized ResourceLoader getResourceLoader(GeneratedMetamodel metamodel) { if (resourceLoader != null) return resourceLoader; URI uri = metamodel.eResource().getURI(); if (ClasspathUriUtil.isClasspathUri(uri)) { ResourceSet resourceSet = metamodel.eResource().getResourceSet(); uri = resourceSet.getURIConverter().normalize(uri); } IFile grammarFile = getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true))); IJavaProject javaProject = JavaCore.create(grammarFile.getProject()); try { if (javaProject.exists()) { ClassLoader classLoader = createClassLoader(javaProject); resourceLoader = new ResourceLoaderImpl(classLoader); return resourceLoader; } } catch (CoreException e) { logger.error("Error creating execution context for java project '" + grammarFile.getProject().getName() + "'", e); } return super.getResourceLoader(metamodel); }
Example #13
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@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 #14
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@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 #15
Source File: XtextLinker.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
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 #16
Source File: EMFGeneratorFragment.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Override public String[] getExportedPackagesRt(Grammar grammar) { List<GeneratedMetamodel> typeSelect = org.eclipse.xtext.EcoreUtil2.typeSelect( grammar.getMetamodelDeclarations(), GeneratedMetamodel.class); Set<String> exportedPackages = new LinkedHashSet<String>(); if (modelPluginID == null) { for (GeneratedMetamodel generatedMetamodel : typeSelect) { final String modelPackage = Strings.skipLastToken( getGeneratedEPackageName(grammar, generatedMetamodel.getEPackage()), "."); exportedPackages.add(modelPackage); exportedPackages.add(modelPackage + ".impl"); exportedPackages.add(modelPackage + ".util"); } } return exportedPackages.toArray(new String[exportedPackages.size()]); }
Example #17
Source File: XtextGenerator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override protected void checkConfigurationInternal(final Issues issues) { this.initialize(); final MweIssues generatorIssues = new MweIssues(this, issues); this.configuration.checkConfiguration(generatorIssues); final HashMap<String, Grammar> uris = new HashMap<String, Grammar>(); for (final XtextGeneratorLanguage language : this.languageConfigs) { { language.checkConfiguration(generatorIssues); Iterable<GeneratedMetamodel> _filter = Iterables.<GeneratedMetamodel>filter(language.getGrammar().getMetamodelDeclarations(), GeneratedMetamodel.class); for (final GeneratedMetamodel generatedMetamodel : _filter) { { final String nsURI = generatedMetamodel.getEPackage().getNsURI(); boolean _containsKey = uris.containsKey(nsURI); if (_containsKey) { String _name = uris.get(nsURI).getName(); String _plus = ((("Duplicate generated grammar with nsURI \'" + nsURI) + "\' in ") + _name); String _plus_1 = (_plus + " and "); String _name_1 = language.getGrammar().getName(); String _plus_2 = (_plus_1 + _name_1); generatorIssues.addError(_plus_2); } else { uris.put(nsURI, language.getGrammar()); } } } } } }
Example #18
Source File: ValidatorFragment2.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected Collection<EPackage> getRegistryPackagesToValidate() { final Collection<EPackage> packages = GrammarUtil.allEPackagesToValidate(this.getGrammar()); final Function1<GeneratedMetamodel, EPackage> _function = (GeneratedMetamodel it) -> { return it.getEPackage(); }; packages.removeAll(IterableExtensions.<EPackage>toList(IterableExtensions.<GeneratedMetamodel, EPackage>map(Iterables.<GeneratedMetamodel>filter(GrammarUtil.allMetamodelDeclarations(this.getGrammar()), GeneratedMetamodel.class), _function))); return packages; }
Example #19
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected List<GeneratedMetamodel> getInheritedGeneratedMetamodels(ReferencedMetamodel metamodel) { List<GeneratedMetamodel> allGeneratedMetamodels = new ArrayList<GeneratedMetamodel>(); Grammar grammar = GrammarUtil.getGrammar(metamodel); Set<Grammar> visited = Sets.newHashSet(); for (Grammar usedGrammar : grammar.getUsedGrammars()) Iterables.addAll(allGeneratedMetamodels, getAllGeneratedMetamodels(usedGrammar, visited)); if (allGeneratedMetamodels.isEmpty()) return Collections.emptyList(); return allGeneratedMetamodels; }
Example #20
Source File: XtextLocationInFileProvider.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #21
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private Iterable<GeneratedMetamodel> getAllGeneratedMetamodels(Grammar grammar, Set<Grammar> visited) { Iterable<GeneratedMetamodel> result = Iterables.filter(grammar.getMetamodelDeclarations(), GeneratedMetamodel.class); for (Grammar gr : grammar.getUsedGrammars()) { if (visited.add(gr)) result = Iterables.concat(result, getAllGeneratedMetamodels(gr, visited)); } return result; }
Example #22
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private Set<String> getGeneratedEPackageURIs() { List<GeneratedMetamodel> list = EcoreUtil2.typeSelect(grammar.getMetamodelDeclarations(), GeneratedMetamodel.class); return Sets.newLinkedHashSet(Iterables.transform(list, new Function<GeneratedMetamodel, String>() { @Override public String apply(GeneratedMetamodel from) { return from.getEPackage()!=null?from.getEPackage().getNsURI() : null; } })); }
Example #23
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Check public void checkJavaPackageNamingConventions(GeneratedMetamodel metamodel){ Severity severity = getIssueSeverities(getContext(), getCurrentObject()).getSeverity(INVALID_JAVAPACKAGE_NAME); if (severity == null || severity == Severity.IGNORE) { // Don't perform any check if the result is ignored return; } final String metamodelName = Strings.emptyIfNull(metamodel.getName()); if (!Strings.equal(metamodelName, metamodelName.toLowerCase())) { addIssue("The generated metamodel name must not contain uppercase characters", metamodel, XtextPackage.eINSTANCE.getGeneratedMetamodel_Name(), INVALID_JAVAPACKAGE_NAME, metamodel.getName()); } }
Example #24
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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(); }
Example #25
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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(); }
Example #26
Source File: XtextLinkingService.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private boolean isReferencedByUsedGrammar(GeneratedMetamodel generatedMetamodel, String nsURI) { final Grammar grammar = GrammarUtil.getGrammar(generatedMetamodel); if (grammar != null) { final Set<Grammar> visitedGrammars = Sets.newHashSet(grammar); for (Grammar usedGrammar: grammar.getUsedGrammars()) { if (isReferencedByUsedGrammar(usedGrammar, nsURI, visitedGrammars)) return true; } } return false; }
Example #27
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private void propageValidationResult(Diagnostic diagnostic, GeneratedMetamodel metamodel, ValidationMessageAcceptor acceptor) { if (diagnostic.getSeverity() != Diagnostic.OK) { if (diagnostic.getCode() != 0) { List<?> data = diagnostic.getData(); if (!data.isEmpty() && data.get(0) instanceof EObject) { doPropagateValidationResult(diagnostic, metamodel, acceptor); } } for(Diagnostic child: diagnostic.getChildren()) propageValidationResult(child, metamodel, acceptor); } }
Example #28
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Check public void checkGeneratedPackageNotEmpty(GeneratedMetamodel metamodel) { EPackage pack = metamodel.getEPackage(); if (pack != null && pack.getEClassifiers().isEmpty()) { getMessageAcceptor().acceptError( "Generated package '" + metamodel.getName() + "' may not be empty.", metamodel, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, EMPTY_GENERATED_PACKAGE); } }
Example #29
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Check public void checkGeneratedPackageForNameClashes(GeneratedMetamodel metamodel) { EPackage pack = metamodel.getEPackage(); Multimap<String, ENamedElement> constantNameToElement = HashMultimap.create(); Multimap<String, ENamedElement> accessorNameToElement = HashMultimap.create(); if (pack != null) { for(EClassifier classifier: pack.getEClassifiers()) { String accessorName = classifier.getName(); if ("Class".equals(accessorName) || "Name".equals(accessorName)) accessorName += "_"; accessorNameToElement.put("get" + accessorName, classifier); String classifierConstantName = CodeGenUtil2.format(classifier.getName(), '_', null, true, true).toUpperCase(); constantNameToElement.put(classifierConstantName, classifier); if (classifier instanceof EClass) { for(EStructuralFeature feature: ((EClass) classifier).getEAllStructuralFeatures()) { String featureConstantPart = CodeGenUtil2.format(feature.getName(), '_', null, false, false).toUpperCase(); String featureConstantName = classifierConstantName + "__" + featureConstantPart; constantNameToElement.put(featureConstantName, feature); String featureAccessorName = "get" + classifier.getName() + "_" + Strings.toFirstUpper(feature.getName()); accessorNameToElement.put(featureAccessorName, feature); } } } } createMessageForNameClashes(constantNameToElement); createMessageForNameClashes(accessorNameToElement); }
Example #30
Source File: XtextValidator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Check public void checkGeneratedPackage(GeneratedMetamodel metamodel) { Map<Object, Object> context = getContext(); if (context != null) { Diagnostician diagnostician = (Diagnostician) context.get(EValidator.class); if (diagnostician != null) checkGeneratedPackage(metamodel, diagnostician, context); } }