Help needed in Xtext [message #1049351] |
Thu, 25 April 2013 18:44 |
rohit ahuja 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 #1049381 is a reply to message #1049371] |
Thu, 25 April 2013 19:35 |
|
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
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03862 seconds