Created
July 29, 2014 02:46
-
-
Save mather/7766f72099c4a9820295 to your computer and use it in GitHub Desktop.
IgnoreCaseなパーサコンビネータ ref: http://qiita.com/mather314/items/0535f237f007c13942dc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait SampleParsers extends RegexParsers { | |
| /** 文字列に".i"を付けると大文字小文字を区別せずマッチさせるようにする拡張 */ | |
| implicit class IgnoreCaseString(s: String) { | |
| def i: Parser[String] = ("""(?i)\Q""" + s + """\E""").r | |
| } | |
| def keyword: Parser[String] = "keyword:".i | |
| def value: Parser[String] = "[a-zA-Z0-9]+".r | |
| def parameter: Parser[(String,String)] = keyword ~ value ^^ { case k~v => (k,v) } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.specs2.mutable.Specification | |
| import org.specs2.matcher.ParserMatchers | |
| class SampleParsersSpec extends Specification with ParserMatchers { | |
| "keyword" should { | |
| "match with ignore-case" in { | |
| keyword must succeedOn("KEYWORD:") | |
| keyword must succeedOn("Keyword:") | |
| keyword must succeedOn("keyWord:") | |
| } | |
| "fail in other pattern" in { | |
| keyword must failOn("key-word:") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment