Skip to content

Instantly share code, notes, and snippets.

@okomok
Created February 10, 2009 18:22
Show Gist options
  • Save okomok/61513 to your computer and use it in GitHub Desktop.
Save okomok/61513 to your computer and use it in GitHub Desktop.
old star
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