为什么error方法会返回错误?

时间:2015-05-29 14:25:48

标签: xtext xtend

我想验证对应于以下语法片段的输入:

Declaration:
    name = ID "=" brCon=BracketContent
;

    BracketContent:
        decCon=DecContent (comp+=COMPARATOR content+=DecContent)*
    ;

        DecContent:
            (neg=("!"|"not"))? singleContent=VarContent (op+=OPERATOR nextCon+=VarContent)*
        ;

我的验证看起来像是:

  @Check
  def checkNoCycleInHierarchy(Declaration dec) {
    if(dec.decCon.singleContent.reference == null) {
        return
    }

    var names = newArrayList

    var con = dec.decCon.singleContent

    while(con.reference != null) {
        con = getThatReference(con).singleContent

        if(names.contains(getParentName(con))) {
            val errorMsg = "Cycle in hierarchy!"
            error(errorMsg, 
                SQFPackage.eINSTANCE.bracketContent_DecCon,
                CYCLE_IN_HIERARCHY)

            return
        }

        names.add(getParentName(con))
    }
  }

但是,当我使用testCaseit测试此验证时,会返回一条错误消息:

Expected ERROR 'raven.sqf.CycleInHierarchy' on Declaration at [-1:-1] but got
ERROR (org.eclipse.emf.ecore.impl.EClassImpl@5a7fe64f (name: Declaration) (instanceClassName: null) (abstract: false, interface: false).0) 'Error executing EValidator', offset null, length null
ERROR (org.eclipse.emf.ecore.impl.EClassImpl@5a7fe64f (name: Declaration) (instanceClassName: null) (abstract: false, interface: false).0) 'Error executing EValidator', offset null, length null

我无法弄清楚它有什么问题,所以我希望你们中的某个人可能会有一个想法。

问候Krzmbrzl

2 个答案:

答案 0 :(得分:1)

您的测试实用程序会告诉您验证程序未产生预期的验证错误(" CycleInHierarchy")。 相反,验证器产生错误"错误执行EValidator"。 这意味着在执行验证程序时抛出异常。

答案 1 :(得分:0)

原来这是一个内部错误......我仍然不确定出了什么问题,但我已经重写了我的验证方法,现在它按预期工作了。
现在该方法如下所示:

enter code here@Check
  def checkNoCycleInHierarchy(Declaration dec) {
    if(dec.varContent.reference == null) {
        //proceed only if there is a reference
        return
    }

    var content = dec.varContent

    var names = newArrayList

    while(content.reference != null && !names.contains(getParentName(content))) {
        names.add(getParentName(content))
        content = content.reference.varContent

        if(names.contains(getParentName(content))) {
            val errorMsg = "Cycle in hierarchy!"
            error(errorMsg,
                SQFPackage.eINSTANCE.declaration_BrCon,
                CYCLE_IN_HIERARCHY)

                return
        }
    }
  }

我怀疑在这种情况下使用我的“getThatReference”存在问题。

问候Krzmbrzl

相关问题