Create a parser based on a data grammar. The data is translated into combinators.
Create a parser based on a data grammar. The data is translated into combinators.
(create-parser data)(create-parser data other-parsers)Create a parser based on a data grammar definition. If a map with rules is supplied, a map of parsers is returned. Otherwise a single parser is returned. The following example shows what a data grammar looks like:
{;; terminals
literal "foo"
character \c
regex #"[a-z]"
regex-tag #crusti/regex "[a-z]" ; EDN support
eof $
;; refs, chains, choices and grouping
reference literal
chain (literal regex)
choices (literal / regex / "alice" "bob")
named-group (:my-name literal / "the end" $)
auto-named-group= (literal / "the end" $)
;; quantifiers
zero-to-many (literal *)
one-to-many ("bar"+)
zero-to-one ("foo" "bar"?) ; bar is optional here
;; lookaheads
lookahead (& regex)
negative-lookahead (!"alice")
;; cuts
soft-cut ('[' > expr? ']') ; note the >
hard-cut ((class-open class class-close >>)*) ; note the >>
;; direct combinator calls
combinator-call [:with-error :fail #crusti/parser ("fooba" #"r|z")]
custom-combinator [:my.app/my-combinator ...]}
To capture nodes in the parse result, you need to use named groups.
If you postfix a rule name with =, the expression is automatically
captured using the rule's name (without the postfix). Please read up
on this at crustimoney.combinators/grammar.
Optionally an existing map of parsers can be supplied, which can refered to by the data grammar. For example:
(create-parser '{root ("Hello " email)}
{:email (regex "...")})
If you want to use an EDN grammar file or string, you can use
#crusti/regex tagged literal for regular expressions. To read
this, use the following:
(clojure.edn/read-string {:readers *data-readers*} ...)
Create a parser based on a data grammar definition. If a map with
rules is supplied, a map of parsers is returned. Otherwise a single
parser is returned. The following example shows what a data grammar
looks like:
{;; terminals
literal "foo"
character \c
regex #"[a-z]"
regex-tag #crusti/regex "[a-z]" ; EDN support
eof $
;; refs, chains, choices and grouping
reference literal
chain (literal regex)
choices (literal / regex / "alice" "bob")
named-group (:my-name literal / "the end" $)
auto-named-group= (literal / "the end" $)
;; quantifiers
zero-to-many (literal *)
one-to-many ("bar"+)
zero-to-one ("foo" "bar"?) ; bar is optional here
;; lookaheads
lookahead (& regex)
negative-lookahead (!"alice")
;; cuts
soft-cut ('[' > expr? ']') ; note the >
hard-cut ((class-open class class-close >>)*) ; note the >>
;; direct combinator calls
combinator-call [:with-error :fail #crusti/parser ("fooba" #"r|z")]
custom-combinator [:my.app/my-combinator ...]}
To capture nodes in the parse result, you need to use named groups.
If you postfix a rule name with `=`, the expression is automatically
captured using the rule's name (without the postfix). Please read up
on this at `crustimoney.combinators/grammar`.
Optionally an existing map of parsers can be supplied, which can
refered to by the data grammar. For example:
(create-parser '{root ("Hello " email)}
{:email (regex "...")})
If you want to use an EDN grammar file or string, you can use
`#crusti/regex` tagged literal for regular expressions. To read
this, use the following:
(clojure.edn/read-string {:readers *data-readers*} ...)(vector-tree data)Low-level protocol function which translates the data type
into an intermediary vector-based representation. See
crustimoney.vector-grammar for more on this format. This can be
useful for debugging, or adding your own data type.
In the latter case, add your type like so:
(extend-type java.util.Date
DataGrammar
(vector-tree [date]
[:my-namespace/my-flexible-date-parser date]))
To see which data types are already supported, use (-> DataGrammar :impls keys)
Low-level protocol function which translates the data type
into an intermediary vector-based representation. See
`crustimoney.vector-grammar` for more on this format. This can be
useful for debugging, or adding your own data type.
In the latter case, add your type like so:
(extend-type java.util.Date
DataGrammar
(vector-tree [date]
[:my-namespace/my-flexible-date-parser date]))
To see which data types are already supported, use `(->
DataGrammar :impls keys)`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 |