crow/parse.crow (source)
grammar[t] record mut
(has non-public fields)
Opaque grammar type parsing an 'a' value.
Use the functions in this module to build a grammar.
parse[t] (t, parse-error) result(a t grammar, text string)
Match a string against a grammar.
THe whole string must match the grammar.
If the string has leading or trailing spaces you must use a grammar that includes those.
force[t] t(a (t, parse-error) result)
Return a parse result or throw on error.
parse-error record exception variant-member
exception variant-memberinputstringiterstring-iteratorexpectedstring
== bool(a parse-error, b parse-error)
show string(a parse-error)
grammar-skip record mut
This is like 'grammar' but produces no result.
new grammar-skip()
Grammar matching empty strings
skip[t] grammar-skip(a t grammar)
Use to discard the result of any grammar.
and-return[t] t grammar(a grammar-skip, value t)
text-delimited string grammar(begin string, end string)
text-not-containing string grammar(ending string)
text-not-containing-any string grammar(possible-endings string array)
~~[t, u] (t, u) tuple2 grammar(a t grammar, b u grammar)
Expects to parse 'a' followed by 'b'.
By default this does not allow spaces in between. For that, use a ~~ spaces ~~ b.
~~[t] t grammar(a t grammar, b grammar-skip)
~~[t] t grammar(a grammar-skip, b t grammar)
~~ grammar-skip(a grammar-skip, b grammar-skip)
~~[t] t grammar(a string, b t grammar)
Skips an exact string, then parses 'b'
~~[t] t grammar(a t grammar, b string)
Parses 'a', then skips an exact string
~~ grammar-skip(a string, b grammar-skip)
~~ grammar-skip(a grammar-skip, b string)
/[t] t grammar(a t grammar, b t grammar)
Tries to parse with 'a', and if that fails, tries to parse with 'b' instead.
map[out, in] out grammar(a in grammar, f mut out(in))
Uses a function to transform the result if parsing is successful.
optional[t] t? grammar(a t grammar)
Allows parsing to fail, returning an empty option if it does.
optional grammar-skip(a string)
Optionally skips an exact string.
many[t] t array grammar(a t grammar)
Parses the same grammar as many times as possible (including 0 times).
many grammar-skip(a grammar-skip)
one-or-more[t] t array grammar(a t grammar)
Parses 'a' at least once, then as many times as possible.
one-or-more grammar-skip(a grammar-skip)
separated-by[t] t array grammar(a t grammar, separator string)
Parses 'a' as many times as possible, with 'separator' in between.
This does not allow a trailing separator;
it is a parse error to have a separator not followed by another 'a'.
separated-by[t] t array grammar(a t grammar, separator grammar-skip)
lazy record nominal
Use this to allow recursive grammars.
with-block[t] t grammar(anonymous lazy, f data t grammar(void))
exact grammar-skip(a string)
Matches 'a' exactly.
exact[t] t grammar(a string, value t)
Equivalent to a.exact map _ => value
spaces grammar-skip()
Matches any amount of whitespace (including none) and discards it.
quoted-string string grammar()
A double-quoted string as in JSON, including escape sequences.
word string grammar()
Matches and returns a sequence of letters.
bool bool grammar()
Parse a boolean as 'true' or 'false'
nat nat64 grammar()
Parses a natural number in decimal format.
This allows leading 0s which have no effect.
int int64 grammar()
Parses a signed integer in decimal format.
This allows e.g. "1", "+23", "-45".
float float64 grammar()
Parse a decimal number.
This allows e.g. "+1.23".