Liking cljdoc? Tell your friends :D

strojure.parsesso.core


afterclj/s

(after q p)
(after q qq p)
(after q qq qqq & more)

This parser tries to apply the parsers in order, until last of them succeeds. Returns the value of the last parser, discards result of all preceding parsers.

  • Fails: when any of tried parsers fails.
  • Consumes: when any of tried parsers consumes some input.
This parser tries to apply the parsers in order, until last of them succeeds.
Returns the value of the last parser, discards result of all preceding
parsers.

- Fails: when any of tried parsers fails.
- Consumes: when any of tried parsers consumes some input.
sourceraw docstring

any-tokenclj/s

This parser accepts any kind of token. Returns the accepted token.

  • Fails: at the end of input.
  • Consumes: when succeeds.
This parser accepts any kind of token. Returns the accepted token.

- Fails: at the end of input.
- Consumes: when succeeds.
sourceraw docstring

betweenclj/s

(between p around)
(between p open close)

Parses open, followed by p and close. Returns the value returned by p.

  • Fails: when any of parses fail.
  • Consumes: in all cases except when open fails without consuming any input.

Example:

(defn braces [p]
  (-> p (between (char/one-of? "{")
                 (char/one-of? "}"))))
Parses `open`, followed by `p` and `close`. Returns the value returned by `p`.

- Fails: when any of parses fail.
- Consumes: in all cases except when `open` fails without consuming any input.

Example:

    (defn braces [p]
      (-> p (between (char/one-of? "{")
                     (char/one-of? "}"))))
sourceraw docstring

bindclj/s

(bind p f)

This parser applies parser p and then parser (f x) where x is a return value of the parser p.

  • Fails: when any of parsers p or (f x) fails.
  • Consumes: when any of parsers p or (f x) consumes some input.
This parser applies parser `p` and then parser `(f x)` where x is a return
value of the parser `p`.

- Fails: when any of parsers `p` or `(f x)` fails.
- Consumes: when any of parsers `p` or `(f x)` consumes some input.
sourceraw docstring

bind-letclj/smacro

(bind-let [& bindings] & body)

Expands into nested bind forms and a function body.

The pattern:

(bind p (fn [x]
          (bind q (fn [y]
                    ...
                    (result (f x y ...))))))

can be more conveniently be written as:

(bind-let [x p
           y q
           ...]
  (result (f x y ...)))
Expands into nested bind forms and a function body.

The pattern:

    (bind p (fn [x]
              (bind q (fn [y]
                        ...
                        (result (f x y ...))))))

can be more conveniently be written as:

    (bind-let [x p
               y q
               ...]
      (result (f x y ...)))
sourceraw docstring

choiceclj/s

(choice p q)
(choice p q qq)
(choice p q qq & more)

This parser tries to apply the parsers in order, until one of them succeeds. Returns the value of the succeeding parser.

  • Fails:
    • when any of tried parsers fails consuming some input.
    • when all tried parsers fail without consuming any input.
  • Consumes:
    • when any of tried parsers consumes some input.

The parser first applies p. If it succeeds, the value of p is returned. If p fails without consuming any input, parser q is tried and so on.

The parser is called predictive since q is only tried when parser p didn't consume any input (i.e. the look ahead is 1). This non-backtracking behaviour allows for both an efficient implementation of the parser combinators and the generation of good error messages.

This parser tries to apply the parsers in order, until one of them succeeds.
Returns the value of the succeeding parser.

- Fails:
    - when any of tried parsers fails consuming some input.
    - when all tried parsers fail without consuming any input.
- Consumes:
    - when any of tried parsers consumes some input.

The parser first applies `p`. If it succeeds, the value of `p` is returned. If
`p` fails _without consuming any input_, parser `q` is tried and so on.

The parser is called _predictive_ since `q` is only tried when parser `p`
didn't consume any input (i.e. the look ahead is 1). This non-backtracking
behaviour allows for both an efficient implementation of the parser
combinators and the generation of good error messages.
sourceraw docstring

debug-parserclj/s

(debug-parser p label)

This parser prints to the console the remaining parser state at the time it is invoked. It then continues to apply parser p, and if p fails will indicate that the label has been backtracked. It is intended to be used for debugging parsers by inspecting their intermediate states.

  • Fails: when p fails.
  • Consumes: when p consumes some input.

Example:

(parse (after (char/one-of? "aeiou")
              (-> (char/one-of? "nope")
                  (debug-parser "one-of-nope")))
       "atest")

> one-of-nope: (\t \e \s \t)
> one-of-nope backtracked

> error at line 1, column 2:
> unexpected "t"
> expecting character of "nope"
This parser prints to the console the remaining parser state at the time it
is invoked. It then continues to apply parser `p`, and if `p` fails will
indicate that the label has been backtracked. It is intended to be used for
debugging parsers by inspecting their intermediate states.

- Fails: when `p` fails.
- Consumes: when `p` consumes some input.

Example:

    (parse (after (char/one-of? "aeiou")
                  (-> (char/one-of? "nope")
                      (debug-parser "one-of-nope")))
           "atest")

    > one-of-nope: (\t \e \s \t)
    > one-of-nope backtracked

    > error at line 1, column 2:
    > unexpected "t"
    > expecting character of "nope"
sourceraw docstring

debug-stateclj/s

(debug-state label)

This parser prints the remaining parser state at the time it is invoked. It is intended to be used for debugging parsers by inspecting their intermediate states.

  • Fails: never.
  • Consumes: never.

Example:

(parse (after (char/one-of? "aeiou")
              (debug-state "label"))
       "atest")

> label: (\t \e \s \t)
This parser prints the remaining parser state at the time it is invoked. It
is intended to be used for debugging parsers by inspecting their intermediate
states.

- Fails: never.
- Consumes: never.

Example:

    (parse (after (char/one-of? "aeiou")
                  (debug-state "label"))
           "atest")

    > label: (\t \e \s \t)
sourceraw docstring

do-parserclj/smacro

(do-parser & body)

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.
sourceraw docstring

eachclj/s

(each ps)

This parser tries to apply parsers in order until all of them succeeds. Returns a sequence of values returned by every parser.

  • Fails: when any of tried parsers fails.
  • Consumes: when any of tried parsers consumes some input.
This parser tries to apply parsers in order until all of them succeeds.
Returns a sequence of values returned by every parser.

- Fails: when any of tried parsers fails.
- Consumes: when any of tried parsers consumes some input.
sourceraw docstring

eofclj/s

(eof)
(eof x)

This parser only succeeds with value x at the end of the input.

  • Fails: when input is not completely consumed.
  • Consumes: never.
This parser only succeeds with value `x` at the end of the input.

- Fails: when input is not completely consumed.
- Consumes: never.
sourceraw docstring

expectingclj/s

(expecting p msg)

This parser behaves as parser p, but whenever the parser p fails without consuming any input, it replaces expect error messages with the expect error message msg.

This is normally used at the end of a set alternatives where we want to return an error message in terms of a higher level construct rather than returning all possible characters. For example, if the expr parser from the 'maybe' example would fail, the error message is: '...: expecting expression'. Without the expecting combinator, the message would be like '...: expecting "let" or alphabetic character', which is less friendly.

The parsers fail, fail-unexpected and expecting are the three parsers used to generate error messages. Of these, only expecting is commonly used.

This parser behaves as parser `p`, but whenever the parser `p` fails _without
consuming any input_, it replaces expect error messages with the expect error
message `msg`.

This is normally used at the end of a set alternatives where we want to return
an error message in terms of a higher level construct rather than returning
all possible characters. For example, if the `expr` parser from the 'maybe'
example would fail, the error message is: '...: expecting expression'. Without
the `expecting` combinator, the message would be like '...: expecting "let"
or alphabetic character', which is less friendly.

The parsers `fail`, `fail-unexpected` and `expecting` are the three parsers
used to generate error messages. Of these, only `expecting` is commonly used.
sourceraw docstring

expecting-metaclj/s

(expecting-meta obj msg)

Attaches expecting error message to obj, i.e. to token predicate function.

Attaches expecting error message to `obj`, i.e. to token predicate function.
sourceraw docstring

failclj/s

(fail)
(fail msg)

This parser always fails with message msg without consuming any input.

  • Fails: always.
  • Consumes: never.
This parser always fails with message `msg` without consuming any input.

- Fails: always.
- Consumes: never.
sourceraw docstring

fail-unexpectedclj/s

(fail-unexpected msg)

This parser always fails with an unexpected error message msg without consuming any input.

  • Fails: always.
  • Consumes: never.
This parser always fails with an unexpected error message `msg` without
consuming any input.

- Fails: always.
- Consumes: never.
sourceraw docstring

look-aheadclj/s

(look-ahead p)

Parses p without consuming any input. If p fails and consumes some input, so does look-ahead. Combine with maybe if this is undesirable.

  • Fails: when p fails.
  • Consumes: when p fails and consumes some input.
Parses `p` without consuming any input. If `p` fails and consumes some input,
so does `look-ahead`. Combine with `maybe` if this is undesirable.

- Fails: when `p` fails.
- Consumes: when `p` fails and consumes some input.
sourceraw docstring

many-someclj/s

(many-some p)

This parser applies the parser p one or more times. Returns a sequence of the returned values of p.

  • Fails: when p does not succeed at least once.
  • Consumes: when p consumes some input.

Example:

(def word (many-some char/alpha?)

This parser applies the parser `p` _one_ or more times. Returns a sequence of
the returned values of `p`.

- Fails: when `p` does not succeed at least once.
- Consumes: when `p` consumes some input.

Example:

   (def word
     (many-some char/alpha?)
sourceraw docstring

many-tillclj/s

(many-till p end)

This parser applies parser p zero or more times until parser end succeeds. Returns a sequence of values returned by p.

  • Fails:
    • when p fails.
    • when end does not succeed before end of input.
  • Consumes:
    • when p or end consumes some input.

Example:

(def simple-comment
  (after (word "<!--")
         (many-till any-token (maybe (word "-->")))))

Note the overlapping parsers any-token and (word "-->"), and therefore the use of the maybe combinator.

This parser applies parser `p` _zero_ or more times until parser `end`
succeeds. Returns a sequence of values returned by `p`.

- Fails:
    - when `p` fails.
    - when `end` does not succeed before end of input.
- Consumes:
    - when `p` or `end` consumes some input.

Example:

    (def simple-comment
      (after (word "<!--")
             (many-till any-token (maybe (word "-->")))))

Note the overlapping parsers `any-token` and `(word "-->")`, and
therefore the use of the `maybe` combinator.
sourceraw docstring

many-zeroclj/s

(many-zero p)

This parser applies the parser p zero or more times. Returns a sequence of the returned values or p.

  • Fails: when p fails and consumes some input.
  • Consumes: when p consumes some input.

Example:

(def identifier
  (bind-let [c char/alpha?
             cs (many-zero (choice char/alpha-numeric?
                                   (char/one-of? "_")))]
    (result (cons c cs))))
This parser applies the parser `p` zero or more times. Returns a sequence of
the returned values or `p`.

- Fails: when `p` fails and consumes some input.
- Consumes: when `p` consumes some input.

Example:

    (def identifier
      (bind-let [c char/alpha?
                 cs (many-zero (choice char/alpha-numeric?
                                       (char/one-of? "_")))]
        (result (cons c cs))))
sourceraw docstring

maybeclj/s

(maybe p)

This parser behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs.

  • Fails: when p fails.
  • Consumes: when p succeeds and consumes some input.

This combinator is used whenever arbitrary look ahead is needed. Since it pretends that it hasn't consumed any input when p fails, the choice combinator will try its second alternative even when the first parser failed while consuming input.

The maybe combinator can for example be used to distinguish identifiers and reserved words. Both reserved words and identifiers are a sequence of letters. Whenever we expect a certain reserved word where we can also expect an identifier we have to use the maybe combinator. Suppose we write:

(def identifier
  (many-some char/alpha?))

(def let-expr
  (after (word "let")
         ...))

(def expr
  (-> (choice let-expr
              identifier)
      (expecting "expression"))

If the user writes "lexical", the parser fails with: unexpected "x", expecting "t" of (word "let"). Indeed, since the choice combinator only tries alternatives when the first alternative hasn't consumed input, the identifier parser is never tried (because the prefix "le" of the (word "let") parser is already consumed). The right behaviour can be obtained by adding the maybe combinator:

(def let-expr
  (after (maybe (word "let"))
         ...))
This parser behaves like parser `p`, except that it pretends that it hasn't
consumed any input when an error occurs.

- Fails: when `p` fails.
- Consumes: when `p` succeeds and consumes some input.

This combinator is used whenever arbitrary look ahead is needed. Since it
pretends that it hasn't consumed any input when `p` fails, the `choice`
combinator will try its second alternative even when the first parser failed
while consuming input.

The `maybe` combinator can for example be used to distinguish identifiers and
reserved words. Both reserved words and identifiers are a sequence of letters.
Whenever we expect a certain reserved word where we can also expect an
identifier we have to use the `maybe` combinator. Suppose we write:

    (def identifier
      (many-some char/alpha?))

    (def let-expr
      (after (word "let")
             ...))

    (def expr
      (-> (choice let-expr
                  identifier)
          (expecting "expression"))

If the user writes "lexical", the parser fails with: `unexpected "x",
expecting "t" of (word "let")`. Indeed, since the `choice` combinator
only tries alternatives when the first alternative hasn't consumed input, the
`identifier` parser is never tried (because the prefix "le" of the `(word
"let")` parser is already consumed). The right behaviour can be obtained by
adding the `maybe` combinator:

    (def let-expr
      (after (maybe (word "let"))
             ...))
sourceraw docstring

not-followed-byclj/s

(not-followed-by q)
(not-followed-by p q)

This parser behaves like parser p, except that it only succeeds when parser q fails. This parser can be used to implement the 'longest match' rule. For example, when recognizing keywords (for example let), we want to make sure that a keyword is not followed by a legal identifier character, in which case the keyword is actually an identifier (for example lets). We can write this behaviour as follows:

(-> (word "let")
    (not-followed-by char/alpha-numeric?))
  • Fails:
    • when p fails.
    • when q succeeds.
  • Consumes:
    • when p consumes some input.
This parser behaves like parser `p`, except that it only succeeds when parser
`q` fails. This parser can be used to implement the 'longest match' rule. For
example, when recognizing keywords (for example `let`), we want to make sure
that a keyword is not followed by a legal identifier character, in which case
the keyword is actually an identifier (for example `lets`). We can write this
behaviour as follows:

    (-> (word "let")
        (not-followed-by char/alpha-numeric?))

- Fails:
    - when `p` fails.
    - when `q` succeeds.
- Consumes:
    - when `p` consumes some input.
sourceraw docstring

optionalclj/s

(optional p)
(optional p x)

This parser tries to apply parser p. If p fails without consuming input, it returns the value x (or nil), otherwise the value returned by p. Unlike Haskell's optional combinator it does not discard the result of p and behaves like option combinator.

  • Fails: when p fails and consumes come input.
  • Consumes: when p consumes some input.
This parser tries to apply parser `p`. If `p` fails without consuming input,
it returns the value `x` (or `nil`), otherwise the value returned by `p`.
Unlike Haskell's `optional` combinator it does not discard the result of `p`
and behaves like `option` combinator.

- Fails: when `p` fails and consumes come input.
- Consumes: when `p` consumes some input.
sourceraw docstring

parseclj/s≠

clj
(parse p input)
(parse p input {:keys [pos tab user-state] :as options})
cljs
(parse p input)
(parse p input opts)

Executes parser p given input sequence of tokens, returns result value or throws exception on parsing error.

Options:

:pos The instance of InputPos or keyword for pos/init-pos to init parser pos. By default pos is initialized to TextPos for string input or first token of char type, or IndexPos otherwise.

:tab Tab size for TextPos, which is 8 by default.

:user-state Initial value of user state.

Executes parser `p` given `input` sequence of tokens, returns result value or
throws exception on parsing error.

Options:

**`:pos`** The instance of InputPos or keyword for `pos/init-pos` to
init parser pos. By default pos is initialized to TextPos for string input or
first token of char type, or IndexPos otherwise.

**`:tab`** Tab size for TextPos, which is 8 by default.

**`:user-state`** Initial value of user state.
sourceraw docstring

parse*clj/s≠

clj
(parse* p input)
(parse* p input {:keys [pos tab user-state] :as options})
cljs
(parse* p input)
(parse* p input opts)

Executes parser p given input sequence of tokens, returns reply record. See parse for available options.

Executes parser `p` given `input` sequence of tokens, returns reply record.
See `parse` for available options.
sourceraw docstring

register-word-testclj/s

(register-word-test k f)

Associates keyword k with test-fn of the word parser.

Associates keyword `k` with test-fn of the `word` parser.
sourceraw docstring

renderclj/s

(render obj)

Returns string representation of the obj in parser error messages.

Returns string representation of the `obj` in parser error messages.
sourceraw docstring

resultclj/s

(result x)

This parser always succeeds with value x without consuming any input.

  • Fails: never.
  • Consumes: never.
This parser always succeeds with value `x` without consuming any input.

- Fails: never.
- Consumes: never.
sourceraw docstring

sep-by-end-someclj/s

(sep-by-end-some p sep)

Parses one or more occurrences of p, separated and ended by sep. Returns a sequence of values returned by p.

Parses _one_ or more occurrences of `p`, separated and ended by `sep`.
Returns a sequence of values returned by `p`.
sourceraw docstring

sep-by-end-zeroclj/s

(sep-by-end-zero p sep)

Parses zero or more occurrences of p, separated and ended by sep. Returns a sequence of values returned by p.

Parses _zero_ or more occurrences of `p`, separated and ended by `sep`.
Returns a sequence of values returned by `p`.
sourceraw docstring

sep-by-opt-end-someclj/s

(sep-by-opt-end-some p sep)

Parses one or more occurrences of p, separated and optionally ended by sep. Returns a sequence of values returned by p.

Parses _one_ or more occurrences of `p`, separated and optionally ended by
`sep`. Returns a sequence of values returned by `p`.
sourceraw docstring

sep-by-opt-end-zeroclj/s

(sep-by-opt-end-zero p sep)

Parses zero or more occurrences of p, separated and optionally ended by sep. Returns a sequence of values returned by p.

Parses _zero_ or more occurrences of `p`, separated and optionally ended by
`sep`. Returns a sequence of values returned by `p`.
sourceraw docstring

sep-by-someclj/s

(sep-by-some p sep)

Parses one or more occurrences of p, separated by sep. Returns a sequence of values returned by p.

Parses _one_ or more occurrences of `p`, separated by `sep`. Returns a
sequence of values returned by `p`.
sourceraw docstring

sep-by-zeroclj/s

(sep-by-zero p sep)

Parses zero or more occurrences of p, separated by sep. Returns a sequence of values returned by p.

(defn comma-sep [p]
  (sep-by-zero p (after (char/one-of? ",")
                        (skip-zero char/whitespace?))))
Parses _zero_ or more occurrences of `p`, separated by `sep`. Returns a
sequence of values returned by `p`.

    (defn comma-sep [p]
      (sep-by-zero p (after (char/one-of? ",")
                            (skip-zero char/whitespace?))))
sourceraw docstring

skip-someclj/s

(skip-some p)

This parser applies the parser p one or more times, skipping its result.

  • Fails: when p does not succeed at least once.
  • Consumes: when p consumes some input.
This parser applies the parser `p` _one_ or more times, skipping its result.

- Fails: when `p` does not succeed at least once.
- Consumes: when `p` consumes some input.
sourceraw docstring

skip-zeroclj/s

(skip-zero p)

This parser applies the parser p zero or more times, skipping its result.

  • Fails: when p fails and consumes some input.
  • Consumes: when p consumes some input.

Example:

(def spaces
  (skip-zero char/whitespace?))
This parser applies the parser `p` zero or more times, skipping its result.

- Fails: when `p` fails and consumes some input.
- Consumes: when `p` consumes some input.

Example:

    (def spaces
      (skip-zero char/whitespace?))
sourceraw docstring

timesclj/s

(times n p)

Parses n occurrences of p. If n is smaller or equal to zero, the parser equals to (return nil). Returns a sequence of n values returned by p.

Parses `n` occurrences of `p`. If `n` is smaller or equal to zero, the parser
equals to `(return nil)`. Returns a sequence of `n` values returned by `p`.
sourceraw docstring

tokenclj/s

(token pred)
(token pred msg)

This parser accepts a token when (pred token) returns logical true, and optional expecting msg. The pred can carry expecting error message in ::expecting metadata. 1-arity behaves as pred and can be used in predicate composition.

  • Fails: when (pred token) return logical false.
  • Consumes: when succeeds.
This parser accepts a token when `(pred token)` returns logical true, and
optional expecting `msg`. The `pred` can carry expecting error message in
`::expecting` metadata. 1-arity behaves as `pred` and can be used in predicate
composition.

- Fails: when `(pred token)` return logical false.
- Consumes: when succeeds.
sourceraw docstring

tupleclj/s

(tuple p q & ps)

This parser tries to apply argument parsers in order until all of them succeeds. Returns a sequence of values returned by every parser. It is a 2+ arity version of the each parser.

  • Fails: when any of tried parsers fails.
  • Consumes: when any of tried parsers consumes some input.
This parser tries to apply argument parsers in order until all of them
succeeds. Returns a sequence of values returned by every parser. It is a 2+
arity version of the `each` parser.

- Fails: when any of tried parsers fails.
- Consumes: when any of tried parsers consumes some input.
sourceraw docstring

withclj/s

(with p f)
(with p f g)
(with p f g h)
(with p f g h & more)

This parser applies series of functions to the result value of the parser p.

  • Fails: when p fails.
  • Consumes: when p consumes some input.
This parser applies series of functions to the result value of the parser `p`.

- Fails: when `p` fails.
- Consumes: when `p` consumes some input.
sourceraw docstring

wordclj/s

(word tokens)
(word tokens test-fn)

Parses a sequence of tokens given by ts and returns ts. The optional function (test-fn word-token input-token) is used to match tokens differently than simple equality. The test-fn can be referred by keyword registered using register-word-test-fn. There are two predefined keywords registered: :default for = and :i for case insensitive char comparison.

  • Fails: when any of tokens don't match the input.
  • Consumes: when at least first token matches the input.

Example:

(def let-keyword (word "let"))

(def let-keyword-ignorecase (word "select" :i))
Parses a sequence of tokens given by `ts` and returns `ts`. The optional
function `(test-fn word-token input-token)` is used to match tokens
differently than simple equality. The `test-fn` can be referred by keyword
registered using `register-word-test-fn`. There are two predefined keywords
registered: `:default` for `=` and `:i` for case insensitive char comparison.

- Fails: when any of tokens don't match the input.
- Consumes: when at least first token matches the input.

Example:

    (def let-keyword (word "let"))

    (def let-keyword-ignorecase (word "select" :i))
sourceraw docstring

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

× close