Experimental combinators. These may get promoted, or changed, or dismissed.
These combinators are not available in the string- or data-driven grammar (yet). To use them with those, combine them in a larger grammar like so:
(require '[crustimoney.experimental.combinators :as e])
(grammar
(create-parser
"root= <- stream
expr= <- '{' [0-9]+ '}'")
{:stream (e/stream*
(e/with-callback handle-expr
(e/recover (ref :expr) (regex ".*?}")))})
Experimental combinators. These may get promoted, or changed,
or dismissed.
These combinators are not available in the string- or data-driven
grammar (yet). To use them with those, combine them in a larger
grammar like so:
(require '[crustimoney.experimental.combinators :as e])
(grammar
(create-parser
"root= <- stream
expr= <- '{' [0-9]+ '}'")
{:stream (e/stream*
(e/with-callback handle-expr
(e/recover (ref :expr) (regex ".*?}")))})(range parser min max)Like a repeat, but the times the wrapped parser is matched must lie
within the given range. It will not try to parse more than max
times.
Like a repeat, but the times the wrapped `parser` is matched must lie within the given range. It will not try to parse more than `max` times.
(recover parser recovery)Like a choice, capturing errors of the first choice, including
soft-cuts in its scope.
If the first parser fails, the recovery parser is tried. If it
succeeds, it results in a success node like this:
[:crusti/recovered {:start .., :end .., :errors #{..}}]
The errors are those of the first parser, and can be extracted using
success->recovered-errors. If recovery parser fails, the result
will also be the errors of first parser.
Example usage:
(repeat* (recover (with-name :content
(chain (literal "{")
(regex #"\d+")
(literal "}")))
(regex ".*?}")))
Parsing something like {42}{nan}{100} would result in:
[nil {:start 0, :end 14}
[:content {:start 0, :end 4}]
[:crusti/recovered {:start 4, :end 9, :errors #{{:key :expected-match, :at 5, ...}}}]
[:content {:start 9, :end 14}]]
Like a `choice`, capturing errors of the first choice, including
soft-cuts in its scope.
If the first `parser` fails, the `recovery` parser is tried. If it
succeeds, it results in a success node like this:
[:crusti/recovered {:start .., :end .., :errors #{..}}]
The errors are those of the first parser, and can be extracted using
`success->recovered-errors`. If recovery parser fails, the result
will also be the errors of first parser.
Example usage:
(repeat* (recover (with-name :content
(chain (literal "{")
(regex #"\d+")
(literal "}")))
(regex ".*?}")))
Parsing something like `{42}{nan}{100}` would result in:
[nil {:start 0, :end 14}
[:content {:start 0, :end 4}]
[:crusti/recovered {:start 4, :end 9, :errors #{{:key :expected-match, :at 5, ...}}}]
[:content {:start 9, :end 14}]](stream* parser)Like repeat*, but does does not keep its children. Can be used in
combination with with-callback combinator, for example.
Like `repeat*`, but does does not keep its children. Can be used in combination with `with-callback` combinator, for example.
(stream+ parser)Like repeat+, but does does not keep its children. Can be used in
combination with with-callback combinator, for example.
Like `repeat+`, but does does not keep its children. Can be used in combination with `with-callback` combinator, for example.
(with-callback callback parser)Pushes (success) result of parser to the 2-arity callback
function. The callback receives the text and the success result.
If callback is a symbol, it is resolved using requiring-resolve.
Pushes (success) result of `parser` to the 2-arity `callback` function. The callback receives the text and the success result. If `callback` is a symbol, it is resolved using `requiring-resolve`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |