Xtext - 验证重复名称 [英] Xtext - Validating for duplicate names

查看:36
本文介绍了Xtext - 验证重复名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下语法,但我想对此进行一些验证.如果玩家"列表中有重复的名字,我想出错.

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.

语法:

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

FootballObject:
     Player | Coach
;

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

Player:
    'Player' name=ID
;

我尝试了以下方法:

@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?

推荐答案

最简单的方法是通过调用 error 来标记列表条目,而不是在引用的播放器上而是在 playersList 本身并调用带有索引的 error 方法.例如

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)

这篇关于Xtext - 验证重复名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆