关于参考:Xtext DSL语法作用域定制

Xtext DSL grammar scoping customization

我知道有一个简单的解决方案,但是我不知道如何实现。

我正在寻找实现答案的方法,而不是我需要做的事情。答案的一半已经在此页面上:
Xtext交叉引用和作用域

我的问题是下面的语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;

// Relationship = [new table name] : [shared key name] -> ref_table(ref_id)
Relationship:
    name=ID":" newEntityName=ID"->" refEntityName=[Entity|ID]"("refName=[Attribute|ID]")"; // <- Problem here

编写模型时,无法获得" refName = [Attribute | ID]"来引用实体内部的属性。
在下面的代码中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
DOMAINMODEL auctionHouse{
    ENTITYS {

       lots{      
          NUMBER id0
          NUMBER lotNo
          STRING name
          STRING priceEstimate
          STRING description
       }  
       auctions{
          NUMBER id1
          NUMBER date
          STRING description
       }
       auction_lots{
          NUMBER id2
          STRING lot_id
          NUMBER auction_id
       }

    }

    ENTITY_RELATIONSHIP {
        auction_lots : lot_id -> lots(id0) // <- Will not find 'id0'
        auction_lots : auction_id -> auctions(id1) // <- Will not find 'id1'
    }

}

如何扩大范围?
如何区分两个具有相同名称但作用域不同的属性?


引用的问题在于,在该范围内根本找不到它们,您可以做的是引入一个限定名称,并在交叉引用中使用该名称并相应地更改语法,即:-

1
2
3
4
5
QualifiedName:
    ID ('.' ID)*;

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

现在应该可以使用合格的ID进行引用:

1
2
3
4
ENTITY_RELATIONSHIP {
    auction_lots : lot_id -> auctionHouse.lots.id0
    auction_lots : auction_id -> auctionHouse.auctions.id1
}

如果您不能像这样通过使用Xtext处理名称的默认方式来更改语法,则您需要研究提供自己的合格名称,对此的一个很好的文章是《合格名称文章》