Skip to main content


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 15:05 Go to next message
Davide G is currently offline Davide GFriend
Messages: 36
Registered: October 2013
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 07:08]

Report message to a moderator

Re: [Xtext/Xpand] Code generation [message #1129434 is a reply to message #1129368] Tue, 08 October 2013 16:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14738
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: [Xtext/Xpand] Code generation [message #1130023 is a reply to message #1129368] Wed, 09 October 2013 06:50 Go to previous messageGo to next message
Davide G is currently offline Davide GFriend
Messages: 36
Registered: October 2013
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 07:07 Go to previous messageGo to next message
Davide G is currently offline Davide GFriend
Messages: 36
Registered: October 2013
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 07:49 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14738
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Previous Topic:JET2 setVariable
Next Topic:JET2 Exception with existing files
Goto Forum:
  


Current Time: Sat Dec 14 20:54:27 GMT 2024

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

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

Back to the top