syntax/parse: clean up integrable stxclass code
This commit is contained in:
parent
cc716392da
commit
14089e0ac6
|
@ -1,7 +1,6 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require "sc.rkt"
|
(require "sc.rkt"
|
||||||
"keywords.rkt"
|
"keywords.rkt"
|
||||||
syntax/parse/private/residual ;; keep abs.
|
|
||||||
(for-syntax racket/base))
|
(for-syntax racket/base))
|
||||||
|
|
||||||
(provide identifier
|
(provide identifier
|
||||||
|
@ -22,41 +21,49 @@
|
||||||
expr
|
expr
|
||||||
static)
|
static)
|
||||||
|
|
||||||
|
|
||||||
|
(define (expr-stx? x)
|
||||||
|
(not (keyword-stx? x)))
|
||||||
|
|
||||||
|
(define ((stxof pred?) x) (and (syntax? x) (pred? (syntax-e x))))
|
||||||
|
(define keyword-stx? (stxof keyword?))
|
||||||
|
(define boolean-stx? (stxof boolean?))
|
||||||
|
(define string-stx? (stxof string?))
|
||||||
|
(define char-stx? (stxof char?))
|
||||||
|
(define number-stx? (stxof number?))
|
||||||
|
(define integer-stx? (stxof integer?))
|
||||||
|
(define exact-integer-stx? (stxof exact-integer?))
|
||||||
|
(define exact-nonnegative-integer-stx? (stxof exact-nonnegative-integer?))
|
||||||
|
(define exact-positive-integer-stx? (stxof exact-positive-integer?))
|
||||||
|
|
||||||
;; == Integrable syntax classes ==
|
;; == Integrable syntax classes ==
|
||||||
|
|
||||||
(define-integrable-syntax-class identifier (quote "identifier") identifier?)
|
(define-integrable-syntax-class identifier (quote "identifier") identifier?)
|
||||||
(define-integrable-syntax-class expr (quote "expression") expr-stx?)
|
(define-integrable-syntax-class expr (quote "expression") expr-stx?)
|
||||||
(define-integrable-syntax-class keyword (quote "keyword") keyword-stx?)
|
(define-integrable-syntax-class keyword (quote "keyword") keyword-stx?)
|
||||||
|
(define-integrable-syntax-class boolean (quote "boolean") boolean-stx?)
|
||||||
;; == Normal syntax classes ==
|
(define-integrable-syntax-class character (quote "character") char-stx?)
|
||||||
|
(define-integrable-syntax-class str (quote "string") string-stx?)
|
||||||
(define-syntax-rule (define-pred-stxclass name pred)
|
(define-integrable-syntax-class number (quote "number") number-stx?)
|
||||||
(define-syntax-class name #:attributes () #:opaque #:commit
|
(define-integrable-syntax-class integer (quote "integer") integer-stx?)
|
||||||
(pattern (~and x (~fail #:unless (pred (syntax-e #'x)))))))
|
(define-integrable-syntax-class exact-integer (quote "exact-integer") exact-integer-stx?)
|
||||||
|
(define-integrable-syntax-class exact-nonnegative-integer
|
||||||
;;(define-pred-stxclass identifier symbol?)
|
(quote "exact-nonnegative-integer")
|
||||||
;;(define-pred-stxclass keyword keyword?)
|
exact-nonnegative-integer-stx?)
|
||||||
(define-pred-stxclass boolean boolean?)
|
(define-integrable-syntax-class exact-positive-integer
|
||||||
(define-pred-stxclass character char?)
|
(quote "exact-positive-integer")
|
||||||
|
exact-positive-integer-stx?)
|
||||||
(define-syntax-class str #:attributes () #:opaque #:commit
|
|
||||||
#:description "string"
|
|
||||||
(pattern (~and x (~fail #:unless (string? (syntax-e #'x))))))
|
|
||||||
|
|
||||||
(define-pred-stxclass number number?)
|
|
||||||
(define-pred-stxclass integer integer?)
|
|
||||||
(define-pred-stxclass exact-integer exact-integer?)
|
|
||||||
(define-pred-stxclass exact-nonnegative-integer exact-nonnegative-integer?)
|
|
||||||
(define-pred-stxclass exact-positive-integer exact-positive-integer?)
|
|
||||||
|
|
||||||
;; Aliases
|
;; Aliases
|
||||||
(define-syntax id (make-rename-transformer #'identifier))
|
(define-syntax id (make-rename-transformer #'identifier))
|
||||||
(define-syntax nat (make-rename-transformer #'exact-nonnegative-integer))
|
(define-syntax nat (make-rename-transformer #'exact-nonnegative-integer))
|
||||||
(define-syntax char (make-rename-transformer #'character))
|
(define-syntax char (make-rename-transformer #'character))
|
||||||
|
|
||||||
|
;; == Normal syntax classes ==
|
||||||
|
|
||||||
(define notfound (box 'notfound))
|
(define notfound (box 'notfound))
|
||||||
|
|
||||||
(define-syntax-class (static pred name)
|
(define-syntax-class (static pred [name #f])
|
||||||
#:attributes (value)
|
#:attributes (value)
|
||||||
#:description name
|
#:description name
|
||||||
#:commit
|
#:commit
|
||||||
|
|
|
@ -563,13 +563,13 @@ Conventions:
|
||||||
[#s(pat:post attrs pattern)
|
[#s(pat:post attrs pattern)
|
||||||
#`(let ([pr (ps-add-post pr)])
|
#`(let ([pr (ps-add-post pr)])
|
||||||
(parse:S x cx pattern pr es k))]
|
(parse:S x cx pattern pr es k))]
|
||||||
[#s(pat:integrated _attrs name argu predicate description)
|
[#s(pat:integrated _attrs name predicate description)
|
||||||
(with-syntax ([(name-attr ...)
|
(with-syntax ([(name-attr ...)
|
||||||
(if (identifier? #'name)
|
(if (identifier? #'name)
|
||||||
#'([#s(attr name 0 #t) x*])
|
#'([#s(attr name 0 #t) x*])
|
||||||
#'())])
|
#'())])
|
||||||
#'(let ([x* (datum->syntax cx x cx)])
|
#'(let ([x* (datum->syntax cx x cx)])
|
||||||
(if (app-argu predicate x* argu)
|
(if (predicate x*)
|
||||||
(let-attributes (name-attr ...) k)
|
(let-attributes (name-attr ...) k)
|
||||||
(let ([es (cons (expect:thing 'description #t) es)])
|
(let ([es (cons (expect:thing 'description #t) es)])
|
||||||
(fail (failure pr es))))))])]))
|
(fail (failure pr es))))))])]))
|
||||||
|
@ -818,7 +818,7 @@ Conventions:
|
||||||
(else (fail result))))]
|
(else (fail result))))]
|
||||||
;; -- (x:sc ... . ()) where sc is an integrable stxclass like id or expr
|
;; -- (x:sc ... . ()) where sc is an integrable stxclass like id or expr
|
||||||
[(parse:dots x cx (#s(ehpat (attr0)
|
[(parse:dots x cx (#s(ehpat (attr0)
|
||||||
#s(pat:integrated _attrs _name _argu pred? desc)
|
#s(pat:integrated _attrs _name pred? desc)
|
||||||
#f))
|
#f))
|
||||||
#s(pat:datum () ()) pr es k)
|
#s(pat:datum () ()) pr es k)
|
||||||
#'(let-values ([(status result) (predicate-ellipsis-parser x cx pr es pred? desc)])
|
#'(let-values ([(status result) (predicate-ellipsis-parser x cx pr es pred? desc)])
|
||||||
|
|
|
@ -40,7 +40,7 @@ A SinglePattern is one of
|
||||||
(pat:commit Base SinglePattern)
|
(pat:commit Base SinglePattern)
|
||||||
(pat:reflect Base stx Arguments (listof SAttr) id (listof IAttr))
|
(pat:reflect Base stx Arguments (listof SAttr) id (listof IAttr))
|
||||||
(pat:post Base SinglePattern)
|
(pat:post Base SinglePattern)
|
||||||
(pat:integrated Base id/#f Arguments id string)
|
(pat:integrated Base id/#f id string)
|
||||||
|
|
||||||
A ListPattern is a subtype of SinglePattern; one of
|
A ListPattern is a subtype of SinglePattern; one of
|
||||||
(pat:datum Base '())
|
(pat:datum Base '())
|
||||||
|
@ -69,7 +69,7 @@ A ListPattern is a subtype of SinglePattern; one of
|
||||||
(define-struct pat:commit (attrs pattern) #:prefab)
|
(define-struct pat:commit (attrs pattern) #:prefab)
|
||||||
(define-struct pat:reflect (attrs obj argu attr-decls name nested-attrs) #:prefab)
|
(define-struct pat:reflect (attrs obj argu attr-decls name nested-attrs) #:prefab)
|
||||||
(define-struct pat:post (attrs pattern) #:prefab)
|
(define-struct pat:post (attrs pattern) #:prefab)
|
||||||
(define-struct pat:integrated (attrs name argu predicate description) #:prefab)
|
(define-struct pat:integrated (attrs name predicate description) #:prefab)
|
||||||
|
|
||||||
#|
|
#|
|
||||||
A ActionPattern is one of
|
A ActionPattern is one of
|
||||||
|
@ -306,9 +306,9 @@ A SideClause is one of
|
||||||
(define (create-pat:post pattern)
|
(define (create-pat:post pattern)
|
||||||
(make pat:post (pattern-attrs pattern) pattern))
|
(make pat:post (pattern-attrs pattern) pattern))
|
||||||
|
|
||||||
(define (create-pat:integrated name argu predicate description)
|
(define (create-pat:integrated name predicate description)
|
||||||
(let ([attrs (if name (list (make attr name 0 #t)) null)])
|
(let ([attrs (if name (list (make attr name 0 #t)) null)])
|
||||||
(make pat:integrated attrs name argu predicate description)))
|
(make pat:integrated attrs name predicate description)))
|
||||||
|
|
||||||
;; ----
|
;; ----
|
||||||
|
|
||||||
|
|
|
@ -704,8 +704,8 @@
|
||||||
(define (parse-pat:var* stx allow-head? name sc argu [pfx "."])
|
(define (parse-pat:var* stx allow-head? name sc argu [pfx "."])
|
||||||
(check-no-delimit-cut-in-not stx (stxclass-delimit-cut? sc))
|
(check-no-delimit-cut-in-not stx (stxclass-delimit-cut? sc))
|
||||||
(cond [(stxclass/s? sc)
|
(cond [(stxclass/s? sc)
|
||||||
(if (and (stxclass-integrate sc) (null? (arguments-kws argu)))
|
(if (and (stxclass-integrate sc) (equal? argu no-arguments))
|
||||||
(parse-pat:id/s/integrate name (stxclass-integrate sc) argu)
|
(parse-pat:id/s/integrate name (stxclass-integrate sc))
|
||||||
(parse-pat:id/s name
|
(parse-pat:id/s name
|
||||||
(stxclass-parser sc)
|
(stxclass-parser sc)
|
||||||
argu
|
argu
|
||||||
|
@ -727,9 +727,9 @@
|
||||||
(define bind (name->bind name))
|
(define bind (name->bind name))
|
||||||
(create-pat:var bind parser argu (id-pattern-attrs attrs prefix) (length attrs) commit?))
|
(create-pat:var bind parser argu (id-pattern-attrs attrs prefix) (length attrs) commit?))
|
||||||
|
|
||||||
(define (parse-pat:id/s/integrate name integrate argu)
|
(define (parse-pat:id/s/integrate name integrate)
|
||||||
(define bind (name->bind name))
|
(define bind (name->bind name))
|
||||||
(create-pat:integrated bind argu
|
(create-pat:integrated bind
|
||||||
(integrate-predicate integrate)
|
(integrate-predicate integrate)
|
||||||
(integrate-description integrate)))
|
(integrate-description integrate)))
|
||||||
|
|
||||||
|
|
|
@ -193,21 +193,11 @@
|
||||||
["runtime-report.rkt"
|
["runtime-report.rkt"
|
||||||
(syntax-patterns-fail)])
|
(syntax-patterns-fail)])
|
||||||
|
|
||||||
;; == predicates and parsers
|
;; == specialized ellipsis parser
|
||||||
|
|
||||||
(provide keyword-stx?
|
|
||||||
expr-stx?
|
|
||||||
predicate-ellipsis-parser)
|
|
||||||
|
|
||||||
(define (keyword-stx? x)
|
|
||||||
(and (syntax? x) (keyword? (syntax-e x))))
|
|
||||||
|
|
||||||
(define (expr-stx? x)
|
|
||||||
(not (keyword-stx? x)))
|
|
||||||
|
|
||||||
;; Specialized ellipsis parser
|
|
||||||
;; returns (values 'ok attr-values) or (values 'fail failure)
|
;; returns (values 'ok attr-values) or (values 'fail failure)
|
||||||
|
|
||||||
|
(provide predicate-ellipsis-parser)
|
||||||
|
|
||||||
(define (predicate-ellipsis-parser x cx pr es pred? desc)
|
(define (predicate-ellipsis-parser x cx pr es pred? desc)
|
||||||
(let ([elems (stx->list x)])
|
(let ([elems (stx->list x)])
|
||||||
(if (and elems (andmap pred? elems))
|
(if (and elems (andmap pred? elems))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user