Overview
Parser combinators after FParsec: small parsers, combined into grammars, with errors that say where and what.
(import (text parser-combinators))
;; A list of integers: [1, 2, 3]
(: ints (Parser (List int)))
(def ints
(between (pchar #\[) (pchar #\])
(sep-by (pleft pint spaces) (pleft (pchar #\,) spaces))))
(run-parser ints "[1, 2, 3]") ; (Ok '(1 2 3))
(println (run-parser ints "[1, x]"))
;; Parse error at line 1, column 5:
;; [1, x]
;; ^
;; Expecting: an integer
;; Unexpected: 'x'
A (Parser %a) reads from the front of some text and answers an
%a, or fails. Small parsers that read one thing — a character
(pchar), a literal (pstring), a number (pint) — are
combined into bigger ones: in sequence (pright, ppipe2),
as alternatives (por, choice), repeated (many,
sep-by), with operators (chainl1). A grammar is a
parser like any other, and run-parser runs one on a string.
Consuming. A parser moves through the text as it reads, and
one that fails may already have moved. Whether it did is what everything
here turns on. por tries its second parser only when the first
failed without reading anything: once a parser has read part of
something, its error is the one worth reporting, not the next
alternative’s complaint that it does not fit either. When a grammar
does need to back up and try again, it says so with attempt.
pstring, pint and pdouble back up by themselves,
so a choice between literals needs no attempt.
Errors. A failure records where it happened and what was
expected there. Two failures at the same place combine, so a choice
reports every alternative it tried: Expecting: 'yes' or 'no'.
plabel names what a parser expects in the reader’s words,
pfail says something else entirely, and the error printed by
run-parser/message shows the line with a caret under the column.
Columns count characters, not bytes.
The end. run-parser stops where the grammar stops and
does not mind text after it. To insist on all of it, end the grammar
with eof.
Recursive grammars. A grammar of defuns may call itself
freely. One built of defs needs a parser to refer to before it is
defined: pforward makes the reference, pforwarded uses it,
and pforward-set! fills it in. plazy is the other way.
Copies and slices. Text a parser answers is copied out of the
input, except from many-satisfy/slice and
many-satisfy1/slice, which answer slices of it: faster for long
runs, but each one keeps the whole input alive while it is held.
See also: run-parser, por, attempt, Parser
Reference
any-ofattemptbetweenchainl1chainr1choiceerror->stringfollowed-bylook-aheadmake-statemanymany-charsmany-chars1many-foldmany-satisfymany-satisfy/slicemany-satisfy1many-satisfy1/slicemany-tillmany1none-ofnot-followed-byoptopt-oroptionalparse-error-positionpbindpbothpcharpchar/cipfailpforwardpforward-set!pforwardedplabelplazypleftpmapporppipe2ppipe3prepeatpreturnprightpsequencepstringpstring/cipzerorun-parserrun-parser/messagesatisfysep-bysep-by1sep-end-bysep-end-by1skip-manyskip-many1state-position
Types
Parser
type alias ↑ Types
A parser: reads from a State and answers a value or a Failure.
%a- What it answers.
A parser is a function, so a parser written by hand is a
(fun (st) ...) that reads st and answers (Ok v) or
(Err f). Most grammars never write one: the combinators here
build them.
See also: run-parser, make-state
State
record ↑ Types
Where a parser is in its input. Made by make-state; run-parser makes one for you.
text- The input.
pos- The cursor: where the next character is read.
line- The line pos is on, from 1.
line-start- Where that line begins, for the column.
See also: make-state, state-position
Position
record ↑ Types
A line and a column, both from 1. The column counts characters.
line- The line, from 1.
column- The column, from 1, in characters.
See also: pposition, parse-error-position
Failure
record ↑ Types
How a parser failed: where, and what was expected there. run-parser turns it into a ParseError.
See also: ParseError, run-parser
ParseError
record ↑ Types
A failed parse: the input, where it failed, and what was expected.
source- The input.
err-pos- Where the parse failed.
err-line- The line of err-pos, from 1.
err-line-start- Where that line begins.
items- What was expected and found there; read it through error->string.
displayed of a ParseError is error->string, so
println shows the whole message.
See also: error->string, parse-error-position
ErrorItem
union ↑ Types
One thing an error says.
Expected- Something that would have been accepted: 'x', an integer.
Unexpected- Something found that should not be there.
ErrorMessage- A sentence of its own, from pfail.
ErrorItems
union ↑ Types
What an error says, gathered from every parser that failed at its position.
See also: error->string
ParserRef
record ↑ Types
A parser to be filled in later, for a grammar of defs that refers to itself.
%a- What the parser answers.
target- The parser, once pforward-set! has set it.
See also: pforward, pforwarded, pforward-set!
Functions
run-parser
function ↑ Functions
(: run-parser (-> (Parser %a) string (Result ParseError %a)))
(run-parser p text)
Runs a parser on a string.
p- The parser.
text- The input.
- returns
- What p answers, or where and why it failed.
(run-parser pint "42") ; (Ok 42)
(run-parser pint "42abc") ; (Ok 42): the rest is not looked at
(run-parser (pleft pint eof) "42abc")
;; (Err ...): Expecting: end of input
See also: run-parser/message, eof
run-parser/message
function ↑ Functions
(: run-parser/message (-> (Parser %a) string (Result string %a)))
(run-parser/message p text)
Runs a parser on a string, with a failure as the message error->string writes.
p- The parser.
text- The input.
- returns
- What p answers, or the error as text.
(run-parser/message (pstring "yes") "no")
;; (Err "Parse error at line 1, column 1:\nno\n^\nExpecting: 'yes'\nUnexpected: 'n'")
See also: run-parser
error->string
function ↑ Functions
(: error->string (-> ParseError string))
(error->string e)
An error as a message: where, the line with a caret under the column, and what was expected and found.
e- The error.
- returns
- The message, several lines long.
(match (run-parser (por (pstring "yes") (pstring "no")) "maybe")
((Err e) (println (error->string e)))
((Ok _) unit))
;; Parse error at line 1, column 1:
;; maybe
;; ^
;; Expecting: 'yes' or 'no'
;; Unexpected: 'm'
See also: run-parser/message, parse-error-position
parse-error-position
function ↑ Functions
(: parse-error-position (-> ParseError Position))
(parse-error-position e)
Where an error is.
e- The error.
- returns
- Its line and column.
(match (run-parser (pright (pstring "ab\n") pint) "ab\nx")
((Err e) (println (parse-error-position e))) ; line 2, column 1
((Ok _) unit))
make-state
function ↑ Functions
(: make-state (-> string State))
(make-state text)
A State at the start of a string, for calling a parser by hand.
text- The input.
- returns
- The state.
(def st (make-state "12 34"))
(pint st) ; (Ok 12)
(state-position st) ; line 1, column 3
See also: run-parser, state-position
state-position
function ↑ Functions
(: state-position (-> State Position))
(state-position st)
Where a state is.
st- The state.
- returns
- Its line and column.
See also: pposition, make-state
preturn
function ↑ Functions
(: preturn (-> %a (Parser %a)))
(preturn v)
Succeeds with a value, reading nothing.
v- The value.
- returns
- A parser answering v.
(run-parser (preturn 7) "anything") ; (Ok 7)
pzero
function ↑ Functions
(: pzero (Parser %a))
(pzero st)
Fails, reading nothing and expecting nothing.
st- The state: pzero is a parser, used as one rather than called.
- returns
- A failure with nothing to say.
pfail
function ↑ Functions
(: pfail (-> string (Parser %a)))
(pfail msg)
Fails with a message of its own.
msg- What the error says.
- returns
- A parser that always fails.
(pbind pint (fun (n) (if (< n 256) (preturn n) (pfail "a byte is at most 255"))))
See also: plabel
pbind
function ↑ Functions
(: pbind (-> (Parser %a) (-> %a (Parser %b)) (Parser %b)))
(pbind p f)
Runs p, and then the parser f makes from what p answered.
p- The first parser.
f- Makes the second parser from p's value.
- returns
- The second parser's value.
;; A count, then that many letters: "3abc"
(pbind pint (fun (n) (prepeat n letter)))
pmap
function ↑ Functions
(: pmap (-> (-> %a %b) (Parser %a) (Parser %b)))
(pmap f p)
Changes what a parser answers.
f- Applied to the value.
p- The parser.
- returns
- f of p's value.
(run-parser (pmap string-length (many-satisfy char-alphabetic?)) "hello!") ; (Ok 5)
pright
function ↑ Functions
(: pright (-> (Parser %a) (Parser %b) (Parser %b)))
(pright p q)
p, then q, answering q’s value.
p- Read and dropped.
q- Answers.
- returns
- q's value.
(run-parser (pright (pchar #\$) pint) "$42") ; (Ok 42)
pleft
function ↑ Functions
(: pleft (-> (Parser %a) (Parser %b) (Parser %a)))
(pleft p q)
p, then q, answering p’s value.
p- Answers.
q- Read and dropped.
- returns
- p's value.
(run-parser (pleft pint (pchar #\%)) "42%") ; (Ok 42)
See also: pright
pboth
function ↑ Functions
(: pboth (-> (Parser %a) (Parser %b) (Parser (Tuple %a %b))))
(pboth p q)
p, then q, answering both.
p- The first.
q- The second.
- returns
- Both values, as a tuple.
(run-parser (pboth letter digit) "a1") ; (Ok (Tuple #\a #\1))
See also: ppipe2
ppipe2
function ↑ Functions
(: ppipe2 (-> (Parser %a) (Parser %b) (-> %a %b %c) (Parser %c)))
(ppipe2 p q f)
p, then q, combined by f.
p- The first.
q- The second.
f- Makes the result from both values.
- returns
- f of both values.
(run-parser (ppipe2 pint (pright (pchar #\x) pint) *) "6x7") ; (Ok 42)
ppipe3
function ↑ Functions
(: ppipe3 (-> (Parser %a) (Parser %b) (Parser %c) (-> %a %b %c %d) (Parser %d)))
(ppipe3 p q r f)
p, q and r, combined by f.
p- The first.
q- The second.
r- The third.
f- Makes the result from the three values.
- returns
- f of the three values.
;; 2026-10-05
(ppipe3 pint (pright (pchar #\-) pint) (pright (pchar #\-) pint)
(fun (y m d) (Tuple y m d)))
psequence
function ↑ Functions
(: psequence (-> (List (Parser %a)) (Parser (List %a))))
(psequence ps)
Each parser in turn, answering all their values.
ps- The parsers, in order.
- returns
- Their values, in order.
(run-parser (psequence (list digit letter digit)) "1a2") ; (Ok '(#\1 #\a #\2))
por
function ↑ Functions
(: por (-> (Parser %a) (Parser %a) (Parser %a)))
(por p q)
p, or q when p failed without reading anything.
p- Tried first.
q- Tried when p failed where it began.
- returns
- The value of whichever succeeded.
(def answer (por (pstring "yes") (pstring "no")))
(run-parser answer "no") ; (Ok "no")
(run-parser answer "maybe") ; (Err ...): Expecting: 'yes' or 'no'
Two failures at the same position become one error expecting what
either expected. When p read something before it failed,
q is not tried; see attempt.
choice
function ↑ Functions
(: choice (-> (List (Parser %a)) (Parser %a)))
(choice ps)
The first of several parsers that succeeds, under por’s rule.
ps- The alternatives, in order.
- returns
- The value of the first that succeeds.
(choice (list (pstring "red") (pstring "green") (pstring "blue")))
;; on "pink": Expecting: 'red', 'green' or 'blue'
See also: por
attempt
function ↑ Functions
(: attempt (-> (Parser %a) (Parser %a)))
(attempt p)
Runs p, and on failure moves back to where p began.
p- The parser.
- returns
- p's value.
(def a-then-b (pright (pchar #\a) (pstring "b")))
(run-parser (por a-then-b (pstring "ac")) "ac")
;; (Err ...): Expecting: 'b'. a-then-b read the a, so "ac" is not tried.
(run-parser (por (attempt a-then-b) (pstring "ac")) "ac") ; (Ok "ac")
This is what lets por try another alternative after one that
read part of its input. Use it around the alternative that may read
and then fail, and no wider: an error found inside an attempt
is reported at its start.
See also: por, look-ahead
look-ahead
function ↑ Functions
(: look-ahead (-> (Parser %a) (Parser %a)))
(look-ahead p)
Runs p and moves back to where it began, whether p succeeded or not.
p- The parser.
- returns
- p's value, with nothing read.
(run-parser (pboth (look-ahead letter) (many-satisfy char-alphabetic?)) "abc")
;; (Ok (Tuple #\a "abc"))
See also: followed-by, attempt
followed-by
function ↑ Functions
(: followed-by (-> (Parser %a) string (Parser void)))
(followed-by p what)
Succeeds, reading nothing, when p would succeed here.
p- The parser to test with.
what- What the error says was expected.
- returns
- A parser answering nothing.
(pleft pint (followed-by (pchar #\;) "';'")) ; 42 only when a ; comes next
See also: not-followed-by, look-ahead
not-followed-by
function ↑ Functions
(: not-followed-by (-> (Parser %a) string (Parser void)))
(not-followed-by p what)
Succeeds, reading nothing, when p would fail here.
p- The parser to test with.
what- What the error says was found.
- returns
- A parser answering nothing.
;; The keyword "if", but not the start of "iffy".
(pleft (pstring "if") (not-followed-by alphanumeric "a longer name"))
See also: followed-by
plabel
function ↑ Functions
(: plabel (-> (Parser %a) string (Parser %a)))
(plabel p what)
Names what p expects, in the words an error should use.
p- The parser.
what- What to call it.
- returns
- p, with its expectation renamed.
(plabel (many-satisfy1 char-alphabetic? "a letter") "a name")
;; on "42": Expecting: a name
Only a failure where p began is renamed: once p has read
something, its own error says more than the label.
See also: pfail
satisfy
function ↑ Functions
(: satisfy (-> (-> char bool) string (Parser char)))
(satisfy pred what)
One character that pred accepts.
pred- Accepts or refuses a character.
what- What an error calls it.
- returns
- The character.
(run-parser (satisfy char-upper-case? "a capital") "Bob") ; (Ok #\B)
See also: many-satisfy, any-of
pchar
function ↑ Functions
(: pchar (-> char (Parser char)))
(pchar c)
One particular character.
c- The character.
- returns
- c.
(run-parser (pchar #\x) "xyz") ; (Ok #\x)
pchar/ci
function ↑ Functions
(: pchar/ci (-> char (Parser char)))
(pchar/ci c)
One particular character, in either case.
c- The character.
- returns
- The character as the input has it.
(run-parser (pchar/ci #\x) "XYZ") ; (Ok #\X)
any-of
function ↑ Functions
(: any-of (-> string (Parser char)))
(any-of s)
One character of those in a string.
s- The characters accepted.
- returns
- The character.
(run-parser (any-of "+-*/") "*2") ; (Ok #\*)
none-of
function ↑ Functions
(: none-of (-> string (Parser char)))
(none-of s)
One character that is not in a string.
s- The characters refused.
- returns
- The character.
;; A string literal's body: anything but a quote.
(between (pchar #\") (pchar #\") (many-chars (none-of "\"")))
pstring
function ↑ Functions
(: pstring (-> string (Parser string)))
(pstring s)
A literal, all of it or nothing: a partial match reads nothing.
s- The literal.
- returns
- s.
(run-parser (por (pstring "foo") (pstring "far")) "far") ; (Ok "far")
;; "foo" read the f before it failed, and gave it back.
See also: pstring/ci, por
pstring/ci
function ↑ Functions
(: pstring/ci (-> string (Parser string)))
(pstring/ci s)
A literal in any case, all of it or nothing.
s- The literal.
- returns
- The text as the input has it.
(run-parser (pstring/ci "select") "SELECT *") ; (Ok "SELECT")
many-satisfy
function ↑ Functions
(: many-satisfy (-> (-> char bool) (Parser string)))
(many-satisfy pred)
The characters pred accepts, as many as come. Never fails.
pred- Accepts or refuses a character.
- returns
- The run, which may be empty.
(run-parser (many-satisfy char-numeric?) "2026-10") ; (Ok "2026")
See also: many-satisfy1, many-satisfy/slice
many-satisfy1
function ↑ Functions
(: many-satisfy1 (-> (-> char bool) string (Parser string)))
(many-satisfy1 pred what)
The characters pred accepts, at least one.
pred- Accepts or refuses a character.
what- What an error calls one.
- returns
- The run.
(def identifier (many-satisfy1 char-alphabetic? "a letter"))
See also: many-satisfy, many-satisfy1/slice
many-satisfy/slice
function ↑ Functions
(: many-satisfy/slice (-> (-> char bool) (Parser string)))
(many-satisfy/slice pred)
many-satisfy, answering a slice of the input instead of a copy.
pred- Accepts or refuses a character.
- returns
- The run, sharing the input's bytes.
;; Lines, looked at and dropped: no line is copied.
(many (pleft (many-satisfy/slice #(not (char=? &1 #\newline))) pnewline))
Faster than many-satisfy for a run of more than a few bytes,
and it allocates less. A slice keeps the whole input alive for as long
as it is held, so keep one with string-copy.
See also: many-satisfy, many-satisfy1/slice
many-satisfy1/slice
function ↑ Functions
(: many-satisfy1/slice (-> (-> char bool) string (Parser string)))
(many-satisfy1/slice pred what)
many-satisfy1, answering a slice of the input instead of a copy.
pred- Accepts or refuses a character.
what- What an error calls one.
- returns
- The run, sharing the input's bytes.
See also: many-satisfy1, many-satisfy/slice
many-chars
function ↑ Functions
(: many-chars (-> (Parser char) (Parser string)))
(many-chars p)
The characters a parser reads, as many as it reads, as a string.
p- Reads one character.
- returns
- The characters, which may be none.
;; Escapes as well as plain characters, which many-satisfy cannot do.
(many-chars (por (pright (pchar #\\) any-char) (none-of "\"\\")))
See also: many-satisfy, many-chars1
many-chars1
function ↑ Functions
(: many-chars1 (-> (Parser char) (Parser string)))
(many-chars1 p)
many-chars, at least one character.
p- Reads one character.
- returns
- The characters.
See also: many-chars
many
function ↑ Functions
(: many (-> (Parser %a) (Parser (List %a))))
(many p)
p, as many times as it succeeds.
p- The parser repeated. It has to read something each time.
- returns
- Every value, which may be none.
(run-parser (many (pleft pint spaces)) "1 2 3") ; (Ok '(1 2 3))
The repetition ends when p fails where it began. A failure
after p read something fails the whole many. A p
that succeeds without reading would repeat forever, and panics.
many1
function ↑ Functions
(: many1 (-> (Parser %a) (Parser (List %a))))
(many1 p)
p, at least once and as many times as it succeeds.
p- The parser repeated.
- returns
- Every value.
See also: many
skip-many
function ↑ Functions
(: skip-many (-> (Parser %a) (Parser void)))
(skip-many p)
p, as many times as it succeeds, keeping nothing.
p- The parser repeated.
- returns
- Nothing.
See also: many, skip-many1
skip-many1
function ↑ Functions
(: skip-many1 (-> (Parser %a) (Parser void)))
(skip-many1 p)
p, at least once, keeping nothing.
p- The parser repeated.
- returns
- Nothing.
See also: skip-many
many-fold
function ↑ Functions
(: many-fold (-> (-> %a %s %s) %s (Parser %a) (Parser %s)))
(many-fold f init p)
p, as many times as it succeeds, folding the values as they come.
f- Combines a value with what was folded so far.
init- Where the fold starts.
p- The parser repeated.
- returns
- The fold.
(run-parser (many-fold + 0 (pleft pint spaces)) "1 2 3") ; (Ok 6)
See also: many
many-till
function ↑ Functions
(: many-till (-> (Parser %a) (Parser %end) (Parser (List %a))))
(many-till p end)
p, until end succeeds. end is tried first.
p- The parser repeated.
end- What ends it; its value is dropped.
- returns
- p's values.
;; A comment: everything up to */
(pright (pstring "/*") (many-till any-char (pstring "*/")))
prepeat
function ↑ Functions
(: prepeat (-> int (Parser %a) (Parser (List %a))))
(prepeat n p)
p exactly n times.
n- How many.
p- The parser repeated.
- returns
- The n values.
(run-parser (prepeat 3 digit) "12345") ; (Ok '(#\1 #\2 #\3))
sep-by
function ↑ Functions
(: sep-by (-> (Parser %a) (Parser %sep) (Parser (List %a))))
(sep-by p sep)
p, separated by sep, zero or more times.
p- The item.
sep- The separator; its value is dropped.
- returns
- The items.
(run-parser (sep-by pint (pchar #\,)) "1,2,3") ; (Ok '(1 2 3))
(run-parser (sep-by pint (pchar #\,)) "") ; (Ok '())
See also: sep-by1, sep-end-by
sep-by1
function ↑ Functions
(: sep-by1 (-> (Parser %a) (Parser %sep) (Parser (List %a))))
(sep-by1 p sep)
p, separated by sep, at least once.
p- The item.
sep- The separator.
- returns
- The items.
See also: sep-by
sep-end-by
function ↑ Functions
(: sep-end-by (-> (Parser %a) (Parser %sep) (Parser (List %a))))
(sep-end-by p sep)
sep-by, also accepting a separator after the last item.
p- The item.
sep- The separator.
- returns
- The items.
(run-parser (sep-end-by pint (pchar #\;)) "1;2;") ; (Ok '(1 2))
See also: sep-by
sep-end-by1
function ↑ Functions
(: sep-end-by1 (-> (Parser %a) (Parser %sep) (Parser (List %a))))
(sep-end-by1 p sep)
sep-end-by, at least one item.
p- The item.
sep- The separator.
- returns
- The items.
See also: sep-end-by
between
function ↑ Functions
(: between (-> (Parser %open) (Parser %close) (Parser %a) (Parser %a)))
(between popen pclose p)
p, between an opening and a closing parser.
popen- Read first and dropped.
pclose- Read last and dropped.
p- Answers.
- returns
- p's value.
(run-parser (between (pchar #\() (pchar #\)) pint) "(42)") ; (Ok 42)
opt
function ↑ Functions
(: opt (-> (Parser %a) (Parser (Option %a))))
(opt p)
p if it is there.
p- The parser.
- returns
- (Some value), or None when p failed where it began.
(run-parser (opt (pchar #\-)) "5") ; (Ok None)
opt-or
function ↑ Functions
(: opt-or (-> (Parser %a) %a (Parser %a)))
(opt-or p fallback)
p if it is there, and otherwise a fallback.
p- The parser.
fallback- The value when p is not there.
- returns
- p's value or the fallback.
(run-parser (opt-or pint 1) "x") ; (Ok 1)
optional
function ↑ Functions
(: optional (-> (Parser %a) (Parser void)))
(optional p)
p if it is there, keeping nothing.
p- The parser.
- returns
- Nothing.
See also: opt
chainl1
function ↑ Functions
(: chainl1 (-> (Parser %a) (Parser (-> %a %a %a)) (Parser %a)))
(chainl1 p op)
One or more p, joined by operators, grouped from the left.
p- An operand.
op- An operator, answering how to combine two operands.
- returns
- The operands, combined.
(def minus (pright (pchar #\-) (preturn -)))
(run-parser (chainl1 pint minus) "10-3-2") ; (Ok 5): (10-3)-2
Precedence is one chainl1 per level, the tighter inside the
looser: a sum is a chainl1 of products, and a product a
chainl1 of factors.
See also: chainr1
chainr1
function ↑ Functions
(: chainr1 (-> (Parser %a) (Parser (-> %a %a %a)) (Parser %a)))
(chainr1 p op)
One or more p, joined by operators, grouped from the right.
p- An operand.
op- An operator, answering how to combine two operands.
- returns
- The operands, combined.
(def minus (pright (pchar #\-) (preturn -)))
(run-parser (chainr1 pint minus) "10-3-2") ; (Ok 9): 10-(3-2)
See also: chainl1
pforward
function ↑ Functions
(: pforward (-> (ParserRef %a)))
(pforward)
A parser to refer to before it is defined.
- returns
- An empty reference, to fill in with pforward-set!.
;; Nested lists of integers: [1, [2, 3]]
(type (: Nested (Union (: Num int) (: Nest (List Nested)))))
(: value-ref (ParserRef Nested))
(def value-ref (pforward))
(def value (pforwarded value-ref))
(def nested
(pmap Nest (between (pchar #\[) (pchar #\]) (sep-by value (pleft (pchar #\,) spaces)))))
(pforward-set! value-ref (por (pmap Num pint) nested))
See also: pforwarded, pforward-set!, plazy
pforwarded
function ↑ Functions
(: pforwarded (-> (ParserRef %a) (Parser %a)))
(pforwarded r)
The parser a reference holds, whenever it is run.
r- The reference.
- returns
- A parser that runs what r holds at the time.
See also: pforward
pforward-set!
function ↑ Functions
(: pforward-set! (-> (ParserRef %a) (Parser %a) void))
(pforward-set! r p)
Fills in a reference.
r- The reference.
p- The parser it stands for.
See also: pforward
plazy
function ↑ Functions
(: plazy (-> (-> (Parser %a)) (Parser %a)))
(plazy thunk)
A parser made the first time it is needed, by a function.
thunk- Makes the parser. Called once.
- returns
- The parser it makes.
;; A grammar of functions. Building (expr) would build (expr) again,
;; forever; plazy builds the inner one when it is first run.
(: expr (-> (Parser int)))
(defun (expr)
(por pint (plazy (fun () (between (pchar #\() (pchar #\)) (expr))))))
(run-parser (expr) "((7))") ; (Ok 7)
See also: pforward
Values
pposition
value ↑ Values
(: pposition (Parser Position))
Where the parser is, reading nothing.
;; Each word with where it starts.
(many (pleft (pboth pposition (many-satisfy1 char-alphabetic? "a word")) spaces))
See also: Position, state-position
any-char
value ↑ Values
(: any-char (Parser char))
Any one character. Fails only at the end of the input.
spaces
value ↑ Values
(: spaces (Parser Unit))
Spaces, tabs and line breaks, as many as come. Never fails.
(pleft pint spaces) ; a number and whatever space follows it
See also: spaces1
spaces1
value ↑ Values
(: spaces1 (Parser void))
At least one whitespace character.
See also: spaces
pnewline
value ↑ Values
(: pnewline (Parser char))
A line break: “\n”, “\r\n” or “\r”. Answers #\newline whichever it was.
See also: spaces
letter
value ↑ Values
(: letter (Parser char))
One letter, by char-alphabetic?.
See also: digit, alphanumeric
eof
value ↑ Values
(: eof (Parser void))
Succeeds only at the end of the input.
(run-parser (pleft pint eof) "42") ; (Ok 42)
(run-parser (pleft pint eof) "42x") ; (Err ...): Expecting: end of input
See also: run-parser
pint
value ↑ Values
(: pint (Parser int))
An integer: an optional sign and ASCII digits.
(run-parser pint "-17 apples") ; (Ok -17)
A sign with no digits after it reads nothing, so pint needs no
attempt in a choice. A number too large for an int
throws, as string->int does: it is not a parse error.
pdouble
value ↑ Values
(: pdouble (Parser double))
A number with an optional fraction and exponent: 3, -0.5, 6.02e23.
(run-parser pdouble "6.02e23") ; (Ok 6.02e23)
(run-parser pdouble "1.x") ; (Ok 1.0), and ".x" is left
It needs a digit before any point, so .5 is not a number here,
and a point or an exponent with no digit after it is left for what
comes next.
See also: pint