Xtext DSL语法范围定制

Cha*_*nry 4 dsl grammar reference scoping xtext

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

我正在寻找如何实现答案而不是我需要做的事情.答案的一半已经在这个页面上: Xtext交叉引用和范围

我的问题是下面的语法:

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
Run Code Online (Sandbox Code Playgroud)

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

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'
    }

}
Run Code Online (Sandbox Code Playgroud)

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

Ian*_*ick 5

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

QualifiedName:
    ID ('.' ID)*;

Relationship:
    name=ID ":" newEntityName=ID "->" refName=[Attribute|QualifiedName];
Run Code Online (Sandbox Code Playgroud)

现在应该可以使用限定ID进行引用:

ENTITY_RELATIONSHIP {
    auction_lots : lot_id -> auctionHouse.lots.id0
    auction_lots : auction_id -> auctionHouse.auctions.id1
}
Run Code Online (Sandbox Code Playgroud)

如果您不能像这样更改语法以使用Xtext处理名称的默认方式,那么您需要考虑提供自己的合格名称,这是一篇很好的文章就是这个合格的名称文章