Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Class members reference with xtext(How to implement the class members reference in expressions)
Class members reference with xtext [message #1797650] Sat, 03 November 2018 19:15 Go to next message
Eclipse UserFriend
Hi all
I have implemented the grammar(please, see it below) and there are classes(aka database tables) and operations. I want to add class members to expressions.
I know that variable reference can be implemented something like the following
Atomic returns Expression:
	{IntConstant} value=INT |
	{StringConstant} value=STRING |
	{BoolConstant} value=('true' | 'false') |
	{VariableRef} value=[VariableDeclaration];

but maybe it's possible to add the link on the class member reference? Something like this.
Atomic returns Expression:
	{IntConstant} value=INT |
	{StringConstant} value=STRING |
	{BoolConstant} value=('true' | 'false') |
	{VariableRef} value=[VariableDeclaration]|
       {ClassMemberRef} value=[ClassDefinition.member];


I need it to use it in the type checking and also check that given member really there are in the given class

I know that with content independent grammar is impossible, but maybe it's possible in XText?
Regards,
Vladimir

My grammar is
grammar org.blockchain.rell.Rell with org.eclipse.xtext.common.Terminals

generate rell "http://www.blockchain.org/rell/Rell"

Model:
	entities+=ClassDefinition*
	operations+=Operation*;

ClassDefinition:
	'class' name=ID ('extends' superType=[ClassDefinition])? '{'
	attributes+=Attribute*
	'}';

Operation:
	"operation" name=ID "(" parameters=RelAttrubutesList? ")" "{" statements+=Statement* "}";

Statement:
	( relation=Relational |  variable=Variable | varInit=VariableInit) ';';

Variable:
	declaration=VariableDeclaration (('=' expression=Expression)?);

Relational:
	Update | Delete | Create;

Update:
	'update' entity=[ClassDefinition] '(' conditions=Conditions? ')' '{' variableList+=UpdateVariableInit
	(',' variableList+=UpdateVariableInit*)? '}';


Delete:
	'delete' entity=[ClassDefinition] '(' conditions=Conditions? ')';

Create:
	'create' entity=[ClassDefinition] '(' conditions=Conditions? ')';

Conditions:
	elements+=ConditionElement (',' elements+=ConditionElement*)?;

ConditionElement:
	name=ID ('==' | '!=' | '>' | '<' | '>=' | '<=') expr=Expression;

VariableInit:
	name=[VariableDeclaration] '=' expression=Expression;

UpdateVariableInit:
	name = ID '=' expression=Expression
;
Expression:
	or=Or;

Or returns Expression:
	And ({Or.left=current} "or" right=And)*;

And returns Expression:
	Equality ({And.left=current} "and" right=Equality)*;

Equality returns Expression:
	Comparison ({Equality.left=current} op=("==" | "!=")
	right=Comparison)*;

Comparison returns Expression:
	PlusOrMinus ({Comparison.left=current} op=(">=" | "<=" | ">" | "<")
	right=PlusOrMinus)*;

PlusOrMinus returns Expression:
	MulOrDiv (({Plus.left=current} '+' | {Minus.left=current} '-')
	right=MulOrDiv)*;

Atomic returns Expression:
	{IntConstant} value=INT |
	{StringConstant} value=STRING |
	{BoolConstant} value=('true' | 'false') |
	{VariableRef} value=[VariableDeclaration];

MulOrDiv returns Expression:
	Primary ({MulOrDiv.left=current} op=('*' | '/')
	right=Primary)*;

Primary returns Expression:
	'(' Expression ')' |
	{Not} "not" expression=Primary |
	Atomic;

RelAttrubutesList:
	value+=VariableDeclaration (',' value+=VariableDeclaration)*;

Attribute:
	modificator=Prefix? variable=VariableDeclaration ';';

VariableDeclaration:
	name=ID ':' type=TypeReference;

TypeReference:
	primitive=PrimitiveType | entityType=ClassType;

PrimitiveType:
	primitiveType=(Text | Integer | Json | ByteArray | Boolean);

ClassType:
	entityRef=[ClassDefinition];

Boolean:
	'bool';

Json:
	'json';

Integer:
	'integer' | 'timestamp';

Text:
	'text' | 'tuid' | 'name';

ByteArray:
	'byte_array' | 'signer' | 'guid' | 'pubkey';

Prefix:
	'key' | 'index';

Re: Class members reference with xtext [message #1797651 is a reply to message #1797650] Sat, 03 November 2018 19:22 Go to previous messageGo to next message
Eclipse UserFriend
I don't really understand your question.
If you want to reference attributes simply reference attributes

The main problem is syntax and ambiguity.

So either you need to disambigue on grammar level

E.g. attr=[Attribute|FQN]

With FQN: ID "." ID;

Or you need to introduce a common super type

Referrable: Attribute|VariableDecl;
And use that one in your reference
Re: Class members reference with xtext [message #1797652 is a reply to message #1797651] Sat, 03 November 2018 20:14 Go to previous messageGo to next message
Eclipse UserFriend
Well, please consider for example two java classes
class ClassA{
bool a
}
class ClassB{
int a;
}

In case we do following
ClassA a;
Class b;

a.a=b.a;

Java compiler will generate an error(incorrect types)

As well as in the case we do
a.a=b.c;
(no field c) in class B

So I want to do the same. I know that is possible to do with the validators. But I need a lot of the job. Maybe it's possible to do the same with grammar?
Not a problem to change the grammar but with saving the common sense.
Re: Class members reference with xtext [message #1797660 is a reply to message #1797652] Sun, 04 November 2018 04:48 Go to previous messageGo to next message
Eclipse UserFriend
Well I think you are searching for a thing callled dot or path expression
https://www.dietrich-it.de/xtext/2013/05/18/xtext-and-dot-expressions.html

Plus scoping and typesystem

[Updated on: Sun, 04 November 2018 04:49] by Moderator

Re: Class members reference with xtext [message #1797668 is a reply to message #1797660] Sun, 04 November 2018 12:11 Go to previous message
Eclipse UserFriend
Yes, look like it is what I need. Thank you
Previous Topic:Xtext upgrade
Next Topic:Signer information error
Goto Forum:
  


Current Time: Sat Jun 14 01:45:26 EDT 2025

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

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

Back to the top