Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Help with xtext grammar void method(Create a grammar to void method)
Help with xtext grammar void method [message #813255] Mon, 05 March 2012 03:02 Go to next message
Leonardo Torres is currently offline Leonardo TorresFriend
Messages: 16
Registered: March 2012
Junior Member
Hi guys,

If possible, I would like of an example or some tutorial of the grammar that define a void method.


Thanks in advance!
Re: Help with xtext grammar void method [message #813626 is a reply to message #813255] Mon, 05 March 2012 14:17 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Sorry, but I don't understand your question. What do you mean by "Xtext
void method" ?

- henrik

On 2012-05-03 4:02, Leonardo Torres wrote:
> Hi guys,
> If possible, I would like of an example or some tutorial of the grammar
> that define a void method.
>
>
> Thanks in advance!
Re: Help with xtext grammar void method [message #813711 is a reply to message #813626] Mon, 05 March 2012 16:28 Go to previous messageGo to next message
Leonardo Torres is currently offline Leonardo TorresFriend
Messages: 16
Registered: March 2012
Junior Member
I´m sorry. Let me explain

I'm creating a DSL for Oriented Objetct teach purposes, so I can create rule for entities (class) properties (attibute) and method with return. However, I don´t know to create a rule for method without return. Thus, I would like of an exemple of the rule for a method without return.

Thank for help-me

Re: Help with xtext grammar void method [message #813726 is a reply to message #813711] Mon, 05 March 2012 16:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13425
Registered: July 2009
Senior Member
Hat about simply makeing the return type optional in the grammar

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Help with xtext grammar void method [message #813791 is a reply to message #813726] Mon, 05 March 2012 18:07 Go to previous messageGo to next message
Leonardo Torres is currently offline Leonardo TorresFriend
Messages: 16
Registered: March 2012
Junior Member
Thanks for answer.

I did that :
Method:	
	 'operacao' name= ValidID '('(param+=FullJvmFormalParameter (','
	   param+=FullJvmFormalParameter)*)?') retorna' resultType = ResultType
	       body=XBlockExpression	    
;
ResultType :	
	NoResult | Result
;

NoResult:
	'nada'
;

Result :	 
	type=JvmTypeReference 
;

It is works! In the Editor:

operacao test() retorna nada{	        
        var int nome = 2;
    	return 2;		
}


Where can I validate the semantic ? Because in this case above, It´s wrong. The word 'nada' means no return, but I can still write the statement return.
Re: Help with xtext grammar void method [message #813823 is a reply to message #813791] Mon, 05 March 2012 18:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13425
Registered: July 2009
Senior Member
Hi,

it would have been very helpful if youd mentione using Xbase before.
what you are askin for is about xbases type system.
if you translate your stuff correctly in the jvmmodelinferrer it will work out of the box.
so simply infer void as return type

NoResult:
	{NoResult}'nada'
;


package org.eclipse.xtext.example.domainmodel.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.common.types.JvmDeclaredType
import org.eclipse.xtext.example.domainmodel.domainmodel.Entity
import org.eclipse.xtext.example.domainmodel.domainmodel.Operation
import org.eclipse.xtext.example.domainmodel.domainmodel.Property
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.util.IAcceptor
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.eclipse.xtext.example.domainmodel.domainmodel.Result

class DomainmodelJvmModelInferrer extends AbstractModelInferrer {
	
	@Inject extension JvmTypesBuilder
	@Inject extension IQualifiedNameProvider

	def dispatch infer(Entity e, IAcceptor<JvmDeclaredType> acceptor, boolean prelinkingPhase) {
		acceptor.accept(
			e.toClass( e.fullyQualifiedName ) [
				documentation = e.documentation
				if (e.superType != null)
					superTypes += e.superType.cloneWithProxies
					
				for ( f : e.features ) {
					switch f {
				
						Property : {
							members += f.toField(f.name, f.type)
							members += f.toGetter(f.name, f.type)
							members += f.toSetter(f.name, f.type)
						}
				
						Operation : {
							members += f.toMethod(f.name, if (f.result instanceof Result) (f.result as Result).type else f.newTypeRef("java.lang.void")) [
								documentation = f.documentation
								for (p : f.params) {
									parameters += p.toParameter(p.name, p.parameterType)
								}
								body = f.body
							]
						}
					}
				}
			]
		)
	}
	
}


this will work in the editor
but will still lead to a compile error in the generated java code (if the ops body is empty)
so a bug for this will be welcome

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 05 March 2012 18:56]

Report message to a moderator

Re: Help with xtext grammar void method [message #813845 is a reply to message #813823] Mon, 05 March 2012 19:30 Go to previous messageGo to next message
Leonardo Torres is currently offline Leonardo TorresFriend
Messages: 16
Registered: March 2012
Junior Member
Thanks for all answer.
Re: Help with xtext grammar void method [message #813904 is a reply to message #813845] Mon, 05 March 2012 20:51 Go to previous messageGo to next message
Leonardo Torres is currently offline Leonardo TorresFriend
Messages: 16
Registered: March 2012
Junior Member
Just one more question. Are there any indication about xtext/xbase/xtend book´s ?
Re: Help with xtext grammar void method [message #813909 is a reply to message #813823] Mon, 05 March 2012 20:51 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3107
Registered: July 2009
Senior Member
Hi Christian,

f.newTypeRef("void")
instead of
f.newTypeRef("java.lang.void")

should do the trick.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


Am 05.03.12 19:56, schrieb Christian Dietrich:
> Hi,
>
> it would have been very helpful if youd mentione using Xbase before.
> what you are askin for is about xbases type system.
> if you translate your stuff correctly in the jvmmodelinferrer it will
> work out of the box.
> so simply infer void as return type
>
> NoResult:
> {NoResult}'nada'
> ;
>
> package org.eclipse.xtext.example.domainmodel.jvmmodel
>
> import com.google.inject.Inject
> import org.eclipse.xtext.common.types.JvmDeclaredType
> import org.eclipse.xtext.example.domainmodel.domainmodel.Entity
> import org.eclipse.xtext.example.domainmodel.domainmodel.Operation
> import org.eclipse.xtext.example.domainmodel.domainmodel.Property
> import org.eclipse.xtext.naming.IQualifiedNameProvider
> import org.eclipse.xtext.util.IAcceptor
> import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
> import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
> import org.eclipse.xtext.example.domainmodel.domainmodel.Result
>
> class DomainmodelJvmModelInferrer extends AbstractModelInferrer {
>
> @Inject extension JvmTypesBuilder
> @Inject extension IQualifiedNameProvider
>
> def dispatch infer(Entity e, IAcceptor<JvmDeclaredType> acceptor,
> boolean prelinkingPhase) {
> acceptor.accept(
> e.toClass( e.fullyQualifiedName ) [
> documentation = e.documentation
> if (e.superType != null)
> superTypes += e.superType.cloneWithProxies
>
> for ( f : e.features ) {
> switch f {
>
> Property : {
> members += f.toField(f.name, f.type)
> members += f.toGetter(f.name, f.type)
> members += f.toSetter(f.name, f.type)
> }
>
> Operation : {
> members += f.toMethod(f.name, if (f.result instanceof Result) (f.result
> as Result).type else f.newTypeRef("java.lang.void")) [
> documentation = f.documentation
> for (p : f.params) {
> parameters += p.toParameter(p.name, p.parameterType)
> }
> body = f.body
> ]
> }
> }
> }
> ]
> )
> }
>
> }
>
> this will work in the editor
> but will still lead to a compile error in the generated java code
> so a bug for this will be welcome
>
> ~Christian
Re: Help with xtext grammar void method [message #813916 is a reply to message #813909] Mon, 05 March 2012 21:06 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13425
Registered: July 2009
Senior Member
Hi Sebastian,

yes it does

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:linking cross-references manually
Next Topic:Scoping question, references to sub-elements of auto-imported elements
Goto Forum:
  


Current Time: Tue Apr 07 11:43:25 GMT 2020

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

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

Back to the top