Liking cljdoc? Tell your friends :D

crustimoney.combinator-grammar

This grammar allows you to create a parser model using functions.

Each combinator creates a parser model that is suitable for use with core's main parse (and compile) function, and many take other parser combinators as their argument; they are composable.

The functions in this namespace are in direct relation to the crustimoney.combinators they compile to, except that ref is optional. To refer to other parsers in a recursive grammar, you simply use its keyword. For example:

{:root (chain (literal "foo") :bax)
 :bax  (choice (literal "bar")
               (literal "baz"))}

As with any recursive grammar, you can auto-capture a rule by adding the = postfix to its name.

This grammar allows you to create a parser model using functions.

Each combinator creates a parser model that is suitable for use with
core's main `parse` (and `compile`) function, and many take other
parser combinators as their argument; they are composable.

The functions in this namespace are in direct relation to the
`crustimoney.combinators` they compile to, except that `ref` is
optional. To refer to other parsers in a recursive grammar, you
simply use its keyword. For example:

    {:root (chain (literal "foo") :bax)
     :bax  (choice (literal "bar")
                   (literal "baz"))}

As with any recursive grammar, you can auto-capture a rule by adding
the `=` postfix to its name.
raw docstring

>clj

An alias for soft-cut.

An alias for soft-cut.
sourceraw docstring

>>clj

An alias for hard-cut.

An alias for hard-cut.
sourceraw docstring

chainclj

(chain & parsers)

Chain multiple consecutive parsers.

The chain combinator supports cuts. At least one normal parser must precede a cut. That parser must consume input, which no other parser (via a choice) up in the combinator tree could also consume at that point.

Two kinds of cuts are supported. A "hard" cut and a "soft" cut, which can be inserted in the chain using hard-cut (or >>) and soft-cut (or >). Both types of cuts improve error messages, as they limit backtracking.

With a hard cut, the parser is instructed to never backtrack before the end of this chain. A well placed hard cut has a major benefit, next to better error messages. It allows for substantial memory optimization, since the packrat caches can evict everything before the cut. This can turn memory requirements from O(n) to O(1). Since PEG parsers are memory hungry, this can be a big deal.

With a soft cut, backtracking can still happen outside the chain, but errors will not escape inside the chain after a soft cut. The advantage of a soft cut over a hard cut, is that they can be used at more places without breaking the grammar.

For example, the following parser benefits from a soft-cut:

(choice (chain (maybe (chain (literal "{")
                             soft-cut
                             (literal "foo")
                             (literal "}")))
               (literal "bar"))
        (literal "baz")))

When parsing "{foo", it will nicely report that a "}" is missing. Without the soft-cut, it would report that "bar" or "baz" are expected, ignoring the more likely error.

When parsing "{foo}eve", it will nicely report that "bar" or "baz" is missing. Placing a hard cut would only report "bar" missing, as it would never backtrack to try the "baz" choice.

Soft cuts do not influence the packrat caches, so they do not help performance wise. A hard cut is implicitly also a soft cut.

Chain multiple consecutive parsers.

The chain combinator supports cuts. At least one normal parser must
precede a cut. That parser must consume input, which no other
parser (via a choice) up in the combinator tree could also consume
at that point.

Two kinds of cuts are supported. A "hard" cut and a "soft" cut,
which can be inserted in the chain using `hard-cut` (or `>>`) and
`soft-cut` (or `>`). Both types of cuts improve error messages, as
they limit backtracking.

With a hard cut, the parser is instructed to never backtrack before
the end of this chain. A well placed hard cut has a major benefit,
next to better error messages. It allows for substantial memory
optimization, since the packrat caches can evict everything before
the cut. This can turn memory requirements from O(n) to O(1). Since
PEG parsers are memory hungry, this can be a big deal.

With a soft cut, backtracking can still happen outside the chain,
but errors will not escape inside the chain after a soft cut. The
advantage of a soft cut over a hard cut, is that they can be used at
more places without breaking the grammar.

For example, the following parser benefits from a soft-cut:

    (choice (chain (maybe (chain (literal "{")
                                 soft-cut
                                 (literal "foo")
                                 (literal "}")))
                   (literal "bar"))
            (literal "baz")))

When parsing "{foo", it will nicely report that a "}" is
missing. Without the soft-cut, it would report that "bar" or
"baz" are expected, ignoring the more likely error.

When parsing "{foo}eve", it will nicely report that "bar" or
"baz" is missing. Placing a hard cut would only report "bar"
missing, as it would never backtrack to try the "baz" choice.

Soft cuts do not influence the packrat caches, so they do not help
performance wise. A hard cut is implicitly also a soft cut.
sourceraw docstring

choiceclj

(choice & parsers)

Match the first of the ordered parsers that is successful.

Match the first of the ordered parsers that is successful.
sourceraw docstring

eofclj

(eof)

Succeed only if the entire text has been parsed.

Succeed only if the entire text has been parsed.
sourceraw docstring

hard-cutclj

A hard-cut for use within chain.

A hard-cut for use within `chain`.
sourceraw docstring

literalclj

(literal text)

A parser that matches an exact literal string.

A parser that matches an exact literal string.
sourceraw docstring

lookaheadclj

(lookahead parser)

Lookahead for the given parser, i.e. succeed if the parser does, without advancing the parsing position.

Lookahead for the given parser, i.e. succeed if the parser does,
without advancing the parsing position.
sourceraw docstring

maybeclj

(maybe parser)

Try to parse the given parser, but succeed anyway.

Try to parse the given parser, but succeed anyway.
sourceraw docstring

negateclj

(negate parser)

Negative lookahead for the given parser, i.e. this succeeds if the parser does not.

Negative lookahead for the given parser, i.e. this succeeds if the
parser does not.
sourceraw docstring

refclj

(ref key)

Refer to another parser by its key in the grammar. Only valid inside recursive grammars, for example:

{:foo  (literal {:text "foo"})
 :root (ref :foo)}

Note that this function is generally not needed, as all other functions in this namespace transform keywords in parser position automatically to refs. It can aid in readability though, for example:

(with-error :error :rule)

vs

(with-error :error
  (ref :rule))
Refer to another parser by its key in the grammar. Only valid inside
recursive grammars, for example:

    {:foo  (literal {:text "foo"})
     :root (ref :foo)}

Note that this function is generally not needed, as all other
functions in this namespace transform keywords in parser position
automatically to refs. It can aid in readability though, for example:

    (with-error :error :rule)

vs

    (with-error :error
      (ref :rule))
sourceraw docstring

regexclj

(regex pattern)

A parser that matches the given regular expression (string or pattern).

A parser that matches the given regular expression (string or
pattern).
sourceraw docstring

repeat*clj

(repeat* parser)

Eagerly try to match the given parser as many times as possible.

Eagerly try to match the given parser as many times as possible.
sourceraw docstring

repeat+clj

(repeat+ parser)

Eagerly try to match the parser as many times as possible, expecting at least one match.

Eagerly try to match the parser as many times as possible, expecting
at least one match.
sourceraw docstring

soft-cutclj

A soft-cut for use within chain.

A soft-cut for use within `chain`.
sourceraw docstring

with-errorclj

(with-error key parser)

Wrap the parser, replacing any errors with a single error with the supplied error key.

Wrap the parser, replacing any errors with a single error with the
supplied error key.
sourceraw docstring

with-nameclj

(with-name key parser)

Wrap the parser, assigning a name to the (success) result of the parser. Nameless parse results are filtered out during parsing.

Wrap the parser, assigning a name to the (success) result of the
parser. Nameless parse results are filtered out during parsing.
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close