mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
initial commit
This commit is contained in:
commit
9d6d0a3bb1
16 changed files with 719 additions and 0 deletions
8
tests/org.eclipse.xtext.generator.tests/.classpath
Normal file
8
tests/org.eclipse.xtext.generator.tests/.classpath
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="src-gen"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
|
||||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
34
tests/org.eclipse.xtext.generator.tests/.project
Normal file
34
tests/org.eclipse.xtext.generator.tests/.project
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>org.eclipse.xtext.generator.tests</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.ManifestBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.SchemaBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.api.tools.apiAnalysisBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.pde.PluginNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.pde.api.tools.apiAnalysisNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,7 @@
|
|||
#Wed May 07 16:13:48 CEST 2008
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.5
|
19
tests/org.eclipse.xtext.generator.tests/META-INF/MANIFEST.MF
Normal file
19
tests/org.eclipse.xtext.generator.tests/META-INF/MANIFEST.MF
Normal file
|
@ -0,0 +1,19 @@
|
|||
Manifest-Version: 1.0
|
||||
Bundle-ManifestVersion: 2
|
||||
Bundle-Name: Xtext2 Generator Tests Plug-in
|
||||
Bundle-SymbolicName: org.eclipse.xtext.generator.tests
|
||||
Bundle-Version: 1.0.0
|
||||
Bundle-Vendor: eclipse.org
|
||||
Bundle-RequiredExecutionEnvironment: J2SE-1.5
|
||||
Require-Bundle: org.eclipse.xtext.core;bundle-version="1.0.0",
|
||||
org.eclipse.xtext;bundle-version="1.0.0",
|
||||
org.eclipse.xtext.generator;bundle-version="1.0.0",
|
||||
org.openarchitectureware.xtext.core;bundle-version="4.3.0",
|
||||
org.openarchitectureware.xtext.core.base;bundle-version="4.3.0",
|
||||
org.openarchitectureware.xtext.generator;bundle-version="4.3.0",
|
||||
org.junit,
|
||||
org.antlr;bundle-version="3.0.0",
|
||||
org.openarchitectureware.dependencies;bundle-version="4.3.0",
|
||||
org.eclipse.emf.compare,
|
||||
org.eclipse.emf.compare.diff,
|
||||
org.eclipse.emf.compare.match
|
4
tests/org.eclipse.xtext.generator.tests/build.properties
Normal file
4
tests/org.eclipse.xtext.generator.tests/build.properties
Normal file
|
@ -0,0 +1,4 @@
|
|||
source.. = src/
|
||||
output.. = bin/
|
||||
bin.includes = META-INF/,\
|
||||
.
|
|
@ -0,0 +1,86 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.xtext;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.m2t.type.emf.EmfRegistryMetaModel;
|
||||
import org.eclipse.xtext.generator.tests.AbstractGeneratorTest;
|
||||
import org.eclipse.xtext.generator.tests.Invocation;
|
||||
import org.eclipse.xtext.parser.XtextGrammarTestFactory;
|
||||
import org.openarchitectureware.expression.ExecutionContextImpl;
|
||||
import org.openarchitectureware.xtend.XtendFacade;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*
|
||||
*/
|
||||
public class XtextGrammarTest extends AbstractGeneratorTest {
|
||||
|
||||
public void testBootstrapping() throws Exception {
|
||||
List<Invocation> parse = parse("generate foo 'bar' Foo : name=ID;");
|
||||
Iterator<Invocation> iter = parse.iterator();
|
||||
for (Invocation invocation : parse) {
|
||||
System.out.println(invocation);
|
||||
}
|
||||
assertEquals("create(GeneratedMetamodel)", iter.next().toString());
|
||||
assertEquals("set(name,[@2,9:11='foo',<4>,1:9])", iter.next().toString());
|
||||
assertEquals("set(nsURI,[@4,13:17=''bar'',<5>,1:13])", iter.next().toString());
|
||||
assertEquals("create(Grammar)", iter.next().toString());
|
||||
assertEquals("add(metamodelDeclarations,GeneratedMetamodel)", iter.next().toString());
|
||||
assertEquals("create(ParserRule)", iter.next().toString());
|
||||
assertEquals("set(name,[@6,19:21='Foo',<4>,1:19])", iter.next().toString());
|
||||
assertEquals("create(Assignment)", iter.next().toString());
|
||||
assertEquals("set(feature,[@10,25:28='name',<4>,1:25])", iter.next().toString());
|
||||
assertEquals("set(operator,[@11,29:29='=',<23>,1:29])", iter.next().toString());
|
||||
assertEquals("create(RuleCall)", iter.next().toString());
|
||||
assertEquals("set(name,[@12,30:31='ID',<4>,1:30])", iter.next().toString());
|
||||
assertEquals("set(terminal,RuleCall)", iter.next().toString());
|
||||
assertEquals("set(alternatives,Assignment)", iter.next().toString());
|
||||
assertEquals("add(parserRules,ParserRule)", iter.next().toString());
|
||||
}
|
||||
|
||||
public void testInstantiate() throws Exception {
|
||||
EObject grammar = (EObject) parse("generate foo 'bar' Foo : name=ID;", new XtextGrammarTestFactory());
|
||||
assertWithXtend("'Foo'","parserRules.first().name",grammar);
|
||||
assertWithXtend("'name'","parserRules.first().alternatives.feature",grammar);
|
||||
}
|
||||
|
||||
public void testInstantiateXtextGrammar() throws Exception {
|
||||
InputStream bootGrammar = getClass().getClassLoader().getResourceAsStream(getClass().getName().replace('.','/')+".xtext");
|
||||
EObject grammar = (EObject) parse(bootGrammar , new XtextGrammarTestFactory());
|
||||
assertWithXtend("true","parserRules.select(e|e.name=='AbstractToken').first()!=null",grammar);
|
||||
assertWithXtend("'AbstractElement'","parserRules.select(e|e.name=='AbstractToken').first().type.name",grammar);
|
||||
// Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
|
||||
// "xmi", new XMIResourceFactoryImpl());
|
||||
// Resource resource = new ResourceSetImpl().createResource(URI.createURI("foo.xmi"));
|
||||
// resource.getContents().add(grammar);
|
||||
// resource.save(null);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XtendFacade getXtendFacade() {
|
||||
ExecutionContextImpl ctx = new ExecutionContextImpl();
|
||||
ctx.registerMetaModel(new EmfRegistryMetaModel() {
|
||||
@Override
|
||||
protected EPackage[] allPackages() {
|
||||
return new EPackage[]{XtextGrammarTestEPackageAccess.getXtextTestEPackage()};
|
||||
}
|
||||
});
|
||||
return XtendFacade.create(ctx);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
|
||||
generate XtextTest "http://www.eclipse.org/2008/Test/XtextTest"
|
||||
|
||||
Grammar :
|
||||
metamodelDeclarations+=AbstractMetamodelDeclaration*
|
||||
parserRules+=ParserRule*
|
||||
('lexing' ':'
|
||||
lexerRules+=LexerRule+)?
|
||||
;
|
||||
|
||||
AbstractRule : LexerRule | ParserRule;
|
||||
|
||||
AbstractMetamodelDeclaration :
|
||||
GeneratedMetamodel | ReferencedMetamodel;
|
||||
|
||||
GeneratedMetamodel :
|
||||
'generate' name=ID nsURI=STRING ('as' alias=ID)?;
|
||||
|
||||
ReferencedMetamodel :
|
||||
'import' uri=STRING ('as' alias=ID)?;
|
||||
|
||||
LexerRule :
|
||||
name=ID ':'
|
||||
body=LEXER_BODY
|
||||
;
|
||||
|
||||
ParserRule :
|
||||
name=ID ('returns' type=TypeRef)? ':'
|
||||
alternatives=Alternatives
|
||||
';'
|
||||
;
|
||||
|
||||
TypeRef :
|
||||
name=ID
|
||||
;
|
||||
|
||||
Alternatives returns AbstractElement:
|
||||
Group ({Alternatives.groups+=current} '|' groups+=Group)*
|
||||
;
|
||||
|
||||
Group returns AbstractElement:
|
||||
AbstractToken ( {current=Group.abstractTokens+=current} abstractTokens+=AbstractToken)*
|
||||
;
|
||||
|
||||
AbstractToken returns AbstractElement:
|
||||
(Assignment |
|
||||
Action |
|
||||
AbstractTerminal) cardinality=('?'|'*'|'+')?;
|
||||
|
||||
Assignment returns Assignment:
|
||||
feature=ID operator=('+='|'='|'?=') terminal=AbstractTerminal;
|
||||
|
||||
Action returns Action:
|
||||
'{' ('current' '=')? typeName=TypeRef ('.' feature=ID operator=('='|'+=') 'current')? '}';
|
||||
|
||||
AbstractTerminal returns AbstractElement:
|
||||
Keyword | RuleCall | ParenthesizedElement
|
||||
;
|
||||
|
||||
ParenthesizedElement returns AbstractElement:
|
||||
'(' Alternatives ')'
|
||||
;
|
||||
|
||||
Keyword :
|
||||
value=STRING
|
||||
;
|
||||
|
||||
RuleCall :
|
||||
name=ID ;
|
||||
|
||||
// lexing:
|
||||
|
||||
// Problem: LEXER_BODY kann nicht in xtxt beschrieben werden, da das erste '#>' bereits matcht
|
||||
// Lösung: built-in lexer token
|
||||
// LEXER_BODY : <# '<#' ( options {greedy=false;} : . )* '#>' #>
|
|
@ -0,0 +1,80 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.generator.tests;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.tools.ant.filters.StringInputStream;
|
||||
import org.eclipse.xtext.core.parser.IElementFactory;
|
||||
import org.openarchitectureware.xtend.XtendFacade;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractGeneratorTest extends TestCase {
|
||||
|
||||
public Object parse(String model, IElementFactory factory) throws Exception {
|
||||
String name = getClass().getPackage().getName() + ".parser." + getClass().getSimpleName();
|
||||
String parser = name + "Parser";
|
||||
|
||||
Class<?> parserClass = getClass().getClassLoader().loadClass(parser);
|
||||
Object parserInstance = parserClass.newInstance();
|
||||
return parserClass.getMethod("parse", InputStream.class, IElementFactory.class).invoke(parserInstance,
|
||||
new StringInputStream(model), factory);
|
||||
}
|
||||
|
||||
public Object parse(InputStream model, IElementFactory factory) throws Exception {
|
||||
String name = getClass().getPackage().getName() + ".parser." + getClass().getSimpleName();
|
||||
String parser = name + "Parser";
|
||||
|
||||
Class<?> parserClass = getClass().getClassLoader().loadClass(parser);
|
||||
Object parserInstance = parserClass.newInstance();
|
||||
return parserClass.getMethod("parse", InputStream.class, IElementFactory.class).invoke(parserInstance,
|
||||
model, factory);
|
||||
}
|
||||
|
||||
public List<Invocation> parse(String model) throws Exception {
|
||||
final List<Invocation> calls = new ArrayList<Invocation>();
|
||||
parse(model, new IElementFactory() {
|
||||
|
||||
public void add(Object _this, String feature, Object value) {
|
||||
calls.add(new Invocation("add", feature, value));
|
||||
}
|
||||
|
||||
public Object create(String typeName) {
|
||||
calls.add(new Invocation("create", typeName, null));
|
||||
return typeName;
|
||||
}
|
||||
|
||||
public void set(Object _this, String feature, Object value) {
|
||||
calls.add(new Invocation("set", feature, value));
|
||||
}
|
||||
});
|
||||
return calls;
|
||||
}
|
||||
|
||||
protected void assertWithXtend(String left, String right, Object _this) {
|
||||
assertWithXtend(left+" != "+right, left, right, _this);
|
||||
}
|
||||
protected void assertWithXtend(String message, String left, String right, Object _this) {
|
||||
XtendFacade f = getXtendFacade();
|
||||
f = f.cloneWithExtensions("__compare(Object this) : __left(this) == __right(this);__left(Object this) : "+left+"; __right(Object this) :"+right+";");
|
||||
assertTrue(message,(Boolean) f.call("__compare",_this));
|
||||
}
|
||||
|
||||
protected XtendFacade getXtendFacade() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.generator.tests;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.xtext.GeneratorFacade;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.XtextEPackageAccess;
|
||||
import org.eclipse.xtext.XtextGrammarTest;
|
||||
import org.eclipse.xtext.XtextPackage;
|
||||
import org.eclipse.xtext.grammargen.tests.SimpleTest;
|
||||
import org.eclipse.xtext.grammargen.tests.SimpleTest2;
|
||||
import org.eclipse.xtext.parser.XtextFactory;
|
||||
import org.eclipse.xtext.parser.XtextParser;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*
|
||||
*/
|
||||
public class GenerateAllTestGrammars {
|
||||
private static final String PATH = "./src-gen";
|
||||
|
||||
private final static Class<?>[] testclasses = new Class[] { SimpleTest.class, SimpleTest2.class, XtextGrammarTest.class };
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
for (Class<?> c : testclasses) {
|
||||
String filename = c.getName().replace('.', '/') + ".xtext";
|
||||
System.out.println("loading " + filename);
|
||||
InputStream resourceAsStream = c.getClassLoader().getResourceAsStream(filename);
|
||||
|
||||
//TODO make Xtext2Factory manual so one can overwrite 'getEPackages' in order to support generated epackages
|
||||
EPackage.Registry.INSTANCE.put(XtextEPackageAccess.XTEXT_NS_URI, XtextPackage.eINSTANCE);
|
||||
XtextParser xtext2Parser= new XtextParser();
|
||||
Grammar grammarModel = (Grammar) xtext2Parser.parse(resourceAsStream, new XtextFactory());
|
||||
|
||||
GeneratorFacade.generate(grammarModel,c.getSimpleName(),c.getPackage().getName().replace('.', '/'),PATH);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.generator.tests;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*
|
||||
*/
|
||||
public class Invocation {
|
||||
|
||||
public final String method;
|
||||
public final String feature;
|
||||
public final Object param;
|
||||
Invocation(String method, String feature, Object param) {
|
||||
super();
|
||||
this.feature = feature;
|
||||
this.method = method;
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return method+"("+feature+(param!=null?","+param:"")+")";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.grammargen.tests;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.antlr.runtime.CommonToken;
|
||||
import org.eclipse.xtext.generator.tests.AbstractGeneratorTest;
|
||||
import org.eclipse.xtext.generator.tests.Invocation;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*
|
||||
*/
|
||||
public class SimpleTest extends AbstractGeneratorTest {
|
||||
|
||||
public void testFoo() throws Exception {
|
||||
List<Invocation> parse = parse("HOLLA");
|
||||
assertEquals("create", parse.get(0).method);
|
||||
assertEquals("Foo", parse.get(0).feature);
|
||||
|
||||
assertEquals("set", parse.get(1).method);
|
||||
assertEquals("name", parse.get(1).feature);
|
||||
assertEquals("HOLLA", ((CommonToken)parse.get(1).param).getText());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
generate SimpleTest "http://eclipse.org/xtext/tests/SimpleTest"
|
||||
|
||||
Foo : name=ID;
|
|
@ -0,0 +1,47 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.grammargen.tests;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.antlr.runtime.CommonToken;
|
||||
import org.eclipse.xtext.generator.tests.AbstractGeneratorTest;
|
||||
import org.eclipse.xtext.generator.tests.Invocation;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
*
|
||||
*/
|
||||
public class SimpleTest2 extends AbstractGeneratorTest {
|
||||
|
||||
public void testFoo() throws Exception {
|
||||
List<Invocation> parse = parse("keyword bla 3 {}");
|
||||
assertEquals("create", parse.get(0).method);
|
||||
assertEquals("Child", parse.get(0).feature);
|
||||
|
||||
assertEquals("set", parse.get(1).method);
|
||||
assertEquals("name", parse.get(1).feature);
|
||||
assertEquals("bla", ((CommonToken)parse.get(1).param).getText());
|
||||
|
||||
assertEquals("set", parse.get(2).method);
|
||||
assertEquals("number", parse.get(2).feature);
|
||||
assertEquals("3", ((CommonToken)parse.get(2).param).getText());
|
||||
|
||||
assertEquals("create", parse.get(3).method);
|
||||
assertEquals("Model", parse.get(3).feature);
|
||||
|
||||
assertEquals("add", parse.get(4).method);
|
||||
assertEquals("contents", parse.get(4).feature);
|
||||
assertEquals("Child",parse.get(4).param);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
*******************************************************************************/
|
||||
generate SimpleTest2 "http://eclipse.org/xtext/tests/SimpleTest2"
|
||||
|
||||
Model :
|
||||
(contents+=Child)*;
|
||||
|
||||
Child :
|
||||
(optional?='optional')? "keyword" name=ID number=INT '{'
|
||||
|
||||
'}';
|
|
@ -0,0 +1,109 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* committers of openArchitectureWare - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext2ecore;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.emf.compare.diff.metamodel.AttributeChange;
|
||||
import org.eclipse.emf.compare.diff.metamodel.DiffElement;
|
||||
import org.eclipse.emf.compare.diff.metamodel.DiffGroup;
|
||||
import org.eclipse.emf.compare.diff.metamodel.DiffModel;
|
||||
import org.eclipse.emf.compare.diff.metamodel.ReferenceChange;
|
||||
import org.eclipse.emf.compare.diff.service.DiffService;
|
||||
import org.eclipse.emf.compare.match.api.IMatchEngine;
|
||||
import org.eclipse.emf.compare.match.api.MatchOptions;
|
||||
import org.eclipse.emf.compare.match.engine.GenericMatchEngine;
|
||||
import org.eclipse.emf.compare.match.metamodel.MatchModel;
|
||||
import org.eclipse.emf.ecore.EAttribute;
|
||||
import org.eclipse.emf.ecore.EReference;
|
||||
import org.eclipse.emf.ecore.EStructuralFeature;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
|
||||
/**
|
||||
* @author Jan Köhnlein
|
||||
*
|
||||
*/
|
||||
public class EcoreModelComparator {
|
||||
|
||||
private Map<String, Object> options;
|
||||
private IMatchEngine matchEngine;
|
||||
private List<EStructuralFeature> ignoredFeatures = new ArrayList<EStructuralFeature>();
|
||||
|
||||
public EcoreModelComparator() {
|
||||
options = new HashMap<String, Object>();
|
||||
options.put(MatchOptions.OPTION_DISTINCT_METAMODELS, Boolean.TRUE);
|
||||
matchEngine = new GenericMatchEngine();
|
||||
}
|
||||
|
||||
public boolean modelsDiffer(Resource left, Resource right)
|
||||
throws InterruptedException {
|
||||
boolean modelsDiffer = false;
|
||||
MatchModel matchModel = matchEngine.resourceMatch(left, right, options);
|
||||
DiffModel diffModel = DiffService.doDiff(matchModel);
|
||||
if (diffModel != null) {
|
||||
for (DiffElement diffElement : diffModel.getOwnedElements()) {
|
||||
modelsDiffer |= checkDiff(diffElement);
|
||||
}
|
||||
}
|
||||
return modelsDiffer;
|
||||
}
|
||||
|
||||
public void addIgnoredFeature(EStructuralFeature feature) {
|
||||
ignoredFeatures.add(feature);
|
||||
}
|
||||
|
||||
private boolean checkDiff(DiffElement diffElement) {
|
||||
boolean hasDiff = false;
|
||||
if (!ignoreDiff(diffElement)) {
|
||||
printDiff(diffElement);
|
||||
hasDiff = true;
|
||||
}
|
||||
for (DiffElement childDiffElement : diffElement.getSubDiffElements()) {
|
||||
hasDiff |= checkDiff(childDiffElement);
|
||||
}
|
||||
return hasDiff;
|
||||
}
|
||||
|
||||
private boolean ignoreDiff(DiffElement diffElement) {
|
||||
if (diffElement instanceof AttributeChange) {
|
||||
return ignoredFeatures.contains(((AttributeChange) diffElement)
|
||||
.getAttribute());
|
||||
} else if (diffElement instanceof ReferenceChange) {
|
||||
return ignoredFeatures.contains(((ReferenceChange) diffElement)
|
||||
.getReference());
|
||||
}
|
||||
return diffElement instanceof DiffGroup;
|
||||
}
|
||||
|
||||
private void printDiff(DiffElement diffElement) {
|
||||
if (diffElement instanceof AttributeChange) {
|
||||
AttributeChange change = (AttributeChange) diffElement;
|
||||
EAttribute attribute = change.getAttribute();
|
||||
System.err.println("Detected attribute difference: "
|
||||
+ attribute.getName());
|
||||
System.err.println("\t" + change.getLeftElement());
|
||||
System.err.println("\t" + change.getRightElement());
|
||||
} else if (diffElement instanceof ReferenceChange) {
|
||||
ReferenceChange change = (ReferenceChange) diffElement;
|
||||
EReference reference = change.getReference();
|
||||
System.err.println("Detected reference difference: "
|
||||
+ reference.getName());
|
||||
System.err.println("\t" + change.getLeftElement());
|
||||
System.err.println("\t" + change.getRightElement());
|
||||
} else {
|
||||
// TODO: add more sysouts here...
|
||||
System.err.println(diffElement.getClass());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 itemis AG and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* committers of openArchitectureWare - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext2ecore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.antlr.runtime.RecognitionException;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.EcorePackage;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
|
||||
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
|
||||
import org.eclipse.m2t.type.emf.EmfRegistryMetaModel;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.XtextEPackageAccess;
|
||||
import org.eclipse.xtext.XtextPackage;
|
||||
import org.eclipse.xtext.parser.XtextFactory;
|
||||
import org.eclipse.xtext.parser.XtextParser;
|
||||
import org.eclipse.xtext.xtextutil.XtextutilPackage;
|
||||
import org.openarchitectureware.expression.ExecutionContextImpl;
|
||||
import org.openarchitectureware.xtend.XtendFacade;
|
||||
|
||||
/**
|
||||
* @author Jan Köhnlein
|
||||
*
|
||||
*/
|
||||
public class TestBootstrapModel extends TestCase {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testParseXtextGrammarTransformXtend() throws IOException, RecognitionException, InterruptedException {
|
||||
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(
|
||||
"org/eclipse/xtext/XTextGrammarTest.xtext");
|
||||
|
||||
// TODO make Xtext2Factory manual so one can overwrite' getEPackages' in
|
||||
// order to support generated epackages
|
||||
EPackage.Registry.INSTANCE.put(XtextEPackageAccess.XTEXT_NS_URI, XtextPackage.eINSTANCE);
|
||||
XtextParser xtext2Parser = new XtextParser();
|
||||
Grammar grammarModel = (Grammar) xtext2Parser.parse(resourceAsStream, new XtextFactory());
|
||||
|
||||
ResourceSetImpl resourceSet = new ResourceSetImpl();
|
||||
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
|
||||
Resource grammarResource = resourceSet.createResource(URI.createFileURI("xtext.xmi"));
|
||||
grammarResource.getContents().add(grammarModel);
|
||||
// grammarResource.save(null);
|
||||
|
||||
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl());
|
||||
|
||||
EmfRegistryMetaModel emfRegistryMetaModel = new EmfRegistryMetaModel() {
|
||||
EPackage[] packages = new EPackage[] { XtextPackage.eINSTANCE, XtextutilPackage.eINSTANCE,
|
||||
EcorePackage.eINSTANCE };
|
||||
|
||||
@Override
|
||||
protected EPackage[] allPackages() {
|
||||
return packages;
|
||||
}
|
||||
};
|
||||
|
||||
ExecutionContextImpl executionContext = new ExecutionContextImpl();
|
||||
executionContext.registerMetaModel(emfRegistryMetaModel);
|
||||
XtendFacade facade = XtendFacade.create(executionContext, "org::eclipse::xtext::xtext2ecore::Xtext2Ecore");
|
||||
List<EPackage> result = (List<EPackage>) facade.call("transform", grammarModel);
|
||||
|
||||
Resource generatedGrammarMetaModelResource = resourceSet.createResource(URI
|
||||
.createFileURI("xtext_generated.ecore"));
|
||||
generatedGrammarMetaModelResource.getContents().addAll(result);
|
||||
// generatedGrammarMetaModelResource.save(null);
|
||||
|
||||
Resource originalGrammarMetaModelResource;
|
||||
if (XtextPackage.eINSTANCE.eResource() == null) {
|
||||
originalGrammarMetaModelResource = resourceSet.createResource(URI.createURI(XtextPackage.eINSTANCE
|
||||
.getNsURI()));
|
||||
} else {
|
||||
originalGrammarMetaModelResource = XtextPackage.eINSTANCE.eResource();
|
||||
}
|
||||
|
||||
EcoreModelComparator ecoreModelComparator = new EcoreModelComparator();
|
||||
ecoreModelComparator.addIgnoredFeature(EcorePackage.Literals.ECLASSIFIER__INSTANCE_CLASS_NAME);
|
||||
ecoreModelComparator.addIgnoredFeature(EcorePackage.Literals.ECLASSIFIER__INSTANCE_TYPE_NAME);
|
||||
ecoreModelComparator.addIgnoredFeature(EcorePackage.Literals.EPACKAGE__NS_URI);
|
||||
ecoreModelComparator.addIgnoredFeature(EcorePackage.Literals.ENAMED_ELEMENT__NAME);
|
||||
ecoreModelComparator.addIgnoredFeature(EcorePackage.Literals.EPACKAGE__NS_PREFIX);
|
||||
assertFalse(ecoreModelComparator.modelsDiffer(originalGrammarMetaModelResource,
|
||||
generatedGrammarMetaModelResource));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue