Created
May 7, 2010 19:09
-
-
Save kmizu/393880 to your computer and use it in GitHub Desktop.
This file contains 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 scala.util.parsing.combinator._ | |
object Example extends RegexParsers { | |
/** String#rの型はRegexで、Regexは~なんてメソッドは当然持っていない | |
* (~はParserのメソッドで、レシーバのParserと引数のParserを連結した新しい | |
* Parserを生成するメソッド) | |
* だが、Regex → Parser[String]へのimplicit conversionがRegexParsersで | |
* 定義されているため、Regexに~を呼び出すと、Parser[String]に自動変換される。 | |
* もちろん、引数に来たRegexもParser[String]に変換される。 | |
* RubyでやるならRegex相当クラスを拡張して、~メソッドを直接定義してやる、などの方法が | |
* 考えられるが、グローバルにクラスを汚染することになるし、それだと引数の位置にRegexが | |
* 来た場合にどうしようも無い。Parser#~を再定義して、引数にRegexも取れるようにすれば | |
* いけるかもしれないけど、無駄に労力がかかる。 | |
*/ | |
lazy val field: Parser[(String, String)] = | |
"""[a-zA-Z_][a-zA-Z0-9_]*""".r ~ """=""" ~ """[0-9]+""".r ^^ { | |
case name ~ _ ~ value => (name, value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment