copying node model information between resources [message #1066686] |
Wed, 03 July 2013 18:08 |
Mitchell Schrock Messages: 2 Registered: July 2013 |
Junior Member |
|
|
I'm trying to programmatically merge information between two parsed Xtext resources (same grammar), which involves copying information from one resource to another. I am able to do this fairly easily using the EcoreUtil.copy method. A mock-up example is below:
// assume a grammar which generates a parent-child Ecore definition
// get required Ecore objects from Xtext resources
Parent parent1 = (Parent) resource1.getContents().get(0);
Parent parent2 = (Parent) resource2.getContents().get(0);
// create copy of parent2's first child Ecore object
Child parent2FirstChild = parent2.getChildren().get(0);
Child childCopy = EcoreUtil.copy(parent2FirstChild);
// add child copy to parent1
parent1.getChildren().add(childCopy);
// make any other changes to parent1
// save parent1 Xtext resource so it gets serialized back to text
resource1.save(null);
While this method correctly transfers any semantic/ecore information from parent2 to parent1, it does not transfer extra information like comments and whitespace from the parse tree/node model. I don't care much about whitespace, but preserving comments is important. Is there a way to modify a resource's node model or copy nodes from one resource to another?
One workaround is to make comments and whitespace explicit in the grammar, so that they would be part of the generated Ecore model, but then I would need to add those rules everywhere in the grammar, making it really messy.
I am using Xtext 2.2.1. Thanks.
[Updated on: Wed, 03 July 2013 18:53] Report message to a moderator
|
|
|
Re: copying node model information between resources [message #1066779 is a reply to message #1066686] |
Thu, 04 July 2013 10:38 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
No, the parser is the only component that should change the node model.
It is optimized in a way that will make it pretty hard and very error
prone to be changed from the outside.
I'd rather do a second pass, after the semantic model has been
serialized and try to reconcile the comments in the textual representation.
Am 03.07.13 20:42, schrieb Mitchell Schrock:
> I'm trying to programmatically merge information between two parsed
> Xtext resources (same grammar), which involves copying information from
> one resource to another. I am able to do this fairly easily use the
> EcoreUtil.copy method. A mock-up example is below:
>
>
> // assume a grammar which generates a parent-child Ecore definition
>
> // get required Ecore objects from Xtext resources
> Parent parent1 = (Parent) resource1.getContents().get(0);
> Parent parent2 = (Parent) resource2.getContents().get(0);
>
> // create copy of parent2's first child Ecore object
> Child parent2FirstChild = parent2.getChildren().get(0);
> Child childCopy = EcoreUtil.copy(parent2FirstChild);
>
> // add child copy to parent1
> parent1.getChildren().add(childCopy);
>
> // make any other changes to parent1
>
> // save parent1 Xtext resource so it gets serialized back to text
> resource1.save(null);
>
>
> While this method correctly transfers any semantic/ecore information
> from parent2 to parent1, it does not transfer extra information like
> comments and whitespace from the parse tree/node model. I don't care
> much about whitespace, but preserving comments is important. Is there a
> way to modify a resource's node model or copy nodes from one resource to
> another?
>
> One workaround is to make comments and whitespace explicit in the
> grammar, so that they would be part of the generated Ecore model, but
> then I would need to add those rules everywhere in the grammar, making
> it really messy.
>
> I am using Xtext 2.2.1. Thanks.
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
|
Powered by
FUDForum. Page generated in 0.02731 seconds