XText entity example, primitive type

做~自己de王妃 提交于 2019-12-25 07:10:00

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/23710059/xtext-entity-example-primitive-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!