Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Best way to extend Xtend?
Best way to extend Xtend? [message #812958] Sun, 04 March 2012 16:19 Go to next message
Lionel Villard is currently offline Lionel Villard
Messages: 11
Registered: January 2012
Junior Member
Hi,

I'm trying to extend Xtend (!) with a new XPrimaryExpression. My first attempt was to create a new XText grammar like this:

grammar com.ibm.mwc.xtext.Coava with org.eclipse.xtext.xtend2.Xtend2

and then to override the XPrimaryExpression defined in XBase, something like this:

XPrimaryExpression returns XExpression:
XConstructorCall |
XBlockExpression |
XSwitchExpression |
XFeatureCall |
XLiteral |
XIfExpression |
XForLoopExpression |
XWhileExpression |
XDoWhileExpression |
XThrowExpression |
XReturnExpression |
XTryCatchFinallyExpression |
XParenthesizedExpression |
XMyNewExpression;

The main reason it does not work is XExpression is sealed.

So my question is what is the recommended way to create a grammar based on XTend2?

Thanks
Lionel
Re: Best way to extend Xtend? [message #813019 is a reply to message #812958] Sun, 04 March 2012 18:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6302
Registered: July 2009
Senior Member
Hi,


how does your XMyNewExpression look like?
it should be something like

XMyNewExpression returns xbase::XExpression:
{XMyNewExpression } .....
Re: Best way to extend Xtend? [message #813036 is a reply to message #813019] Sun, 04 March 2012 19:00 Go to previous messageGo to next message
Lionel Villard is currently offline Lionel Villard
Messages: 11
Registered: January 2012
Junior Member
I tried that but nope, I still get this error:

Cannot add supertype 'XExpression' to sealed type 'XExpression'


Re: Best way to extend Xtend? [message #813051 is a reply to message #813036] Sun, 04 March 2012 19:31 Go to previous messageGo to next message
Sebastian Benz is currently offline Sebastian Benz
Messages: 6
Registered: March 2011
Junior Member

Did you try to generate your language from your grammar (ignoring the validation error? I get the same validation error, but the generation works.

Sebastian
Re: Best way to extend Xtend? [message #813116 is a reply to message #813051] Sun, 04 March 2012 21:34 Go to previous messageGo to next message
Lionel Villard is currently offline Lionel Villard
Messages: 11
Registered: January 2012
Junior Member
Thanks for the tip but nope it does not work:

1465 [main] ERROR ipse.xtext.generator.LanguageConfig - [TransformationDiagnostic: null:5 Cannot add supertype 'XExpression' to sealed type 'XExpression'. (ErrorCode: CannotCreateTypeInSealedMetamodel)]

Lionel
Re: Best way to extend Xtend? [message #813151 is a reply to message #813116] Sun, 04 March 2012 22:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6302
Registered: July 2009
Senior Member
Can you please post your grammar using the xbase import?
Re: Best way to extend Xtend? [message #813384 is a reply to message #812958] Mon, 05 March 2012 07:37 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan Koehnlein
Messages: 656
Registered: July 2009
Senior Member
Xtend itself is not really made for being extended. You might have to
copy a lot of the infrastructure and know exactly what you're doing.
Furthermore, there is not much syntactic space left to add your own
expressions without collisions with existing concepts.

My assumption is you forgot to add your own Ecore model - generated or
imported - in which your XNewExpression goes.

Am 04.03.12 17:19, schrieb Lionel Villard:
> Hi,
>
> I'm trying to extend Xtend (!) with a new XPrimaryExpression. My first
> attempt was to create a new XText grammar like this:
>
> grammar com.ibm.mwc.xtext.Coava with org.eclipse.xtext.xtend2.Xtend2
> and then to override the XPrimaryExpression defined in XBase, something
> like this:
>
> XPrimaryExpression returns XExpression:
> XConstructorCall |
> XBlockExpression |
> XSwitchExpression |
> XFeatureCall |
> XLiteral |
> XIfExpression |
> XForLoopExpression |
> XWhileExpression |
> XDoWhileExpression |
> XThrowExpression |
> XReturnExpression |
> XTryCatchFinallyExpression |
> XParenthesizedExpression |
> XMyNewExpression;
>
> The main reason it does not work is XExpression is sealed.
>
> So my question is what is the recommended way to create a grammar based
> on XTend2?
>
> Thanks
> Lionel


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Best way to extend Xtend? [message #813518 is a reply to message #813384] Mon, 05 March 2012 11:15 Go to previous messageGo to next message
Sebastian Benz is currently offline Sebastian Benz
Messages: 6
Registered: March 2011
Junior Member

Hi!

Extending Xtend works actually quite well (but Jan is right about the limited syntactic possibilites). Here is my grammar:

grammar org.jnario.spec.Spec with org.eclipse.xtend.core.Xtend

import "http  www jnario org /jnario/spec/Spec" 
import "http  www eclipse org /xtend" as xtend
import "http  www eclipse org /xtext/xbase/Xbase" as xbase
import "http  www eclipse org /xtext/common/JavaVMTypes" as types
import "http  www jnario org /Jnario"

...

XPrimaryExpression returns xbase::XExpression:
	XConstructorCall |
	XBlockExpression |
	XSwitchExpression |
	XFeatureCall |
	XLiteral |
	XIfExpression |
	XForLoopExpression |
	XWhileExpression |
	XDoWhileExpression |
	XThrowExpression |
	XReturnExpression |
	XTryCatchFinallyExpression |
	XParenthesizedExpression |
	Assertion |
	Matcher;



I have defined my metamodel in a separate ecore file. Further things you need to do:

- Import org.eclipse.xtend.core & org.eclipse.xtext.xbase as projects with source folders into your workspace. This is necessary to make the EcoreGenerator find the generated custom classes.
- Patch the EcoreGenerator (custom classes in Xtend use a different naming scheme):

// GenerateSpec.mwe2
component = org.jnario.PatchedEcoreGenerator {
		genModel = "platform:/resource/${projectName}/model/Spec.genmodel"
		srcPath ="platform:/resource/${projectName}/src"
		srcPath ="platform:/resource/org.eclipse.xtext.common.types/src"
		srcPath ="platform:/resource/org.eclipse.xtext.xbase/src"
		srcPath ="platform:/resource/org.eclipse.xtend.core/src"
		srcPath ="platform:/resource/org.jnario/src"
	}

and the patched EcoreGenerator:
package org.jnario;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.mwe2.ecore.EcoreGenerator;

import com.google.common.base.Function;

public class PatchedEcoreGenerator extends EcoreGenerator {
	

	private boolean customClasses;

	protected final class PatchedMapper implements Function<String, String> {
		public String apply(String from) {
			if (from.startsWith("org.eclipse.emf.ecore"))
				return null;
			for(String srcPath: srcPaths) {
				URI createURI = URI.createURI(srcPath+"/"+from.replace('.', '/')+"Custom.java");
				String customClassName = from+"Custom";
				if (URIConverter.INSTANCE.exists(createURI, null)) {
					return customClassName;
				}
				createURI = URI.createURI(srcPath+"/"+from.replace('.', '/')+"ImplCustom.java");
				if (URIConverter.INSTANCE.exists(createURI, null)) {
					return customClassName;
				}
				if (from.endsWith("Impl") && customClasses) {
					generate(from,customClassName,createURI);
					return customClassName;
				}
			}
			return null;
		}
	}
	
	@Override
	public void setGenerateCustomClasses(boolean generateCustomClasses) {
		customClasses = generateCustomClasses;
		super.setGenerateCustomClasses(generateCustomClasses);
	}

}


This works for me so far. It is actually not necessary to copy a lot infrastructure. The only thing you need to do is to copy the runtime and ui module from xtend.

Hope that helps.

Sebastian

P.S. I had to change the imported package URIs as I am not allowed to post any links in messages...
Re: Best way to extend Xtend? [message #813669 is a reply to message #813518] Mon, 05 March 2012 15:30 Go to previous messageGo to next message
Lionel Villard is currently offline Lionel Villard
Messages: 11
Registered: January 2012
Junior Member
This is very useful. I ended up just adding these lines in the .mw2 file:

registerGeneratedEPackage = "org.eclipse.xtext.xtend2.xtend2.Xtend2Package"
registerGenModelFile = "platform:/resource/org.eclipse.xtext.xtend2/model/Xtend2.genmodel"

and it works.

However the problem now is parser in the UI package does not compile. The generated GrammarAccess contains nested classes for the overloaded grammar rules, but the generated parser in the UI plugin accesses the grammar as a flat set of classes.

Any ideas what could be wrong?

Lionel
Re: Best way to extend Xtend? [message #813694 is a reply to message #813669] Mon, 05 March 2012 16:06 Go to previous messageGo to next message
Lionel Villard is currently offline Lionel Villard
Messages: 11
Registered: January 2012
Junior Member
I think I solved it: my new grammar rule was wrong (forgot xbase:: prefix in my new rule):

XNew returns xbase::XExpression:

Re: Best way to extend Xtend? [message #819131 is a reply to message #812958] Mon, 12 March 2012 14:34 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo Bettini
Messages: 1355
Registered: July 2009
Senior Member
Hi

this works for me (note the import and the prefix xbase::)

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase

....

XLiteral returns xbase::XExpression:
EnvironmentAccess |
XClosure |
XBooleanLiteral |
XIntLiteral |
XNullLiteral |
XStringLiteral |
XTypeLiteral
;

On 03/04/2012 05:19 PM, Lionel Villard wrote:
> Hi,
>
> I'm trying to extend Xtend (!) with a new XPrimaryExpression. My first
> attempt was to create a new XText grammar like this:
>
> grammar com.ibm.mwc.xtext.Coava with org.eclipse.xtext.xtend2.Xtend2
> and then to override the XPrimaryExpression defined in XBase, something
> like this:
>
> XPrimaryExpression returns XExpression:
> XConstructorCall |
> XBlockExpression |
> XSwitchExpression |
> XFeatureCall |
> XLiteral |
> XIfExpression |
> XForLoopExpression |
> XWhileExpression |
> XDoWhileExpression |
> XThrowExpression |
> XReturnExpression |
> XTryCatchFinallyExpression |
> XParenthesizedExpression |
> XMyNewExpression;
>
> The main reason it does not work is XExpression is sealed.
>
> So my question is what is the recommended way to create a grammar based
> on XTend2?
>
> Thanks
> Lionel


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Re: Best way to extend Xtend? [message #976730 is a reply to message #819131] Thu, 08 November 2012 20:14 Go to previous messageGo to next message
Iuzuz Denker is currently offline Iuzuz Denker
Messages: 2
Registered: November 2012
Junior Member
Hello Xtend-frieds,
I also want to extend Xtend and try out some ideas. But some things has been changed since last message (at least the URLs) and the guidance seems not to work any more. I must say I am an Xtext beginner.

Steps, I tried:


  • Installed a new Eclipse with Xtext
  • Created a new Xtext project (=4 projects)
  • Changed the with-clause to "with org.eclipse.xtend.core.Xtend" -> can't resolve the grammar
  • Added to the project org.xtext.example.mydsl a manifest dependence to ...xtend.core -> the error disappear
  • added to the .mwe2 file:
    registerGeneratedEPackage = "org.eclipse.xtend.core.xtend.XtendPackage"
    registerGenModelFile = "platform:/resource/org.eclipse.xtend.core/model/Xtend.genmodel"
    

  • "Generate Xtext Artifacts" (on the .xtext file) -> Successful (with the download)


After the (successful) generating there is a compile error in th ..ui project:
Quote:
The project was not built since its build path is incomplete. Cannot find the class file for
org.eclipse.xtend.core.services.XtendGrammarAccess$ImportElements. Fix the build path then try building this project



  • Added to the project org.xtext.example.mydsl.ui a manifest dependences to all ..xtend.. and ..xbase.. (without .source)


I get 6 erroros, the base seems to be, the package
org.eclipse.xtend.core.ui and the class are not in the classpath
org.eclipse.xtend.core.ui.contentassist.XtendProposalProvider

But if I ctrl-click on it, it is found in one of the dependences.

Have any one a suggestion?
What do I make wrong?

Thanks, Iuzuz

[Updated on: Thu, 08 November 2012 21:19]

Report message to a moderator

Re: Best way to extend Xtend? [message #976805 is a reply to message #976730] Thu, 08 November 2012 21:37 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2832
Registered: July 2009
Senior Member
Hi Iuzuz,

it might be that the generated package is wrong. The
XtendProposalProvider is in xtend.ide.contentassist and not in core.ui..
I'm afraid you'll have to adapt that manually

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 08.11.12 21:28, schrieb Iuzuz Denker:
> Hello Xtend-frieds,
> I also want to extend Xtend and try out some ideas. But some things has
> been changed since last message (at least the URLs) and the guidance
> seems not to work any more. I must say I am an Xtext beginner.
>
> Steps, I tried:
>
>
> Installed a new Eclipse with Xtext
> Created a new Xtext project (=4 projects)
> Changed the with-clause to "with org.eclipse.xtend.core.Xtend" -> can't
> resolve the grammar
> Added to the project org.xtext.example.mydsl a manifest dependence to
> ...xtend.core -> the error disappear
> added to the .mwe2 file:
> registerGeneratedEPackage = "org.eclipse.xtend.core.xtend.XtendPackage"
> registerGenModelFile =
> "platform:/resource/org.eclipse.xtend.core/model/Xtend.genmodel"
>
> "Generate Xtext Artifacts" (on the .xtext file) -> Successful (with the
> download)
>
>
> After the (successful) generating there is a compile error in th ..ui
> project:
> Quote:
>> The project was not built since its build path is incomplete. Cannot
>> find the class file for
>> org.eclipse.xtend.core.services.XtendGrammarAccess$ImportElements. Fix
>> the build path then try building this project
>
>
>
> Added to the project org.xtext.example.mydsl a manifest dependences to
> all ..xtend.. and ..xbase.. (without .source)
>
> I get 6 erroros, the base seems to be, the package
> org.eclipse.xtend.core.ui and the class are not in the classpath
> org.eclipse.xtend.core.ui.contentassist.XtendProposalProvider
>
> But if I ctrl-click on it, it is found in one of the dependences.
>
> Have any one a suggestion?
> What do I make wrong?
>
> Thanks, Iuzuz
Re: Best way to extend Xtend? [message #977872 is a reply to message #976805] Fri, 09 November 2012 16:53 Go to previous message
Iuzuz Denker is currently offline Iuzuz Denker
Messages: 2
Registered: November 2012
Junior Member
Thank you Sebastian,
it helps.

But after generating artifacts again, the problem is again there.
As a workaround I created a class in ...ui/src:
package org.eclipse.xtend.core.ui.contentassist;
public class XtendProposalProvider 
  extends org.eclipse.xtend.ide.contentassist.XtendProposalProvider {}


Thank you again.
Previous Topic:How to access annotations in JvmAnnotaionReference?
Next Topic:Quick fix with linked-mode editing?
Goto Forum:
  


Current Time: Thu Sep 18 19:48:34 GMT 2014

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

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