progs/parser3.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 22 Feb 2016 22:09:31 +0000
changeset 397 cf3ca219c727
parent 93 4794759139ea
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
     1
package object parser {
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
     3
// parser combinators 
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
     4
// with input type I and return type T
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
     5
//
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
     6
// needs to be compiled with scalac parser3.scala
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
abstract class Parser[I <% Seq[_], T] {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  def parse(ts: I): Set[(T, I)]
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
  def parse_all(ts: I) : Set[T] =
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    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
    13
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    14
  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
    15
    case t::Nil => t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    16
    case _ => { println ("Parse Error") ; sys.exit(-1) }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    17
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    18
    
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  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
    20
  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
    21
  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
    22
  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
    23
  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
    24
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
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
    27
  def parse(sb: I) = 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    for ((head1, tail1) <- p.parse(sb); 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
         (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
    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 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
    33
  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
    34
}
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
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
    37
  def parse(sb: I) = 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
    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
    39
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
    41
}