author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Tue, 19 Nov 2013 23:38:49 +0000 | |
changeset 196 | 7182786d9c68 |
parent 185 | ea8b94d4755e |
child 360 | c6c574d2ca0c |
permissions | -rw-r--r-- |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
import scala.language.implicitConversions |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
import scala.language.reflectiveCalls |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
abstract class Parser[I <% Seq[_], T] { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
def parse(ts: I): Set[(T, I)] |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
178
diff
changeset
|
7 |
def parse_all(ts: I) : Set[T] = |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, (T, S)] { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
def parse(sb: I) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
for ((head1, tail1) <- p.parse(sb); |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
(head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
class AltParser[I <% Seq[_], T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
def parse(sb: I) = p.parse(sb) ++ q.parse(sb) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
def parse(sb: I) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
for ((head, tail) <- p.parse(sb)) yield (f(head), tail) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
|
183
b17eff695c7f
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
178
diff
changeset
|
26 |
// atomic parsers |
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
27 |
case class CharParser(c: Char) extends Parser[String, Char] { |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
28 |
def parse(sb: String) = |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
29 |
if (sb.head == c) Set((c, sb.tail)) else Set() |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
30 |
} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
31 |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
case class StringParser(s: String) extends Parser[String, String] { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
def parse(sb: String) = { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
val (prefix, suffix) = sb.splitAt(s.length) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
if (prefix == s) Set((prefix, suffix)) else Set() |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
case object NumParser extends Parser[String, String] { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
val reg = "[0-9]+".r |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
def parse(sb: String) = reg.findPrefixOf(sb) match { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
case None => Set() |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
case Some(s) => Set(sb.splitAt(s.length)) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
implicit def string2parser(s : String) = StringParser(s) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
implicit def StringOps(s: String) = new { |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
def || (q : => Parser[String, String]) = new AltParser[String, String](s, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
def || (r: String) = new AltParser[String, String](s, r) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
def ~[S] (q : => Parser[String, S]) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
new SeqParser[String, String, S](s, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
def ~ (r: String) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
new SeqParser[String, String, String](s, r) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
lazy val Pal : Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
(("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } || |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
|
185
ea8b94d4755e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
73 |
println("Palindrom" + Pal.parse_all("ababbaba")) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
lazy val P : Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
"(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || "" |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
P.parse_all("(((()()))())") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
P.parse_all("(((()()))()))") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
P.parse_all(")(") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
lazy val E: Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
(F ~ "*" ~ F) ==> { case ((x, y), z) => x + y + z } || F |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
lazy val F: Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
((T ~ "+" ~ T) ==> { case ((x, y), z) => x + y + z } || |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
(T ~ "-" ~ T) ==> { case ((x, y), z) => x + y + z } || T) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
lazy val T: Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
("(" ~ E ~ ")") ==> { case ((x, y), z) => x + y + z } || NumParser |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
|
185
ea8b94d4755e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
91 |
println(E.parse_all("1*2+3")) |
ea8b94d4755e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
92 |
println(E.parse_all("1+2*3")) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
println(E.parse_all("1+2+3")) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
// non-ambiguous vs ambiguous |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
lazy val U : Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
("1" ~ U) ==> { case (x, y) => x + y } || "" |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
U.parse_all("1" * 100 + "0") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
lazy val S : Parser[String, String] = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || "" |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
S.parse_all("1" * 15) |