Utilities

Working with syntax is easy if you use the correct utilities. Because Gerbil is a meta-scheme almost all such things are somewhere in the Expander Runtime

So, if you've moved beyond Identifiers, this could be what you're looking for.

stx-boolean?

(stx-boolean? ...)

Please document me!

stx-keyword?

(stx-keyword? ...)

Determine if the syntax represents a keyword.

> (defsyntax (foo stx) 
    (syntax-case stx () 
	  ((_ key) (stx-keyword? #'key) #'"YAY!")
      ((_ no) #'"NAY")))
> (foo bar:)
"YAY!"
> (foo 'bar)
"NAY

stx-char?

(stx-char? ...)

Please document me!

stx-number?

(stx-number? ...)

Please document me!

stx-fixnum?

(stx-fixnum? ...)

Please document me!

stx-string?

(stx-string? stx)

Determines if the syntax is a string.

> (defsyntax (foo stx) 
   (syntax-case stx () 
    ((_ str) (stx-string? #'key) #'"YAY!") ((_ no) #'"Nay")))
> (foo bar:)
"Nay"
> (foo "bar")
"YAY!

Please document me!

stx-null?

(stx-null? ...)

Please document me!

stx-pair?

(stx-pair? ...)

Please document me!

stx-pair/null?

(stx-pair/null? ...)

Please document me!

stx-list?

(stx-list? ...)

Please document me!

stx-box?

(stx-box? ...)

Please document me!

stx-vector?

(stx-vector? ...)

Please document me!

stx-datum?

(stx-datum? ...)

Please document me!

stx-eq?

(stx-eq? ...)

Please document me!

stx-eqv?

(stx-eqv? ...)

Please document me!

stx-equal?

(stx-equal? ...)

Please document me!

stx-false?

(stx-false? ...)

Please document me!

stx-e

(stx-e ...)

Please document me!

locat

A "locat" object represents a source code location. The location is a 2 or 3 element vector composed of the container of the source code (a file, a text editor window, etc) and one or two positions within that container (a character offset, a line/column index, a text bookmark, an expression, etc). When there are two positions they are the start and end positions of the source code location.

Source code location containers and positions can be encoded with any concrete type, except that positions cannot be pairs. The procedure ##container->path takes a container object and returns #f if the container does not denote a file, otherwise it returns the absolute path of the file as a string. The procedure ##container->id takes a container object and returns a string that can be used to identify the container when it is not a file (e.g. the name of a text editor window).

The procedure ##position->filepos takes a position object and returns a fixnum encoding the line and column position (see function ##make-filepos).

stx-source

(stx-source stx) -> locat | #f
  stx := syntax

Returns the source location of a syntax object AST stx.

The locat structure includes the container and one or two positions, accessed by ##locat-container, ##locat-start-position and ##locat-end-position.

The procedure ##position->filepos takes a position object and returns a ##filepos The filepos has line and column information, accessed with ##filepos-line and ##filepos-col.

$ cat > /tmp/foo.ss <<'EOF'
(defsyntax (foo stx) 
  (let* ((locat (stx-source stx))
         (con (##locat-container locat))
		 (path (##container->path con))
		 (start-pos (##locat-start-position locat))
		 (start-filepos (##position->filepos start-pos))
		 (start-line (##filepos-line start-filepos))
		 (start-col (##filepos-col start-filepos))
		 (end-pos (##locat-end-position locat))
		 (end-filepos (##position->filepos end-pos))
		 (end-line (##filepos-line end-filepos))
		 (end-col (##filepos-col end-filepos)))
    (displayln "Where? " path " start: line " start-line " col " start-col
	            " end: line " end-line " col " end-col)
 #'()))
       
	   (foo
	   
	   
)
EOF
$ gxc /tmp/foo.ss
Where? /private/tmp/foo.ss start: line 16 col 7 end: line 19 col 1

stx-wrap-source

(stx-wrap-source stx src) -> syntax
  stx := any
  src := locat

:::

Produces a new syntax object with source location src if stx is not wrapped as an AST already, otherwise returns stx unchanged.

The locat structure can be constructed with (##make-locat container start-position end-position), where:

  • container is a string denoting a file path, or a list containing a symbol denoting the provenance (string, port, etc.).

  • start-position can be constructed with: (##filepos->position (##make-filepos line col off)).

  • end-position can be #f if the location is a position, or a number similar to start-position indicating the end position (not included) if the location is a range.

stx-car

(stx-car ...)

Please document me!

stx-cdr

(stx-cdr ...)

Please document me!

stx-length

(stx-length ...)

Please document me!

stx-for-each

(stx-for-each ...)

Please document me!

stx-map

(stx-map ...)

Please document me!

stx-foldl

(stx-foldl ...)

Please document me!

stx-foldr

(stx-foldr ...)

Please document me!

stx-reverse

(stx-reverse ...)

Please document me!

stx-last

(stx-last ...)

Please document me!

stx-last-pair

(stx-last-pair ...)

Please document me!

stx-list-tail

(stx-list-tail ...)

Please document me!

stx-list-ref

(stx-list-ref ...)

Please document me!

stx-andmap

(stx-andmap ...)

Please document me!

stx-ormap

(stx-ormap ...)

Please document me!

stx-plist?

(stx-plist? ...)

Please document me!

stx-getq

(stx-getq ...)

Please document me!