Parsing from a tree [message #1777338] |
Tue, 28 November 2017 15:46 |
Nicolas Hili Messages: 40 Registered: March 2017 |
Member |
|
|
Hi,
I am trying to parse a String to get the corresponding AST in Xtext. To do so, I followed this example: https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F
However, my grammar refers to the UML metamodel and therefore, I have cross-references between the model that is parsed by my Xtext grammar and some model elements defined in a UML resource. To support the cross-referencing when parsing my String, I tried the following:
MyDSLActivator activator = MyDSLActivator.getInstance();
Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.getResource(uri, true); // uri is the URI of the UML resource
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl")); // I also create a dummy resource for my parsed model
InputStream in = new ByteArrayInputStream(myStringToParse.getBytes());
try {
resource.load(in, resourceSet.getLoadOptions());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Action model = (Action) resource.getContents().get(0);
I am able to parse my string but all references to UML elements are proxies. I tried to add the following after the last line:
EcoreUtil2.resolveAll(resource);
But it does not seem to resolve any proxy references. Did I miss something? I would have thought that if both my UML resource and my MyDSL resource (that refers to the first one) are in the same Xtext resource set, proxies would be automatically resolved.
Thanks in advance for your answer,
Nicolas
|
|
|
|
|
Re: Parsing from a tree [message #1777369 is a reply to message #1777345] |
Tue, 28 November 2017 22:56 |
Nicolas Hili Messages: 40 Registered: March 2017 |
Member |
|
|
Hi,
Thanks both for your answers,
I have already followed Christian's blog and my UML elements are already exported. I am already capable of parsing with an StyledTextXtextAdaptor:
styledText = new StyledText(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
styledText.setFont(JFaceResources.getTextFont());
styledText.layout();
// adapt to xtext
xtextAdapter = new StyledTextXtextAdapter(injector, IXtextFakeContextResourcesProvider.NULL_CONTEXT_PROVIDER);
xtextAdapter.adapt(styledText);
styledTextObservable = new StyledTextObservableValue(styledText, null, SWT.FocusOut);
styledTextObservable.addChangeListener(new IChangeListener() {
@Override
public void handleChange(ChangeEvent event) {
IParseResult xtextParseResult = xtextAdapter.getXtextParseResult();
Action root = (Action)xtextParseResult.getRootASTElement();
}
Interestingly, the code above works and all references are solved. However, when trying to parse in standalone following this link https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F, it does not resolve the references.
I tried Ed's solution and added UMLResourcesUtil.init(resourceSet), but it does not change anything,
Nicolas
|
|
|
|
|
|
Re: Parsing from a tree [message #1777469 is a reply to message #1777459] |
Wed, 29 November 2017 14:34 |
Nicolas Hili Messages: 40 Registered: March 2017 |
Member |
|
|
I am not sure how I can make it clearer...
I am *not* using MyDslStandaloneSetup (cf my first message).
I am inside Eclipse, I already have my injector:
Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
Still in Eclipse, I have a String to parse and I want to get my object tree. I use the following code:
InputStream in = new ByteArrayInputStream(myStringToParse.getBytes());
try {
resource.load(in, resourceSet.getLoadOptions());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Action model = (Action) resource.getContents().get(0);
I took it from the following link: https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F even though in the link, it is said that it is for loading a model in a standalone application (which I am not doing). However, I could not find any other links that explain how to load a model from a String within Eclipse, so I adapted this code.
As a prerequisite, as my grammar refers to UML elements defined in another UML file (I have already exported the UML element as explained in your blog), I create a XtextResourceSet containing both a resource containing the String to parse (cf. the code above) and my UML file:
Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.getResource(uri, true); // uri is the URI of the UML resource
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl"));
I also added the line UMLResourcesUtil.init(resourceSet) as I was told by Ed that I must do it. I also try other stuff, such as: resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
But still, when getting the model from my parsed String: Action model = (Action) resource.getContents().get(0);
References are unresolved.
I was assuming the scenario is quite common:
Quote:I am inside Eclipse and I want to parse a String. That's is. Nothing more.
The only specificities (that causes me these issues) is that my String to parse contains cross-references to a single UML file. But I have already made steps to ensure that 1) the UML resource is loaded in the same resource set, and 2) the "resolved all" option of the resource set is loaded. Besides, I have an example where I can do it in a StyledText. So cross-references from my Xtext resource to my UML resource work.
Really, I do not think how to make it clearer. The problem seems to be quite simple. I am still surprised that there is no code snippet showing how to parse a String with Xtext within Eclipse and that the only resource mentioned how to do it outside (as a standalone application).
Nicolas
|
|
|
Re: Parsing from a tree [message #1777471 is a reply to message #1777469] |
Wed, 29 November 2017 14:42 |
|
If you
a) running inside Eclipse
b) have all resources in one ResourceSet (your string content resource, and all UML resources you are dependent on, including transitive)
c) have UML elements in the index
it should work out of the box.
To debug into this, you'd have to examine the evaluated scope in the error case. A starting point is DefaultLinkingService#getLinkedObjects. There set a breakpoint on "Collections.emptyList()". If you hit that, drop the stack frame and start over where getScope() is called. The scope needs to contain the name of your referenced element.
|
|
|
Re: Parsing from a tree [message #1777472 is a reply to message #1777469] |
Wed, 29 November 2017 14:45 |
|
- dont use the resolve all loadoption
Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.getResource(uri, true); // uri is the URI of the UML resource
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl"));
instead: (pseudo code)
Injector injector = activator.getInjector(MyDSLActivator.MyDSL_MyDSL);
ResourceSet resourceSet = injector.getInstance(IResourceSetProvider.class).get(projectOfUml);
resourceSet.getResource(uri, true); // uri is the URI of the UML resource
Resource resource = resourceSet.createResource(URI.createURI("platform:/resource/project/(src)/example.mydsl"));
[/code]
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Parsing from a tree [message #1777598 is a reply to message #1777471] |
Thu, 30 November 2017 16:27 |
Nicolas Hili Messages: 40 Registered: March 2017 |
Member |
|
|
Thanks Karsten, Christian, for your answers,
Last Christian's answer works, by applying the modification, all the references are now resolved.
Karsten Thoms wrote on Wed, 29 November 2017 14:42If you
b) have all resources in one ResourceSet (your string content resource, and all UML resources you are dependent on, including transitive)
That might explain it. I guess this is what happens when getting the resource set from the specified project. That would be worth adding this example to the Xtext FAQ.
Thanks again,
Nicolas
|
|
|
|
Powered by
FUDForum. Page generated in 0.02611 seconds