Hi all,
I've chosen a "protocol specification" pet-project to teach myself XText. After going through all the tutorials, I've added XBase to my (ridiculously simple ) grammar that looks like this:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
ProtocolDeclaration:
'protocol' name=QualifiedName
'specification' '{'
messages+=MessageDeclaration+
'}';
MessageDeclaration:
'message' name=ValidID '{'
fields+=FieldDeclaration+
'}';
FieldDeclaration:
// name=ValidID ':' type=JvmTypeReference; // this works for java types
name=ValidID ':' type=FieldType; // but I want my own names (below)
FieldType:
// my very own type names, should transform into existing classes:
'TypeA' | // --> my.existing.TypeNamedX
'TypeB' // --> my.other.existing.TypeNamedY
;
The goal is to have my own "type names" that actually map to existing Java classes (like java.lang.String) which I've written myself.
My next baby step is to generate Java code from the above updated grammar using my the generated MyDslJvmModelInferrer, which I (boldly ) started writing as follows:
def dispatch void infer(MessageDeclaration element, IAcceptor<JvmDeclaredType> acceptor, boolean isPrelinkingPhase) {
acceptor.accept(element.toClass(element.name) [
documentation = element.documentation
for (field : element.fields) {
// field.toField(field.name, field.type) [
field.toField(field.name, [WHAT_CAN_I_DO_HERE?]) [
makePublic
]
}
])
}
...but I've no idea what to provide create the field type reference to my actual classes. I'm looking for a way to do something like this:
var myTypeRef;
if (field.name == 'TypeA') {
myTypeRef = 'my.existing.TypeNamedX';
} else if (field.name == 'TypeB') {
myTypeRef = 'my.other.existing.TypeNamedY';
}
field.toField(field.name, myTypeRef) [
makePublic
])