equal
deleted
inserted
replaced
95 |
95 |
96 // NumParserInt transforms a "string integer" into a propper Int |
96 // NumParserInt transforms a "string integer" into a propper Int |
97 // (needs "new" because MapParser is not a case class) |
97 // (needs "new" because MapParser is not a case class) |
98 |
98 |
99 val NumParserInt = MapParser(NumParser, (s: String) => s.toInt) |
99 val NumParserInt = MapParser(NumParser, (s: String) => s.toInt) |
100 NumParserInt.parse("123abc") |
100 NumParserInt.parse("a123abc") |
101 |
101 |
102 // the following string interpolation allows us to write |
102 // the following string interpolation allows us to write |
103 // StrParser(_some_string_) more conveniently as |
103 // StrParser(_some_string_) more conveniently as |
104 // |
104 // |
105 // p"<_some_string_>" |
105 // p"<_some_string_>" |
137 |
137 |
138 |
138 |
139 |
139 |
140 // A parser for palindromes (just returns them as string) |
140 // A parser for palindromes (just returns them as string) |
141 // since the parser is recursive it needs to be lazy |
141 // since the parser is recursive it needs to be lazy |
|
142 val foo = foo + foo |
|
143 |
|
144 lazy val Foo : Parser[String, String] = p"a" || p"b" || p"" |
|
145 |
|
146 |
|
147 lazy val Pal : Parser[String, String] = { |
|
148 (p"a" ~ Pal ~ p"a") || |
|
149 (p"b" ~ Pal ~ p"b") || |
|
150 p"a" || p"b" || p"" |
|
151 } |
|
152 |
142 lazy val Pal : Parser[String, String] = { |
153 lazy val Pal : Parser[String, String] = { |
143 (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } || |
154 (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } || |
144 (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } || |
155 (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } || |
145 p"a" || p"b" || p"" |
156 p"a" || p"b" || p"" |
146 } |
157 } |
147 |
158 |
148 // examples |
159 // examples |
149 Pal.parse("abaaba") |
160 Pal.parse_all("abaaba") |
150 Pal.parse("abacaaba") |
161 Pal.parse("abacaaba") |
151 |
162 |
152 println("Palindrome: " + Pal.parse_all("abaaaba")) |
163 println("Palindrome: " + Pal.parse_all("abaaaba")) |
153 |
164 |
154 // A parser for wellnested parentheses |
165 // A parser for wellnested parentheses |
176 (F ~ p"*" ~ T).map{ case ((x, _), z) => x * z } || F } |
187 (F ~ p"*" ~ T).map{ case ((x, _), z) => x * z } || F } |
177 lazy val F: Parser[String, Int] = { |
188 lazy val F: Parser[String, Int] = { |
178 (p"(" ~ E ~ p")").map{ case ((_, y), _) => y } || NumParserInt } |
189 (p"(" ~ E ~ p")").map{ case ((_, y), _) => y } || NumParserInt } |
179 |
190 |
180 println(E.parse_all("2*2*2")) |
191 println(E.parse_all("2*2*2")) |
|
192 println(E.parse_all("5+3+4")) |
181 println(E.parse_all("1+3+4")) |
193 println(E.parse_all("1+3+4")) |
182 println(E.parse("1+3+4")) |
|
183 println(E.parse_all("4*2+3")) |
194 println(E.parse_all("4*2+3")) |
184 println(E.parse_all("4*(2+3)")) |
195 println(E.parse_all("4*(2+3)")) |
185 println(E.parse_all("(4)*(((2+3)))")) |
196 println(E.parse_all("(4)*(((2+3)))")) |
186 println(E.parse_all("4/2+3")) |
197 println(E.parse_all("4/2+3")) |
187 println(E.parse("1 + 2 * 3")) |
198 println(E.parse("1 + 2 * 3")) |