Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Help needed in Xtext
Help needed in Xtext [message #1049351] Thu, 25 April 2013 18:44 Go to next message
rohit ahuja is currently offline rohit ahujaFriend
Messages: 27
Registered: February 2013
Junior Member
Hi All,

I need to implement something like below:

// a simple structure, there can be any no, similar to C
typedef struct
{
int a;
int b;
....
}emp;

//any no of instances of the structure
emp emp1;
emp emp2;

//assigning values to the instances
emp1.a=2;
emp2.b=3;

//function definition
void func(emp emp1)
{
x=1;
y=emp1.a;
}

The grammar that i wrote is:

Model:
((struct+=Struct) | (instance+=Instance) | (function+=Function))*
;

// a simple structure, there can be any no, similar to C
//typedef struct
//{
//int a;
//int b;
//....
//}emp;


Struct:
'typedef struct {'
(params+=Parameter)+
'}'name=ID ';'
;
Parameter:
'int' name=ID;


//emp emp1;
Instance:
struct=[Struct] name=ID';'
;
//i need to put something like emp1.a = 3; , which can be anywhere after the emp1 declaration, similar to the concept that variable can be used only after its defined. So how do i include it in grammar. Is it possible to do without any validator, by creating some grammar rule? How do i judge where to put in the rule as weel, as this statement can be basically anywhere afterwards

//Also for creating a variable such as emp1.a, i basically need to refer
name=[Instance]'.'param=[Parameter] '=' value=INT, but it gives some error in the editor, and doesnt consider the Parameter . Is it possible to refer it this way, or any other way




Function:
('void')? name=ID '(' ((struct+=[Struct] structVarName+=ID) (',' struct+=[Struct] structVarName+=ID)*)? ')' '{'
(assignments+=Assignment)+
'}'
;

Assignment:
name=ID '=' value=INT';'
;

Can someone please suggest?

Thanks,
Rohit
Re: Help needed in Xtext [message #1049371 is a reply to message #1049351] Thu, 25 April 2013 19:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

stil the same answer as before

Instance:
struct=[Parameter|FQN] name=ID';' 
;

FQN: ID ("." ID);


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Help needed in Xtext [message #1049381 is a reply to message #1049371] Thu, 25 April 2013 19:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Here is an extended sample

Model:
((struct+=Struct) | (instance+=Instance) | (ass+=Assignment))*
;

Struct:
'typedef struct {'
(params+=Parameter)+
'}'name=ID ';'
;
Parameter:
'int' name=ID;


//emp emp1;
Instance:
struct=[Struct] name=ID';' 
;


Assignment:
inst=[Instance] "."param=[Parameter]'=' value=INT';'
;


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

	def IScope scope_Assignment_param(Assignment a, EReference ref) {
		 Scopes::scopeFor(a.inst.struct.params)
	}

}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Help needed in Xtext [message #1049670 is a reply to message #1049381] Fri, 26 April 2013 06:16 Go to previous messageGo to next message
rohit ahuja is currently offline rohit ahujaFriend
Messages: 27
Registered: February 2013
Junior Member
Hi Christian,

Thanks for the reply

If i use this, the grammar doesn't give any error, but once i check in the UI editor(the runtime eclipse environment), the param=[Parameter], thing doesn't work, it says couldn't resolve reference to Parameter, even though the parameter is defined

Assignment:
inst=[Instance] "."param=[Parameter]'=' value=INT';'
;

On the other hand, if i use

Re: Help needed in Xtext [message #1049671 is a reply to message #1049670] Fri, 26 April 2013 06:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Sorry it worked perfect with your example . sure you did not forget
to use my complete grammar and to adopt the scope provider.
Especially the scope provider is keen on naming.

--
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Help needed in Xtext [message #1049697 is a reply to message #1049671] Fri, 26 April 2013 07:03 Go to previous messageGo to next message
rohit ahuja is currently offline rohit ahujaFriend
Messages: 27
Registered: February 2013
Junior Member
Thanks again,

naming of the IScope was the issue, now it works as expected, didn't realize that the name of the IScope could be the issue i used some random name earlier.

Thanks a lot Smile


Re: Help needed in Xtext [message #1049703 is a reply to message #1049697] Fri, 26 April 2013 07:09 Go to previous messageGo to next message
rohit ahuja is currently offline rohit ahujaFriend
Messages: 27
Registered: February 2013
Junior Member
One more thing something like below works, however it shouldn't since struct is defined after but it is used before, i know i can write a validator, is there any other way directly from the grammar?

emp emp1;
emp1.X =4;

typedef struct {
int X;
int Y;
int Z;
} emp;

Re: Help needed in Xtext [message #1049708 is a reply to message #1049703] Fri, 26 April 2013 07:16 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

you have to write a validator for that yourself


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:XText 2.4.1 DomainmodelJvmModelInferrer
Next Topic:XText ValueConverter And Generated Editor Issue
Goto Forum:
  


Current Time: Fri Apr 19 20:41:05 GMT 2024

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

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

Back to the top