Skip to content

Instantly share code, notes, and snippets.

@okomok
okomok / gist:54933
Created January 30, 2009 04:08
Triples
package mada.vec
/*
private[mada] object TriplesVector {
def apply[A](from: Vector[Vector.Triple[A]]): Vector[Vector[A]] = from match {
case from: VectorTriples[_] => from.from // conversion fusion
case _ => new TriplesVector(from)
}
}
@okomok
okomok / gist:54934
Created January 30, 2009 04:09
Triple
package mada.vec
private[mada] object TripleVector {
def apply[A](v: Vector.Triple[A]): Vector[A] = v._1(v._2, v._3)
}
private[mada] object VectorTriple {
def apply[A](v: Vector[A]): Vector.Triple[A] = (v, v.start, v.end)
@okomok
okomok / gist:54935
Created January 30, 2009 04:09
LRule
// Copyright Shunsuke Sogame 2008-2009.
// Distributed under the terms of an MIT-style license.
// See: "Packrat Parsers Can Support Left Recursion"
package mada.peg
@okomok
okomok / gist:54936
Created January 30, 2009 04:12
LRule test
// Copyright Shunsuke Sogame 2008-2009.
// Distributed under the terms of an MIT-style license.
// See: "Packrat Parsers Can Support Left Recursion"
package madatest.peg
@okomok
okomok / gist:55884
Created February 1, 2009 15:49
fold using reduce
private[mada] object Fold {
def apply[A](v: Vector[A], z: A, op: (A, A) => A): A = {
(Vector.single(z) ++ v).reduce(op)
}
}
private[mada] object Folder {
def apply[A](v: Vector[A], z: A, op: (A, A) => A): Vector[A] = {
(Vector.single(z) ++ v).reducer(op)
@okomok
okomok / gist:58241
Created February 4, 2009 18:08
rejected memoize
private[mada] object Memoize {
def apply[A](p: Peg[A]): Peg[A] = new MemoizePeg(p)
}
private[mada] class MemoizePeg[A](override val self: Peg[A]) extends PegProxy[A] {
val memoTable = new scala.collection.jcl.HashMap[TripleKey[A], Int]
override def parse(v: Vector[A], start: Int, end: Int) = {
val key = new TripleKey(v, start, end)
@okomok
okomok / gist:58249
Created February 4, 2009 18:17
rejected triple key
private[mada] case class TripleKey[A](v: Vector[A], start: Int, end: Int) {
override def equals(that: Any) = that match {
case that: TripleKey[_] => (v eq that.v) && (start == that.start) && (end == that.end)
case _ => false
}
override def hashCode = {
HashCode.ofRef(v) + HashCode.ofInt(start) * 7 + HashCode.ofInt(end) * 41
}
}
@okomok
okomok / gist:61513
Created February 10, 2009 18:22
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
}
@okomok
okomok / gist:61514
Created February 10, 2009 18:22
old plus
private[mada] class PlusPeg[A](p: Peg[A]) extends PegProxy[A] with Reluctant[A] {
override val self = p >> p.*
override def before(that: Peg[A]) = p >> (p.* before that)
override def until(that: Peg[A]) = p >> (p.* until that)
}
@okomok
okomok / gist:61515
Created February 10, 2009 18:22
old opt
private[mada] class OptPeg[A](p: Peg[A]) extends PegProxy[A] with Reluctant[A] {
override val self = p | Peg.eps[A]
override def before(that: Peg[A]) = (p >> ~that) | ~that
override def until(that: Peg[A]) = (p >> that) | that
}