58 // convenience |
58 // convenience |
59 implicit def string2parser(s: String) = StringParser(s) |
59 implicit def string2parser(s: String) = StringParser(s) |
60 implicit def char2parser(c: Char) = CharParser(c) |
60 implicit def char2parser(c: Char) = CharParser(c) |
61 |
61 |
62 implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new { |
62 implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new { |
63 def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
63 def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q) |
64 def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
64 def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
65 def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
65 def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
66 } |
66 } |
67 |
67 |
68 implicit def StringOps(s: String) = new { |
68 implicit def StringOps(s: String) = new { |
69 def || (q : => Parser[String, String]) = new AltParser[String, String](s, q) |
69 def ||(q : => Parser[String, String]) = new AltParser[String, String](s, q) |
70 def || (r: String) = new AltParser[String, String](s, r) |
70 def ||(r: String) = new AltParser[String, String](s, r) |
71 def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f) |
71 def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f) |
72 def ~[S] (q : => Parser[String, S]) = |
72 def ~[S](q : => Parser[String, S]) = |
73 new SeqParser[String, String, S](s, q) |
73 new SeqParser[String, String, S](s, q) |
74 def ~ (r: String) = |
74 def ~(r: String) = |
75 new SeqParser[String, String, String](s, r) |
75 new SeqParser[String, String, String](s, r) |
76 } |
76 } |
77 |
77 |
78 // NumParserInt can now be written as _ ===> _ |
78 // NumParserInt can now be written as _ ===> _ |
79 // the first part is the parser, and the second the |
79 // the first part is the parser, and the second the |
128 (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T |
128 (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T |
129 lazy val T: Parser[String, Int] = |
129 lazy val T: Parser[String, Int] = |
130 (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F |
130 (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F |
131 lazy val F: Parser[String, Int] = |
131 lazy val F: Parser[String, Int] = |
132 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParserInt |
132 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParserInt |
133 |
|
134 /* same parser but producing a string |
|
135 lazy val E: Parser[String, String] = |
|
136 (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T |
|
137 lazy val T: Parser[String, String] = |
|
138 (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F |
|
139 lazy val F: Parser[String, String] = |
|
140 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser |
|
141 */ |
|
142 |
133 |
143 println(E.parse_all("1+3+4")) |
134 println(E.parse_all("1+3+4")) |
144 println(E.parse("1+3+4")) |
135 println(E.parse("1+3+4")) |
145 println(E.parse_all("4*2+3")) |
136 println(E.parse_all("4*2+3")) |
146 println(E.parse_all("4*(2+3)")) |
137 println(E.parse_all("4*(2+3)")) |
148 println(E.parse_all("4/2+3")) |
139 println(E.parse_all("4/2+3")) |
149 println(E.parse("1 + 2 * 3")) |
140 println(E.parse("1 + 2 * 3")) |
150 println(E.parse_all("(1+2)+3")) |
141 println(E.parse_all("(1+2)+3")) |
151 println(E.parse_all("1+2+3")) |
142 println(E.parse_all("1+2+3")) |
152 |
143 |
153 |
144 /* same parser but producing a string |
|
145 lazy val E: Parser[String, String] = |
|
146 (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T |
|
147 lazy val T: Parser[String, String] = |
|
148 (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F |
|
149 lazy val F: Parser[String, String] = |
|
150 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser |
|
151 */ |
154 |
152 |
155 // no left-recursion allowed, otherwise will loop |
153 // no left-recursion allowed, otherwise will loop |
156 lazy val EL: Parser[String, Int] = |
154 lazy val EL: Parser[String, Int] = |
157 (EL ~ "+" ~ EL ==> { case ((x, y), z) => x + z} || |
155 (EL ~ "+" ~ EL ==> { case ((x, y), z) => x + z} || |
158 EL ~ "*" ~ EL ==> { case ((x, y), z) => x * z} || |
156 EL ~ "*" ~ EL ==> { case ((x, y), z) => x * z} || |
197 |
195 |
198 One.parse("1") |
196 One.parse("1") |
199 One.parse("111") |
197 One.parse("111") |
200 |
198 |
201 (One ~ One).parse("111") |
199 (One ~ One).parse("111") |
202 (One ~ One ~ One).parse("111") |
200 (One ~ One ~ One).parse("1111") |
203 (One ~ One ~ One ~ One).parse("1111") |
201 (One ~ One ~ One ~ One).parse("1111") |
204 |
202 |
205 (One || Two).parse("111") |
203 (One || Two).parse("111") |
206 |
204 |
207 |
205 |
208 // a problem with the arithmetic expression parser -> gets |
206 // a problem with the arithmetic expression parser -> gets |
209 // slow with deep nestedness |
207 // slow with deeply nested parentheses |
210 E.parse("1") |
208 E.parse("1") |
211 E.parse("(1)") |
209 E.parse("(1)") |
212 E.parse("((1))") |
210 E.parse("((1))") |
213 E.parse("(((1)))") |
211 E.parse("(((1)))") |
214 E.parse("((((1))))") |
212 E.parse("((((1))))") |
215 E.parse("((((((1))))))") |
213 E.parse("((((((1))))))") |
216 E.parse("(((((((1)))))))") |
214 E.parse("(((((((1)))))))") |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 /* |
|
220 |
|
221 starting symbols |
|
222 tokenise/detokenise |
|
223 :paste |
|
224 pairs in sequences |
|
225 |
|
226 */ |