@Override public void linkModel(EObject model, IDiagnosticConsumer consumer) { if (model instanceof Grammar) { final Xtext2EcoreTransformer transformer = createTransformer((Grammar) model, consumer); //TODO duplicate transformer.removeGeneratedPackages(); super.linkModel(model, consumer); updateOverriddenRules((Grammar) model); try { transformer.transform(); } catch (Exception e) { log.error(e.getMessage(), e); consumer.consume(new ExceptionDiagnostic(e), Severity.ERROR); } if (!model.eResource().eAdapters().contains(packageRemover)) model.eResource().eAdapters().add(packageRemover); } else { super.linkModel(model, consumer); } }
@Test public void testAbstractLanguageToMetamodel() throws Exception { XtextResource r = getResource("classpath:/" + AbstractTestLanguage.class.getName().replace('.', '/') + ".xtext"); Grammar element = (Grammar) r.getParseResult().getRootASTElement(); if (!r.getErrors().isEmpty()) { EList<Diagnostic> errors = r.getErrors(); for (Diagnostic syntaxError : errors) { logger.debug(syntaxError.getMessage() + " - " + syntaxError.getLine()); } fail(errors.toString()); } List<TerminalRule> lexerRules = GrammarUtil.allTerminalRules(element); assertEquals(8, lexerRules.size()); List<EPackage> list = Xtext2EcoreTransformer.doGetGeneratedPackages(element); assertNotNull(list); assertEquals(0, list.size()); }
@Test public void testConcreteLanguageToMetamodel() throws Exception { XtextResource r = getResource("classpath:/" + ConcreteTestLanguage.class.getName().replace('.', '/') + ".xtext"); Grammar element = (Grammar) r.getContents().get(0); List<TerminalRule> lexerRules = GrammarUtil.allTerminalRules(element); assertEquals("Number of lexer-rules in Grammar", 9, lexerRules.size()); List<EPackage> list = Xtext2EcoreTransformer.doGetGeneratedPackages(element); EPackage metaModel = list.get(0); assertNotNull(metaModel); assertNotNull(metaModel.getNsPrefix()); assertNotNull(metaModel.getNsURI()); assertNotNull(metaModel.getName()); EList<EClassifier> types = metaModel.getEClassifiers(); EClass type = (EClass) types.get(1); EStructuralFeature feature = type.getEStructuralFeature("elements"); assertNotNull(feature.getEType()); }
@Override public XtextResource doGetResource(final InputStream in, final URI uri) throws Exception { XtextResourceSet rs = this.<XtextResourceSet>get(XtextResourceSet.class); rs.getURIConverter().getURIMap().put(URI.createPlatformPluginURI("org.eclipse.xtext/", false), URI.createURI("classpath:/")); rs.getURIConverter().getURIMap().put(URI.createPlatformPluginURI("org.eclipse.xtext.xbase/", false), URI.createURI("classpath:/")); rs.getURIConverter().getURIMap().put(URI.createPlatformPluginURI("org.eclipse.xtext.common.types/", false), URI.createURI("classpath:/")); rs.getURIConverter().getURIMap().put(URI.createPlatformPluginURI("org.eclipse.emf.ecore/", false), URI.createURI("classpath:/")); rs.getURIConverter().getURIMap().put(URI.createPlatformResourceURI("org.eclipse.xtext.xbase/", false), URI.createURI("classpath:/")); rs.getURIConverter().getURIMap().put(URI.createPlatformResourceURI("org.eclipse.emf.ecore/", false), URI.createURI("classpath:/")); rs.getURIConverter().getURIMap().put(URI.createPlatformResourceURI("org.eclipse.xtext.common.types/", false), URI.createURI("classpath:/")); rs.setClasspathURIContext(this.getClass()); Resource _createResource = this.getResourceFactory().createResource(uri); final XtextResource resource = ((XtextResource) _createResource); rs.getResources().add(resource); XtextLinker linker = new XtextLinker() { @Override protected Xtext2EcoreTransformer createTransformer(final Grammar grammar, final IDiagnosticConsumer consumer) { Xtext2EcoreTransformer result = super.createTransformer(grammar, consumer); ErrorAcceptor _errorAcceptor = result.getErrorAcceptor(); Xtext2EcoreTransformerTest.MyErrorAcceptor _myErrorAcceptor = new Xtext2EcoreTransformerTest.MyErrorAcceptor(_errorAcceptor, Xtext2EcoreTransformerTest.this.errorAcceptorMock); result.setErrorAcceptor(_myErrorAcceptor); return result; } }; ILinker _linker = resource.getLinker(); linker.setScopeProvider(((XtextLinker) _linker).getScopeProvider()); ILinker _linker_1 = resource.getLinker(); linker.setLinkingService(((Linker) _linker_1).getLinkingService()); ILinker _linker_2 = resource.getLinker(); linker.setLinkingHelper(((Linker) _linker_2).getLinkingHelper()); XtextLinker.PackageRemover _packageRemover = new XtextLinker.PackageRemover(); linker.setPackageRemover(_packageRemover); LinkingDiagnosticMessageProvider _linkingDiagnosticMessageProvider = new LinkingDiagnosticMessageProvider(); linker.setDiagnosticMessageProvider(_linkingDiagnosticMessageProvider); OnChangeEvictingCache _onChangeEvictingCache = new OnChangeEvictingCache(); linker.setCache(_onChangeEvictingCache); resource.setLinker(linker); resource.load(in, null); return resource; }
@Override public void setUp() throws Exception { super.setUp(); with(XtextStandaloneSetup.class); model = "grammar datatypetests with org.eclipse.xtext.common.Terminals\n" + "import 'http://www.eclipse.org/emf/2002/Ecore' as ecore\n" + "generate metamodel 'http://fooo'\n" + "Start:\n" + " id=ValidId id2=ValidId2 failure1=FailureId failure2=Failure2;\n" + "ValidId returns ecore::EString: ID '.' ID;\n" + "ValidId2 returns ecore::EString: ID '.' ValidId;\n" + "FailureId returns ecore::EString: name=ID;\n" + "Failure2 returns ecore::EString: name=Start;"; resource = getResourceFromStringAndExpect(model, 2); grammar = (Grammar) resource.getContents().get(0); transformer = new Xtext2EcoreTransformer(grammar); errorAcceptor = new TestErrorAcceptor(); transformer.setErrorAcceptor(errorAcceptor); }
@Override public XtextResource doGetResource(final InputStream in, final URI uri) throws Exception { XtextResourceSet rs = this.<XtextResourceSet>get(XtextResourceSet.class); rs.setClasspathURIContext(this.getClass()); Resource _createResource = this.getResourceFactory().createResource(uri); final XtextResource resource = ((XtextResource) _createResource); rs.getResources().add(resource); XtextLinker linker = new XtextLinker() { @Override protected Xtext2EcoreTransformer createTransformer(final Grammar grammar, final IDiagnosticConsumer consumer) { Xtext2EcoreTransformer result = super.createTransformer(grammar, consumer); ErrorAcceptor _errorAcceptor = result.getErrorAcceptor(); Xtext2EcoreTransformerTest.MyErrorAcceptor _myErrorAcceptor = new Xtext2EcoreTransformerTest.MyErrorAcceptor(_errorAcceptor, Xtext2EcoreTransformerTest.this.errorAcceptorMock); result.setErrorAcceptor(_myErrorAcceptor); return result; } }; ILinker _linker = resource.getLinker(); linker.setScopeProvider(((XtextLinker) _linker).getScopeProvider()); ILinker _linker_1 = resource.getLinker(); linker.setLinkingService(((Linker) _linker_1).getLinkingService()); ILinker _linker_2 = resource.getLinker(); linker.setLinkingHelper(((Linker) _linker_2).getLinkingHelper()); XtextLinker.PackageRemover _packageRemover = new XtextLinker.PackageRemover(); linker.setPackageRemover(_packageRemover); LinkingDiagnosticMessageProvider _linkingDiagnosticMessageProvider = new LinkingDiagnosticMessageProvider(); linker.setDiagnosticMessageProvider(_linkingDiagnosticMessageProvider); OnChangeEvictingCache _onChangeEvictingCache = new OnChangeEvictingCache(); linker.setCache(_onChangeEvictingCache); resource.setLinker(linker); resource.load(in, null); return resource; }
private List<EPackage> getEPackagesFromGrammar(final String xtextGrammar, final int expectedErrors) throws Exception { this.errorAcceptorMock.replay(); EObject _modelAndExpect = this.getModelAndExpect(xtextGrammar, expectedErrors); final Grammar grammar = ((Grammar) _modelAndExpect); this.errorAcceptorMock.verify(); final List<EPackage> metamodels = Xtext2EcoreTransformer.doGetGeneratedPackages(grammar); Assert.assertNotNull(metamodels); return metamodels; }
@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")); }
@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()); }
protected Xtext2EcoreTransformer createTransformer(Grammar grammar, IDiagnosticConsumer consumer) { final Xtext2EcoreTransformer transformer = new Xtext2EcoreTransformer(grammar); transformer.setErrorAcceptor(new TransformationDiagnosticsProducer(consumer)); transformer.setPostProcessor(postProcessor); return transformer; }