The main way to build a new language/DSL using XText is to first define the grammar.
Initially we create a basic grammar as follows:
grammar com.beenleigh.lemma.Lemma with org.eclipse.xtext.common.Terminals
This is an import statement for the entities - such as ID - which are defined in the XText Terminals grammar.
generate lemma "http://www.beenleigh.com/lemma/Lemma"
Lemmamodel :
(elements += AbstractItem)*
;
Here we are defining the overall language, which consists of zero or more DataItems, the exact structure is yet to be defined.
DataModel:
'DataModel' name = QualifiedName '{'
(elements += AbstractItem)*
'}'
;
The DataModel is a uniquely identified metamodel for a particular dataset, and so it is acting in the same way as a package is for java, or as a namespace is for XML. It can contain any number of Lemma entities, or AbstractItems.
AbstractItem:
DataModel | DataClass | DataType | Import
;
Currently we can compose a DataModel of Imports, DataClasses and DataTypes - although later we may expand this to include DataElements and other DataItems.
QualifiedName:
ID('.' ID)*
;
This allows an ID to consist of name sections, separated by a 'dot'.
Import:
'import' importedNamespace = QualifiedNameWithWildcard
;
QualifiedNameWithWildcard:
QualifiedName '.*'?
;This allows us to import other DataModels, or rather components from other data models into the DataModel, using the ID system already loosely defined. For an individual registry these id's would be completely unique and implemented as IRI's.
DataType:
'datatype' name = ID
;
This allows is to uniquely identify a DataType - or type, normally these would be given as "int", "char", etc
DataClass:
'DataClass' name = ID ('extends' superType = [DataClass])? '{'
(dataelements += DataElement)*
'}'
;
This allows us to implement DataClasses, which can "extend" other DataClasses, for Lemma this means a kind of subtyping, since we are not - as yet - including operations in the language.
DataElement:
(many ?= 'many')? name = ID ':' type = [DataType|QualifiedName]
;
This relies on the xtext "Terminals" package, so the ID is simply a string, and is defined in that package. We need the ID to be unique, not just any old name, however that can be better enforced in the code generator.
First we take the XText grammar and generate artefacts:
And then we take the XText file and generate the Lemma editor/environment:
This means that after opening a new instance of eclipse (with these plugins referenced) we can start to write in Lemma, and eclipse will act as an editor:
This gives a basis for this data language, now we need to transform some XSD datasets into it and begin work on using model driven engineering to build new applications.
Initially we create a basic grammar as follows:
grammar com.beenleigh.lemma.Lemma with org.eclipse.xtext.common.Terminals
This is an import statement for the entities - such as ID - which are defined in the XText Terminals grammar.
generate lemma "http://www.beenleigh.com/lemma/Lemma"
Lemmamodel :
(elements += AbstractItem)*
;
Here we are defining the overall language, which consists of zero or more DataItems, the exact structure is yet to be defined.
DataModel:
'DataModel' name = QualifiedName '{'
(elements += AbstractItem)*
'}'
;
The DataModel is a uniquely identified metamodel for a particular dataset, and so it is acting in the same way as a package is for java, or as a namespace is for XML. It can contain any number of Lemma entities, or AbstractItems.
AbstractItem:
DataModel | DataClass | DataType | Import
;
Currently we can compose a DataModel of Imports, DataClasses and DataTypes - although later we may expand this to include DataElements and other DataItems.
QualifiedName:
ID('.' ID)*
;
This allows an ID to consist of name sections, separated by a 'dot'.
Import:
'import' importedNamespace = QualifiedNameWithWildcard
;
QualifiedNameWithWildcard:
QualifiedName '.*'?
;This allows us to import other DataModels, or rather components from other data models into the DataModel, using the ID system already loosely defined. For an individual registry these id's would be completely unique and implemented as IRI's.
DataType:
'datatype' name = ID
;
This allows is to uniquely identify a DataType - or type, normally these would be given as "int", "char", etc
DataClass:
'DataClass' name = ID ('extends' superType = [DataClass])? '{'
(dataelements += DataElement)*
'}'
;
This allows us to implement DataClasses, which can "extend" other DataClasses, for Lemma this means a kind of subtyping, since we are not - as yet - including operations in the language.
DataElement:
(many ?= 'many')? name = ID ':' type = [DataType|QualifiedName]
;
This relies on the xtext "Terminals" package, so the ID is simply a string, and is defined in that package. We need the ID to be unique, not just any old name, however that can be better enforced in the code generator.
First we take the XText grammar and generate artefacts:
And then we take the XText file and generate the Lemma editor/environment:
This means that after opening a new instance of eclipse (with these plugins referenced) we can start to write in Lemma, and eclipse will act as an editor:
This gives a basis for this data language, now we need to transform some XSD datasets into it and begin work on using model driven engineering to build new applications.





