O'Reilly logo

Implementing Domain-Specific Languages with Xtext and Xtend by Lorenzo Bettini

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Custom scoping

As soon as a DSL introduces more involved features, such as nested blocks or inheritance relations, the scope provider must be customized according to the semantics of the language.

The Xtext generator generates an Xtend stub class for implementing a custom scope provider. In the SmallJava DSL it is SmallJavaScopeProvider.

One way of customizing the scope provider is to redefine the method getScope; this requires you to manually check the reference and the context's class, for example:

override getScope(EObject context, EReference reference) {
  if (reference == SmallJavaPackage::eINSTANCE.SJSymbolRef_Symbol) {
    if (context instanceof ...)
      ...
  } else if (reference ==
        SmallJavaPackage::eINSTANCE.SJMemberSelection_Member) {
      ...
  } else ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required