1 // CW3 |
1 // CW3 |
2 |
2 |
3 import $file.lexer |
3 import scala.language.implicitConversions |
4 import lexer._ |
4 |
|
5 import $file.lexer, lexer._ |
5 |
6 |
6 case class ~[+A, +B](x: A, y: B) |
7 case class ~[+A, +B](x: A, y: B) |
7 |
8 |
8 // parser combinators |
9 // parser combinators |
9 |
10 |
10 abstract class Parser[I, T](using is: I => Seq[_]) { |
11 abstract class Parser[I, T](using is: I => Seq[?]) { |
11 def parse(in: I): Set[(T, I)] |
12 def parse(in: I): Set[(T, I)] |
12 |
13 |
13 def parse_all(in: I) : Set[T] = |
14 def parse_all(in: I) : Set[T] = |
14 for ((hd, tl) <- parse(in); |
15 for ((hd, tl) <- parse(in); |
15 if is(tl).isEmpty) yield hd |
16 if is(tl).isEmpty) yield hd |
16 } |
17 } |
17 |
18 |
18 // alternative parser |
19 // alternative parser |
19 class AltParser[I, T](p: => Parser[I, T], |
20 class AltParser[I, T](p: => Parser[I, T], |
20 q: => Parser[I, T])(using I => Seq[_]) extends Parser[I, T] { |
21 q: => Parser[I, T])(using I => Seq[?]) extends Parser[I, T] { |
21 def parse(in: I) = p.parse(in) ++ q.parse(in) |
22 def parse(in: I) = p.parse(in) ++ q.parse(in) |
22 } |
23 } |
23 |
24 |
24 // sequence parser |
25 // sequence parser |
25 class SeqParser[I, T, S](p: => Parser[I, T], |
26 class SeqParser[I, T, S](p: => Parser[I, T], |
26 q: => Parser[I, S])(using I => Seq[_]) extends Parser[I, ~[T, S]] { |
27 q: => Parser[I, S])(using I => Seq[?]) extends Parser[I, ~[T, S]] { |
27 def parse(in: I) = |
28 def parse(in: I) = |
28 for ((hd1, tl1) <- p.parse(in); |
29 for ((hd1, tl1) <- p.parse(in); |
29 (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2) |
30 (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2) |
30 } |
31 } |
31 |
32 |
32 // map parser |
33 // map parser |
33 class MapParser[I, T, S](p: => Parser[I, T], |
34 class MapParser[I, T, S](p: => Parser[I, T], |
34 f: T => S)(using I => Seq[_]) extends Parser[I, S] { |
35 f: T => S)(using I => Seq[?]) extends Parser[I, S] { |
35 def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl) |
36 def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl) |
36 } |
37 } |
37 |
38 |
38 // more convenient syntax for parser combinators |
39 // more convenient syntax for parser combinators |
39 extension [I, T](p: Parser[I, T])(using I => Seq[_]) { |
40 extension [I, T](p: Parser[I, T])(using I => Seq[?]) { |
40 def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q) |
41 def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q) |
41 def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
42 def ~[S] (q : => Parser[I, S])(using S => Seq[?]) = new SeqParser[I, T, S](p, q) |
42 def map[S](f: => T => S) = new MapParser[I, T, S](p, f) |
43 def map[S](f: => T => S) = new MapParser[I, T, S](p, f) |
43 } |
44 } |
44 |
45 |
45 /* |
46 /* |
46 // atomic parser for (particular) strings |
47 // atomic parser for (particular) strings |
69 } |
70 } |
70 } |
71 } |
71 |
72 |
72 // Implicit definitions to go from a token |
73 // Implicit definitions to go from a token |
73 // or a list of tokens to a TokenListParser |
74 // or a list of tokens to a TokenListParser |
74 implicit def token2parser(t: Token) : Parser[List[Token], Token] = |
75 given Conversion[Token, Parser[List[Token], Token]] = (t => TokenParser(t)) |
75 TokenParser(t) |
76 //implicit def token2parser(t: Token) : Parser[List[Token], Token] = |
|
77 // TokenParser(t) |
76 |
78 |
|
79 /* |
77 extension (t: Token) { |
80 extension (t: Token) { |
78 def || (q : => Parser[List[Token], Token]) = |
81 def || (q : => Parser[List[Token], Token]) = |
79 new AltParser[List[Token], Token](t, q) |
82 new AltParser[List[Token], Token](t, q) |
80 def ~[S](q : => Parser[List[Token], S]) = |
83 def ~[S](q : => Parser[List[Token], S]) = |
81 new SeqParser[List[Token], Token, S](t, q) |
84 new SeqParser[List[Token], Token, S](t, q) |
82 } |
85 } |
83 |
86 */ |
84 |
87 |
85 |
88 |
86 // Abstract Syntax Trees |
89 // Abstract Syntax Trees |
87 abstract class Stmt |
90 abstract class Stmt |
88 abstract class AExp |
91 abstract class AExp |