xtext

Xtext entity example, primitive type


What I'm trying to do, is modify a bit the basic entity DSL example, so that it supports primitives, and something like this can be done:

entity Dog {
   name : String
}

entity Person {
   name : String
   dog : Dog
}

So that the type of the members can be either a reference to the name of another entity, or a predefined primitive. I'm also looking for a way, so there is content assist in the generated editor for both the primitives, and the entity names.

Here is my .xtext so far:

Model:
    (entites+=Entity)*;

Entity:
    'entity' name=ID '{'
    (members+=Member)*
    '}';

AbstractType:
    Entity | PrimitiveType;

PrimitiveType:
    name='Integer' | name='String';

Member:
    (many?='many')? name=ID ':' (type=[AbstractType]);

In this case, Integer and String are recognized by the editor as keywords, but they are marked as error, with the message:

mismatched input 'Integer' expecting RULE_ID

I tried using the solution for this question: Defining Primitives within xtext Grammar , which was half-decent, because the primitives were not marked as error, but there were no content assist for them.

So what is the proper way of doing this?


Solution

  • xxxx=[YYYYY] is a cross reference. this is a reference to something that is defined somewhere else. in your model yo have nowhere an instance of a PrimitiveType so you cannot have a instance of it. so you have to define them explicitely

    Model:
    (primitives+= PrimitiveType)*;
    PrimitiveType:
    'datatype' (name='Integer' | name='String');
    

    model

    datatype String
    datatype Integer
    entity Dog {
        name : String
    }
    
    entity Person {
       name : String
       dog : Dog
    }
    

    or have to come up with a completely different grammar

    Model:
        (entites+=Entity)*;
    
    Entity:
        'entity' name=ID '{'
        (members+=Member)*
        '}';
    
    Type:
        EntityRefence | SimpleDataType;
    
    EntityRefence:
        entity=[Entity]
    ;
    
    SimpleDataType:
        type=PrimitiveType
    ;
    
    
    enum PrimitiveType:
        String | Integer
    ;
    
    Member:
        (many?='many')? name=ID ':' type=Type;