You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/case-studies/parser/error-handling.md
+2-8Lines changed: 2 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,11 +129,8 @@ Implement a parser for digits using the regular expression `"[0-9]"` to match a
129
129
~~~scala
130
130
packageunderscore.parser
131
131
132
-
objectNumericParser {
133
-
132
+
objectNumericParser:
134
133
valdigits=Parser.regex("[0-9]").+
135
-
136
-
}
137
134
~~~
138
135
</div>
139
136
@@ -184,14 +181,11 @@ If `add` was a normal method we'ld only print `"Hi"` once.
184
181
If we make the parameter of `~` and `|` call-by-name, our `Parser` will work. Try it and you'll see another issue---the way the grammar is written we'll stop after parsing the first number. (Try `expression.parse("123+456")` and you'll see.) The solution is to rewrite the grammar so we look for compound expressions first and we proceed left-to-right.
@@ -60,19 +56,14 @@ case class Parser(parse: String => ParseResult) {
60
56
loop("", input)
61
57
}
62
58
63
-
}
64
-
65
-
objectParser {
66
-
59
+
objectParser:
67
60
defstring(literal: String):Parser=
68
61
Parser { input =>
69
62
if(input.startsWith(literal))
70
63
ParseResult(literal, input.drop(literal.size))
71
64
else
72
65
ParseResult("", input)
73
66
}
74
-
75
-
}
76
67
~~~
77
68
78
69
What does the code do? The first thing is to look at what a `Parser` is. It is basically a wrapper around a function `String => ParseResult`. The `String` parameter is the input to parse, and returned is the result of parsing that `String`.
0 commit comments