2011-02-28 52 views
6

在編制本規範:「Scala是不是一個封閉類」

import org.specs.Specification 
import org.specs.matcher.extension.ParserMatchers 

class ParserSpec extends Specification with ParserMatchers { 
    type Elem = Char 

    "Vaadin DSL parser" should { 
    "parse attributes in parentheses" in { 
     DslParser.attributes must(
     succeedOn(stringReader("""(attr1="val1")""")). 
      withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String")))) 
    } 
    } 
} 

我得到以下錯誤:

ParserSpec.scala:21 
error: scala is not an enclosing class 
withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String")))) 
     ^

我不明白這裏的錯誤信息都沒有。爲什麼會出現?

Scala版本是2.8.1,規格版本是1.6.7.2。

DslParser.attributes具有類型Parser[Map[String, AttrVal]]和組合子succeedOnwithResult定義如下:當編譯器被真正想要的信號類型錯誤或類型推斷故障可發生

trait ParserMatchers extends Parsers with Matchers { 
    case class SucceedOn[T](str: Input, 
          resultMatcherOpt: Option[Matcher[T]]) extends Matcher[Parser[T]] { 
    def apply(parserBN: => Parser[T]) = { 
     val parser = parserBN 
     val parseResult = parser(str) 
     parseResult match { 
     case Success(result, remainingInput) => 
      val succParseMsg = "Parser "+parser+" succeeded on input "+str+" with result "+result 
      val okMsgBuffer = new StringBuilder(succParseMsg) 
      val koMsgBuffer = new StringBuilder(succParseMsg) 
      val cond = resultMatcherOpt match { 
      case None => 
       true 
      case Some(resultMatcher) => 
       resultMatcher(result) match { 
       case (success, okMessage, koMessage) => 
        okMsgBuffer.append(" and ").append(okMessage) 
        koMsgBuffer.append(" but ").append(koMessage) 
        success 
       } 
      } 
      (cond, okMsgBuffer.toString, koMsgBuffer.toString) 
     case _ => 
      (false, "Parser succeeded", "Parser "+parser+": "+parseResult) 
     } 
    } 

    def resultMust(resultMatcher: Matcher[T]) = this.copy(resultMatcherOpt = Some(resultMatcher)) 

    def withResult(expectedResult: T) = resultMust(beEqualTo(expectedResult)) 

    def ignoringResult = this.copy(resultMatcherOpt = None) 
    } 

    def succeedOn[T](str: Input, expectedResultOpt: Option[Matcher[T]] = None) = 
    SucceedOn(str, expectedResultOpt) 

    implicit def stringReader(str: String): Reader[Char] = new CharSequenceReader(str) 
} 

回答

8

此消息。這是一個bug(或bug家族)。

要找到問題,逐步添加顯式類型和類型參數;將複雜表達式分解成更小的子表達式。

對於獎勵積分,產生一個獨立的例子並提交一個錯誤。