-
Notifications
You must be signed in to change notification settings - Fork 50
Documentation and new parsers rest,take,eof #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ | |
[](https://github.com/jamesdbrock) | ||
[](https://github.com/robertdp) | ||
|
||
A monadic parser combinator library based on Haskell's [Parsec](https://hackage.haskell.org/package/parsec). | ||
A monadic parser combinator library based on Haskell’s | ||
[Parsec](https://hackage.haskell.org/package/parsec). | ||
|
||
## Installation | ||
|
||
|
@@ -22,26 +23,41 @@ Here is a basic tutorial introduction to monadic parsing with this package. | |
|
||
### Parsers | ||
|
||
A parser turns a string into a data structure. Parsers in this library have the type `Parser s a`, where `s` is the type of the input string, and `a` is the type of the data which the parser will produce on success. `Parser s a` is a monad. It’s defined in the module `Text.Parsing.Parser`. | ||
A parser turns a string into a data structure. Parsers in this library have the type `Parser s a`, where `s` is the type of the input string, and `a` is the type of the data which the parser will produce on success. `Parser s` is a monad. It’s defined in the module `Text.Parsing.Parser`. | ||
|
||
Monads can be used to provide context for a computation, and that’s how we use them in monadic parsing. The context provided by the `Parser` monad is *the parser’s current location in the input string*. Parsing starts at the beginning of the input string. | ||
Monads can be used to provide context for a computation, and that’s how we use them in monadic parsing. | ||
The context provided by the `Parser s` monad is __the parser’s current location in the input string__. | ||
Parsing starts at the beginning of the input string. | ||
|
||
Parsing requires two more capabilities: *choice* and *failure*. | ||
Parsing requires two more capabilities: __alternative__ and __failure__. | ||
|
||
We need *choice* to be able to make decisions about what kind of thing we’re parsing depending on the input which we encouter. This is provided by the `Alt` typeclass instance of the `Parser` monad, particularly the `<|>` operator. That operator will first try the left parser and if that fails, then it will backtrack the input string and try the right parser. | ||
We need __alternative__ to be able to choose what kind of thing we’re parsing depending | ||
on the input which we encounter. This is provided by the `<|>` “alt” | ||
operator of the `Alt` typeclass instance of the `Parser s` monad. | ||
The expression `p_left <|> p_right` will first try the `p_left` parser and if that fails | ||
__and consumes no input__ then it will try the `p_right` parser. | ||
|
||
We need *failure* in case the input stream is not parseable. This is provided by the `fail` function, which calls the `throwError` function of the `MonadThrow` typeclass instance of the `Parser` monad. The result of running a parser has type `Either ParseError a`, so if the parse succeeds then the result is `Right a` and if the parse fails then the result is `Left ParseError`. | ||
We need __failure__ in case the input stream is not parseable. This is provided by the `fail` | ||
function, which calls the `throwError` function of the `MonadThrow` typeclass instance of | ||
the `Parser s` monad. | ||
|
||
|
||
### Running a parser | ||
|
||
To run a parser, call the function `runParser :: s -> Parser s a -> Either ParseError a` in the `Text.Parsing.Parser` module, and supply it with an input string and a parser. | ||
To run a parser, call the function `runParser :: s -> Parser s a -> Either ParseError a` in | ||
the `Text.Parsing.Parser` module, and supply it with an input string and a parser. | ||
If the parse succeeds then the result is `Right a` and if the parse fails then the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come there are all these extra line breaks in this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for editing convenience. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm curious why this was done too because it messes up the |
||
result is `Left ParseError`. | ||
|
||
### Primitive parsers | ||
|
||
Each type of input string needs primitive parsers. Primitive parsers for input string type `String` are in the `Text.Parsing.Parser.String` module. We can use these primitive parsers to write other `String` parsers. | ||
Each type of input string needs primitive parsers. | ||
Primitive parsers for input string type `String` are in the `Text.Parsing.Parser.String` module. | ||
We can use these primitive parsers to write other `String` parsers. | ||
|
||
Here is a parser `ayebee :: Parser String Boolean` which will accept only two input strings: `"ab"` or `"aB"`. It will return `true` if the `b` character is uppercase. It will return `false` if the `b` character is lowercase. It will fail with a `ParseError` if the input string is anything else. This parser is written in terms of the primitive parser `char :: Parser String Char`. | ||
Here is a parser `ayebee :: Parser String Boolean` which will accept only two input | ||
strings: `"ab"` or `"aB"`. | ||
It will return `true` if the `b` character is uppercase. | ||
It will return `false` if the `b` character is lowercase. | ||
It will fail with a `ParseError` if the input string is anything else. | ||
This parser is written in terms of the primitive parser `char :: Parser String Char`. | ||
|
||
```purescript | ||
ayebee :: Parser String Boolean | ||
|
@@ -61,24 +77,33 @@ and then the parser will succeed and return `Right true`. | |
|
||
#### [✨ Run the `ayebee` parser in your browser on *Try PureScript!*](https://try.purescript.org/?github=/purescript-contrib/purescript-parsing/main/docs/examples/QuickStart.purs) | ||
|
||
When you write a real parser you will usually want to return a more complicated data structure than a single `Boolean`. See [*Parse, don't validate*](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/). | ||
|
||
### More parsers | ||
|
||
There are other `String` parsers in the module `Text.Parsing.Parser.Token`, for example the parser `letter :: Parser String Char` which will accept any single alphabetic letter. | ||
|
||
### Parser combinators | ||
|
||
A parser combinator is a function which takes a parser as an argument and returns a new parser. The `many` combinator, for example, will repeat a parser as many times as it can. So the parser `many letter` will have type `Parser String (Array Char)`. Parser combinators are in this package in the module `Text.Parsing.Parser.Combinators`. | ||
A parser combinator is a function which takes a parser as an argument and returns a new parser. The `many` combinator, for example, will repeat a parser as many times as it can. So the parser `many letter` will have type `Parser String (Array Boolean)`. Running that parser | ||
|
||
```purescript | ||
runParser "aBabaB" (many ayebee) | ||
``` | ||
|
||
will return `Right [true, false, true]`. | ||
|
||
Parser combinators are in this package in the module `Text.Parsing.Parser.Combinators`. | ||
|
||
## Further reading | ||
|
||
Here is the original short classic [FUNCTIONAL PEARLS *Monadic Parsing in Haskell*](https://www.cs.nott.ac.uk/~pszgmh/pearl.pdf) by Graham Hutton and Erik Meijer. | ||
Here is the original short classic [FUNCTIONAL PEARLS *Monadic Parsing in Haskell*](https://www.cs.nott.ac.uk/~pszgmh/pearl.pdf) by Graham Hutton and Erik Meijer. | ||
|
||
[*Revisiting Monadic Parsing in Haskell*](https://vaibhavsagar.com/blog/2018/02/04/revisiting-monadic-parsing-haskell/) by Vaibhav Sagar is a reflection on the Hutton, Meijer FUNCTIONAL PEARL. | ||
|
||
[*Parse, don't validate*](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/) by Alexis King is about what it means to “parse” something, without any mention of monads. | ||
|
||
[*Parsec: “try a <|> b” considered harmful*](http://blog.ezyang.com/2014/05/parsec-try-a-or-b-considered-harmful/) by Edward Z. Yang is about how to decide when to backtrack | ||
from a failed alternative. | ||
|
||
There are lots of other great monadic parsing tutorials on the internet. | ||
|
||
## Related Packages | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "choice" -> "alternative" change makes the language fairly awkward. What was the reason for changing it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because “alternative” is the term used by the
Alt
typeclass. For clarity I like to try to always use the same word when I'm talking about the same thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "choice" is a more familiar word to use here because you clarify what you mean by "choice" in the next few paragraphs:
Alt
.