Specific Class type in xtext grammar [message #1734611] |
Thu, 09 June 2016 22:41  |
Eclipse User |
|
|
|
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 #1734651 is a reply to message #1734620] |
Fri, 10 June 2016 07:58   |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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?
|
|
|
Re: Specific Class type in xtext grammar [message #1734661 is a reply to message #1734653] |
Fri, 10 June 2016 08:30   |
Eclipse User |
|
|
|
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 #1734687 is a reply to message #1734675] |
Fri, 10 June 2016 10:36   |
Eclipse User |
|
|
|
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 #1734742 is a reply to message #1734691] |
Fri, 10 June 2016 22:41   |
Eclipse User |
|
|
|
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 #1734752 is a reply to message #1734751] |
Sat, 11 June 2016 06:50   |
Eclipse User |
|
|
|
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
}
]
}
}
|
|
|
Re: Specific Class type in xtext grammar [message #1734761 is a reply to message #1734752] |
Sat, 11 June 2016 12:32   |
Eclipse User |
|
|
|
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  |
Eclipse User |
|
|
|
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. 
Thank you Christian.
|
|
|
Powered by
FUDForum. Page generated in 0.05428 seconds