mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-16 00:38:56 +00:00
added test for languageinheritance, first steps towards generation of abstract languages
This commit is contained in:
parent
8938071a53
commit
2aa57b0034
5 changed files with 35 additions and 25 deletions
|
@ -14,6 +14,7 @@ import org.eclipse.emf.ecore.resource.Resource;
|
|||
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil;
|
||||
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
|
||||
import org.eclipse.xtext.resource.ClassloaderClasspathUriResolver;
|
||||
|
||||
public class EcoreUtil2 extends EcoreUtil {
|
||||
private static Log log = LogFactory.getLog(EcoreUtil2.class);
|
||||
|
@ -59,18 +60,22 @@ public class EcoreUtil2 extends EcoreUtil {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses CurrentThread Classloader! runtime only!
|
||||
*/
|
||||
public static final EPackage loadEPackage(String uriAsString) {
|
||||
if (EPackage.Registry.INSTANCE.containsKey(uriAsString))
|
||||
return EPackage.Registry.INSTANCE.getEPackage(uriAsString);
|
||||
URI uri = URI.createURI(uriAsString);
|
||||
uri = new ClassloaderClasspathUriResolver().resolve(null, uri);
|
||||
Resource resource = new ResourceSetImpl().getResource(uri, true);
|
||||
for (TreeIterator<EObject> allContents = resource.getAllContents(); allContents.hasNext();) {
|
||||
EObject next = allContents.next();
|
||||
if (next instanceof EPackage) {
|
||||
EPackage ePackage = (EPackage) next;
|
||||
if (ePackage.getNsURI() != null && ePackage.getNsURI().equals(uriAsString)) {
|
||||
// if (ePackage.getNsURI() != null && ePackage.getNsURI().equals(uriAsString)) {
|
||||
return ePackage;
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
log.error("Could not load EPackage with nsURI" + uriAsString);
|
||||
|
|
|
@ -9,28 +9,23 @@
|
|||
|
||||
package org.eclipse.xtext;
|
||||
|
||||
import static org.eclipse.emf.ecore.util.EcoreUtil.getRootContainer;
|
||||
import static org.eclipse.xtext.EcoreUtil2.eAllContentsAsList;
|
||||
import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
|
||||
import static org.eclipse.xtext.EcoreUtil2.getContainerOfType;
|
||||
import static org.eclipse.xtext.EcoreUtil2.typeSelect;
|
||||
import static org.eclipse.emf.ecore.util.EcoreUtil.*;
|
||||
import static org.eclipse.xtext.EcoreUtil2.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.emf.common.util.EList;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.common.util.WrappedException;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.builtin.IXtextBuiltin;
|
||||
import org.eclipse.xtext.conversion.IValueConverterService;
|
||||
import org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor;
|
||||
import org.eclipse.xtext.parsetree.reconstr.callbacks.SimpleSerializingCallback;
|
||||
import org.eclipse.xtext.service.ILanguageDescriptor;
|
||||
import org.eclipse.xtext.service.LanguageDescriptorFactory;
|
||||
import org.eclipse.xtext.service.ServiceRegistry;
|
||||
import org.eclipse.xtext.builtin.XtextBuiltinGrammarAccess;
|
||||
import org.eclipse.xtext.util.Strings;
|
||||
import org.eclipse.xtext.util.XtextSwitch;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -136,15 +131,22 @@ public class GrammarUtil {
|
|||
}
|
||||
String id = getSuperGrammarId(_this);
|
||||
if (id == null) {
|
||||
id = IXtextBuiltin.ID;
|
||||
return new XtextBuiltinGrammarAccess().getGrammar();
|
||||
}
|
||||
ILanguageDescriptor descriptor = LanguageDescriptorFactory.get(id);
|
||||
if (descriptor == null)
|
||||
throw new IllegalStateException("Language '"+id+"' has not been set up properly");
|
||||
IGrammarAccess service = ServiceRegistry.getService(descriptor, IGrammarAccess.class);
|
||||
if (service == null)
|
||||
throw new IllegalStateException("Language '"+id+"' has not been set up properly");
|
||||
Grammar superGrammar = service.getGrammar();
|
||||
String classpathURI = "classpath:/"+id.replace('.', '/')+".xtext";
|
||||
// ILanguageDescriptor descriptor = LanguageDescriptorFactory.get(id);
|
||||
// if (descriptor == null)
|
||||
// throw new IllegalStateException("Language '"+id+"' has not been set up properly");
|
||||
// IGrammarAccess service = ServiceRegistry.getService(descriptor, IGrammarAccess.class);
|
||||
// if (service == null)
|
||||
// throw new IllegalStateException("Language '"+id+"' has not been set up properly");
|
||||
Resource resource = _this.eResource().getResourceSet().getResource(URI.createURI(classpathURI),true);
|
||||
try {
|
||||
resource.load(null);
|
||||
} catch (IOException e) {
|
||||
throw new WrappedException(e);
|
||||
}
|
||||
Grammar superGrammar = (Grammar) resource.getContents().get(0);
|
||||
return superGrammar == _this ? null : superGrammar;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
// when LEXER_BODY has been removed use the following...
|
||||
abstract language XtextBuiltIns
|
||||
abstract language org.eclipse.xtext.builtin.XtextBuiltIn_Temp
|
||||
|
||||
import "http://ecore" as ecore;
|
||||
import "http://www.eclipse.org/emf/2002/Ecore" as ecore;
|
||||
|
||||
lexing:
|
||||
|
||||
ID : "('^')?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*";
|
||||
INT : "('0'..'9')+";
|
||||
INT returns ecore::EInt
|
||||
: "('0'..'9')+";
|
||||
STRING : "
|
||||
'\"' ( '\\\\' ('b'|'t'|'n'|'f'|'r'|'\\\"'|'\\''|'\\\\') | ~('\\\\'|'\"') )* '\"' |
|
||||
'\\'' ( '\\\\' ('b'|'t'|'n'|'f'|'r'|'\\\"'|'\\''|'\\\\') | ~('\\\\'|'\\'') )* '\\''
|
|
@ -52,6 +52,8 @@ public class ClassloaderClasspathUriResolver implements IClasspathUriResolver {
|
|||
pathAsString = pathAsString.substring(1);
|
||||
}
|
||||
URL resource = classLoader.getResource(pathAsString);
|
||||
if (resource==null)
|
||||
throw new IllegalStateException("Couldn't find resource on classpath. URI was '"+classpathUri+"'");
|
||||
URI fileUri = URI.createURI(resource.toURI().toString());
|
||||
return fileUri;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue