Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to get the list of classes for scope
How to get the list of classes for scope [message #1803009] Wed, 20 February 2019 17:37 Go to next message
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
Hi all, I have the grammar(see it below) and there is the following rule.


DotValue:
	varRef=VariableDeclarationRef '.' (dotPart=DotValue?);
....
VariableDeclaration:
	name=ValuableID ((COLON type=TypeReference)?)
...
VariableDeclarationRef:
	decl=[VariableDeclaration|ValuableID];


As you can see, sometimes we don't specify the type of variable and get it from context. So the question is: in what way I can to get the ClassDefinition instance by className what I have in the variable?

Regards,
Vladimir
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])? '{'
	attributeListField+=AttributeListField*
	'}';

AttributeListField:
	mutable=Mutable? key=Key? index=Index? attributeList+=RelAttrubutesList ';';

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

Statement:
	(relation=Relational | variable=OperationVariable | varInitiation=VarInitiation) ';';

VarInitiation:
	VariableDeclarationRef ('=' expression=Expression)?;

OperationVariable:
	assessModificator=(Var | Val) variable=Variable;

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

Relational:
	Create;

Create:
	'create' (entity=[ClassDefinition]) '(' (createWhatPart+=CreateWhatPart (','
	createWhatPart+=CreateWhatPart)*)? ')';

CreateWhatPart:
	(varDeclRef=[VariableDeclaration|ValuableID] '=')?
	condition+=Expression;

TupleValue:
	'(' members+=TupleValueMember (',' tuplePart+=TupleValueMember)* ')';

TupleValueMember:
	(name=ValuableID '=')? value=Expression;

ClassMemberDefinition:
	(classRef=ClassRef)? "." variableDeclarationRef=VariableDeclarationRef;

ClassRef:
	value=[TableNameWithAlias];

TableNameWithAlias:
	{ClassRefDecl} (name=ID COLON classDef=[ClassDefinition]) |
	{JustNameDecl} name=[ClassDefinition];

VariableDeclarationRef:
	decl=[VariableDeclaration|ValuableID];

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=(EQUALS | NOT_EQUALS | EQUALSS | NOT_EQUALSS)
	right=Comparison)*;

Comparison returns Expression:
	PlusOrMinus ({Comparison.left=current} op=(MORE_EQUAL | LESS_EQUAL | MORE | LESS)
	right=PlusOrMinus)*;

PlusOrMinus returns Expression:
	MulOrDiv (({Plus.left=current} PlusChar | {Minus.left=current} MinusChar)
	right=MulOrDiv)*;

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

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

Atomic returns Expression:
	{IntConstant} value=IntWithMinus |
	{StringConstant} value=STRING |
	{ByteArrayConstant} value=BYTE_ARRAY_VALUE |
	{BoolConstant} value=('true' | 'false') |
	{MemberDefinition} value=ClassMemberDefinition |
	{CreateAtom} value=Create
	| {TupleRes} value=TupleValue
	| {DotValue} value=DotValue
	| {VariableRef} value=VariableDeclarationRef;

DotValue:
	varRef=VariableDeclarationRef '.' (dotPart=DotValue?);

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

VariableDeclaration:
	name=ValuableID ((COLON type=TypeReference)?);

TupleVarDeclaration:
	(name=ValuableID COLON)? type=TypeReference;

TypeReference:
	primitive=PrimitiveType | entityType=ClassType | tuple=TupleType | list=ListType | set=SetType | map=MapType;

ListType:
	List ListSpecification;

SetType:
	Set ListSpecification;

MapType:
	Map MapSpecification;

ListSpecification:
	LESS listType=TypeReference (QuestionChar?) MORE;

MapSpecification:
	LESS keySpec=TypeReference (QuestionChar?) ',' valSpec=TypeReference (QuestionChar?) MORE;

TupleType:
	'(' varDecl+=TupleVarDeclaration (',' varDecl+=TupleVarDeclaration)* ')';

ClassType:
	type=[ClassDefinition];

PrimitiveType:
	Text | Tuid | Pubkey | Name | Timestamp | Integer | Json | ByteArray | Boolean;

ValuableID:
	PrimitiveType | ID;

AmpersandModificator:
	PlusChar | MulChar | QuestionChar;

IntWithMinus:
	MinusChar? INT;

Name:
	'name';

Pubkey:
	'pubkey';

Timestamp:
	'timestamp';

Tuid:
	'tuid';

Boolean:
	'boolean';

Json:
	'json';

Integer:
	'integer';

Text:
	'text';

ByteArray:
	'byte_array';

List:
	'list';

Map:
	'map';

Set:
	'set';

Key:
	'key';

Index:
	'index';

Mutable:
	'mutable';

Var:
	'var';

Val:
	'val';

Ampersand:
	'@';

QuestionChar:
	'?';

PlusChar:
	'+';

MinusChar:
	'-';

MulChar:
	'*';

DivChar:
	'/';

terminal EQUALS:
	'==';

terminal EQUALSS:
	'===';

terminal NOT_EQUALS:
	'!=';

terminal NOT_EQUALSS:
	'!==';

terminal MORE_EQUAL:
	'>=';

terminal LESS_EQUAL:
	'<=';

terminal MORE:
	'>';

terminal LESS:
	'<';

terminal COLON:
	':';

terminal NULL:
	'null';

terminal BYTE_ARRAY_CHAR:
	'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'A' | 'B' | 'C' | 'D'
	| 'E' | 'F';

terminal BYTE_ARRAY_PAIR:
	BYTE_ARRAY_CHAR BYTE_ARRAY_CHAR;

terminal BYTE_ARRAY_VALUE:
	'x' (('\'' BYTE_ARRAY_PAIR* '\'') | ('"' BYTE_ARRAY_PAIR* '"'));
Re: How to get the list of classes for scope [message #1803101 is a reply to message #1803009] Fri, 22 February 2019 12:25 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi,

could you please rephrase the question? What problem do you want to solve? Could you please provide a few lines of sample code with comments such that it becomes easier to follow. Unfortunately I don't have the time to understand the entire grammar and make some sense of it.

Thank you
Sebastian
Re: How to get the list of classes for scope [message #1803118 is a reply to message #1803101] Fri, 22 February 2019 16:47 Go to previous messageGo to next message
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
Well, I want to get the reference on the ClassDefinition by its name. In what I can to do it?

[Updated on: Fri, 22 February 2019 16:55]

Report message to a moderator

Re: How to get the list of classes for scope [message #1803126 is a reply to message #1803118] Fri, 22 February 2019 19:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Please explain what you want to achieve. E.g. using sample models and pseudo code for scoping

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get the list of classes for scope [message #1803142 is a reply to message #1803126] Sat, 23 February 2019 11:55 Go to previous messageGo to next message
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
I want to create the method something like following
getEObject(String name, RuleName ruleName) what will return list of EObject with given name and the rule.
Re: How to get the list of classes for scope [message #1803143 is a reply to message #1803142] Sat, 23 February 2019 12:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
But why. What is the problem you want to solve

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sat, 23 February 2019 12:30]

Report message to a moderator

Re: How to get the list of classes for scope [message #1803152 is a reply to message #1803143] Sat, 23 February 2019 17:51 Go to previous messageGo to next message
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
Well, as I told, I want to create the scope for the expression like <classReference> . <field> and as you can see, in the my grammar, I have default type. I.e. if the field name is the same with the type name I can skip the type.
Re: How to get the list of classes for scope [message #1803177 is a reply to message #1803152] Sun, 24 February 2019 19:49 Go to previous messageGo to next message
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
This is the fragment of the my ScopeProvider

case (container instanceof DotValue): {
				
				
				val DotValue dotValue = container as DotValue;
				
				
				if (dotValue.eContainer instanceof DotValue) {
					val parentDotValue= dotValue.eContainer as DotValue;
					val parentValue=parentDotValue.decl;
					
					
					switch(parentValue){
						case (parentValue instanceof ClassMemberDefinition):{
							val classMemberDefinition=parentValue as ClassMemberDefinition;
							if (classMemberDefinition.variableDeclarationRef!==null){
								val parentClassMemberDef=classMemberDefinition.variableDeclarationRef;
								val typeFor=parentClassMemberDef.typeFor
								if (typeFor instanceof RellClassType){
									val variableDeclarationList=(typeFor as RellClassType).rellClassDefinition.attributeListField.makeVariableDeclarationList
									return Scopes::scopeFor(variableDeclarationList)
								}
							}
							
						}
					}
					return variableDeclarationRefScope(variableDeclarationRef,
						dotValue.eContainer.notExressionContainer, ref);
				} else {
					return super.getScope(variableDeclarationRef, ref)

				}

......
def typeFor(VariableDeclarationRef variableDeclarationRef){
		if (variableDeclarationRef.name instanceof VariableDeclaration){
			val variableDeclaration=variableDeclarationRef.name as VariableDeclaration;
			
			val TypeReference typeReference=variableDeclaration.type
			if (typeReference!==null){
				return typeReference.typeForTypeRef				
			}
			
			
		
			return null
		}
	}



In the other hand see my variable declaration ref rule

VariableDeclarationRef:
	name=[VariableDeclaration|ValuableID];
....
VariableDeclaration:
	name=ValuableID ((DOUBLE_QUOTE type=TypeReference)?);




Sometimes I don't specify the variable type - just get it by the name(variable name is the same with the type name)
So, I want to get the EOblect by name and type of it. In what way I can to do it?

Regards,
Vladimir
Re: How to get the list of classes for scope [message #1803178 is a reply to message #1803177] Sun, 24 February 2019 20:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
option 1: avoid problem by using IDerivedStateComputer and SyntheticLinkingSupport to create the reference by naming convention if not there
https://www.eclipse.org/forums/index.php/m/1769803

option 2: using a filtering scope for your field references and filter by prefix

option 3: search the xtext index similar to here https://www.eclipse.org/forums/index.php/m/754503/?srch=xtext+getVisibleContainers#msg_754503 (+ local manual search)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get the list of classes for scope [message #1803212 is a reply to message #1803178] Mon, 25 February 2019 11:24 Go to previous messageGo to next message
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
May I ask to tell me something more about the second option?
Re: How to get the list of classes for scope [message #1803213 is a reply to message #1803212] Mon, 25 February 2019 11:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
in the scope provider you can stick delegateGetScope(the default impl) into a new FilteringScope.
the filtering scope then lets you filter the stuff.

=> instead of collection the fields manually you allow all and filter them by name.
am not sure if this works in your usecase


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get the list of classes for scope [message #1803215 is a reply to message #1803213] Mon, 25 February 2019 11:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
You might play w ith the import scope class as well

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get the list of classes for scope [message #1803234 is a reply to message #1803215] Mon, 25 February 2019 13:51 Go to previous messageGo to next message
kozhaev Vladimir is currently offline kozhaev VladimirFriend
Messages: 108
Registered: July 2009
Senior Member
Is it a good idea to move back by the EObject.eContainer until reach the root. Then go down to see what classes I have with this name? Then get the scope by fields of the class?
Re: How to get the list of classes for scope [message #1803235 is a reply to message #1803234] Mon, 25 February 2019 14:04 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
It depends

It won't work with multiple files
For local ones for sure


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:DSL Generate Documentation
Next Topic:Error saving an EMF model with Crossreferences in a new XtextResource
Goto Forum:
  


Current Time: Thu Apr 18 14:13:15 GMT 2024

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

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

Back to the top