matcher.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 14 Nov 2012 08:46:00 +0000
changeset 63 dff4b062a8a9
parent 62 5988e44ea048
child 64 2d625418c011
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// regular expressions including NOT
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
abstract class Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case object NULL extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case object EMPTY extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class CHAR(c: Char) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case class STAR(r: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case class NOT(r: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// some convenience for typing in regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def charlist2rexp(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case Nil => EMPTY
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// nullable function: tests whether the regular 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// expression can recognise the empty string
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def nullable (r: Rexp) : Boolean = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case NULL => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case EMPTY => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case CHAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  case STAR(_) => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  case NOT(r) => !(nullable(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
// tests whether a regular expression 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
// cannot recognise more
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
def no_more (r: Rexp) : Boolean = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case NULL => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  case EMPTY => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  case CHAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case ALT(r1, r2) => no_more(r1) && no_more(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  case STAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  case NOT(r) => !(no_more(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// derivative of a regular expression w.r.t. a character
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
def der (c: Char, r: Rexp) : Rexp = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  case NULL => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  case EMPTY => NULL  case CHAR(d) => if (c == d) EMPTY else NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  case SEQ(r1, r2) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    else SEQ(der(c, r1), r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  case NOT(r) => NOT(der (c, r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
// regular expression for specifying 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
// ranges of characters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def RANGE(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case Nil => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case c::s => ALT(CHAR(c), RANGE(s))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
// one or more
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
def PLUS(r: Rexp) = SEQ(r, STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
type Rule[T] = (Rexp, List[Char] => T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
def error (s: String) = throw new IllegalArgumentException ("Cannot tokenize: " + s)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
def munch[T](r: Rexp, action: List[Char] => T, s: List[Char], t: List[Char]) : Option[(List[Char], T)] = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
    case Nil if (nullable(r)) => Some(Nil, action(t))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
    case Nil => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
    case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, action(t))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    case c::s if (no_more(der (c, r))) => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    case c::s => munch(der (c, r), action, s, t ::: List(c))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
def one_token[T](rs: List[Rule[T]], s: List[Char]) : (List[Char], T) = {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
 val somes = rs.map { (r) => munch(r._1, r._2, s, Nil) } .flatten
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
 if (somes == Nil) error(s.mkString) else (somes sortBy (_._1.length) head)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
def tokenize[T](rs: List[Rule[T]], s: List[Char]) : List[T] = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  case Nil => Nil
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  case _ => one_token(rs, s) match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    case (rest, token) => token :: tokenize(rs, rest) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100