【问题标题】:Xtext DSL grammar scoping customizationXtext DSL 语法范围定制
【发布时间】:2013-06-14 05:46:03
【问题描述】:

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

我正在寻找如何实施答案,而不是我需要做什么。一半的答案已经在此页面上: Xtext cross referencing and scoping

我的问题在于以下语法:

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]”来引用实体内部的属性。 在下面的代码中

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

}

如何扩大范围? 如何区分两个同名但范围不同的属性?

【问题讨论】:

    标签: reference grammar dsl xtext scoping


    【解决方案1】:

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

    QualifiedName:
        ID ('.' ID)*;
    
    Relationship:
        name=ID ":" newEntityName=ID "->" refName=[Attribute|QualifiedName];
    

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

    ENTITY_RELATIONSHIP {
        auction_lots : lot_id -> auctionHouse.lots.id0
        auction_lots : auction_id -> auctionHouse.auctions.id1
    }
    

    如果您不能像这样更改语法以使用 Xtext 处理名称的默认方式,那么您需要考虑提供自己的限定名称,这是一篇很棒的文章 Qualified Names Article

    【讨论】:

    • 谢谢你这解决了我所有的问题。这是一个完美的答案!哦,但仅供参考,模型接受“auctionHouse.lots.id0”而不是“ENTITYS.lots.id0”
    • 为了完整起见:“请注意,| 栏不是交叉引用上下文中的替代项,而是用于指定解析字符串的语法。” eclipse.org/Xtext/documentation/102_domainmodelwalkthrough.html
    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多