Home » Modeling » TMF (Xtext) » importing native java source code(how does xtend do it)
| | |
Re: importing native java source code [message #985738 is a reply to message #984329] |
Thu, 15 November 2012 21:42 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
Hmmm, I don't get what you want.
In Xbase expressions, your DSL methods are callable if you have inferred
a JvmOperation from them (JvmModelInferrer). This is not so much
connected to imports as methods are usually called on objects, and
imports allow to refer to types with unqualified names. An exception to
this rule are static methods and extension methods.
Maybe you could give an example what you're trying to achieve?
Am 14.11.12 16:56, schrieb Martin Baker:
> *Author:* Jan
>> Kohnlein *Date:* Wed, 14 November 2012 07:23
>> Xtend is a general purpose language with a lot of additional stuff.
>> You're likely to get lost when copying from there. What you want is to
>> reuse the Java part of it. You can do this by extending Xbase. I'd
>> suggest to have a look at the 7 Languages documentation:
>>
>> http://www.eclipse.org/Xtext/7languages.html
>>
>> To answer your question nevertheless: Xtend uses an imported Ecore model
>> (i.e. not derived from the grammar). So the operations have been added
>> manually.
>
> Thanks very much for your reply, yes I did look at the 7 Languages. Most
> of them use the magic 'importedNamespace' name like this:
> 'import' importedNamespace=QualifiedNameWithWildcard;
>
> If I understand this correctly this will let me use other files in my
> language, java library code, and it will see my own native
> classes/interfaces but not their internal structure. That is, It won't
> allow me to call methods on my own native java code.
>
> The only exception is httprouting which uses:
> 'import' importedType=[ types::JvmType | QualifiedName];
> plus a custom ImportedNamespaceScopeProvider
> I assume this will let me call methods on my own native java code?
>
> Since I want to do all of these then I guess I need to combine both
> types of import.
>
> I will experiment with this, it does seem quite messy at the moment, the
> documentation hints that there are some improvements coming to this
> aspect of xbase so I will look forward to any improvements.
>
> Thanks,
>
> Martin
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
Re: importing native java source code [message #985822 is a reply to message #985738] |
Fri, 16 November 2012 09:47 |
Martin Mising name Messages: 32 Registered: July 2009 |
Member |
|
|
> *Subject:* Re: importing native java source code *Author:* Jan
> Kohnlein *Date:* Thu, 15 November 2012 16:42
> Hmmm, I don't get what you want.
>
> In Xbase expressions, your DSL methods are callable if you have inferred
> a JvmOperation from them (JvmModelInferrer). This is not so much
> connected to imports as methods are usually called on objects, and
> imports allow to refer to types with unqualified names. An exception to
> this rule are static methods and extension methods.
>
> Maybe you could give an example what you're trying to achieve?
Yes, this code that I have listed below runs fine on xtend, but I want it to run in my own DSL (and it almost does, but not quite) the line:
rep1=newArrayList(te.getRep())
causes the error
Couldn't resolve reference to JvmIdentifiableElement 'getRep'.
The 'interfaces' directory contains native java interfaces and enums, the package 'euc' contains other classes in my language.
The approach that I have taken is to borrow parts of the xtend grammar to create a sort of 'xtend lite' without the richstring, annotations and many other features but basically functional, which I can add to later to give some specific functionality that I need. I realise, from what you said recently, that this in not the approach that you would have recommended. However, I have come this far and it seems very close to working, if I could just solve these problems.
My grammar is here
My ImportedNamespaceScopeProvider is here
My JvmModelInferrer is here
All other files are left with their default values as generated by mwe2.
Here is the code which was originally .xtend but is now in my dsl language (.euclid)
package euc
import interfaces.Field
import interfaces.Domain
import interfaces.TYPE
import java.util.List
public class Vect implements Domain {
val Field te;
val int dimen;
var TypeExpression[] rep1
var TypeExpression rep
new(Field t,int d){
te=t
dimen =d
rep1=newArrayList(te.getRep())
rep=new TypeExpression(TYPE::RECORD,rep1);
}
override TypeExpression getRep(){
rep
}
/**
* constructs a compound expression from a list of expressions
* representing the elements.
*
* How can we ensure these elements are of type 'te' which is 'Field'
*/
def Expression vect(Expression[] xs){
return new Expression(xs,this);
}
def Expression operator_plus(Expression a,Expression b){
var Expression[] elements = null;
try {
for(int i: 0..(dimen-1)) {
var Expression element = te.operator_plus(a.elt(i),b.elt(i))
if (elements == null) elements = newArrayList(element)
else elements.add(element)
}
} catch (Exception e) {
println("Vect.add error="+e)
}
return new Expression(elements,this);
}
def Expression operator_multiply(Expression a,Expression b){
return new Expression(a.intValue*b.intValue,this);
}
def Expression[] coerce(Expression a){
return a.arrayValue;
}
}
|
|
|
Re: importing native java source code [message #985932 is a reply to message #985822] |
Fri, 16 November 2012 17:55 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
I don't know the exact semantics of your language, but as I get it, 'te'
is of type 'Field', while 'getRep()' is defined in the class 'Vect'.
This would neither work in Xtend nor Java.
Am 16.11.12 10:47, schrieb Martin Baker:
>> *Subject:* Re: importing native java source code *Author:* Jan
>> Kohnlein *Date:* Thu, 15 November 2012 16:42
>> Hmmm, I don't get what you want.
>>
>> In Xbase expressions, your DSL methods are callable if you have inferred
>> a JvmOperation from them (JvmModelInferrer). This is not so much
>> connected to imports as methods are usually called on objects, and
>> imports allow to refer to types with unqualified names. An exception to
>> this rule are static methods and extension methods.
>>
>> Maybe you could give an example what you're trying to achieve?
>
> Yes, this code that I have listed below runs fine on xtend, but I want
> it to run in my own DSL (and it almost does, but not quite) the line:
> rep1=newArrayList(te.getRep())
> causes the error
> Couldn't resolve reference to JvmIdentifiableElement 'getRep'.
> The 'interfaces' directory contains native java interfaces and enums,
> the package 'euc' contains other classes in my language.
>
> The approach that I have taken is to borrow parts of the xtend grammar
> to create a sort of 'xtend lite' without the richstring, annotations and
> many other features but basically functional, which I can add to later
> to give some specific functionality that I need. I realise, from what
> you said recently, that this in not the approach that you would have
> recommended. However, I have come this far and it seems very close to
> working, if I could just solve these problems.
>
> My grammar is
> https://github.com/martinbaker/euclideanspace/blob/master/com.euclideanspace.euclid/src/com/euclideanspace/euclid/Editor.xtext
>
> My ImportedNamespaceScopeProvider is
> https://github.com/martinbaker/euclideanspace/blob/master/com.euclideanspace.euclid/src/com/euclideanspace/euclid/scoping/EditorImportedNamespaceScopeProvider.java
>
> My JvmModelInferrer is
> https://github.com/martinbaker/euclideanspace/blob/master/com.euclideanspace.euclid/src/com/euclideanspace/euclid/jvmmodel/EditorJvmModelInferrer.xtend
>
> All other files are left with their default values as generated by mwe2.
>
> Here is the code which was originally .xtend but is now in my dsl
> language (.euclid)
> package euc
> import interfaces.Field
> import interfaces.Domain
> import interfaces.TYPE
> import java.util.List
>
> public class Vect implements Domain {
>
> val Field te;
> val int dimen;
> var TypeExpression[] rep1
> var TypeExpression rep
> new(Field t,int d){
> te=t
> dimen =d
> rep1=newArrayList(te.getRep())
> rep=new TypeExpression(TYPE::RECORD,rep1);
> }
>
> override TypeExpression getRep(){
> rep
> }
> /**
> * constructs a compound expression from a list of expressions
> * representing the elements.
> * * How can we ensure these elements are of type 'te' which is 'Field'
> */
> def Expression vect(Expression[] xs){
> return new Expression(xs,this);
> }
>
> def Expression operator_plus(Expression a,Expression b){
> var Expression[] elements = null;
> try {
> for(int i: 0..(dimen-1)) {
> var Expression element = te.operator_plus(a.elt(i),b.elt(i))
> if (elements == null) elements = newArrayList(element)
> else elements.add(element)
> }
> } catch (Exception e) {
> println("Vect.add error="+e)
> }
> return new Expression(elements,this);
> }
>
> def Expression operator_multiply(Expression a,Expression b){
> return new Expression(a.intValue*b.intValue,this);
> }
>
> def Expression[] coerce(Expression a){
> return a.arrayValue;
> }
> }
>
>
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
| |
Goto Forum:
Current Time: Thu Mar 28 10:57:34 GMT 2024
Powered by FUDForum. Page generated in 0.04974 seconds
|