Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » copying node model information between resources(programmatic editing of the node model, including comments and whitespace)
copying node model information between resources [message #1066686] Wed, 03 July 2013 18:08 Go to next message
Mitchell Schrock is currently offline Mitchell SchrockFriend
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 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
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
Re: copying node model information between resources [message #1067427 is a reply to message #1066779] Mon, 08 July 2013 15:26 Go to previous message
Mitchell Schrock is currently offline Mitchell SchrockFriend
Messages: 2
Registered: July 2013
Junior Member
Thank you. I think I'm going to make the comment and whitespace rules explicit in the grammar so that they become part of the generated Ecore model. I guess that makes sense because I actually do care about the data those tokens represent. They really shouldn't be hidden in this case.
Previous Topic:Bi Directional Cross-Document Referance on XTEXT
Next Topic:Avoid expensive scope calculation
Goto Forum:
  


Current Time: Wed Apr 24 17:46:38 GMT 2024

Powered by FUDForum. Page generated in 0.02731 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top