On this page:
4.1 Raising exn: fail: read
raise-read-error
raise-read-eof-error
4.2 Module Reader
#%module-begin
wrap-read-all
Version: 4.1

4 Reader Helpers

4.1 Raising exn:fail:read

 (require syntax/readerr)

(raise-read-error

 

msg-string

 

 

 

 

 

 

source

 

 

 

 

 

 

line

 

 

 

 

 

 

col

 

 

 

 

 

 

pos

 

 

 

 

 

 

span)

 

 

any

  msg-string : string?

  source : any/c

  line : (or/c number? false/c)

  col : (or/c number? false/c)

  pos : (or/c number? false/c)

  span : (or/c number? false/c)

Creates and raises an exn:fail:read exception, using msg-string as the base error message.

Source-location information is added to the error message using the last five arguments (if the error-print-source-location parameter is set to #t). The source argument is an arbitrary value naming the source location – usually a file path string. Each of the line, pos arguments is #f or a positive exact integer representing the location within source-name (as much as known), col is a non-negative exact integer for the source column (if known), and span is #f or a non-negative exact integer for an item range starting from the indicated position.

The usual location values should point at the beginning of whatever it is you were reading, and the span usually goes to the point the error was discovered.

(raise-read-eof-error

 

msg-string

 

 

 

 

 

 

source

 

 

 

 

 

 

line

 

 

 

 

 

 

col

 

 

 

 

 

 

pos

 

 

 

 

 

 

span)

 

 

any

  msg-string : string?

  source : any/c

  line : (or/c number? false/c)

  col : (or/c number? false/c)

  pos : (or/c number? false/c)

  span : (or/c number? false/c)

Like raise-read-error, but raises exn:fail:read:eof instead of exn:fail:read.

4.2 Module Reader

 (require syntax/module-reader)

The syntax/module-reader language provides support for defining #lang readers.

(#%module-begin module-path)

Causes a module written in the syntax/module-reader language to define and provide read and read-syntax functions, making the module an implementation of a reader. In particular, the exported reader functions read all S-expressions until an end-of-file, and it packages them into a new module in the module-path language.

That is, a module something/lang/reader implemented as

  (module reader module-syntax/module-reader

    module-path)

creates a reader that converts #lang something into

  (module name-id module-path

    ....)

where name-id is derived from the name of the port used by the reader.

For example, scheme/base/lang/reader is implemented as

  (module reader module-syntax/module-reader

    scheme/base)

(wrap-read-all

 

mod-path

 

 

 

 

 

 

in

 

 

 

 

 

 

read

 

 

 

 

 

 

mod-path-stx

 

 

 

 

 

 

src

 

 

 

 

 

 

line

 

 

 

 

 

 

col

 

 

 

 

 

 

pos)

 

 

any/c

  mod-path : module-path?

  in : input-port?

  read : (input-port . -> . any/c)

  mod-path-stx : syntax?

  src : (or/c syntax? #f)

  line : number?

  col : number?

  pos : number?

Repeatedly calls read on in until an end of file, collecting the results in order into lst, and derives a name-id from (object-name in). The last five arguments are used to construct the syntax object for the language position of the module. The result is roughly

  `(module ,name-id ,mod-path ,@lst)