Carl Eastlund <cce@ccs.neu.edu>
There are two libraries in this collection for dealing with source locations; one for manipulating representations of them, and the other for quoting the location of a particular piece of source code.
This module defines utilities for manipulating representations of source locations, including both srcloc structures and all the values accepted by datum->syntax ’s third argument: syntax objects, lists, vectors, and #f.
procedure
( source-location? x)→boolean?
x:any/cprocedure
x:any/cprocedure
x:any/c
#t
#t
#t
#f
#t
#f
#t
#f
procedure
( check-source-location! namex)→void?
name:symbol?x:any/c
this-example: expected a source location with line number
and column number both numeric or both #f; got 1 and #f
respectively: (srcloc 'bad 1 #f 1 0)
this-example: expected a source location (a list of 5
elements); got an improper list: '(bad 1 0 1 0 . tail)
this-example: expected a source location with a positive
line number or #f (second element); got line number 0:
'#(bad 0 0 0 0)
procedure
( build-source-location loc...)→srcloc?
loc:source-location?procedure
loc:source-location?procedure
( build-source-location-vector loc...)→source-location-vector?
loc:source-location?procedure
( build-source-location-syntax loc...)→syntax?
loc:source-location?
(srcloc #f #f #f #f #f)
'(#f #f #f #f #f)
'#(#f #f #f #f #f)
#<syntax ()>
(srcloc #f #f #f #f #f)
'(#f #f #f #f #f)
'#(#f #f #f #f #f)
#<syntax ()>
(srcloc 'here 1 2 3 4)
'(here 1 2 3 4)
'#(here 1 2 3 4)
#<syntax:here:1:2 ()>
(srcloc 'here 1 2 3 12)
'(here 1 2 3 12)
'#(here 1 2 3 12)
#<syntax:here:1:2 ()>
(srcloc #f #f #f #f #f)
'(#f #f #f #f #f)
'#(#f #f #f #f #f)
#<syntax ()>
procedure
( source-location-known? loc)→boolean?
loc:source-location?
#f
#f
#t
#f
#t
#f
#t
procedure
( source-location-source loc)→any/c
loc:source-location?procedure
( source-location-line loc)→(or/c exact-positive-integer? #f)
loc:source-location?
#f
1
2
3
4
procedure
( source-location-end loc)
loc:source-location?
#f
7
#f
#f
procedure
#:sourcesource#:lineline#:columncolumn#:positionpositionloc:source-location?source:any/c
'(here #f #f #f #f)
'(there 20 79 3 4)
'#(everywhere 1 2 #f #f)
procedure
( source-location->string loc)→string?
loc:source-location?procedure
( source-location->prefix loc)→string?
loc:source-location?
"here:1:2"
"here::3-7"
"here"
":1:2"
"::3-7"
""
"here:1:2: "
"here::3-7: "
"here: "
":1:2: "
"::3-7: "
""
Changed in version 8.1.0.5 of package base: Changed format to separate a line and column with : instead of .
procedure
( syntax-source-directory stx)→(or/c path? #f)
stx:syntax?procedure
( syntax-source-file-name stx)→(or/c path? #f)
stx:syntax?
#<path:/tmp/dir/>
#<path:somewhere.rkt>
#f
#f
Added in version 6.3 of package base.
The following macros evaluate to various aspects of their own source location.
Note: The examples below illustrate the use of these macros and the representation of their output. However, due to the mechanism by which they are generated, each example is considered a single character and thus does not have realistic line, column, and character positions.
Furthermore, the examples illustrate the use of source location quoting inside macros, and the difference between quoting the source location of the macro definition itself and quoting the source location of the macro’s arguments.
syntax
( quote-srcloc )
(srcloc 'eval 2 0 2 1)
> (not-here)(srcloc 'eval 3 0 3 1)
> (not-here)(srcloc 'eval 3 0 3 1)
> (here)(srcloc 'eval 7 0 7 1)
> (here)(srcloc 'eval 8 0 8 1)
syntax
syntax
syntax
syntax
syntax
You can achieve an effect similar to __FILE__ from Perl or Ruby and __file__ from Python by using quote-source-file .
Quote various fields of the source location of form, or of the whole macro application if no form is given.
'(eval 2 0 2 1)
> (not-here)'(eval 3 0 3 1)
> (not-here)'(eval 3 0 3 1)
> (here)'(eval 7 0 7 1)
> (here)'(eval 8 0 8 1)
syntax
syntax
'("eval:2:0" "eval:2:0: ")
> (not-here)'("eval:3:0" "eval:3:0: ")
> (not-here)'("eval:3:0" "eval:3:0: ")
> (here)'("eval:7:0" "eval:7:0: ")
> (here)'("eval:8:0" "eval:8:0: ")
syntax
( quote-module-name submod-path-element...)
Returns a path, symbol, list, or 'top-level, where 'top-level is produced when used outside of a module. A list corresponds to a submodule in the same format as the result of variable-reference->module-name. Any given submod-path-elements (as in a submod form) are added to form a result submodule path.
To produce a name suitable for use in printed messages, apply path->relative-string/library when the result is a path.
> a-name'A
> c-name'(A C)
> c-name2'(A C "..")
> b-name'B
'top-level
'A
syntax
( quote-module-path submod-path-element...)
Builds the result using quote , a path, submod , or 'top-level, where 'top-level is produced when used outside of a module. Any given submod-path-elements (as in a submod form) are added to form a result submodule path.
> a-path''A
> c-path'(submod 'A C)
> c-path2'(submod 'A C "..")
> b-path''B
'top-level
''A