Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Specific Class type in xtext grammar
Specific Class type in xtext grammar [message #1734611] Thu, 09 June 2016 22:41 Go to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
I am experimenting with creating a small command based language. I am trying to specify the type of "command" class. For example:

Click SomeWebPage.LOGIN_BUTTON ;

both Click and LOGIN_BUTTON are of specific types. Click implements ICommand and LOGIN_BUTTON implements IElement.

I know I can get this in the validator, and proposal, but for implmenting an interpreter, I like to specify those types.

Currently, this is the relevant parts:

Action:
	ActionType config=ActionConfiguration ";";
ActionType:
	type=JvmTypeReference;
ActionConfiguration:
	{ActionConfiguration} component=(ActionComponent)? (data+=Datum)?;
ActionComponent:
	ele=[jvmTypes::JvmTypeReference];
Datum:
	content=Property | Value;
Property:
	QualifiedName;
Value:
	value=(Number | STRING);


Is there a documentation or tutorial with example, that shows how to do this ?

Thank you
Re: Specific Class type in xtext grammar [message #1734620 is a reply to message #1734611] Fri, 10 June 2016 03:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
Can you a be more specific what you mean.
It is not possible to do such things on grammar level.



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Specific Class type in xtext grammar [message #1734651 is a reply to message #1734620] Fri, 10 June 2016 07:58 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Thank you Christian.

ActionType needs to implement some interface ICommand or IAction.
I was trying to do this on the grammar level. Since it can not be done in the grammar I will have to do it in the validator. Any other option ?

Thank you
Re: Specific Class type in xtext grammar [message #1734652 is a reply to message #1734651] Fri, 10 June 2016 08:01 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
By the way what about reference type ?
For example :

ActionComponent:
comp= [jvmTypes::JvmAnyTypeReference] ;

Can I specify the type of parameter to the Action. For example comp is a reference that needs to implement other interface.
I guess the same thing applies here. Right ?


Re: Specific Class type in xtext grammar [message #1734653 is a reply to message #1734652] Fri, 10 June 2016 08:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
hi i am still not sure what this question targets

what do you want to reference? a class?

that is what myRef=JvmParameterizedTypeReference does.
as we have discussed in the other example you can adapt validation and content assist to restrict.

can you come up with example model files what you want to do?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Specific Class type in xtext grammar [message #1734661 is a reply to message #1734653] Fri, 10 June 2016 08:30 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Christian,
I guess you answered the question. Here's a quick example:


test testSearch using (some.xml ) {
Fill SomePage.SEARCH_TEXT : "How to use this" ;
Click SomePage.SEARCH_BUTTON ;
}

When this file is compiled or interpreted, I would like to get errors like :

-Fill does not implement org.somepackage.IAction
-SomePage.SEARCH_BUTTON is not an instance of IElement

This can be done in the validator as you said earlier, but I was wondering if the grammar allows this kind of restrictions.

Thank you
Re: Specific Class type in xtext grammar [message #1734669 is a reply to message #1734661] Fri, 10 June 2016 08:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
is SomePage.SEARCH_BUTTON
an xprexpression?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com

[Updated on: Fri, 10 June 2016 08:42]

Report message to a moderator

Re: Specific Class type in xtext grammar [message #1734673 is a reply to message #1734669] Fri, 10 June 2016 08:52 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
No it's not an expression. I am not sure what difference does it make.
It is a reference to other static object, or method. It has to be of a specific class type.

Re: Specific Class type in xtext grammar [message #1734675 is a reply to message #1734673] Fri, 10 June 2016 08:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
yes but where do SomePage and SEARCH_BUTTON come from

and how does the grammar concept for

SomePage.SEARCH_BUTTON

look like?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Specific Class type in xtext grammar [message #1734687 is a reply to message #1734675] Fri, 10 June 2016 10:36 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Christian,
This part is not yet done, as I am still in the prototype phase. I know I need to re-factor much of the grammar when done. SomePage is just a regular java class, and SEARCH_BUTTON is nothing but a static variable/method with specific return type. Let's assume the SEARCH_BUTTON type is IElement.

Here's the relevant part:
Action:
	ActionType config=ActionConfiguration ";";

ActionType:
	type=JvmTypeReference;

ActionConfiguration:
	{ActionConfiguration} component=(ActionComponent)? (":" data+=Datum)?;

ActionComponent:
	comp=[jvmTypes::JvmParameterizedTypeReference];
	


This is of course not working at the moment as the model file doesn't recognize or see SomePage.java even if it's in the same directory.
So I will need to find another way to do it.

Thank you

Re: Specific Class type in xtext grammar [message #1734691 is a reply to message #1734687] Fri, 10 June 2016 11:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
ActionComponent:
comp=[jvmTypes::JvmType|QualifiedName];

or

ref=vmParameterizedTypeReference


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com

[Updated on: Fri, 10 June 2016 11:17]

Report message to a moderator

Re: Specific Class type in xtext grammar [message #1734742 is a reply to message #1734691] Fri, 10 June 2016 22:41 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
I don't understand what you mean. Are you asking or suggesting ??
In all cases, I tried both, but none worked.
I am not sure if this is a typo :

ref=vmParameterizedTypeReference
instead of :
ref=JvmParameterizedTypeReference

For example, given this code:

test testSearch using (some.xml ) {
Click MyPage.element ;
Fill MyPage.text : "How to use this" ;
}

When using:

ActionComponent:
ref=JvmParameterizedTypeReference ;

OR

ActionComponent:
comp=[jvmTypes::JvmType|QualifiedName];

I get the same results. "MyPage.element" and "MyPage.text" can not be resolved to a type.
Of course it's there and MyPage.java is in the same package. Fields are public and static. Of course there's no content-assist.

I have been reading about the linking in xtext, however I don't think it's related to the grammar. If you have any advice, please let me know.

Thank you

Re: Specific Class type in xtext grammar [message #1734751 is a reply to message #1734742] Sat, 11 June 2016 06:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
Can you please post a complete grammar and sample Java classes.


Btw a jv,parameterized txpe references references the type only.

This is why I was asking if you use xbase expression to do the reference to the element


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Specific Class type in xtext grammar [message #1734752 is a reply to message #1734751] Sat, 11 June 2016 06:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
so what i miss in your grammar is somthing like

Model:
	
	(imports=XImportSection)?
	actions+=Action*;
	
Action:
	"action" type=[types::JvmDeclaredType] "." field=[types::JvmField]
;


class MyDslScopeProvider extends AbstractMyDslScopeProvider {
	
	override getScope(EObject context, EReference reference) {
		if (reference === MyDslPackage.Literals.ACTION__FIELD) {
			if (context instanceof Action) {
				val result = Scopes.scopeFor(context.type.members.filter(JvmField).filter[isStatic],[QualifiedName.create(simpleName)], IScope.NULLSCOPE) //TODO filter
				println(result)
				return result
			}
		}
		return super.getScope(context, reference)
	}

}


or

Model:
	
	(imports=XImportSection)?
	actions+=Action*;
	
Action:
	"action" target=XMemberFeatureCall
;


class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder
	def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		
 		acceptor.accept(element.toClass("my.company.greeting.MyTest")) [
 			var i = 0;
 			for (a : element.actions) {
 				members += a.toMethod("getActionTarget" + i, typeRef("a.b.c.IAction")) [
 					body = a.target
				]
				i = i + 1
			}
		]
	}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Specific Class type in xtext grammar [message #1734761 is a reply to message #1734752] Sat, 11 June 2016 12:32 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Coool.
My last attempt was:

Action:
	ActionType config=ActionConfiguration ";";

ActionType:
	type=JvmTypeReference;

ActionConfiguration:
	{ActionConfiguration} component=(ActionComponent)? ("::" data+=Datum)?;

ActionComponent:
	{XFeatureCall} comp = XFeatureCall ;


After checking http://download.eclipse.org/modeling/tmf/xtext/javadoc/2.9/?d I felt that XFeatureCall was what I am after.
It's a bit unclear when to use existing Rules and when to create my own. Using existing Rules, has the advantage of additional eclipse support out of the box.


However, I will try your suggestion and share the results and code.
Thank you
Re: Specific Class type in xtext grammar [message #1734762 is a reply to message #1734761] Sat, 11 June 2016 13:10 Go to previous message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
This is working.

ActionType:
	type=JvmTypeReference;

ActionConfiguration:
	{ActionConfiguration} component=(ActionComponent)? ("::" data+=Datum)?;

ActionComponent:
//	{XFeatureCall} comp = XFeatureCall ;
	type=[jvmTypes::JvmDeclaredType] "." field=[jvmTypes::JvmField];
	

and
	override getScope(EObject context, EReference reference) {
		if (reference === MyDslPackage.Literals.ACTION_COMPONENT__FIELD) {
			if (context instanceof ActionComponent) {
				val result = Scopes.scopeFor(context.type.members.filter(JvmField).filter[isStatic], [
					QualifiedName.create(simpleName)
				], IScope.NULLSCOPE) // TODO filter
				println(result)
				return result
			}
		}
		return super.getScope(context, reference)
	}


It's recognizing fields only and not methods, but this is great as I can extend it to accommodate method calls. At least it answered my original question, and gave me an idea about where the grammar ends. Knowing that I need to implement this in a different place other than the grammar is what I was missing.
I will try the other way with MyDslJvmModelInferrer and see. Smile

Thank you Christian.
Previous Topic:Parsing comma separated list
Next Topic:Error runnig first proyect
Goto Forum:
  


Current Time: Mon Dec 02 18:11:36 GMT 2024

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

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

Back to the top