Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Issue generating a DSL grammar
Issue generating a DSL grammar [message #1016319] Tue, 05 March 2013 21:41 Go to next message
John Castano is currently offline John Castano
Messages: 7
Registered: December 2012
Junior Member
Hi all,

I am trying of create a grammar in order to define the method body and then generate code in some programming languaje. I want to do something like this :


package example {

import example.*


datatype String
datatype Numeric
datatype Boolean
datatype Date

entity Organization
{
name : String
id : String
country : String

userList : User


define op createUser (user: User){

precondition : user.name NOT IN userList



}
}

entity User
{
name: String
govId:String
country:String

}


entity Employee extends example.User{
fecha: Date
}

entity Dependency
{
name: String

}

}


I have a problem when i want to get the attributes of the input parameters of the Method as you can see in the image in red color . I dont know how i can modify my grammar with the purpose i can get the attributes of the objet used as input to the method. My grammar is as follows


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

generate myDsl "MyDsl"

MyDsl:
(elements+=AbstractElement)*;

PackageDeclaration:
'package' name=QualifiedName '{'
(elements+=AbstractElement)*
'}';

AbstractElement:
PackageDeclaration | Type | Import;

QualifiedName:
ID ('.' ID)*;

Import:
'import' importedNamespace=QualifiedNameWithWildcard;

QualifiedNameWithWildcard:
QualifiedName '.*'?;

Type:
DataType | Entity;

DataType:
'datatype' name=ID;

Entity:
'entity' name=ID
('extends' superType=[Entity|QualifiedName])?
'{'
(attribute+=Attributes)*
/* (features += Feature)**/
(operation+=Operations)*
'}';

Attributes:
(many ?= 'many')? name = ID ':' type = [Type | QualifiedName];


Operations:
'define op ' name=ID '(' parameter = Parameters ')' '{' (precondition+=Preconditions)* '}';


Parameters:
name=ID ':' type=[Type|QualifiedName];

Preconditions:
'precondition :' param=[Parameters|QualifiedName] 'NOT IN' attribute=[Attributes|QualifiedName];




Thanks in Advance
Re: Issue generating a DSL grammar [message #1016499 is a reply to message #1016319] Wed, 06 March 2013 11:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6198
Registered: July 2009
Senior Member
Hi,

i think this is basically a scoping issue never the less.

in your grammar you have

Preconditions:
'precondition :' param=[Parameters|QualifiedName] 'NOT IN' attribute=[Attributes|QualifiedName];

so let us change it to

Preconditions:
'precondition :' param=[Attributes|QualifiedName] 'NOT IN' attribute=[Attributes|QualifiedName];

and use scoping like

class MyDslScopeProvider extends org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider {

	def IScope scope_Preconditions_param(Operations ctx, EReference ref) {
		if (ctx.parameter.type instanceof Entity) {
			Scopes::scopeFor((ctx.parameter.type as Entity).attribute, [Attributes a|QualifiedName::create(ctx.parameter.name, a.name)], IScope::NULLSCOPE)
		} else{
			IScope::NULLSCOPE
		}
	}

}
Re: Issue generating a DSL grammar [message #1016531 is a reply to message #1016499] Wed, 06 March 2013 14:16 Go to previous messageGo to next message
John Castano is currently offline John Castano
Messages: 7
Registered: December 2012
Junior Member
Thanks you so much for your help.

Although I do not really understand when i should use scoping.I will study a little about it.

I'm going to try to do you tell me and then i'll tell you the results.

Regards,
Re: Issue generating a DSL grammar [message #1031015 is a reply to message #1016499] Mon, 01 April 2013 00:12 Go to previous messageGo to next message
John Castano is currently offline John Castano
Messages: 7
Registered: December 2012
Junior Member
Hi Christian,

I did what you recommended me and It Works partially.

I put your instrutions (red color) with some changes (blue color) in the Scope Class, but i have a problem trying to pass the arguments to the method named "scopeFor" when i translated the code "[Attributes a|QualifiedName::create(ctx.parameter.name, a.name)]" because an argument type function is mandatory. If I omit this parameter , i can get the attributes of the entity without a Qualified part.


public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {

IScope scope_Preconditions_param(Operations ctx, EReference ref) {
// if (ctx.parameter.type instanceof Entity) {
// Scopes.scopeFor((ctx.parameter.type as Entity).attribute, [Attributes a|QualifiedName::create(ctx.parameter.name, a.name)], IScope::NULLSCOPE)
// } else{
// IScope :: NULLSCOPE
// }

if(ctx.getParameter().getType() instanceof Entity) {
return Scopes.scopeFor (((Entity)(ctx.getParameter().getType())).getAttribute()/*,QualifiedName.create(ctx.getParameter().getName())*/, IScope.NULLSCOPE);
} else {
return IScope.NULLSCOPE;
}

}

}

Do you know how can i solve this issue?

Thanks in advance for your help.
Re: Issue generating a DSL grammar [message #1031110 is a reply to message #1031015] Mon, 01 April 2013 03:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6198
Registered: July 2009
Senior Member
Hi the op wants a function that turns an attribute to a qualified
name as second param so you have yo pass one. E.g. by using a closure
and xtends autoconversion from closure to single method interface. So
why did you change that from my code?

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Issue generating a DSL grammar [message #1031667 is a reply to message #1031110] Mon, 01 April 2013 23:41 Go to previous messageGo to next message
John Castano is currently offline John Castano
Messages: 7
Registered: December 2012
Junior Member
I'm working with Eclipse framework in the class MyDslScopeProvider such as you can see in the attached image https://skydrive.live.com/redir?resid=505EBE8B3650C880!369&authkey=!AIyAxssJNCttJyc. If I put the code ([Attributes a|QualifiedName::create(ctx.parameter.name, a.name)]) in the class a compilation problem ocurrs. I'm having trouble translating this code by one who can understand the tool in Java.

Thanks for you help.
Re: Issue generating a DSL grammar [message #1031736 is a reply to message #1016319] Tue, 02 April 2013 02:16 Go to previous messageGo to next message
michael m is currently offline michael m
Messages: 17
Registered: March 2013
Junior Member
Hi,

you could try this:
return Scopes.scopeFor (((Entity)(ctx.getParameter().getType())).getAttribute(),
  new com.google.common.base.Function<Attributes, QualifiedName>() { 
    public QualifiedName apply(Attributes a) {
      return QualifiedName.create(ctx.getParameter().getName(), a.getName());
    }
  }
  ,IScope.NULLSCOPE);


and define the method args as final e.g. "final Operations ctx".

[Updated on: Tue, 02 April 2013 02:18]

Report message to a moderator

Re: Issue generating a DSL grammar [message #1031737 is a reply to message #1031667] Tue, 02 April 2013 02:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6198
Registered: July 2009
Senior Member
hmmmmm id thought you would find out that much yourself.
If your class is a java and not an xtend class you have of course to use a annonymous inner class of com.google.common.base.Function<T, QualifiedName>
Re: Issue generating a DSL grammar [message #1033190 is a reply to message #1031737] Wed, 03 April 2013 20:29 Go to previous message
John Castano is currently offline John Castano
Messages: 7
Registered: December 2012
Junior Member
Thank you very much for your help, I was looking for documentation about it but I hadn,t found.


I appreciate your help.
Previous Topic:Apply a quickfix from java class
Next Topic:How to insert non-existing fields into the scope
Goto Forum:
  


Current Time: Wed Aug 20 14:46:01 EDT 2014

Powered by FUDForum. Page generated in 0.06466 seconds