수색…


비고

ParseResult Cases

ParseResult 에는 세 가지 ParseResult 이 있습니다.

  • Match의 시작 부분과 일치 할 다음 문자에 대한 마커가있는 Success.
  • 실패, 매치가 시도 된 곳의 마커가 표시됩니다. 이 경우 구문 분석기는 해당 위치로 되돌아 가고 구문 분석이 계속 진행됩니다.
  • 오류, 구문 분석을 중지합니다. 역 추적 또는 추가 구문 분석이 발생하지 않습니다.

기본 예제

import scala.util.parsing.combinator._

class SimpleParser extends RegexParsers {
  // Define a grammar rule, turn it into a regex, and apply it the input.
  def word: Parser[String] = """[A-Z][a-z]+""".r ^^ { _.toString }
}

object SimpleParser extends SimpleParser {
  val parseAlice = parse(word, "Alice went to Alamo Square.")
  val parseBarb = parse(word, "barb went Upside Down.")
}

//Successfully finds a match
println(SimpleParser.parseAlice)
//Fails to find a match
println(SimpleParser.parseBarb)

출력은 다음과 같습니다.

[1.6] parsed: Alice
res0: Unit = ()

[1.1] failure: string matching regex `[A-Z][a-z]+' expected but `b' found

barb went Upside Down.
^

Alice 예제의 [1.6] 은 일치 시작이 위치 1 에 있고 일치 할 나머지 주먹 문자가 위치 6 에서 시작 함을 나타냅니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow