XText简单例子入门

教程不详细写,直接代码:
1.MyDsl.xtext
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 '{' '}';

2.运行GenerateMyDsl.mwe2
3.Template.xpt
«IMPORT org::xtext::example::mydsl::myDsl»

«EXTENSION templates::Extensions»

«DEFINE main FOR myDsl::Entity-»
	«FILE name+".java"-»
public class «name» {
	public «name»(){}
}
	«ENDFILE-»
«ENDDEFINE»

4.MyDslGenerator.mwe2

下面这句很重要,没有的话一般都不成功:
metaModel = org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel{}

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
	}
}

5.Example.mydsl
entity EmptyEntity {
}

6.Extensions.ext
import org::xtext::example::mydsl::myDsl;

你可能感兴趣的:(java,eclipse,workflow,ext)