|
|
|
Re: Inject xbasecompiler [message #1698468 is a reply to message #1698432] |
Mon, 15 June 2015 19:48 |
|
On 15/06/2015 18:30, Julien Delange wrote:
> I have a set of languages implemented with xtext. These languages also
> common aspects that are defined in a shared xtext language. The common
> language uses xbase to define variable and expression.
>
> Then, in one language, I would like to process and compile the xbase
> variable and expression. The language itself does not use xbase directly
> but uses it through the common language.
>
> When trying to use the XbaseCompiler, I then added a dependency with an
> injection like this (as defined in [1]):
>
> @Inject XbaseCompiler xbaseCompiler
>
> However, when starting my project, I got a long serie of exception (see
> below). How to know how to inject the appropriate class? And how to
> debug injected classes?
>
> Thanks for any help or suggestion!
>
> [1] http://www.rcp-vision.com/1796/xtext-2-1-using-xbase-variables/ and
> http://www.rcp-vision.com/1640/xtext-2-1-using-xbase-expressions/
>
Hi Julien
those blog posts I wrote are rather old now, and won't work anymore: you
should really use a JvmModelInferrer if you use Xbase :)
cheers
Lorenzo
--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Inject xbasecompiler [message #1698623 is a reply to message #1698619] |
Tue, 16 June 2015 17:54 |
Julien Delange Messages: 82 Registered: October 2011 |
Member |
|
|
Sure! Thanks for your help! I modified the domainmodel as follows:
grammar org.eclipse.xtext.example.domainmodel.Domainmodel with org.eclipse.xtext.xbase.Xbase
generate domainmodel "http://www.xtext.org/example/Domainmodel"
DomainModel:
importSection=XImportSection?
elements+=AbstractElement*;
AbstractElement:
PackageDeclaration | Entity;
PackageDeclaration:
'package' name=QualifiedName '{'
elements+=AbstractElement*
'}';
Entity:
'entity' name=ValidID ('extends' superType=JvmParameterizedTypeReference)? '{'
('features'
features+=Feature*)?
('vars'
vars+=XVariableDeclaration*)?
'}';
Feature:
Property | Operation ;
Property:
name=ValidID ':' type=JvmTypeReference;
Operation:
'op' name=ValidID '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' (':' type=JvmTypeReference)?
body=XBlockExpression;
Now, I have the following code
package blabla
{
entity foo
{
features
myprop : int
op op1 (int param1, int param2) : int
{
return param1;
}
vars
val implicit1 = 4
val implicit2 = 5
val float explicit = 5.0
val Integer integerObject = new Integer(5)
val foobar = implicit1 + implicit2
}
}
With that, I have two goals:
1. Detect the type of variables that are not strongly types but have an expression. For example, for implicit1, return int
2. Compiles the expression. For example, for foobar, having something that returns 9 directly
So, I tried to dig the documentation but it seems that some of it is outdated, so, having some pointers on where I should look into will definitively help!
Thanks!
Julien.
|
|
|
|
|
Re: Inject xbasecompiler [message #1698628 is a reply to message #1698627] |
Tue, 16 June 2015 18:46 |
|
so why not change grammar to:
('vars'
vars+=Constant*)?
...
Constant:
"val" (type=JvmParameterizedTypeReference)? name=ID "=" init=XExpression
;
and use in the inferrer
for (v : entity.vars) {
members+=v.toField(v.name, v.type ?: inferredType) [
final = true
static = true
visibility = JvmVisibility.PUBLIC
initializer = v.init
]
}
(5.0 is a double literal in xbase though)
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Tue, 16 June 2015 18:47] Report message to a moderator
|
|
|
|
|
|
|
Re: Inject xbasecompiler [message #1698638 is a reply to message #1698637] |
Tue, 16 June 2015 19:52 |
|
of course i have, but this will not happen if you do not infer something from your stuff.
only then the type will be calculated by the type computer and the scope (for references) is set properly.
if this is the case you can access types. but only at generation time, not on inferral time.
org.eclipse.xtext.xbase.typesystem.IBatchTypeResolver.resolveTypes(EObject) would help you with that.
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: Inject xbasecompiler [message #1698643 is a reply to message #1698639] |
Tue, 16 June 2015 21:26 |
|
hi,
still, if you do not use jvmmodel inferrer there will be no type calculation ?!? (XbaseTypeComputer will never be called by LogicalContainerAwareReentrantTypeResolver, variables will not be visible from each other)
thus i strictly recommend you to use that. e.g. as i proposed it.
or something like that
Entity:
'entity' name=ValidID ('extends' superType=JvmParameterizedTypeReference)? '{'
('features'
features+=Feature*)?
(vars=Vars)?
'}';
Vars returns xbase::XBlockExpression:
{Vars}'vars'
expressions+=XVariableDeclaration*
;
if (entity.vars != null) {
members+=entity.toMethod("vars", Void.TYPE.typeRef()) [
body = entity.vars
]
}
still, compile and interpret is not the same at all ?!?
class ProcessLogicDefault implements IProcessLogic
{
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
@Inject XbaseInterpreter interpreter
override process(PackageDeclaration pkg) {
println ("package" + pkg)
pkg.elements.filter(Entity).forEach[e | processEntity (e)]
}
def processEntity(Entity entity) {
val ctx = new DefaultEvaluationContext()
entity.vars.expressions.filter(XVariableDeclaration).forEach[v | processVar(v, ctx)]
}
def processVar(XVariableDeclaration v, IEvaluationContext ctx) {
try {
val IEvaluationResult r = interpreter.evaluate(v, ctx, CancelIndicator.NullImpl)
println(v.name + " = " + ctx.getValue(QualifiedName.create(v.name)))
} catch(Exception e) {
e.printStackTrace
}
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.06985 seconds