progs/lexer/lexer.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 17 Nov 2023 20:06:43 +0000
changeset 955 47acfd7f9096
parent 947 dc31a099dc98
child 960 c7009356ddd8
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
     1
// A simple lexer inspired by work of Sulzmann $ Lu
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//==================================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// Call the test cases with 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
//   amm lexer.sc small
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//   amm lexer.sc fib
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//   amm lexer.sc loops
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
     9
//   amm lexer.sc email
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
//   amm lexer.sc all
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
// regular expressions including records
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
abstract class Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case object ZERO extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case object ONE extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case class CHAR(c: Char) extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case class STAR(r: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class RECD(x: String, r: Rexp) extends Rexp  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
                // records for extracting strings or tokens
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
// values  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
abstract class Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case object Empty extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case class Chr(c: Char) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
case class Sequ(v1: Val, v2: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
case class Left(v: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
case class Right(v: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
case class Stars(vs: List[Val]) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
case class Rec(x: String, v: Val) extends Val
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
   
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
// some convenience for typing in regular expressions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
def charlist2rexp(s : List[Char]): Rexp = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  case Nil => ONE
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  case c::Nil => CHAR(c)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
}
947
dc31a099dc98 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 945
diff changeset
    42
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
implicit def string2rexp(s : String) : Rexp = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  charlist2rexp(s.toList)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
    46
936
0b5f06539a84 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 846
diff changeset
    47
extension (s: String) {
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
    48
  def $ (r: Rexp) = RECD(s, r)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    51
extension (r: Rexp) {
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    52
  def | (s: Rexp) = ALT(r, s)
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    53
  def % = STAR(r)
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    54
  def ~ (s: Rexp) = SEQ(r, s)
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    55
}
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
    56
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
def nullable(r: Rexp) : Boolean = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case ZERO => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case ONE => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  case CHAR(_) => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
    62
  case SEQ(r1, r2) => nullable(r1) $$ nullable(r2)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
  case STAR(_) => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  case RECD(_, r1) => nullable(r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
def der(c: Char, r: Rexp) : Rexp = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case ZERO => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  case ONE => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  case CHAR(d) => if (c == d) ONE else ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  case SEQ(r1, r2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
    else SEQ(der(c, r1), r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
  case STAR(r) => SEQ(der(c, r), STAR(r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
  case RECD(_, r1) => der(c, r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    79
// extracts a string from a value
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
def flatten(v: Val) : String = v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  case Empty => ""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  case Chr(c) => c.toString
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  case Left(v) => flatten(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  case Right(v) => flatten(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
  case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  case Stars(vs) => vs.map(flatten).mkString
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  case Rec(_, v) => flatten(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
// extracts an environment from a value;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
// used for tokenising a string
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
def env(v: Val) : List[(String, String)] = v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
  case Empty => Nil
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  case Chr(c) => Nil
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  case Left(v) => env(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  case Right(v) => env(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  case Sequ(v1, v2) => env(v1) ::: env(v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  case Stars(vs) => vs.flatMap(env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  case Rec(x, v) => (x, flatten(v))::env(v)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
// The injection and mkeps part of the lexer
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
//===========================================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
def mkeps(r: Rexp) : Val = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  case ONE => Empty
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  case ALT(r1, r2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
  case STAR(r) => Stars(Nil)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
  case RECD(x, r) => Rec(x, mkeps(r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  case (CHAR(d), Empty) => Chr(c) 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   127
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 936
diff changeset
   128
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
// some "rectification" functions for simplification
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
def F_ID(v: Val): Val = v
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  case Right(v) => Right(f2(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  case Left(v) => Left(f1(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  (v:Val) => Sequ(f1(Empty), f2(v))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  (v:Val) => Sequ(f1(v), f2(Empty))
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 800
diff changeset
   144
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
def F_ERROR(v: Val): Val = throw new Exception("error")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
// simplification
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
def simp(r: Rexp): (Rexp, Val => Val) = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  case ALT(r1, r2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
    val (r1s, f1s) = simp(r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
    val (r2s, f2s) = simp(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
    (r1s, r2s) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
      case (_, ZERO) => (r1s, F_LEFT(f1s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
    }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  case SEQ(r1, r2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
    val (r1s, f1s) = simp(r1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
    val (r2s, f2s) = simp(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
    (r1s, r2s) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
      case (ZERO, _) => (ZERO, F_ERROR)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
      case (_, ZERO) => (ZERO, F_ERROR)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
  case r => (r, F_ID)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
// lexing functions including simplification
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  case Nil => if (nullable(r)) mkeps(r) else 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
    { throw new Exception("lexing error") } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
  case c::cs => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
    val (r_simp, f_simp) = simp(der(c, r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
def lexing_simp(r: Rexp, s: String) = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
  env(lex_simp(r, s.toList))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
// The Lexing Rules for the WHILE Language
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
def PLUS(r: Rexp) = r ~ r.%
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
def Range(s : List[Char]) : Rexp = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
  case Nil => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
  case c::Nil => CHAR(c)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
  case c::s => ALT(CHAR(c), Range(s))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
def RANGE(s: String) = Range(s.toList)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
800
9eea6a801e10 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 793
diff changeset
   198
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
val DIGIT = RANGE("0123456789")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
val ID = SYM ~ (SYM | DIGIT).% 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
val NUM = PLUS(DIGIT)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
val SEMI: Rexp = ";"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
846
3a535de22816 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 827
diff changeset
   205
val WHITESPACE = PLUS(" " | "\n" | "\t" | "\r")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
val RPAREN: Rexp = "{"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
val LPAREN: Rexp = "}"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   211
val WHILE_REGS = (("k" $ KEYWORD) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   212
                  ("i" $ ID) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   213
                  ("o" $ OP) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   214
                  ("n" $ NUM) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   215
                  ("s" $ SEMI) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   216
                  ("str" $ STRING) |
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   217
                  ("p" $ (LPAREN | RPAREN)) | 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 947
diff changeset
   218
                  ("w" $ WHITESPACE)).%
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
// Two Simple While Tests
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
//========================
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   224
@arg(doc = "small tests")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
def small() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
  val prog0 = """read n"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
  println(s"test: $prog0")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
  println(lexing_simp(WHILE_REGS, prog0))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
  val prog1 = """read  n; write n"""  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
  println(s"test: $prog1")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
  println(lexing_simp(WHILE_REGS, prog1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
// Bigger Tests
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
//==============
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
// escapes strings and prints them out as "", "\n" and so on
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
def esc(raw: String): String = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
  import scala.reflect.runtime.universe._
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
  Literal(Constant(raw)).toString
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
def escape(tks: List[(String, String)]) =
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
  tks.map{ case (s1, s2) => (s1, esc(s2))}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
val prog2 = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
write "Fib";
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
read n;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
minus1 := 0;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
minus2 := 1;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
while n > 0 do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
  temp := minus2;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
  minus2 := minus1 + minus2;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
  minus1 := temp;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
  n := n - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
};
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
write "Result";
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
write minus2
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   265
@arg(doc = "Fibonacci test")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
def fib() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
  println("lexing fib program")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
  println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n"))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273
val prog3 = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   274
start := 1000;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   275
x := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   276
y := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   277
z := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   278
while 0 < x do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   279
 while 0 < y do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   280
  while 0 < z do {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   281
    z := z - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   282
  };
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   283
  z := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   284
  y := y - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   285
 };     
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   286
 y := start;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   287
 x := x - 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   288
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   289
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   290
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   291
@arg(doc = "Loops test")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   292
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   293
def loops() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   294
  println("lexing Loops")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   295
  println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n"))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   296
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   297
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   298
@arg(doc = "Email Test")
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   299
@main
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   300
def email() = {
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   301
  val lower = "abcdefghijklmnopqrstuvwxyz"
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   302
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   303
  val NAME = RECD("name", PLUS(RANGE(lower ++ "_.-")))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   304
  val DOMAIN = RECD("domain", PLUS(RANGE(lower ++ "-")))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   305
  val RE = RANGE(lower ++ ".")
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   306
  val TOPLEVEL = RECD("top", (RE ~ RE) |
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   307
                             (RE ~ RE ~ RE) | 
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   308
                             (RE ~ RE ~ RE ~ RE) | 
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   309
                             (RE ~ RE ~ RE ~ RE ~ RE) |
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   310
                             (RE ~ RE ~ RE ~ RE ~ RE ~ RE))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   311
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   312
  val EMAIL = NAME ~ "@" ~ DOMAIN ~ "." ~ TOPLEVEL
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   313
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   314
  println(lexing_simp(EMAIL, "christian.urban@kcl.ac.uk"))
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   315
}
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   316
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   317
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   318
@arg(doc = "All tests.")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   319
@main
793
46cc69622a56 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 792
diff changeset
   320
def all() = { small(); fib() ; loops() ; email() } 
792
34132a854d03 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 757
diff changeset
   321
34132a854d03 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 757
diff changeset
   322
34132a854d03 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 757
diff changeset
   323
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   324
/*
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   325
// Ammonite still provides 
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   326
//
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   327
//    scala.reflect.runtime.universe._
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   328
//
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   329
// which has been removed in Scala 3.
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   330
//
945
5070392a1bd7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 944
diff changeset
   331
// for escaping strings in Scala 3 use this equivalent code
5070392a1bd7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 944
diff changeset
   332
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   333
import scala.quoted._
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   334
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   335
def escapeImpl(raw: Expr[String])(using Quotes): Expr[String] = {
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   336
  import quotes.reflect.*
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   337
  Literal(StringConstant(raw.show)).asExprOf[String]
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   338
}
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   339
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   340
inline def esc(inline raw: String): String = ${escapeImpl('{raw})}
827
67c8a6e6a305 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   341
944
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   342
val s = "abc\ndef"       // not escaped
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   343
val e = esc("abc\ndef")  // escaped
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   344
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   345
println(s)
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   346
println(e)
f5d2453c5640 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   347
*/