@@ -88,37 +88,44 @@ trait Parsers[Parser[+_]] { self => // so inner classes may call methods of trai
88
88
89
89
/**
90
90
* Sequences two parsers, ignoring the result of the first.
91
- * We wrap the ignored half in slice, since we don't care about its result. */
91
+ * We wrap the ignored half in slice, since we don't care about its result.
92
+ */
92
93
def skipL [B ](p : Parser [Any ], p2 : => Parser [B ]): Parser [B ] =
93
94
map2(slice(p), p2)((_, b) => b)
94
95
95
96
/**
96
97
* Sequences two parsers, ignoring the result of the second.
97
- * We wrap the ignored half in slice, since we don't care about its result. */
98
+ * We wrap the ignored half in slice, since we don't care about its result.
99
+ */
98
100
def skipR [A ](p : Parser [A ], p2 : => Parser [Any ]): Parser [A ] =
99
101
map2(p, slice(p2))((a, b) => a)
100
102
101
103
def opt [A ](p : Parser [A ]): Parser [Option [A ]] =
102
104
p.map(Some (_)) or succeed(None )
103
105
104
106
/**
105
- * Parser which consumes zero or more whitespace characters. */
107
+ * Parser which consumes zero or more whitespace characters.
108
+ */
106
109
def whitespace : Parser [String ] = " \\ s*" .r
107
110
108
111
/**
109
- * Parser which consumes 1 or more digits. */
112
+ * Parser which consumes 1 or more digits.
113
+ */
110
114
def digits : Parser [String ] = " \\ d+" .r
111
115
112
116
/**
113
- * Parser which consumes reluctantly until it encounters the given string. */
117
+ * Parser which consumes reluctantly until it encounters the given string.
118
+ */
114
119
def thru (s : String ): Parser [String ] = (" .*?" + Pattern .quote(s)).r
115
120
116
121
/**
117
- * Unescaped string literals, like "foo" or "bar". */
122
+ * Unescaped string literals, like "foo" or "bar".
123
+ */
118
124
def quoted : Parser [String ] = string(" \" " ) *> thru(" \" " ).map(_.dropRight(1 ))
119
125
120
126
/**
121
- * Unescaped or escaped string literals, like "An \n important \"Quotation\"" or "bar". */
127
+ * Unescaped or escaped string literals, like "An \n important \"Quotation\"" or "bar".
128
+ */
122
129
def escapedQuoted : Parser [String ] =
123
130
// rather annoying to write, left as an exercise
124
131
// we'll just use quoted (unescaped literals) for now
@@ -132,45 +139,53 @@ trait Parsers[Parser[+_]] { self => // so inner classes may call methods of trai
132
139
token(" [-+]?([0-9]*\\ .)?[0-9]+([eE][-+]?[0-9]+)?" .r)
133
140
134
141
/**
135
- * Floating point literals, converted to a `Double`. */
142
+ * Floating point literals, converted to a `Double`.
143
+ */
136
144
def double : Parser [Double ] =
137
145
doubleString map (_.toDouble) label " double literal"
138
146
139
147
/**
140
- * Attempts `p` and strips trailing whitespace, usually used for the tokens of a grammar. */
148
+ * Attempts `p` and strips trailing whitespace, usually used for the tokens of a grammar.
149
+ */
141
150
def token [A ](p : Parser [A ]): Parser [A ] =
142
151
attempt(p) <* whitespace
143
152
144
153
/**
145
- * Zero or more repetitions of `p`, separated by `p2`, whose results are ignored. */
154
+ * Zero or more repetitions of `p`, separated by `p2`, whose results are ignored.
155
+ */
146
156
def sep [A ](
147
157
p : Parser [A ],
148
158
p2 : Parser [Any ]
149
159
): Parser [List [A ]] = // use `Parser[Any]` since don't care about result type of separator
150
160
sep1(p, p2) or succeed(List ())
151
161
152
162
/**
153
- * One or more repetitions of `p`, separated by `p2`, whose results are ignored. */
163
+ * One or more repetitions of `p`, separated by `p2`, whose results are ignored.
164
+ */
154
165
def sep1 [A ](p : Parser [A ], p2 : Parser [Any ]): Parser [List [A ]] =
155
166
map2(p, many(p2 *> p))(_ :: _)
156
167
157
168
/**
158
- * Parses a sequence of left-associative binary operators with the same precedence. */
169
+ * Parses a sequence of left-associative binary operators with the same precedence.
170
+ */
159
171
def opL [A ](p : Parser [A ])(op : Parser [(A , A ) => A ]): Parser [A ] =
160
172
map2(p, many(op ** p))((h, t) => t.foldLeft(h)((a, b) => b._1(a, b._2)))
161
173
162
174
/**
163
- * Wraps `p` in start/stop delimiters. */
175
+ * Wraps `p` in start/stop delimiters.
176
+ */
164
177
def surround [A ](start : Parser [Any ], stop : Parser [Any ])(p : => Parser [A ]) =
165
178
start *> p <* stop
166
179
167
180
/**
168
- * A parser that succeeds when given empty input. */
181
+ * A parser that succeeds when given empty input.
182
+ */
169
183
def eof : Parser [String ] =
170
184
regex(" \\ z" .r).label(" unexpected trailing characters" )
171
185
172
186
/**
173
- * The root of the grammar, expects no further input following `p`. */
187
+ * The root of the grammar, expects no further input following `p`.
188
+ */
174
189
def root [A ](p : Parser [A ]): Parser [A ] =
175
190
p <* eof
176
191
0 commit comments