0

If I have an Xtext grammar like the one below:

grammar org.xtext.example.mydsl.ServerGeneratorLanguage with org.eclipse.xtext.common.Terminals

generate serverGeneratorLanguage "http://www.xtext.org/example/mydsl/ServerGeneratorLanguage"

Model:
  (types+=Type)*;

Type:
  Server | DomainModel;

Server:
    "SERVER" name=ID "{"
        "CONFIG" "{"
        (Configs+=Config)*
            "}"
  "}";

Config:
    rootConfig | hostNameConfig | portConfig | logConfig | sqldbConfig | resourceConfig;

rootConfig:
    "ROOTDIR" "=" name=ID;

hostNameConfig:
    "HOSTNAME" "=" name=ID;

portConfig:
    "PORT" "=" name=ID;

logConfig:
    "LOG" "=" name=ID;

sqldbConfig:
    "SQLDB" "=" name=ID;

resourceConfig:
    "RESOURCE" "=" name=ID;

DomainModel:
    "DOMAINMODEL" name=ID "{"
        "ENTITYS" "{"
            (Entitys+=Entity)*
        "}"
        "ENTITY_RELATIONSHIP" "{"
            (Relationships+=Relationship)*
        "}" 
    "}";

Entity:
    name=ID "{"
        (Attributes+=Attribute)*
    "}";

Attribute:
    StringAttribute | NumberAttribute | ImageAttribute;

StringAttribute:
    "STRING" name=ID;

NumberAttribute:
    "NUMBER" name=ID;

ImageAttribute:
    "IMAGE" name=ID;

QualifiedName:
    ID ('.' ID)*;

Relationship:
    name=[Attribute|QualifiedName] "->" refName=[Attribute|QualifiedName];

How do I access the name-ID part of each Config object from a higher level than Server. Let me explain:

I want to generate code by collecting data from Server.configs.eClass.name AS WELL AS DomainModel.blahblah.eClass.name. I assume I need to access it from Type, but I can't seem to get there in Xtend.

From Server I can access all the components Server.configs.eClass.name.

Thank you for the help.

Charles Henry
  • 353
  • 3
  • 16

1 Answers1

0

The automatic model inferrer of Xtext should push common features (like 'name' in your case) up the inheritance hierarchy. When it does not do this, you should check:

  • Does each Config have a name feature? Are they the same type? Check this in your grammar.

  • Do RootConfig, HostConfig etc. derive from Config? Check this in the generated classes.

A.H.
  • 63,967
  • 15
  • 92
  • 126
  • I am a little lost with your question, does my code not answer that? I have included a name at the end of each tree. eg 'Config -> rootConfig -> name' as you can see above. But I do not know how to access it from such a high level (Type). I only know how to access is from a close level. (Config) – Charles Henry Mar 25 '13 at 15:05
  • I would not have asked if it would be clear from your code. Your code is not self contained. And my "question" is: Does each grammar rule "below" `Type` have a `name=ID` part? I assume, that you forgot at least one somewhere in some part of the grammar you did not post here. – A.H. Mar 25 '13 at 18:49
  • Ok I have added all of the code above, I have included name=ID for every grammar. What do you think the problem may be? It would be so helpful if I could get this working... – Charles Henry Apr 23 '13 at 12:49
  • @CharlesHenry I don't understand your problem: If I paste your grammar into an Xtext file and generate the artefacts, then the comman supertype of `DomainModel` and `Server` is `Type`. And `Type` has a `name` attribute. So in Xtend you can just use `val Type myType = ...; myType.name`. Exactly what you've asked for at the beginning. – A.H. Apr 23 '13 at 17:45