Skip to content

Commit

Permalink
extra docs for choice
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterbreed committed Oct 18, 2012
1 parent 4dd1c22 commit ab5ab4b
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions docs/guide.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,11 @@ one item. It's like `+` in a regular expression instead of `*`.
(run number-parser1 "100")
; (\1 \0 \0)

### choice
### either

`choice` takes one or more parsers and creates a parser that will try each of
them in order until one parses successfully, and return its result. For example:
`either` is a parser that takes two parsers. If the first one succeeds its
value is returned, if it fails, the second parser is tried and it's value is
returned.

(def number (many1 (digit)))
(def word (many1 (letter)))
Expand All @@ -306,10 +307,38 @@ them in order until one parses successfully, and return its result. For example
(run number-or-word "42")
; (\4 \2)

(run number-or-word "@#$")
; RuntimeException ...

### choice

`choice` takes one or more parsers and creates a parser that will try each of
them in order until one parses successfully, and return its result. It is
different from `either` in that it may take more than two parsers while
`either` can only take 2.

For example:

(def number (many1 (digit)))
(def word (many1 (letter)))
(def other (many1 (any-char)))

(def number-or-word-or-anything (choice number word other))

(run number-or-word-or-anything "dog")
; (\d \o \g)

(run number-or-word-or-anything "42")
; (\4 \2)

(run number-or-word-or-anything "!@#$")
; (\! \@ \# \$)

Notice that we used `many1` when defining the parsers `number` and `word`. If
we had used `many` then this would always parse as a number because if there
were no digits it would successfully return an empty sequence.


### between

`between` is a function that takes three parsers, call them left, right, and
Expand Down

0 comments on commit ab5ab4b

Please sign in to comment.