parser3.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 28 Nov 2012 07:03:29 +0000
changeset 83 bd61b03b9eaa
parent 76 373cf55a3ca5
child 85 1a4065f965fb
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
// parser combinators with input type I and return type T
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Parser[I <% Seq[_], T] {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  def parse(ts: I): Set[(T, I)]
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
  def parse_all(ts: I) : Set[T] =
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
    for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    10
  def parse_single(ts: I) : T = parse_all(ts).toList match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    11
    case t::Nil => t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    12
    case _ => { println ("Parse Error") ; sys.exit(-1) }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    13
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    14
    
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
  def || (right : => Parser[I, T]) : Parser[I, T] = new AltParser(this, right)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  def ==>[S] (f: => T => S) : Parser [I, S] = new FunParser(this, f)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  def ~[S] (right : => Parser[I, S]) : Parser[I, (T, S)] = new SeqParser(this, right)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def ~>[S] (right : => Parser[I, S]) : Parser[I, S] = this ~ right ==> (_._2)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  def <~[S] (right : => Parser[I, S]) : Parser[I, T] = this ~ right ==> (_._1)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, (T, S)] {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  def parse(sb: I) = 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
    for ((head1, tail1) <- p.parse(sb); 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
class AltParser[I <% Seq[_], T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  def parse(sb: I) = 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37