Created
February 10, 2009 18:22
-
-
Save okomok/61513 to your computer and use it in GitHub Desktop.
old star
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
private[mada] class StarPeg[A](p: Peg[A]) extends Reluctant[A] { | |
override def parse(v: Vector[A], start: Int, end: Int): Int = { | |
// StarUntil(p, !p) would include unneeded parsing. | |
var cur = start | |
while (cur != end) { | |
val next = p.parse(v, cur, end) | |
if (next == Peg.FAILURE) { | |
return cur | |
} | |
// Assert(cur != next) // can't assert dynamic parsers. | |
cur = next | |
} | |
cur | |
} | |
override def before(that: Peg[A]) = new StarUntilPeg(p, ~that) | |
override def until(that: Peg[A]) = new StarUntilPeg(p, that) | |
} | |
private[mada] class StarUntilPeg[A](p: Peg[A], q: Peg[A]) extends Peg[A] { | |
override def parse(v: Vector[A], start: Int, end: Int): Int = { | |
var cur = start | |
var next = q.parse(v, cur, end) | |
while (next == Peg.FAILURE) { | |
next = p.parse(v, cur, end) | |
if (next == Peg.FAILURE) { | |
return Peg.FAILURE | |
} | |
cur = next | |
next = q.parse(v, cur, end) | |
} | |
next | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment