1
1
-- | Primitive parsers for working with an input stream of type `String`.
2
2
-- |
3
- -- | All of these primitive parsers will consume their input when they succeed.
3
+ -- | All of these primitive parsers will consume when they succeed.
4
4
-- |
5
- -- | All of these primitive parsers will consume no input when they
5
+ -- | All of these primitive parsers will not consume when they
6
6
-- | fail.
7
7
-- |
8
8
-- | The behavior of these primitive parsers is based on the behavior of the
@@ -204,28 +204,42 @@ match p = do
204
204
-- boundary.
205
205
pure $ Tuple (SCU .take (SCU .length input1 - SCU .length input2) input1) x
206
206
207
- -- | Compile a regular expression string into a regular expression parser.
207
+ -- | Compile a regular expression `String` into a regular expression parser.
208
208
-- |
209
- -- | This function will use the `Data.String.Regex.regex` function to compile and return a parser which can be used
209
+ -- | This function will use the `Data.String.Regex.regex` function to compile
210
+ -- | and return a parser which can be used
210
211
-- | in a `ParserT String m` monad.
212
+ -- | If compilation fails then this function will return `Left` a compilation
213
+ -- | error message.
214
+ -- |
215
+ -- | The returned parser will try to match the regular expression pattern once,
216
+ -- | starting at the current parser position. On success, it will return
217
+ -- | the matched substring.
218
+ -- |
219
+ -- | If the RegExp `String` is constant then we can assume that compilation will
220
+ -- | always succeed and `unsafeCrashWith` if it doesn’t. If we dynamically
221
+ -- | generate the RegExp `String` at runtime then we should handle the
222
+ -- | case where compilation of the RegExp fails.
223
+ -- |
224
+ -- | This function should be called outside the context of a `ParserT String m`
225
+ -- | monad for two reasons:
226
+ -- | 1. If we call this function inside of the `ParserT String m` monad and
227
+ -- | then `fail` the parse when the compilation fails,
228
+ -- | then that could be confusing because a parser failure is supposed to
229
+ -- | indicate an invalid input string.
230
+ -- | If the compilation failure occurs in an `alt` then the compilation
231
+ -- | failure might not be reported at all and instead
232
+ -- | the input string would be parsed incorrectly.
233
+ -- | 2. Compiling a RegExp is expensive and it’s better to do it
234
+ -- | once in advance and then use the compiled RegExp many times than
235
+ -- | to compile the RegExp many times during the parse.
211
236
-- |
212
- -- | This parser will try to match the regular expression pattern starting
213
- -- | at the current parser position. On success, it will return the matched
214
- -- | substring .
237
+ -- | This parser may be useful for quickly consuming a large section of the
238
+ -- | input `String`, because in a JavaScript runtime environment a compiled
239
+ -- | RegExp is a lot faster than a monadic parser built from parsing primitives .
215
240
-- |
216
241
-- | [*MDN Regular Expressions Cheatsheet*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet)
217
242
-- |
218
- -- | This function should be called outside the context of a `ParserT String m` monad, because this function might
219
- -- | fail with a `Left` RegExp compilation error message.
220
- -- | If you call this function inside of the `ParserT String m` monad and then `fail` the parse when the compilation fails,
221
- -- | then that could be confusing because a parser failure is supposed to indicate an invalid input string.
222
- -- | If the compilation failure occurs in an `alt` then the compilation failure might not be reported at all and instead
223
- -- | the input string would be parsed incorrectly.
224
- -- |
225
- -- | This parser may be useful for quickly consuming a large section of the
226
- -- | input `String`, because in a JavaScript runtime environment the RegExp
227
- -- | runtime is a lot faster than primitive parsers.
228
- -- |
229
243
-- | #### Example
230
244
-- |
231
245
-- | This example shows how to compile and run the `xMany` parser which will
@@ -246,7 +260,8 @@ match p = do
246
260
-- | regex "x*" (dotAll <> ignoreCase)
247
261
-- | ```
248
262
-- |
249
- -- | The `dotAll`, `unicode`, and `ignoreCase` flags might make sense for a `regex` parser. The other flags will
263
+ -- | The `dotAll`, `unicode`, and `ignoreCase` flags might make sense for
264
+ -- | a `regex` parser. The other flags will
250
265
-- | probably cause surprising behavior and you should avoid them.
251
266
-- |
252
267
-- | [*MDN Advanced searching with flags*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags)
0 commit comments