▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Contracts for Macro Subexpressions
6 | Debugging
7 | Definitions
8 | Errors
9 | Futures
10 | Functions
11 | Hash Tables
12 | Interface-Oriented Programming for Classes
13 | Lazy Require
14 | Lists
15 | Logging
16 | Macro Testing
17 | Mark Parameters
18 | Match
19 | Open place expressions
20 | Options
21 | Parameter Groups
22 | Pretty-Printing
23 | Re-Contracting Identifiers
24 | Sandbox
25 | Sequences
26 | Strings
27 | Structs
28 | Struct Printing
29 | Syntax
30 | Temporal Contracts: Explicit Contract Monitors
31 | Unix Domain Sockets
32 | 2D Syntax

► | 29 | Syntax

29.1 | Syntax Object Source Locations

On this page:

explode-module-path-index
phase-of-enclosing-module
make-variable-like-transformer
format-unique-id
syntax-within?
syntax-length
29.1`` Syntax Object Source Locations
syntax-source-directory
syntax-source-file-name

5.92
top ← prev up next →

### 29`` Syntax

Ryan Culpepper <ryanc@racket-lang.org>

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

(require unstable/syntax) | package: base

> procedure (explode-module-path-index mpi)
> → (listof (or/c module-path? resolved-module-path? #f))
> mpi : module-path-index?

Unfolds mpi using module-path-index-split, returning a list of the relative module paths together with the terminal resolved module path or #f for the “self” module.

Examples:
> > (explode-module-path-index (car (identifier-binding #'lambda)))
> '("kw.rkt" "pre-base.rkt" "private/base.rkt" racket/base #f)
> > (explode-module-path-index (caddr (identifier-binding #'lambda)))
> '(racket/base #f)
> > (explode-module-path-index (car (identifier-binding #'define-values)))
> '('#%kernel #f)

> syntax (phase-of-enclosing-module)

Returns the phase level of the module in which the form occurs (and for the instantiation of the module in which the form is executed). For example, if a module is required directly by the “main” module (or the top level), its phase level is 0. If a module is required for-syntax by the “main” module (or the top level), its phase level is 1.

Examples:
> > (module helper racket
> (require unstable/syntax)
> (displayln (phase-of-enclosing-module)))
> > (require 'helper)
> 0
> > (require (for-meta 1 'helper))
> 1

> > procedure
> > (make-variable-like-transformer |  | reference-stx |
> > | [ | setter-stx]) |
> → set!-transformer?
> reference-stx : syntax?
> setter-stx : (or/c syntax? #f) = #f

Creates a transformer that replaces references to the macro identifier with reference-stx. Uses of the macro in operator position are interpreted as an application with reference-stx as the function and the arguments as given.

If the macro identifier is used as the target of a set! form, then the set! form expands into the application of setter-stx to the set! expression’s right-hand side, if setter-stx is syntax; otherwise, the identifier is considered immutable and a syntax error is raised.

Examples:
> > (define the-box (box add1))
> > (define-syntax op
> (make-variable-like-transformer
> #'(unbox the-box)
> #'(lambda (v) (set-box! the-box v))))
> > (op 5)
> 6
> > (set! op 0)
> > op
> 0

> > > The subsequent bindings were added by Vincent St-Amour <stamourv@racket-lang.org>.

> > procedure
> > (format-unique-id |  | lctx |  |  |  |
> > |  | fmt |  |  |  |
> > |  | v ... |  |  |  |
> > | [ | #:source src |  |  |  |
> > |  | #:props props |  |  |  |
> > |  | #:cert cert]) |  | → |  | identifier?
> lctx : (or/c syntax? #f)
> fmt : string?
> v : (or/c string? symbol? identifier? keyword? char? number?)
> src : (or/c syntax? #f) = #f
> props : (or/c syntax? #f) = #f
> cert : (or/c syntax? #f) = #f

Like format-id, but returned identifiers are guaranteed to be unique.

> procedure (syntax-within? a b) → boolean?
> a : syntax?
> b : syntax?

Returns true is syntax a is within syntax b in the source. Bounds are inclusive.

> > > The subsequent bindings were added by Eric Dobson <eric.n.dobson@gmail.com>.

> procedure (syntax-length stx) → exact-nonnegative-integer?
> stx : syntax?

Performs (length (syntax->list stx)).

Example:
> > (syntax-length #'(d e f))
> 3

> > > The subsequent bindings were added by Carl Eastlund <cce@racket-lang.org>.

#### 29.1`` Syntax Object Source Locations

> procedure (syntax-source-directory stx) → (or/c path? #f)
> stx : syntax?
> procedure (syntax-source-file-name stx) → (or/c path? #f)
> stx : syntax?

These produce the directory and file name, respectively, of the path with which stx is associated, or #f if stx is not associated with a path.

Examples:
> (define loc
> (list (build-path "/tmp" "dir" "somewhere.rkt")
> #f #f #f #f))
> (define stx1 (datum->syntax #f 'somewhere loc))
> > (syntax-source-directory stx1)
> #<path:/tmp/dir/>
> > (syntax-source-file-name stx1)
> #<path:somewhere.rkt>
> (define stx2 (datum->syntax #f 'nowhere #f))
> > (syntax-source-directory stx2)
> #f
> > (syntax-source-directory stx2)
> #f

top ← prev up next →