Xtext - Validating for duplicate names

假装没事ソ 提交于 2019-12-12 00:58:33

问题


I have the following grammer, but I want to do some validation on this. I want to make an error if there are duplicate names in the "players" list.

Grammer:

Football:
    'Club' name=STRING playerList=PlayerList
     footballObjects+=FootballObject
;

FootballObject:
     Player | Coach
;

PlayerList:
     players+=[Player] ( players+=[Player] )* 
;

Player:
    'Player' name=ID
;

I tried the following:

@Check
def checkGreetingStartsWithCapital(Football model) {
    val names = newHashSet
    for (g : model.playersList.players) {
        if(!names.add(g.name))
            error("duplicate" , g, FOOTBALLPACKAGE.Literals.FOOTBALL__PLAYERS_LIST)
    }
}

But this does not work. Any ideas why?


回答1:


The easiest is to mark the list entry by calling error not on the referenced player but on the playersList itself and call the error method that takes an index as well. e.g.

error("bad", playersList, MyDslPackage.Literals.PLAYERS_LIST__PLAYERS, index)


来源:https://stackoverflow.com/questions/50568506/xtext-validating-for-duplicate-names

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