Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [SOLVED, Xtext/Xpand] Code generation
[SOLVED, Xtext/Xpand] Code generation [message #1129368] Tue, 08 October 2013 11:05 Go to next message
Davide G is currently offline Davide G
Messages: 28
Registered: October 2013
Junior Member
Hi, i'm trying to build a very simple code generator. This is the xtext grammar:

grammar org.xtext.example.mydsl.Entity with org.eclipse.xtext.common.Terminals

generate entity "..."

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID '!';


and in the generator xtend file i have this code:

class EntityGenerator implements IGenerator 
{
	override void doGenerate(Resource resource, IFileSystemAccess fsa) 
	{
		
(error 1)	for(g : resource.allContents.toIterable.filter(Greeting))
		{
(error 2)		fsa.generateFile("documento.txt",g.compile)
			
		}
	}
	
	
	def compile(Greeting g) '''
		<Greeting>
			«g.name»
		<\Greeting>
	'''
}


I get these two errors:

(1) The method or field Greeting is undefined for the type EntityGenerator
(2) Type mismatch: cannot convert from EObject to Greeting

A solution to this problem?
Bye!

[Updated on: Wed, 09 October 2013 03:08]

Report message to a moderator

Re: [Xtext/Xpand] Code generation [message #1129434 is a reply to message #1129368] Tue, 08 October 2013 12:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6125
Registered: July 2009
Senior Member
Did you add an import for greeting?

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: [Xtext/Xpand] Code generation [message #1130023 is a reply to message #1129368] Wed, 09 October 2013 02:50 Go to previous messageGo to next message
Davide G is currently offline Davide G
Messages: 28
Registered: October 2013
Junior Member
yes, these are my imports:

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.xtext.example.mydsl.entity.Greeting

Re: [Xtext/Xpand] Code generation [message #1130040 is a reply to message #1130023] Wed, 09 October 2013 03:07 Go to previous messageGo to next message
Davide G is currently offline Davide G
Messages: 28
Registered: October 2013
Junior Member
Ok i solved. This is the problem: i followed the tutorial on the xtext website, which is not update. The snippet with errors in the website is

override void doGenerate(Resource resource, IFileSystemAccess fsa) {
  for(e: resource.allContents.toIterable.filter(Entity)) {
    fsa.generateFile(
      e.fullyQualifiedName.toString("/") + ".java",
      e.compile)
  }
}


but in the eclipse help, the snippet is

override void doGenerate(Resource resource, IFileSystemAccess fsa) {
  for(e: resource.allContents.toIterable.filter(typeof(Entity))) {
    fsa.generateFile(
      e.fullyQualifiedName.toString("/") + ".java",
      e.compile)
  }
} 


the change is inside the for loop. There was a missing typeof(...).
Re: [Xtext/Xpand] Code generation [message #1130068 is a reply to message #1130040] Wed, 09 October 2013 03:49 Go to previous message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6125
Registered: July 2009
Senior Member
Hmmm looks like you use an outdated Xtext.
The typeof is needed for version < 2.4.2

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Previous Topic:JET2 setVariable
Next Topic:JET2 Exception with existing files
Goto Forum:
  


Current Time: Tue Jul 22 13:27:48 EDT 2014

Powered by FUDForum. Page generated in 0.02389 seconds