简体   繁体   中英

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")

}

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
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM