The core Kern library.
Kern is a library of parser combinators for Clojure. It is useful for implementing recursive-descent parsers based on predictive LL(1) grammars with on-demand, unlimited look-ahead LL(*).
The main inspiration for Kern comes from Parsec, a Haskell library written by Daan Leijen, as well as work by Graham Hutton, Erik Meijer, and William Burge. The name Kern is a token of appreciation to Brian Kernighan (now at Princeton) for his work on programming languages.
Daan Leijen Parsec, a fast combinator parser, 2001 http://legacy.cs.uu.nl/daan/download/parsec/parsec.pdf
Graham Hutton and Erik Meijer Monadic Parser Combinators, 1996 http://eprints.nottingham.ac.uk/237/1/monparsing.pdf
William H. Burge Recursive Programming Techniques Addison-Wesley, 1975
The core Kern library. Kern is a library of parser combinators for Clojure. It is useful for implementing recursive-descent parsers based on predictive LL(1) grammars with on-demand, unlimited look-ahead LL(*). The main inspiration for Kern comes from Parsec, a Haskell library written by Daan Leijen, as well as work by Graham Hutton, Erik Meijer, and William Burge. The name Kern is a token of appreciation to Brian Kernighan (now at Princeton) for his work on programming languages. Daan Leijen Parsec, a fast combinator parser, 2001 http://legacy.cs.uu.nl/daan/download/parsec/parsec.pdf Graham Hutton and Erik Meijer Monadic Parser Combinators, 1996 http://eprints.nottingham.ac.uk/237/1/monparsing.pdf William H. Burge Recursive Programming Techniques Addison-Wesley, 1975
The number of columns to advance for a tab character. By default, a tab takes four columns.
The number of columns to advance for a tab character. By default, a tab takes four columns.
(<$> f p)
Parses p; if successful, it applies f to the value parsed by p.
Parses p; if successful, it applies f to the value parsed by p.
(<*> p & more)
Applies one or more parsers; collects the results in a vector, including nil values. If any parser fails, it stops immediately and fails.
Applies one or more parsers; collects the results in a vector, including nil values. If any parser fails, it stops immediately and fails.
(<+> p & more)
Applies one or more parsers stopping at the first failure. Flattens the result and converts it to a string.
Applies one or more parsers stopping at the first failure. Flattens the result and converts it to a string.
(<:> p)
Parses p; on failure it pretends it did not consume any input.
Parses p; on failure it pretends it did not consume any input.
(<< p q)
(<< p q & more)
Parses p followed by q; keeps p, skips q. If more parsers are given, it keeps the first result and skips the rest.
Parses p followed by q; keeps p, skips q. If more parsers are given, it keeps the first result and skips the rest.
(<?> p msg)
If parser p fails consuming no input, it replaces any Expecting errors with a single Expecting with message msg. This helps to produce more abstract and accurate error messages.
If parser p fails consuming no input, it replaces any Expecting errors with a single Expecting with message msg. This helps to produce more abstract and accurate error messages.
(<|> p q)
(<|> p q & more)
Tries p; if it fails without consuming any input, it tries q. With more parsers, it will stop and succeed if a parser succeeds; it will stop and fail if a parser fails consuming input; or it will try the next one if a parser fails without consuming input.
Tries p; if it fails without consuming any input, it tries q. With more parsers, it will stop and succeed if a parser succeeds; it will stop and fail if a parser fails consuming input; or it will try the next one if a parser fails without consuming input.
(>> p q)
(>> p q & more)
Parses p followed by q; skips p, keeps q. If more parsers are given, it skips all but last and keeps the result of the last.
Parses p followed by q; skips p, keeps q. If more parsers are given, it skips all but last and keeps the result of the last.
(>>= p f)
Binds parser p to function f which gets p's value and returns a new parser. Function p must define a single parameter. The argument it receives is the value parsed by p, not ps' return value, which is a parser state record.
Binds parser p to function f which gets p's value and returns a new parser. Function p must define a single parameter. The argument it receives is the value parsed by p, not ps' return value, which is a parser state record.
(between delim p)
(between open close p)
Applies open, p, close; returns the value of p.
Applies open, p, close; returns the value of p.
(bind [& bindings] & body)
Expands into nested >>= forms and a function body. The pattern:
(>>= p1 (fn [v1] (>>= p2 (fn [v2] ... (return (f v1 v2 ...))))))
can be more conveniently be written as:
(bind [v1 p1 v2 p2 ...] (return (f v1 v2 ...)))
Expands into nested >>= forms and a function body. The pattern: (>>= p1 (fn [v1] (>>= p2 (fn [v2] ... (return (f v1 v2 ...)))))) can be more conveniently be written as: (bind [v1 p1 v2 p2 ...] (return (f v1 v2 ...)))
(char-seq rdr)
Returns characters from rdr as a lazy sequence. rdr must implement java.io.Reader
Returns characters from rdr as a lazy sequence. rdr must implement java.io.Reader
(clear-empty s)
Sets the parser state as not empty. Needed in compound parsers where optional parsers at the end may leave an incorrect :empty state for the parser as a whole.
Sets the parser state as not empty. Needed in compound parsers where optional parsers at the end may leave an incorrect :empty state for the parser as a whole.
Parses a decimal integer delimited by any character that is not a decimal digit.
Parses a decimal integer delimited by any character that is not a decimal digit.
(def- name & more)
Same as def, yielding a private def.
Same as def, yielding a private def.
(defn* name & more)
Same as def, yielding a dynamic def.
Same as def, yielding a dynamic def.
(end-by sep p)
Parses p zero or more times, separated and ended by applications of sep; returns the results of p in a vector.
Parses p zero or more times, separated and ended by applications of sep; returns the results of p in a vector.
(end-by1 sep p)
Parses p one or more times, separated and ended by applications of sep; returns the results of p in a vector.
Parses p one or more times, separated and ended by applications of sep; returns the results of p in a vector.
(expect p msg)
Applies parser p; if it fails (regardless of input consumed) it replaces any expecting errors with expecting msg. This is similar to <?> but works even if some input was consumed.
Applies parser p; if it fails (regardless of input consumed) it replaces any expecting errors with expecting msg. This is similar to <?> but works even if some input was consumed.
(f->s f)
(f->s f e)
Gets a character sequence from a file-like object.
Gets a character sequence from a file-like object.
(fail msg)
Fails without consuming any input, having a single error record with the passed messge msg.
Fails without consuming any input, having a single error record with the passed messge msg.
(failed-empty? s)
Tests if s failed without consuming any input.
Tests if s failed without consuming any input.
(field* cs)
Parses an unquoted text field terminated by any character in cs.
Parses an unquoted text field terminated by any character in cs.
Parses a simple fractional number without an exponent. It is delimited by any character that is not a decimal digit. It cannot start with a period; the first period found must be followed by at least one digit.
Parses a simple fractional number without an exponent. It is delimited by any character that is not a decimal digit. It cannot start with a period; the first period found must be followed by at least one digit.
(fwd p)
Delays the evaluation of a parser that was forward (declare)d and it has not been defined yet. For use in (def)s of no-arg parsers, since the parser expression evaluates immediately.
Delays the evaluation of a parser that was forward (declare)d and it has not been defined yet. For use in (def)s of no-arg parsers, since the parser expression evaluates immediately.
(get-input s)
Gets the input stream from a parser state.
Gets the input stream from a parser state.
(get-position s)
Gets the position in the input stream of a parser state.
Gets the position in the input stream of a parser state.
(get-state s)
Get the user state from the parser state record.
Get the user state from the parser state record.
Parses a hex integer delimited by any character that is not a hex digit.
Parses a hex integer delimited by any character that is not a hex digit.
(look-ahead p)
Applies p and returns the result; it consumes no input.
Applies p and returns the result; it consumes no input.
(many p)
Parses p zero or more times; returns the result(s) in a vector. It stops when p fails, but this parser succeeds.
Parses p zero or more times; returns the result(s) in a vector. It stops when p fails, but this parser succeeds.
(many-till p end)
Parses zero or more p while trying end, until end succeeds. Returns the results in a vector.
Parses zero or more p while trying end, until end succeeds. Returns the results in a vector.
(many0 p)
Like (many) but it won't set the state to :empty. Use instead of (many) if it comes last to avoid overriding non-empty parsing.
Like (many) but it won't set the state to :empty. Use instead of (many) if it comes last to avoid overriding non-empty parsing.
(many1 p)
Parses p one or more times and returns the result(s) in a vector. It stops when p fails, but this parser succeeds.
Parses p one or more times and returns the result(s) in a vector. It stops when p fails, but this parser succeeds.
(member? x coll)
Tests if x is a member of coll.
Tests if x is a member of coll.
(modify-state f & more)
Modify the user state with the result of f, which takes the old user state plus any additional arguments.
Modify the user state with the result of f, which takes the old user state plus any additional arguments.
(none-of* cs)
Succeeds if the next character is not in the supplied string.
Succeeds if the next character is not in the supplied string.
(not-followed-by p)
Succeeds only if p fails; consumes no input.
Succeeds only if p fails; consumes no input.
Parses an octal integer delimited by any character that is not an octal digit.
Parses an octal integer delimited by any character that is not an octal digit.
(one-of* cs)
Succeeds if the next character is in the supplied string.
Succeeds if the next character is in the supplied string.
(option x p)
Applies p; if it fails without consuming input, it returns a parser state record with the :value x as default.
Applies p; if it fails without consuming input, it returns a parser state record with the :value x as default.
(optional p)
Succeeds if p succeeds or if p fails without consuming input.
Succeeds if p succeeds or if p fails without consuming input.
(parse p cs)
(parse p cs src)
(parse p cs src us)
Parses a character sequence; takes an optional label and a user state initial value, which default to nil. Returns a PState record.
cs A seqable object; parse calls (seq) on this value. src Identifies the source of the text, e.g. a filename. us Initializes a field that is maintained by client code.
Parses a character sequence; takes an optional label and a user state initial value, which default to nil. Returns a PState record. cs A seqable object; parse calls (seq) on this value. src Identifies the source of the text, e.g. a filename. us Initializes a field that is maintained by client code.
(parse-data p cs)
(parse-data p cs src)
(parse-data p cs src us)
Works like (parse) but with error diagnostics disabled for better performance. It's intended for data that can be assumed to be correct or its diagnosis postponed.
Works like (parse) but with error diagnostics disabled for better performance. It's intended for data that can be assumed to be correct or its diagnosis postponed.
(parse-data-file p f)
(parse-data-file p f en)
(parse-data-file p f en us)
Works like (parse-file) but with error diagnostics disabled for better performance. It's intended for data files that can be assumed to be correct or its diagnosis postponed.
Works like (parse-file) but with error diagnostics disabled for better performance. It's intended for data files that can be assumed to be correct or its diagnosis postponed.
(parse-file p f)
(parse-file p f en)
(parse-file p f en us)
Parses a file; takes an optional encoding and user state, which default to utf-8 and nil. Returns a PState record.
Parses a file; takes an optional encoding and user state, which default to utf-8 and nil. Returns a PState record.
(predict p)
Applies p; if it succeeds it consumes no input.
Applies p; if it succeeds it consumes no input.
(print-error s)
Prints error messages in a PState record.
Prints error messages in a PState record.
(put-state u)
Put u as the new value for user state in the parser state record.
Put u as the new value for user state in the parser state record.
(return v)
Succeeds without consuming any input. Any carried errors are removed.
Succeeds without consuming any input. Any carried errors are removed.
(run p cs)
(run p cs src)
(run p cs src us)
For testing parsers, e.g. at the REPL. Calls (parse) on the arguments and prints the result. If p succeeds it prints the parsed value; if it fails it prints any error messages.
For testing parsers, e.g. at the REPL. Calls (parse) on the arguments and prints the result. If p succeeds it prints the parsed value; if it fails it prints any error messages.
(run* p cs)
(run* p cs src)
(run* p cs src us)
For testing parsers, e.g. at the REPL. Works like (run) but on success it pretty-prints the resulting parser state.
For testing parsers, e.g. at the REPL. Works like (run) but on success it pretty-prints the resulting parser state.
(runf p f)
(runf p f en)
(runf p f en us)
For testing, e.g. at the REPL, with input from files. Prints the results.
For testing, e.g. at the REPL, with input from files. Prints the results.
(runf* p f)
(runf* p f en)
(runf* p f en us)
For testing, e.g. at the REPL, with input from files. Pretty-prints the results.
For testing, e.g. at the REPL, with input from files. Pretty-prints the results.
(satisfy pred)
Succeeds if the next character satisfies the predicate pred, in which case advances the position of the input stream. It may fail on an unexpected end of input.
Succeeds if the next character satisfies the predicate pred, in which case advances the position of the input stream. It may fail on an unexpected end of input.
(search p)
Applies a parser p, traversing the input as necessary, until it succeeds or it reaches the end of input.
Applies a parser p, traversing the input as necessary, until it succeeds or it reaches the end of input.
(sep-by sep p)
Parses p zero or more times while parsing sep in between; collects the results of p in a vector.
Parses p zero or more times while parsing sep in between; collects the results of p in a vector.
(sep-by1 sep p)
Parses p one or more times while parsing sep in between; collects the results of p in a vector.
Parses p one or more times while parsing sep in between; collects the results of p in a vector.
(sep-end-by sep p)
Parses p zero or more times separated, and optionally ended by sep; collects the results in a vector.
Parses p zero or more times separated, and optionally ended by sep; collects the results in a vector.
(sep-end-by1 sep p)
Parses p one or more times separated, and optionally ended by sep; collects the results in a vector.
Parses p one or more times separated, and optionally ended by sep; collects the results in a vector.
(set-input in)
Sets the input stream in a parser state.
Sets the input stream in a parser state.
(set-position pos)
Sets the position in the input stream of a parser state.
Sets the position in the input stream of a parser state.
(skip p)
(skip p q)
(skip p q & more)
Applies one or more parsers and skips the result. That is, it returns a parser state record with a :value nil.
Applies one or more parsers and skips the result. That is, it returns a parser state record with a :value nil.
(skip-many p)
Parses p zero or more times and skips the results. This is like skip but it can apply p zero, one, or many times.
Parses p zero or more times and skips the results. This is like skip but it can apply p zero, one, or many times.
(skip-many1 p)
Parses p one or more times and skips the results.
Parses p one or more times and skips the results.
(skip-ws p)
Skips whitespaces before parsing p.
Skips whitespaces before parsing p.
(split-on cs)
Splits a string on one of the given characters and whitespace. Removes empty strings from the result.
Splits a string on one of the given characters and whitespace. Removes empty strings from the result.
(sym* x)
Parses a single symbol x (a character).
Parses a single symbol x (a character).
(sym- x)
Parses a single symbol x (a character); not case-sensitive.
Parses a single symbol x (a character); not case-sensitive.
(times n p)
Applies p n times; collects the results in a vector.
Applies p n times; collects the results in a vector.
(token* xs)
(token* xs & more)
Parses a specific string, not necessarily delimited. If more than one are given it will try each choice in turn.
Parses a specific string, not necessarily delimited. If more than one are given it will try each choice in turn.
(token- xs)
(token- xs & more)
Parses a specific string, not necessarily delimited; not case-sensitive. If more than one are given it will try each choice in turn.
Parses a specific string, not necessarily delimited; not case-sensitive. If more than one are given it will try each choice in turn.
(unexpected msg s)
Sets s as failed because an unexpected reason.
Sets s as failed because an unexpected reason.
(unexpected-input in s)
Sets s as failed because of an unexpected input.
Sets s as failed because of an unexpected input.
(value p cs)
(value p cs src)
(value p cs src us)
Calls (parse) on the arguments and returns the actual parsed value, not the PState record.
Calls (parse) on the arguments and returns the actual parsed value, not the PState record.
(word* letter cs)
(word* letter cs & more)
Parses a specific string, delimited by letter. If more than one are given it will try each choice in turn.
Parses a specific string, delimited by letter. If more than one are given it will try each choice in turn.
(word- letter cs)
(word- letter cs & more)
Parses a specific string, delimited by letter; not case-sensitive. If more than one are given it will try each choice in turn.
Parses a specific string, delimited by letter; not case-sensitive. If more than one are given it will try each choice in turn.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close