tur/r7rs/read
(scheme read): read, the datum reader over a textual port.
An on-demand R7RS library (see stdlib/r7rs/time.tur): the load expander splices this file in when a Scheme file imports (scheme read). It reads the R7RS external representation (7.1.2) from a port's bytes, which is enough because every delimiter is ASCII -- the bytes of a UTF-8 character ride through tokens and strings untouched, and a #\ character is decoded.
Covered: lists, dotted pairs and [...]; vectors #(...) and bytevectors #u8(...); strings with every R7RS escape and the line continuation; |...| symbols; #\ characters by glyph, name or #\xHH; #t/#true/#f/#false; numbers with #e #i #x #b #o #d prefixes; ' ` , ,@; ; line comments, nested #| |# and #; datum comments; #!fold-case / #!no-fold-case; and datum labels #n= / #n#, including cycles. A malformed datum raises an error object that read-error? recognizes. At end of input read returns the eof object.
Since: r7rs-lang-plan R8
R7rsRd
(defstruct ^heap R7rsRd [port : any labels : any])
one read call's state: the port (whose fold-case flag the #! directives set) and the datum labels seen so far, as a list of (n . R7rsLabelRef).
R7rsLabelRef
(defstruct ^heap R7rsLabelRef [n : int done : bool value : any])
label n: a placeholder until its datum is complete.
r7rs-rd-port__
(r7rs-rd-port__ [st : R7rsRd] : any)
r7rs-rd-fold?__
(r7rs-rd-fold?__ [st] : bool)
r7rs-rd-set-fold__
(r7rs-rd-set-fold__ [st v : bool] : nil)
r7rs-rd-labels__
(r7rs-rd-labels__ [st : R7rsRd] : any)
r7rs-rd-set-labels__
(r7rs-rd-set-labels__ [st : R7rsRd v] : nil)
r7rs-label-ref?__
(r7rs-label-ref?__ [x] : bool)
r7rs-label-n__
(r7rs-label-n__ [r : R7rsLabelRef] : int)
r7rs-label-done?__
(r7rs-label-done?__ [r : R7rsLabelRef] : bool)
r7rs-label-value__
(r7rs-label-value__ [r : R7rsLabelRef] : any)
r7rs-label-finish__
(r7rs-label-finish__ [r : R7rsLabelRef v] : nil)
r7rs-rd-fail__
(r7rs-rd-fail__ [msg : cstr] : any)
raise a read error.
r7rs-rd-space?__
(r7rs-rd-space?__ [b : int] : bool)
r7rs-rd-bracket?__
(r7rs-rd-bracket?__ [b : int] : bool)
whitespace, a bracket, ", ;, |, or the end.
r7rs-rd-delim?__
(r7rs-rd-delim?__ [b : int] : bool)
r7rs-rd-token-go__
(r7rs-rd-token-go__ [io : R7rsIo out : R7rsIo] : nil)
the bytes up to the next delimiter.
r7rs-rd-token__
(r7rs-rd-token__ [io : R7rsIo] : cstr)
r7rs-rd-line-comment__
(r7rs-rd-line-comment__ [io : R7rsIo] : nil)
---- atmosphere: whitespace and comments --------------------------------
r7rs-rd-block-comment__
(r7rs-rd-block-comment__ [io : R7rsIo depth : int] : nil)
after #|: to the matching |#, nesting.
r7rs-rd-skip__
(r7rs-rd-skip__ [io : R7rsIo st] : nil)
whitespace, comments and directives before a datum.
r7rs-rd-hex-go__
(r7rs-rd-hex-go__ [io : R7rsIo acc : int] : int)
---- strings, symbols, characters ----------------------------------------
r7rs-rd-intraline__
(r7rs-rd-intraline__ [io : R7rsIo] : nil)
r7rs-rd-escape__
(r7rs-rd-escape__ [io : R7rsIo out : R7rsIo] : nil)
after \ in a string or |symbol|.
r7rs-rd-delimited-go__
(r7rs-rd-delimited-go__ [io : R7rsIo out : R7rsIo close : int] : nil)
a string (") or a |symbol|, after the opener.
r7rs-rd-delimited__
(r7rs-rd-delimited__ [io : R7rsIo close : int] : cstr)
r7rs-rd-char-name__
(r7rs-rd-char-name__ [s : cstr] : any)
the character a multi-letter #\name spells.
r7rs-rd-char__
(r7rs-rd-char__ [io : R7rsIo st] : any)
after #\: one character, then any name letters.
r7rs-rd-number__
(r7rs-rd-number__ [s : cstr none] : any)
T0: a number token through the one R7RS number parser (stdlib/r7rs/numsyntax.tur; the source reader's too), so 1/2 and 1+2i are one token (T6: 1+2i a complex number). A number the tower cannot hold is a read error naming the reason, never a symbol and never a different number. none is what a token that is not number syntax at all reads as.
r7rs-rd-hash-token__
(r7rs-rd-hash-token__ [s : cstr] : any)
a # token that is not a bracket or a char.
r7rs-rd-atom__
(r7rs-rd-atom__ [s : cstr st] : any)
a token: a number if it reads as one, else a symbol.
r7rs-rd-label-find__
(r7rs-rd-label-find__ [l n : int] : any)
---- datum labels ------------------------------------------------------------
r7rs-rd-is__
(r7rs-rd-is__ [x ref] : bool)
replace the placeholder ref by val inside x, once per pair or vector (the value may already be cyclic through an earlier label, so the walk keeps a visited table).
r7rs-rd-patch-vec__
(r7rs-rd-patch-vec__ [v : (Vec any) i : int ref val seen : R7rsIdTab] : nil)
r7rs-rd-patch__
(r7rs-rd-patch__ [x ref val seen : R7rsIdTab] : nil)
r7rs-rd-label__
(r7rs-rd-label__ [io : R7rsIo st] : any)
after #, digits then = (define) or # (refer).
r7rs-rd-label-digits__
(r7rs-rd-label-digits__ [io : R7rsIo out : R7rsIo] : cstr)
r7rs-rd-close?__
(r7rs-rd-close?__ [b : int] : bool)
---- lists and the datum ------------------------------------------------------
r7rs-rd-dot?__
(r7rs-rd-dot?__ [io : R7rsIo] : bool)
a lone . (the dotted-pair marker, not ...).
r7rs-rd-reverse-onto__
(r7rs-rd-reverse-onto__ [acc tail] : any)
r7rs-rd-list__
(r7rs-rd-list__ [io : R7rsIo st acc] : any)
after ( or [: the elements, a dotted tail, the close.
r7rs-rd-bytes__
(r7rs-rd-bytes__ [l v : (Vec int)] : (Vec int))
r7rs-rd-quoted__
(r7rs-rd-quoted__ [io : R7rsIo st name : cstr] : any)
r7rs-rd-datum__
(r7rs-rd-datum__ [io : R7rsIo st] : any)
one datum, or the eof object at the end of input.
r7rs-rd-hash__
(r7rs-rd-hash__ [io : R7rsIo st] : any)
a datum that starts with #.
r7rs-rd-datum-req__
(r7rs-rd-datum-req__ [io : R7rsIo st] : any)
a datum that must be there (inside a list, after a quote or a label).
r7rs-read
(r7rs-read [& r : any] : any)
(read [port]): the next datum, or the eof object.
r7rs-bridge-write__
(r7rs-bridge-write__ [x] : cstr)
internal (r7rs-lang-plan T4): x as write text, the form a datum crosses to or from the embedded evaluator in.
r7rs-bridge-read__
(r7rs-bridge-read__ [text : cstr] : any)
internal (T4): the datum text writes.
r7rs-bridge-value__
(r7rs-bridge-value__ [x] : any)
internal (T4): an evaluated expression's value as an any. A top-level expression's value is its elaborated type's representation (a let yielding a vector is the bare Vec), which write cannot read as a Scheme value; passing it through an any parameter widens it the way a procedure's result is.
R7rsBridgeRaised
(defstruct ^heap R7rsBridgeRaised [obj : any])
internal (T4): an object raised on one side of the eval bridge, caught there to be raised again on the other.
r7rs-bridge-raised__
(r7rs-bridge-raised__ [obj] : any)
r7rs-bridge-raised?__
(r7rs-bridge-raised?__ [x] : bool)
r7rs-bridge-raised-obj__
(r7rs-bridge-raised-obj__ [x : R7rsBridgeRaised] : any)
r7rs-bridge-catch__
(r7rs-bridge-catch__ [thunk] : any)
internal (T4): (thunk)'s value, or the object it raised, as an R7rsBridgeRaised.
r7rs-bridge-catch-apply__
(r7rs-bridge-catch-apply__ [f l] : any)
internal (T4): (apply f l), caught.
r7rs-bridge-condition__
(r7rs-bridge-condition__ [r] : cstr)
internal (T4): a caught object as text: an error object is (#t kind message . irritants), anything else (#f . obj).
r7rs-bridge-reraise__
(r7rs-bridge-reraise__ [text : cstr] : any)
internal (T4): raise what a condition text names.
r7rs-bridge-reraise-error__
(r7rs-bridge-reraise-error__ [c] : any)
r7rs-bridge-host-wrap__
(r7rs-bridge-host-wrap__ [native] : any)
internal (T4): the procedure evaluated code sees for a program procedure: it calls the registered native and raises again what the program procedure raised.
r7rs-bridge-host-result__
(r7rs-bridge-host-result__ [r] : any)