Eclipse Xtext元素访问

Eclipse Xtext元素访问,eclipse,xtext,Eclipse,Xtext,我用Xtext写过: grammar org.xtext.example.dsl.Dsl with org.eclipse.xtext.common.Terminals generate dsl "http://www.xtext.org/example/dsl/Dsl" Dsl : (elements += Type)* ; Type: System ; System: 'The system' name = ID 'consists of the follow

我用Xtext写过:

grammar org.xtext.example.dsl.Dsl with org.eclipse.xtext.common.Terminals

generate dsl "http://www.xtext.org/example/dsl/Dsl"

Dsl :
    (elements += Type)*
;

Type:
    System
;

System:
    'The system' name = ID 'consists of the following:
;
现在我运行编辑器并输入系统名。 如何访问Eclipse文件中的系统名?

这是解决方案

org.example.somthing.generator的src文件夹中编写以下代码,包和类是Something.xtend(.xtend)

这用于代码生成,由xtext生成。您将发现doGenerate(Resource Resource,IFileSystemAccess fsa)方法在该方法中编写以下代码

类DomainmodelGenerator实现IGenerator{

@注入扩展IQualifiedNameProvider

重写void doGenerate(资源,IFileSystemAccess fsa){

对于(e:resource.allContents.toIterable.filter(DSL)){

fsa.generateFile(“abcd.txt”,e.compile)

}

def编译(DSL d)“”

«d.System.name»»'
}

现在您必须编写main类来调用上述类的方法

这是代码

公共班机{

@Inject
private Provider<ResourceSet> resourceSetProvider;

@Inject
private IResourceValidator validator;

@Inject
private IGenerator generator;

@Inject
private JavaIoFileSystemAccess fileAccess;

public static void main(String[] args) {
    Injector injector = new DomainmodelStandaloneSetupGenerated()
            .createInjectorAndDoEMFRegistration();
    Main main = injector.getInstance(Main.class);
    main.runGenerator("sample.dmodel");
}

protected void runGenerator(String string) {
    // load the resource
    ResourceSet set = resourceSetProvider.get();
    Resource resource = set.getResource(URI.createURI(string), true);

    List<Issue> list = validator.validate(resource, CheckMode.ALL,
            CancelIndicator.NullImpl);
    if (!list.isEmpty()) {
        for (Issue issue : list) {
            System.err.println(issue);
        }
        return;
    }

    // configure and start the generator
    fileAccess.setOutputPath("output/");
    generator.doGenerate(resource, fileAccess);

    System.out.println("Code generation finished.");
}
@Inject
私有提供者resourceSetProvider;
@注入
私有IResourceValidator验证程序;
@注入
私人发电机;
@注入
私有JavaIoFileSystemAccess文件访问;
公共静态void main(字符串[]args){
注入器注入器=新的DomainModelStandaloneStupGenerated()
.createInjectorAndDoEMFRegistration();
Main=injector.getInstance(Main.class);
main.runGenerator(“sample.dmodel”);
}
受保护的void runGenerator(字符串){
//加载资源
ResourceSet=resourceSetProvider.get();
Resource=set.getResource(URI.createURI(字符串),true);
List List=validator.validate(资源,CheckMode.ALL,
CancelIndicator.NullImpl);
如果(!list.isEmpty()){
针对(问题:列表){
系统错误打印项次(发布);
}
返回;
}
//配置并启动发电机
setOutputPath(“output/”);
generator.doGenerate(资源、文件访问);
System.out.println(“代码生成完成”);
}

}

问题有点模糊,您想做什么?通常,Xtext的主要接口(如生成器、验证器等)会提供一个包含文件AST的资源,然后您可以检查该资源。