我用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文件中访问系统名称?
答案 0 :(得分:1)
这是洗液。
将以下代码写入 org.example.somthing.generator 包中的 src 文件夹中,类为Something.xtend(.xtend)
这是用于代码生成的,它由xtext生成。你会发现doGenerate(资源,IFileSystemAccess fsa)方法在该方法中编写以下代码。
类DomainmodelGenerator实现IGenerator {
@Inject扩展名IQualifiedNameProvider
覆盖void doGenerate(资源资源,IFileSystemAccess fsa){
for(e:resource.allContents.toIterable.filter(DSL)){
fsa.generateFile( “abcd.txt”,e.compile)
}
def compile(DSL d)'''
«d.System.name» '''
}
现在你必须编写主类来调用上面的类的方法..
这是代码......
public class 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.");
}
}