Created
October 31, 2012 15:08
-
-
Save rabitarochan/3987552 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.RegexParsers | |
case class Indent(i: Int) | |
case class Text(s: String) | |
case class Line(i: Indent, t: Text) | |
object Parser extends RegexParsers { | |
def indent: Parser[Indent] = """^\s*""".r ^^ {in => Indent(in.length)} | |
def text: Parser[Text] = """[^\n\r]*""".r ^^ {s => Text(s)} | |
def eol = """\n?\r?""".r | |
def line: Parser[Line] = indent ~ (text <~ eol) ^^ {case in ~ te => Line(in, te)} | |
def parse(s: String) = parseAll(rep(line), s) | |
} | |
object Main extends App { | |
val s = """ hoge\n fuga""" | |
// 自分の環境では、この処理は終わらない。 | |
// 戻り値は List(Line(Indent(2), Text(hoge)), Line(Indent(2), Text(fuga))) を想定しているが、認識違い?? | |
Parser.parse(s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment