html-tokens.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 19 Nov 2012 14:18:42 +0000
changeset 64 2d625418c011
permissions -rw-r--r--
added everything
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
import Console._
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
// regular expressions including NOT
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Rexp {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  def ~ (right : Rexp) = SEQ(this, right)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
  def || (right : Rexp) = ALT(this, right)
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
case object NULL extends Rexp
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case object EMPTY extends Rexp
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class CHAR(c: Char) extends Rexp
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class STAR(r: Rexp) extends Rexp
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
case class NOT(r: Rexp) extends Rexp
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
// some convenience for typing in regular expressions
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
def charlist2rexp(s : List[Char]) : Rexp = s match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  case Nil => EMPTY
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  case c::Nil => CHAR(c)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
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
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
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
// nullable function: tests whether the regular 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
// expression can recognise the empty string
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
def nullable (r: Rexp) : Boolean = r match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  case NULL => false
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  case EMPTY => true
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  case CHAR(_) => false
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  case STAR(_) => true
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  case NOT(r) => !(nullable(r))
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
// tests whether a regular expression 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
// cannot recognise more
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
def no_more (r: Rexp) : Boolean = r match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  case NULL => true
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  case EMPTY => false
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  case CHAR(_) => false
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  case ALT(r1, r2) => no_more(r1) && no_more(r2)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  case STAR(_) => false
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  case NOT(r) => !(no_more(r))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
// derivative of a regular expression w.r.t. a character
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
def der (c: Char, r: Rexp) : Rexp = r match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case NULL => NULL
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case EMPTY => NULL
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case CHAR(d) => if (c == d) EMPTY else NULL
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  case SEQ(r1, r2) => 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
    else SEQ(der(c, r1), r2)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  case STAR(r) => SEQ(der(c, r), STAR(r))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case NOT(r) => NOT(der (c, r))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
def error (s: String) = "Could not lex " + s
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
def munch(r: Rexp, s: List[Char], t: List[Char]) : Option[(List[Char], List[Char])] = {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  //println("Look at" + s);
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  s match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    case Nil if (nullable(r)) => Some(Nil, t)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
    case Nil => None
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
    case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, t)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
    case c::s if (no_more(der (c, r))) => None
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
    case c::s => munch(der (c, r), s, t ::: List(c))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
  }
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
def one_string (regs: List[Rexp], s: List[Char]) : Either[(List[Char], List[Char]), String] = {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
 val somes = regs.map { munch(_, s, Nil) } .flatten
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
 if (somes == Nil) Right(error(s.mkString)) else Left(somes sortBy (_._1.length) head)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
def tokenize (regs: List[Rexp], s: List[Char]) : List[String] = s match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  case Nil => Nil
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  case _ => one_string(regs, s) match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
    case Left((rest, s)) => { println("tokenized: " + s.mkString) ; s.mkString :: tokenize(regs, rest) }
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
    case Right(msg) => { println(msg); sys.exit() }
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  }
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
// regular expression for specifying 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
// ranges of characters
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
def RANGE(s : List[Char]) : Rexp = s match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  case Nil => NULL
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  case c::Nil => CHAR(c)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
  case c::s => CHAR(c) || RANGE(s)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
//one or more
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
def PLUS(r: Rexp) = r ~ STAR(r)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
<font color="red">
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
//some regular expressions
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
val COLOUR = "BLACK" || "BLUE" || "CYAN" || "GREEN" || "MAGENTA" || "RED" || "WHITE" || "YELLOW"
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
//BOLD || BLINK
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
//INVISIBLE
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
//RESET
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
//REVERSED
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
//UNDERLINED
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
println(RED + BOLD + "test")
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
println(RESET)