Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Possible to convert part of STRING to an object's ID using a value converter?
Possible to convert part of STRING to an object's ID using a value converter? [message #1830812] Tue, 04 August 2020 12:37 Go to next message
Eclipse UserFriend
Is it possible to declare a variable within a STRING and then use a substring of that string as an cross reference later for the declaration (with the help of a value converter)?

Use case:
field "injected*PART_REMOVED_OR_MANIPULATED_IN_A_VALUE_CONVERTER";

class TestObject	
	injected {

	}
;

(Where the 'injected' field in TestObject is a cross reference to the earlier declared TemplateField declaration above the TestObject)
I'm thinking something along the following pseudo grammar here, but not sure how to do it since the TemplateField would require an ID but I still only want to provide a STRING for it to be able to use the pseudo code above:

TemplateField: 
	'field' name=STRING '{' '}'
;
ObjectDeclaration:
	'class' name=ID '{'
		field+=[TemplateField|ID]*
	'}' 
;


I have seen how to convert a STRING to an Integer using a value converter, but I'm not sure how to convert that into an ID if that field is missing in the EObject. Can perhaps an additional invisible ID be declared on the TemplateField that can be pick up cross references later?
Re: Possible to convert part of STRING to an object's ID using a value converter? [message #1830813 is a reply to message #1830812] Tue, 04 August 2020 12:46 Go to previous messageGo to next message
Eclipse UserFriend
i am not sure about the substring. can you be more precise with a complete example?
with the one you showed:
if you implement the value converter (or maybe better IQualifiedNameProvider) it should work fine.
https://dietrich-it.de/xtext/2011/07/16/iqualifiednameproviders-in-xtext-2.0/

[Updated on: Tue, 04 August 2020 13:02] by Moderator

Re: Possible to convert part of STRING to an object's ID using a value converter? [message #1830816 is a reply to message #1830813] Tue, 04 August 2020 13:43 Go to previous messageGo to next message
Eclipse UserFriend
Alright, thanks for the link! a more complete example would be this, with the use case below. The substring thing I was talking about is a wildcard token for the template that I want to remove so I can preserve the cross reference.


grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	directives+=Directive*
	;

Directive:
	TemplateDeclaration|ClassDeclaration
;

TemplateDeclaration: 
	'template' name=STRING parameters+=Parameters 
	'{' 
		//... properties here		
	'}'
;

Parameters:
	'(' ID? (',' ID)* ')'
;

ClassDeclaration:
	'class' name=ID '{'
		templates+=Template*
	'}' 
;

Template:
	name=[TemplateDeclaration|ID] parameters+=Parameters '{'
		//...properties
	'}'
;






Usage with wildcard that should be ignored:

template "test*" (arg1, arg2) {
	   //...properties here	
	
}

class TestClass {
	test(parameter1, parameter2) {
	    //...properties here
		
	}
} 

[Updated on: Tue, 04 August 2020 14:13] by Moderator

Re: Possible to convert part of STRING to an object's ID using a value converter? [message #1830819 is a reply to message #1830816] Tue, 04 August 2020 15:05 Go to previous messageGo to next message
Eclipse UserFriend
In this case adopting the name provider should work
Re: Possible to convert part of STRING to an object's ID using a value converter? [message #1830823 is a reply to message #1830819] Tue, 04 August 2020 16:41 Go to previous message
Eclipse UserFriend
It did! And much easier than I thought it would be. Thanks a lot!
Previous Topic:Synchronize changes from the Model to the Embedded Editor in Xtext
Next Topic:Support of OSGI DS 1.3 annotations in Xtend?
Goto Forum:
  


Current Time: Sun Jul 06 00:50:42 EDT 2025

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

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

Back to the top