Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Appending string AND XEpression to body of method
Appending string AND XEpression to body of method [message #1082449] Thu, 08 August 2013 15:22 Go to next message
Thomas Goossens is currently offline Thomas GoossensFriend
Messages: 32
Registered: August 2013
Member
I want to generate the body of a method.

val ofunctionMethod = function.toMethod(name, returntype) [
   
body = op.body //op comes from somewhere above and op.body is an XExpression

]


What I want to do is

generate a method with in its body:

1) some fixed code: "String example = 2"
2) op.body (XExpression)


I've looked at examples that create a CompilationStrategy

body =  [it.append("String example = 2")]  


But even when looking at the java files of this piece of code (eg: I know the code is expanded to Procedure1<ITreeIterator> etc>

I can't figure out how to append my XExpression to the body (if I first append the fixed code)


Any hints are appreciated


Update:
Currently I'm looking at NodeModelUtils to convert my XExpression to a string
and then append it.

Update:
Which is of course completely useless to my because it the code from the parse tree. (I need the compiled files (to java))


Update:
I am thinking of using XBaseCompiler to compile my XExpression to a string and then append it

Cheers

[Updated on: Thu, 08 August 2013 15:46]

Report message to a moderator

Re: Appending string AND XEpression to body of method [message #1082676 is a reply to message #1082449] Thu, 08 August 2013 22:49 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
The last one may work, inject a compiler for it then use that to generate the output.
@Inject XbaseCompiler xbc
...
  body =  [ append('''String example = 2''')
	    xbc.compile(op.body, it, element.newTypeRef('void'))
	   ] 


(or something like that)
Re: Appending string AND XEpression to body of method [message #1082890 is a reply to message #1082676] Fri, 09 August 2013 06:57 Go to previous messageGo to next message
Thomas Goossens is currently offline Thomas GoossensFriend
Messages: 32
Registered: August 2013
Member
Hi.

I was trying something like that yes:

@Inject XbaseCompiler _compiler
...
body = [it.append('int x = 1;\n') _compiler.compile(op.body,it, op.newTypeRef(OMatrix))]



But I get errors much like in this forum post:

http://www.eclipse.org/forums/index.php/t/489735/


java.lang.NullPointerException
	at org.eclipse.xtext.xbase.compiler.TypeReferenceSerializer.serialize(TypeReferenceSerializer.java:90)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.serialize(AbstractXbaseCompiler.java:342)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.serialize(AbstractXbaseCompiler.java:338)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.serialize(AbstractXbaseCompiler.java:334)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler.appendVariableTypeAndName(XbaseCompiler.java:676)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler._toJavaStatement(XbaseCompiler.java:656)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler.doInternalToJavaStatement(XbaseCompiler.java:454)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.internalToJavaStatement(AbstractXbaseCompiler.java:297)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler._toJavaStatement(XbaseCompiler.java:485)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler.doInternalToJavaStatement(XbaseCompiler.java:430)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.internalToJavaStatement(AbstractXbaseCompiler.java:297)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.compile(AbstractXbaseCompiler.java:236)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.compile(AbstractXbaseCompiler.java:107)
	at olang.jvmmodel.OlangJvmModelInferrer$5$1.apply(OlangJvmModelInferrer.java:251)
	at olang.jvmmodel.OlangJvmModelInferrer$5$1.apply(OlangJvmModelInferrer.java:1)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateExecutableBody(JvmModelGenerator.java:1101)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._generateMember(JvmModelGenerator.java:873)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateMember(JvmModelGenerator.java:1880)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator$2.apply(JvmModelGenerator.java:288)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator$2.apply(JvmModelGenerator.java:1)
	at org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_doubleArrow(ObjectExtensions.java:139)
	at org.eclipse.xtext.xbase.compiler.LoopExtensions$1.apply(LoopExtensions.java:39)
	at org.eclipse.xtext.xbase.lib.IterableExtensions.forEach(IterableExtensions.java:399)
	at org.eclipse.xtext.xbase.compiler.LoopExtensions.forEach(LoopExtensions.java:42)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._generateBody(JvmModelGenerator.java:292)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateBody(JvmModelGenerator.java:1852)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateType(JvmModelGenerator.java:211)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._internalDoGenerate(JvmModelGenerator.java:202)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.internalDoGenerate(JvmModelGenerator.java:1835)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.doGenerate(JvmModelGenerator.java:183)
	at org.eclipse.xtext.builder.BuilderParticipant.handleChangedContents(BuilderParticipant.java:299)
	at org.eclipse.xtext.builder.BuilderParticipant.build(BuilderParticipant.java:229)
	at org.eclipse.xtext.builder.impl.RegistryBuilderParticipant.build(RegistryBuilderParticipant.java:60)
	at org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:186)
	at org.eclipse.xtext.builder.impl.XtextBuilder.incrementalBuild(XtextBuilder.java:162)
	at org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:95)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
	at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
	at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
	at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

Re: Appending string AND XEpression to body of method [message #1082989 is a reply to message #1082890] Fri, 09 August 2013 09:59 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
My code example was from an older version with Xtext 2.3 where it did work but I dropped that functionality and then later migrated to 2.4 so I haven't tried it under this version. That post seems to show different behaviour with 2.4

On the other hand, I don't know what's in your XExpression or if the OMatrix type has errors. Try a simple expression of '1' and hard code typeref of 'int' (which works), and then gradually change it till it fails and post back again.
Re: Appending string AND XEpression to body of method [message #1084211 is a reply to message #1082676] Sun, 11 August 2013 05:53 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 09/08/2013 00:49, Ian McDevitt wrote:
> The last one may work, inject a compiler for it then use that to
> generate the output.
>
> @Inject XbaseCompiler xbc
> ..
> body = [ append('''String example = 2''')
> xbc.compile(op.body, it, element.newTypeRef('void'))
> ]
>
> (or something like that)

in my experience, calling the xbase compiler directly in the compilation
strategy does not seem to work anymore in 2.4 ... I managed to achieve
the same result using a custom xbase compiler
(http://www.eclipse.org/forums/index.php/t/499616/)

from what I understand calling the xbase compiler directly does not work
because the xbase expression would not be processed by the new type
computer (see http://www.eclipse.org/forums/index.php/t/502103/ where I
got no answer though).

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Re: Appending string AND XEpression to body of method [message #1084952 is a reply to message #1084211] Mon, 12 August 2013 08:53 Go to previous messageGo to next message
Thomas Goossens is currently offline Thomas GoossensFriend
Messages: 32
Registered: August 2013
Member
It works great! I implemented my own extension to XBaseCompiler as you said and generated. I must say that I don't quite understand yet *why* it works..


nevertheless thanks!
Re: Appending string AND XEpression to body of method [message #1085082 is a reply to message #1084952] Mon, 12 August 2013 12:30 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 12/08/2013 10:53, Thomas Goossens wrote:
> It works great! I implemented my own extension to XBaseCompiler as you
> said and generated. I must say that I don't quite understand yet *why*
> it works..
>
>
> nevertheless thanks!

glad to know it works :)

from what I understand, with the new type system of 2.4 (which is rather
cool! :), the typing of expressions is dealt with by the framework when
you give an Xbase expression a logical container. That is why you must
do something like

body = o.body

in the inferrer.

If you don't do that, the Xbase expressions will not be typed.

So if you need to customize the compilation of Xbase expressions you
must provide a custom XbaseCompiler, and not manually use the
XbaseCompiler in a compilation strategy...

At least, that's how I understand it :)

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Previous Topic:Can you manipulate the AST (ECore model) (XExpression) to add and edit code
Next Topic:Recursive import of references
Goto Forum:
  


Current Time: Thu Apr 25 01:59:28 GMT 2024

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

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

Back to the top