Search code examples
javaxtextxtend

Xtext, import my own mydsl file


In my grammar i have an include rule as follow :

Script:
    includes+=(Include)* assignments+=(Assignment)* clock=Clock? tests+=Test*
;

Include:
    'INCLUDE' importURI=STRING
;

what I want to do is to include files same as the "main" file.

I'm working with an interpreter that handels the .mydsl file.

/* Main exec methode  */

def dispatch void exec(Script s) {

    s.includes.forEach[ i | i.exec]  
    s.assignments.forEach[a | a.exec]
    s.clock.exec
    s.tests.forEach[t|t.exec]
}

/* include methode */
def dispatch void exec(Include i) {

    System.out.println( i.importURI + " included")

}

Solution

  • Xtext imports are not includes. Xtext does not support includes at all. All that Xtext supports are Cross References. You can use namespace based or import uri based global scoping to determine how elements from other files can be found. assume you really want to follow the includes files in your interpreter

    Script:
        includes+=(Include)*
    ;
    
    Include:
        'INCLUDE' includedScript=[Script|STRING]
    ;
    

    And Name Provider

    public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {
    
        QualifiedName qualifiedName(Script script) {
            return QualifiedName.create(script.eResource().getURI().trimFileExtension().lastSegment(), script.eResource().getURI().fileExtension());
        }
    
    }
    

    then you can follow the reference in your interpreter

    class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
    
        override bindIQualifiedNameProvider() {
            MyDslQNP
        }
    
    }