Define own scope: Reference C methods [message #762832] |
Thu, 08 December 2011 17:16 |
thomas.kipar Messages: 13 Registered: July 2011 |
Junior Member |
|
|
Hi,
I am new to Xtext and just started playing around a little with it. What I want to do is to define a DSL that contains cross references to C-Methods (totally analogous to the jvm-types stuff). What do I have to do for this?
What I did is:
- Creating a ecore model that contains a class describing a C-Method (lets call is CMethod)
- Importing that model to my xtext grammar, using the CMethod-class as cross reference in a rule
- Implementing IGlobalScopeProvider. In the getScope(...) method, I check the class of the given EReference
- If its type is CMethod, I return an instance of my own Scope (CMethdScope extends Abstractcope)
- Finally, I have to implement getAllLocalElements() in my CMehtodScope class. I just search for declared methods and return IEObjectDescriptions
For now, I'm unsure how to build the object descriptions. I just could pass a new instance of my CMethod class there. However, when using my generated editor I get a "dangling reference" error. The JVMTypes creates proxy objects, but I do not understand how to use/create some "custom" URIs that are used there ("java"-protocol), so thats where I am stuck.
Please, can somebody explain how do I have to build those IEObjectDescriptions I have to return from my own scope so I can use them in my grammar?
Regards,
Thoams
|
|
|
|
Re: Define own scope: Reference C methods [message #762941 is a reply to message #762907] |
Thu, 08 December 2011 20:56 |
thomas.kipar Messages: 13 Registered: July 2011 |
Junior Member |
|
|
Hi Christian,
thanks for your answer. If I understand it correctly, the example shown in the link uses references to existing instances of EObjects saved in a resource (an UML model in this case). I do not have something like that, since C-Code is not any ecore-resource.
However, think I got the thing running now (finally). Afaik Xtext can only hold references on other EObjects. Thats what I do not have (so I just create some arbitrary once in my scope provider). Doing so fails, because the linker is not able to find those objects somewhere. The solution is already given by the jvm-types stuff (but for quite difficult to understand, at least for me).
In order to reference other "custom" resources, you have to create your own "protocol" (=some string) and register it to the resourceSet attached to the resource given to your scope provider:
pResourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(PROTOCOL, resFactory);
where resFactory must be an instance implementing Resource.Factory (from emf). When returning IEObjectDescription from your scope, you have to set a proxy url (using your protocol) to the object you attach to the description:
InternalEObject internal = (InternalEObject) pObject;
internal.eSetProxyURI(URI.createURI("MY_PROTOCOL:/my/uri/one#frament.1"));
where MY_PROTOCOL is the protocol you used above. When Xtext runs the linker, your implementation of Resource.Factory is invoked in order to load the resource from the given uri (in which you encode the required information).
Hope that's it.
|
|
|
Powered by
FUDForum. Page generated in 0.03165 seconds