教程不详细写,直接代码:
[b][size=medium][color=indigo]1.MyDsl.xtext[/color][/size][/b]
[b][size=medium][color=indigo]2.运行GenerateMyDsl.mwe2[/color][/size][/b]
[b][size=medium][color=indigo]3.Template.xpt[/color][/size][/b]
[b][size=medium][color=indigo]4.MyDslGenerator.mwe2[/color][/size][/b]
[b][color=red]
下面这句很重要,没有的话一般都不成功:
metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}
[/color][/b]
[b][size=medium][color=indigo]5.Example.mydsl[/color][/size][/b]
[b][size=medium][color=indigo]6.Extensions.ext[/color][/size][/b]
[b][size=medium][color=indigo]1.MyDsl.xtext[/color][/size][/b]
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
DomainModel:
entities+=Entity;
Entity:
'entity' name=ID '{' '}';
[b][size=medium][color=indigo]2.运行GenerateMyDsl.mwe2[/color][/size][/b]
[b][size=medium][color=indigo]3.Template.xpt[/color][/size][/b]
«IMPORT org::xtext::example::mydsl::myDsl»
«EXTENSION templates::Extensions»
«DEFINE main FOR myDsl::Entity-»
«FILE name+".java"-»
public class «name» {
public «name»(){}
}
«ENDFILE-»
«ENDDEFINE»
[b][size=medium][color=indigo]4.MyDslGenerator.mwe2[/color][/size][/b]
[b][color=red]
下面这句很重要,没有的话一般都不成功:
metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}
[/color][/b]
module workflow.MyDslGenerator
import org.eclipse.emf.mwe.utils.*
var targetDir = "src-gen"
var fileEncoding = "UTF-8"
var modelPath = "src/model"
Workflow {
component = org.eclipse.xtext.mwe.Reader {
// lookup all resources on the classpath
// useJavaClassPath = true
// or define search scope explicitly
path = modelPath
// this class will be generated by the xtext generator
register = org.xtext.example.mydsl.MyDslStandaloneSetup {}
load = {
slot = "entities"
type = "Entity"
}
}
component = org.eclipse.xpand2.Generator {
metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}
expand = "templates::Template::main FOREACH entities"
outlet = {
path = targetDir
}
fileEncoding = fileEncoding
}
}
[b][size=medium][color=indigo]5.Example.mydsl[/color][/size][/b]
entity EmptyEntity {
}
[b][size=medium][color=indigo]6.Extensions.ext[/color][/size][/b]
import org::xtext::example::mydsl::myDsl;