Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Ensure type safety for Xtext grammar
Ensure type safety for Xtext grammar [message #1222103] Thu, 19 December 2013 17:14 Go to next message
Sebastian Kämpfer is currently offline Sebastian KämpferFriend
Messages: 3
Registered: December 2013
Junior Member
Hi everybody,

I'm fairly new to Xtext, and currently stuck with a problem I could not find a proper solution for.
This problem concerns type safety for a DSL I'm writing which (to my limited knowledge) cannot be solved with cross-references. I hope the example below illustrates the problem good enough:

I am defining complex data types for example with the following strutcture:
Entity BaseClass {
  name : String
  id   : Int
}

Entity DerivedA extends BaseClass {
  foo : String
}

Entity ThirdClass {
  module : BaseClass
}


So lets say I want to create instances of these classes with the following rule:
entities+=Entity
instances+=Instance;

Instance :
 'make' classname=[Entity] name=ID
;

Entity :
 'Entity' name=ID '{'
   features+=Feature
 '}'
;

...

which can be applied like this:
make BaseClass firstBaseInstance
make DerivedA firstDervidedInstance
make DerivedA secondDerivedInstance
make ThirdClass instanceOfThird

This works fine with cross references, but I would also like to realise assignments which could look like this:
set 'module' of 'instanceOfThird' to 'firstBaseInstance'
set 'module' of 'instanceOfThird' to 'firstDerivedInstance'


How would a grammar rule for such an assignment look like so I get an error if the feature 'module' would not have been declared in the scope of 'ThirdClass' or 'firstBaseInstance' would not be of Type 'BaseClass'?

Regards,
Sebastian



Re: Ensure type safety for Xtext grammar [message #1222350 is a reply to message #1222103] Fri, 20 December 2013 08:11 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 248
Registered: June 2013
Senior Member
Hi Sebastian,

in your cross reference, you can use "QualifiedNames" as well so your DSL may look like

set instanceOfThird.module to firstBaseInstance

There is a good example in the documentation, just search for QualifiedName
Re: Ensure type safety for Xtext grammar [message #1222365 is a reply to message #1222103] Fri, 20 December 2013 08:50 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
The grammar rule would be something like:

Assignment :
'set' feature+=[Feature] 'of' of=[Instance] 'to' to=[Instance]

In addition you'd need to implementing the scoping rules for these
references. Especially the first one needs to only look at features
containing in the of instance, which brings me to the following suggestion:

When someone writes such an assignment he'd need to first pick the
feature and the tool would be able to propose something as the 'to' part
deosn'T exist yet. Changing the order will help.

Also you are entering the world of expressions. Mabe you want to set
other values as well, such as a string or an Int?

If Java is your target environment, be sure to have a look at Xbase.

Sven

Am 19/12/13 19:07, schrieb Sebastian Kämpfer:
> Hi everybody,
>
> I'm fairly new to Xtext, and currently stuck with a problem I could not
> find a proper solution for.
> This problem concerns type safety for a DSL I'm writing which (to my
> limited knowledge) cannot be solved with cross-references. I hope the
> example below illustrates the problem good enough:
>
> I am defining complex data types for example with the following strutcture:
>
> Entity BaseClass {
> name : String
> id : Int
> }
>
> Entity DerivedA extends BaseClass {
> foo : String
> }
>
> Entity ThirdClass {
> module : BaseClass
> }
>
>
> So lets say I want to create instances of these classes with the
> following rule:
>
> entities+=Entity
> instances+=Instance;
>
> Instance :
> 'make' classname=[Entity] name=ID
> ;
>
> Entity :
> 'Entity' name=ID '{'
> features+=Feature
> '}'
> ;
>
> ...
>
> which can be applied like this:
>
> make BaseClass firstBaseInstance
> make DerivedA firstDervidedInstance
> make DerivedA secondDerivedInstance
> make ThirdClass instanceOfThird
>
> This works fine with cross references, but I would also like to realise
> assignments which could look like this:
>
> set 'module' of 'instanceOfThird' to 'firstBaseInstance'
> set 'module' of 'instanceOfThird' to 'firstDerivedInstance'
>
>
> How would a grammar rule for such an assignment look like so I get an
> error if the feature 'module' would not have been declared in the scope
> of 'ThirdClass' or 'firstBaseInstance' would not be of Type 'BaseClass'?
>
> Regards,
> Sebastian
>
>
>
>


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Ensure type safety for Xtext grammar [message #1223198 is a reply to message #1222103] Sun, 22 December 2013 18:51 Go to previous messageGo to next message
Sebastian Kämpfer is currently offline Sebastian KämpferFriend
Messages: 3
Registered: December 2013
Junior Member
Thank you Uli and Sven for your quick replies!

Scoping and qualified names are the keywords I was lacking in this context.
I haven't worked with ScopeProviders yet, and only came across the namespace functionality during the package part of the tutorial. Somehow I didn't realized this could be extended to "class members" as well.

I'll try to dig into both of these subjects a little further and let you know which way I decided to go and how.

Regards,
Sebastian
Re: Ensure type safety for Xtext grammar [message #1224991 is a reply to message #1223198] Sat, 28 December 2013 10:43 Go to previous message
Sebastian Kämpfer is currently offline Sebastian KämpferFriend
Messages: 3
Registered: December 2013
Junior Member
So I decided to use Scoping over qualified names. The reasons for that I'll explain in this post.
A tutorial which I found very useful to begin with is this Xtext screencast:
(http://)xtextcasts.org/episodes/17-restricting-scope

The problem with qualified names here is that I could easily do something like this:
Assignment :
 'set' property=[Entity|QualifiedName] '=' value=STRING
;
-------------
Entity Foo {
 var name : String
}

set Foo.name = 'bar'  // works with QNames

but not for "instances" like I need to:
make Foo instanceOfFoo
set instanceOfFoo.name = 'bar' // does not work with QNames


Solution:
If my grammar looks like this:
Root : 
  entities=Entity*
  instances=Instance*
  assignments=Assignment*
;
Entity :
  'Entity' name=ID ('extends' superType=[Entity])? '{'
    properties=Property*
  '}'
;
Property :
  'var' name=ID ':' type=[Type]
;
Instance :
  'make' entity=[Entity] name=ID
;
Assignment :
  'set' instance=[Instance]'::'property=[Property] '=' value=STRING
;

a code like this:
Entity Foo {
 var name : String
}
make Foo instanceOfFoo
set instanceOfFoo::name = 'bar'  // property 'name' is out of scope for this assignment 

would not be possible since a cross reference to the entity's property 'name' is out of scope in the specific assignment.

Solution for this is to customize a scope provider which may look like this:
def IScope scope_Assignment_property(Assignment assignment, EReference eReference) {
    var entity = assignment.instance.entity
    if(entity.superType != null)
      Scopes::scopeFor(entity.properties,scope_superType_property(entity.superType))
    else
      Scopes::scopeFor(entity.properties)
}
	
def IScope scope_superType_property(Entity entity){
    Scopes::scopeFor(entity.properties)
}

This 'imports' the properties defined in the scope of the application of the Entity-Rule into the scope of the application of the Assignment-Rule. In this example I also applied an outer scope (second method) which imports the properties of a parent class :
Entity Foo {
  var name : String
}
Entity Bar extends Foo {
  var id : Int
}
make Bar instanceOfBar
make Foo instanceOfFoo
set instanceOfFoo::name = 'Name'   
set instanceOfBar::name = 'TestMe' // works as well


Regards,
Sebastian

[Updated on: Sat, 28 December 2013 10:50]

Report message to a moderator

Previous Topic:Registering/creating your own fragments to be used in the workflow
Next Topic:[MWE2] Running workflow from plugin, custom components not found
Goto Forum:
  


Current Time: Mon Nov 30 18:10:45 GMT 2020

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

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

Back to the top