Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext code generation using other JVM languages, e.g. Scala
Xtext code generation using other JVM languages, e.g. Scala [message #1091924] Thu, 22 August 2013 05:31 Go to next message
Shelby Moore III is currently offline Shelby Moore IIIFriend
Messages: 42
Registered: August 2013
Member
I want to use Scala for the code generation, because I am writing a fairly sophisticated compiler for a general purpose language (that builds on and outputs to Scala). For example, I want to use Scala's pattern matching on the AST to make the code more succinct. And eventually I will rewrite the compiler to use the language I am building for code generation.

How do I employ another JVM language for code generation?

The default settings apparently create a workflow which puts the source for code generation in a {ProjectName}Generator.xtend file in the org.xtext.{ProjectFileExt}.generator folder.

I have viewed the thread about using .java as the source language, but I didn't even understand what (if anything) was resolved in that thread.

I have seen the a mention of a mixed Scala and Xtext project, but that did not add any insight for me at least.

I have seen the thread about Xtext's Java integration and I don't understand it. I think this may have to do with the ability to embed Java in the grammar? Don't understand if or how this might apply to what I want to accomplish above.

The only other possible relevant pages I have found are:

voelterblog dot blogspot dot com/2009/09/using-scala-for-xtext-scope-definitions.html
www dot assembla dot com/wiki/show/scala-ide/Eclipse_Plugin_Development_using_the_Scala_IDE

I must say that I am lost in a vast sea of APIs that I don't think I could wrap my head around with a big picture understanding in any reasonable time frame.

Could someone offer me some pointers to shorten the learning curve? Or point me to the relevant documentation to read?

[Updated on: Thu, 22 August 2013 13:54]

Report message to a moderator

Re: Xtext code generation using other JVM languages, e.g. Scala [message #1092169 is a reply to message #1091924] Thu, 22 August 2013 12:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13884
Registered: July 2009
Senior Member
Hi,

i am not quite sure what your problem is
first you should note: there is the jvmmodelinferrer magic (and the generator that is based on that)
then there are normal generators that produce text.
i asume the second is fine for you.
if so have a look at "Writing a Code Generator With Xtend" in "15 Minutes Tutorial - Extended" from the docs.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext code generation using other JVM languages, e.g. Scala [message #1092181 is a reply to message #1092169] Thu, 22 August 2013 12:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13884
Registered: July 2009
Senior Member
P.S: if you ant to write the generator itself in scala,
simply delegate to your scala code from the doGenerate method ....


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext code generation using other JVM languages, e.g. Scala [message #1092198 is a reply to message #1092181] Thu, 22 August 2013 12:52 Go to previous messageGo to next message
Shelby Moore III is currently offline Shelby Moore IIIFriend
Messages: 42
Registered: August 2013
Member
Christian Dietrich wrote on Thu, 22 August 2013 08:28
P.S: if you ant to write the generator itself in scala,
simply delegate to your scala code from the doGenerate method ....


I mentioned in the other thread, how to do this?

Christian Dietrich wrote on Thu, 22 August 2013 08:13
Hi,

i am not quite sure what your problem is
first you should note: there is the jvmmodelinferrer magic (and the generator that is based on that)
then there are normal generators that produce text.
i asume the second is fine for you.
if so have a look at "Writing a Code Generator With Xtend" in "15 Minutes Tutorial - Extended" from the docs.


Yes the second is what I want. I already reviewed that tutorial and successfully generated output with doGenerate from either .xtend and .java source files.

My question is how to integrate it with Scala code. The first issue I am asking about now is how to do the delegation to a Scala method from doGenerate.

After that, I want to learn how to do post processing on the EMF model to output Scala extractors for all the generated Java classes, so I can use Scala's pattern matching in the code generation method I want to delegate to above.

Thank you.
Re: Xtext code generation using other JVM languages, e.g. Scala [message #1092204 is a reply to message #1092198] Thu, 22 August 2013 13:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13884
Registered: July 2009
Senior Member
Hmm

this sounds like:"how to call scala from java" and thus is not xtend related.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext code generation using other JVM languages, e.g. Scala [message #1092230 is a reply to message #1092204] Thu, 22 August 2013 13:51 Go to previous messageGo to next message
Shelby Moore III is currently offline Shelby Moore IIIFriend
Messages: 42
Registered: August 2013
Member
Perhaps that is where I have arrived, yet one issue in my mind is how to set the scala-library.jar[1] and the .jar for the Scala containing the delegate method on the classpath searched by the compilation of {ProjectName}Generator.java? Perhaps that is not Xtext related, rather a general Eclipse project configuration issue?

Later I will be concerned about how this all gets exported to various scenarios for using the editor (and do the Scala dependencies get pulled in correctly), yet for now I will just concern myself with the above.

P.S. This is my first time to use Eclipse, Xtext, and Java, although my proficiency with programming languages in general helps. As well my first time to code in Scala in a build environment (before only SimplyScala.com). So I may be conflating issues.

[1] lampwww dot epfl dot ch/~michelou/scala/using-scala-from-java.html
Re: Xtext code generation using other JVM languages, e.g. Scala [message #1092236 is a reply to message #1092230] Thu, 22 August 2013 13:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13884
Registered: July 2009
Senior Member
Hi,

it would be preferred if the exist as osgi bundles. if not

- create a lib folder in the project
- drop the libs there.
- open the manifest file.
- add the libs on the runtime tab/classpath section
- add the lib folder on the build tab of the editor.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext code generation using other JVM languages, e.g. Scala [message #1093701 is a reply to message #1092236] Sat, 24 August 2013 14:07 Go to previous messageGo to next message
Shelby Moore III is currently offline Shelby Moore IIIFriend
Messages: 42
Registered: August 2013
Member
There was no need for importing .jar files.

Xtend can't resolve the type. This should probably be filed as a bug report.

Quote:
package org.xtext.copu.generator


import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess

import org.xtext.copu.copute.File
import org.xtext.copu.generator.CoputeToScala

class CoputeGenerator implements IGenerator {

override void doGenerate(Resource r, IFileSystemAccess fsa) {
if (r.getContents.size == 1 && r.getContents.get(0) instanceof File)
fsa.generateFile("test.scala", CoputeToScala.apply((File)r.getContents.get(0)))
}
}


Yet Java can.

package org.xtext.copu.generator;


import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.generator.IFileSystemAccess;

import org.xtext.copu.copute.File;
import org.xtext.copu.generator.CoputeToScala;

class CoputeGenerator implements IGenerator {
   
   @Override
   public void doGenerate(Resource r, IFileSystemAccess fsa) {
      if (r.getContents().size() == 1 && r.getContents().get(0) instanceof File)
        fsa.generateFile("test.scala", CoputeToScala.apply((File)r.getContents().get(0)));
   }
}


Here is the Scala source.

package org.xtext.copu.generator

import org.xtext.copu.copute.File

object CoputeToScala {
   def apply(file: File): String = ""
}


-----------------------------------------------------

I ended up as follows using CoputeGenerator.xtend to call Java which calls my Scala code above.

package org.xtext.copu.generator


import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess

import org.xtext.copu.copute.File
import org.xtext.copu.generator.CoputeToScalaJ

class CoputeGenerator implements IGenerator {
	
	override void doGenerate(Resource r, IFileSystemAccess fsa) {
	   if (r.getContents.size == 1 && r.getContents.get(0) instanceof File)
	     fsa.generateFile("test.scala", CoputeToScalaJ.apply(r.getContents.get(0)))
	}
}



package org.xtext.copu.generator;

import org.xtext.copu.copute.File;
import org.xtext.copu.generator.CoputeToScala;

class CoputeToScalaJ {
   public static java.lang.String apply(java.lang.Object file) {
      return CoputeToScala.apply((File)file);
   }
}
Re: Xtext code generation using other JVM languages, e.g. Scala [message #1744850 is a reply to message #1092230] Fri, 30 September 2016 18:06 Go to previous message
Andrzej Wasowski is currently offline Andrzej WasowskiFriend
Messages: 7
Registered: June 2015
Junior Member
I know this is an old thread, but I am also interested in this issue. So let me try.

Quote:
After that, I want to learn how to do post processing on the EMF model to output Scala extractors for all the generated Java classes, so I can use Scala's pattern matching in the code generation method I want to delegate to above.


OP, have you ever got to that?
Previous Topic:how to escape #comments in freecode
Next Topic:Xtext lexer ambiguity
Goto Forum:
  


Current Time: Sat Apr 17 09:42:54 GMT 2021

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

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

Back to the top