Home » Modeling » TMF (Xtext) » Using XBlockExpressions and XExpressios without JvmModelInferrer
Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1721812] |
Sun, 31 January 2016 09:34 |
Larry LeBron Messages: 124 Registered: October 2015 |
Senior Member |
|
|
My DSL extends the XBase grammar, but I have written a custom generator because of some quirks in the way my files rely on the structure of the project.
I would like to be able to use XBlockExpressions as well as XExpressions, and have added them to the grammar.
However, unsurprisingly, the scoping and validation in the XBlockExpression is not working. I understand that the JvmModelInferrer typically handles the scoping, so I can take that on, but am having trouble setting up the proper hooks.
I'm experimenting with binding a custom ITypeComputer and adding a custom ScopeProvider delegate via configureIScopeProviderDelegate(), but I just don't see my XExpressions or XBlockExpressions being checked for scopes.
My custom scope provider extends ImportedNamespaceAwareLocalScopeProvider, and they are bound like so:
def Class<? extends ITypeComputer> bindITypeComputer() {
return MyTypeComputer;
}
/* Bind the delegate in the scope provider */
override configureIScopeProviderDelegate(Binder binder) {
binder.bind(IScopeProvider).
annotatedWith(Names.
named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)).
to(MyImportedNamespaceAwareLocalScopeProvider);
}
I have overridden the following methods, just to see if they're working:
// In my scope provider
override IScope getScope(EObject context, EReference reference) {...}
// In my TypeComputer
override computeTypes(XExpression expression, ITypeComputationState state) {...}
override void _computeTypes(XBlockExpression b, ITypeComputationState typeState) {...}
I have only seen some output from the custom getScope, but it only seems to be triggered when I explicitly reference a type. A non-explicit variable declaration such as:
does not seem to trigger any scope checking, nor does it trigger a validation error.
Likewise,
triggers some scope checking, but does not trigger a validation error based on the mismatched types.
Any guidance to help me get the proper framework set up would be greatly appreciated! In the meantime, I'll continue to fumble around with it
|
|
| | | |
Re: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1722169 is a reply to message #1721815] |
Wed, 03 February 2016 08:22 |
|
On 31/01/2016 12:11, Larry LeBron wrote:
> Thanks for the advice, Lorenzo. I hand't really considered that as an
> option.
>
> How does the interplay between the JvmModelInferrer and a custom
> generator pan out?
>
> With a custom generator, can I just hook into the default xtext
> generation and modify it?
>
> Can the generator output something completely different from the
> inferred model if desired?
Hi
What do you mean by "completely different"? If your model inferrer
mapped your DSL elements into Java elements, then all the typing and
scoping has been performed according to that mapping. Generating
something completely different will break the overall semantics, i.e.,
you might generate Java code that does not behave correctly (or it might
not even compile).
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: Using XBlockExpressions and XExpressios without JvmModelInferrer [message #1722301 is a reply to message #1722284] |
Wed, 03 February 2016 23:12 |
Larry LeBron Messages: 124 Registered: October 2015 |
Senior Member |
|
|
Sure, I understand.
My first case of wanting to do this is that I'd like to append a main method to the file generated by any instance of a certain EClass. The contents of this main should be the mostly the same for any instance of this class, customizing only the invocation of the constructor.
For example, imagine this is a valid entry in my language:
I would then like to generate this java file:
class myMain extends SuperMain {
public static void main (String[] args) {
myMain m = new myMain();
m.start(); // call to superclass method
}
}
I am currently doing this in the inferrer:
def dispatch void infer(MainObject mainObj, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
val objName = mainObj.name
acceptor.accept(mainObj.toClass(objName)) [
superTypes += typeRef(SuperMain)
/* add main method */
members += mainObj.toMethod('main', typeRef(Void.TYPE)) [
parameters += mainObj.toParameter("args", typeRef(String).addArrayTypeDimension)
static = true
varArgs = true
body = '''
«objName» m = new «objName»();
m.start();
'''
]
]
}
Considering I don't want this main to be callable from the DSL itself, is the inferrer the best place to handle this customization, or would it be better to:
- Override the generator, appending the main method there
- Override the XBaseCompiler, customizing the compilation output to somehow add the method
- Do something else I haven't yet considered?
Thanks!
[Updated on: Thu, 04 February 2016 00:27] Report message to a moderator
|
|
| |
Goto Forum:
Current Time: Wed Sep 25 11:39:09 GMT 2024
Powered by FUDForum. Page generated in 0.24750 seconds
|