The Eclipse Modeling Framework (EMF) can be used to describe other languages (meta-language)
ECore Model
An example of an Ecore model can be drawn like this:
So what is the structure of this model? We could define this as a meta-model (meta-meta-language?)
see Ecore metamodel on the eclipse website here.
ECore Model Elements
EObject : All these model elements below extend EObject. EObject is the base of all model elements in the same way that all java classes extend Object.
EPackage:
EClass : represents a class, with zero or more attributes and zero or more references.
EAttribute : represents an attribute which has a name and a type.
EReference : represents one end of an association between two classes. It has flag to indicate if it represent a containment and a reference class to which it points.
EDataType : represents the type of an attribute, e.g. int, float or java.util.Date
Generating an ECore model
From Xtext
The grammar file not only defines the synatx of a text file, but also how it maps to an ECore model.
There are 4 types of rule.
Rule type | Returns | identified by | Used by |
---|---|---|---|
Parser Rules | EClass | Parser | |
Enum Rules | EEnum | start with 'enum' | Parser |
Datatype Rules | EDatatype (EString is implied if none has been explicitly declared) | rules that do not:
|
Parser |
Terminal Rules | EDatatype | start with 'terminal' | Lex |
If we want a Syntax element to map to a model element then we should preceed it with one of the following forms:
Syntax Element | Model Element |
---|---|
myName = | |
myName += | returns list |
myName ?= | returns true/false |
he grammar file not only specifies the grammar and generates the lexer and parser but it also has an additional function, to specify the mapping to the Ecore model.
I am not planning to fully explain the ECore (EMF) model structure here, but roughly, we have an EPackage and inside that a set of EClasses and inside that a set of EDatatype, EReference. and EAttribute s, Like this:
EPackage { EClass MyModel { containment entities : Entity[] } EClass Entity { name : EString extends : Entity // the crosslink } } |
ECore model and GMF
More on ECore here