progs/comb1-test.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 26 Sep 2019 11:00:17 +0100
changeset 633 e4889da2fe29
parent 629 1b718d6065c2
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import scala.language.implicitConversions
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
/* Note, in the lectures I did not show the implicit type consraint
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
 * I => Seq[_], which means that the input type 'I' needs to be
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
 * a sequence. */
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
type IsSeq[A] = A => Seq[_]
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
abstract class Parser[I : IsSeq, T] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  def parse(ts: I): Set[(T, I)]
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  def parse_all(ts: I) : Set[T] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    for ((head, tail) <- parse(ts); 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
        if (tail.isEmpty)) yield head
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// convenience for matching later on
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case class ~[+A, +B](_1: A, _2: B)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
                                 q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  def parse(sb: I) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
    for ((head1, tail1) <- p.parse(sb); 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
class AltParser[I : IsSeq, T](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
                              q: => Parser[I, T]) extends Parser[I, T] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
                                 f: T => S) extends Parser[I, S] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  def parse(sb: I) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// atomic parsers for characters, numbers and strings
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
case class CharParser(c: Char) extends Parser[String, Char] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  def parse(sb: String) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
    if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
import scala.util.matching.Regex
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
case class RegexParser(reg: Regex) extends Parser[String, String] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    case None => Set()
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
    case Some(m) => Set((m.matched, m.after.toString))  
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  }
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
val NumParser = RegexParser("[0-9]+".r)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
// NumParserInt2 transforms a "string integer" into an Int;
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// needs new, because FunParser is not a case class
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
val NumParserInt2 = new FunParser(NumParser, (s: String) => s.toInt)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
// convenience
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
implicit def string2parser(s: String) = StringParser(s)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
implicit def char2parser(c: Char) = CharParser(c)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
implicit def StringOps(s: String) = new {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  def || (q : => Parser[String, String]) = new AltParser[String, String](s, q)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  def || (r: String) = new AltParser[String, String](s, r)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  def ~[S] (q : => Parser[String, S]) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
    new SeqParser[String, String, S](s, q)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  def ~ (r: String) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
    new SeqParser[String, String, String](s, r)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
// NumParserInt can now be written as
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
val NumParserInt = NumParser ==> (s => s.toInt)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
lazy val Pal : Parser[String, String] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  (("a" ~ Pal ~ "a") ==> { case x ~ y ~ z => x + y + z } ||
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
   ("b" ~ Pal ~ "b") ==> { case x ~ y ~ z => x + y + z } || "a" || "b" || "")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
Pal.parse_all("abaaaba")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
Pal.parse("abaaaba")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
println("Palindrome: " + Pal.parse_all("abaaaba"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
// well-nested parentheses parser (transforms '(' -> '{' , ')' -> '}' )
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
lazy val P : Parser[String, String] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  "(" ~ P ~ ")" ~ P ==> { case _ ~ x ~ _ ~ y => "{" + x + "}" + y } || ""
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
P.parse_all("(((()()))())")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
P.parse_all("(((()()))()))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
P.parse_all(")(")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
P.parse_all("()")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// Arithmetic Expressions (Terms and Factors)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
lazy val E: Parser[String, Int] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  (T ~ "+" ~ E) ==> { case x ~ y ~ z => x + z } ||
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  (T ~ "-" ~ E) ==> { case x ~ y ~ z => x - z } || T 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
lazy val T: Parser[String, Int] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  (F ~ "*" ~ T) ==> { case x ~ y ~ z => x * z } || F
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
lazy val F: Parser[String, Int] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  ("(" ~ E ~ ")") ==> { case x ~ y ~ z => y } || NumParserInt
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
/* same parser but producing a string
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
lazy val E: Parser[String, String] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
lazy val T: Parser[String, String] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
lazy val F: Parser[String, String] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
*/
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
println(E.parse_all("1+3+4"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
println(E.parse("1+3+4"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
println(E.parse_all("4*2+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
println(E.parse_all("4*(2+3)"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
println(E.parse_all("(4)*((2+3))"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
println(E.parse_all("4/2+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
println(E.parse("1 + 2 * 3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
println(E.parse_all("(1+2)+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
println(E.parse_all("1+2+3"))  
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
// no left-recursion allowed, otherwise will loop
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
lazy val EL: Parser[String, Int] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  (EL ~ "+" ~ EL ==> { case x ~ y ~ z => x + z} || 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
   EL ~ "*" ~ EL ==> { case x ~ y ~ z => x * z} ||
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
   "(" ~ EL ~ ")" ==> { case x ~ y ~ z => y} ||
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
   NumParserInt)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
//println(EL.parse_all("1+2+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
// non-ambiguous vs ambiguous grammars
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
// ambiguous
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
lazy val S : Parser[String, String] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
  ("1" ~ S ~ S) ==> { case x ~ y ~ z => x + y + z } || ""
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
S.parse("1" * 10)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
// non-ambiguous
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
lazy val U : Parser[String, String] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
  ("1" ~ U) ==> { case x ~ y => x + y  } || ""
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
U.parse("1" * 25)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
U.parse("11")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
U.parse("11111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
U.parse("11011")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
U.parse_all("1" * 100)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
U.parse_all("1" * 100 + "0")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
lazy val UCount : Parser[String, Int] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  ("1" ~ UCount) ==> { case x ~ y => y + 1 } || "" ==> { x => 0 }
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
UCount.parse("11111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
UCount.parse_all("11111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
// Single Character parser
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
lazy val One : Parser[String, String] = "1"
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
lazy val Two : Parser[String, String] = "2"
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
One.parse("1")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
One.parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
(One ~ One).parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
(One ~ One ~ One).parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
(One ~ One ~ One ~ One).parse("1111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
(One || Two).parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
// a problem with the parser -> gets slow with nestedness
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
E.parse("1")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
E.parse("(1)")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
E.parse("((1))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
E.parse("(((1)))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
E.parse("((((1))))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
E.parse("((((((1))))))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
E.parse("(((((((1)))))))")