You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
Scheme
41 lines
1.1 KiB
Scheme
23 years ago
|
#cs
|
||
23 years ago
|
(module token-syntax mzscheme
|
||
23 years ago
|
|
||
|
;; The things needed at compile time to handle definition of tokens
|
||
|
|
||
|
(provide make-terminals-def terminals-def-t terminals-def? define-tokens-helper)
|
||
|
|
||
|
(define-struct terminals-def (t))
|
||
|
|
||
23 years ago
|
(define (define-tokens-helper stx runtime empty?)
|
||
23 years ago
|
(syntax-case stx ()
|
||
|
((_ name (terms ...))
|
||
|
(andmap identifier? (syntax->list (syntax (terms ...))))
|
||
|
(datum->syntax-object
|
||
23 years ago
|
runtime
|
||
23 years ago
|
`(begin
|
||
|
(define-syntax ,(syntax name)
|
||
|
(make-terminals-def ',(syntax (terms ...))))
|
||
|
,@(map
|
||
|
(lambda (n)
|
||
23 years ago
|
(if (eq? (syntax-object->datum n) 'error)
|
||
|
(raise-syntax-error
|
||
|
#f
|
||
|
"Cannot define a token named error."
|
||
|
stx))
|
||
23 years ago
|
`(define (,(datum->syntax-object
|
||
|
n
|
||
|
(string->symbol
|
||
|
(format "token-~a" (syntax-object->datum n)))
|
||
|
n)
|
||
|
,@(if empty? '() '(x)))
|
||
|
(make-token ',n ,(if empty? #f 'x))))
|
||
|
(syntax->list (syntax (terms ...)))))
|
||
|
stx))
|
||
|
((_ ...)
|
||
|
(raise-syntax-error
|
||
|
#f
|
||
|
"must have the form (define-tokens name (symbol ...))"
|
||
|
stx))))
|
||
|
)
|