Xtext example of a scoped object
Asked Answered
C

2

8

I'm looking for an example (in XText) of how to implement code completion on an user defined objects members. As far as I can see I need to use IScope, but how all this wires together is unclear.

Given that trait is a user defined type, how do I go about building a grammar to code complete / validate the methods contained within String when I type name.?

trait String {
    def toLowerCase(): String
    def toUpperCase(): String
}

val name = new String()
name.toLowerCase()

Thanks

Chirlin answered 17/10, 2011 at 9:37 Comment(0)
I
17

It highly depends on your grammar what you have to do to adopt scoping. Let us say you have a grammar like

Model:
    statements+=Statement+
;

Statement:
    Trait | VarDef | Call
;

Trait:
    "trait" name=ID "{"
        ops+=Operation*
    "}"
;

Operation:
    "def" name=ID "()" ":" type=[Trait]
;

VarDef:
    "val" name=ID "=" "new" type=[Trait] "()"
;

Call:
    var=[VarDef] "." op=[Operation] "()"
;

then your scopeprovider would look like

public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {

    IScope scope_Call_op(Call call, EReference ref) {
        return Scopes.scopeFor(call.getVar().getType().getOps());
    }
}    

You can find a blog series on the topic here:

https://web.archive.org/web/20130129085620/http://blogs.itemis.de/stundzig/archives/773

Involucrum answered 18/10, 2011 at 19:36 Comment(2)
Excellent, thanks Christian exactly the kickstart I needed ;)Chirlin
For those as clueless about EMF as I was, you may find useful EcoreUtil.getObjectsByType if you need to manually traverse your DSL because what you want to reference can't easily be obtained via the get methods. e.g. You have collections of heterogeneous objects that you need to scope on.Tuppence
A
2

In my book on Xtext, "Implementing Domain-Specific Languages with Xtext and Xtend", https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend , there is a chapter about scoping for a "smaller" Java language (dealing also with inheritance). You can find the sources of examples here: https://github.com/LorenzoBettini/packtpub-xtext-book-examples

Arriviste answered 12/9, 2014 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.