Create a parser (model) based on a data grammar. The data is translated into a single parser or map of parsers. 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 {:key :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).
Keep in mind that create-parser returns a parser model, which can
be combined with other grammars. For example:
(merge
(create-parser '{root ("Hello " email)})
{:email (regex #"...")})
If you want to use an EDN format, you can use #crusti/regex tagged
literal for regular expressions. To be able to read this, use the
following:
(clojure.edn/read-string {:readers *data-readers*} ...)
Create a parser (model) based on a data grammar. The data is
translated into a single parser or map of parsers. 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 {:key :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).
Keep in mind that `create-parser` returns a parser model, which can
be combined with other grammars. For example:
(merge
(create-parser '{root ("Hello " email)})
{:email (regex #"...")})
If you want to use an EDN format, you can use `#crusti/regex` tagged
literal for regular expressions. To be able to read this, use the
following:
(clojure.edn/read-string {:readers *data-readers*} ...)(create-parser data)Create a parser (model) based on a data-grammar definition. If a map with rules is supplied, a map is returned. Otherwise a single parser is returned.
See namespace documentation for the data-grammar format.
Create a parser (model) based on a data-grammar definition. If a map with rules is supplied, a map is returned. Otherwise a single parser is returned. See namespace documentation for the data-grammar format.
(vector-model data)Low-level protocol function which translates the data type into a
vector-based model. 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-model [date]
[:my-namespace/my-flexible-date-parser {:date date}]))
To see which data types are already supported, use (-> DataGrammar :impls keys)
Low-level protocol function which translates the data type into a
vector-based model. 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-model [date]
[:my-namespace/my-flexible-date-parser {:date 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 |