allow comma in templates
This commit is contained in:
parent
86e7b98d65
commit
c45aba592d
|
@ -86,23 +86,52 @@
|
||||||
[else (list)]))
|
[else (list)]))
|
||||||
|
|
||||||
(define-for-syntax (fix-template stx)
|
(define-for-syntax (fix-template stx)
|
||||||
(syntax-parse stx #:literals (honu-:)
|
(define (fix-classes stx)
|
||||||
[(variable:identifier honu-: class:identifier rest ...)
|
(syntax-parse stx #:literals (honu-:)
|
||||||
(with-syntax ([(rest* ...) (fix-template #'(rest ...))])
|
[(variable:identifier honu-: class:identifier rest ...)
|
||||||
(datum->syntax stx (cons #'(~var variable class #:attr-name-separator "_")
|
(with-syntax ([(rest* ...) (fix-template #'(rest ...))])
|
||||||
#'(rest* ...))
|
(datum->syntax stx (cons #'(~var variable class #:attr-name-separator "_")
|
||||||
stx)
|
#'(rest* ...))
|
||||||
#;
|
stx)
|
||||||
#'((~var variable class) rest* ...))]
|
#;
|
||||||
[(one rest ...)
|
#'((~var variable class) rest* ...))]
|
||||||
(with-syntax ([one* (fix-template #'one)]
|
[(one rest ...)
|
||||||
[(rest* ...) (fix-template #'(rest ...))])
|
(with-syntax ([one* (fix-template #'one)]
|
||||||
(datum->syntax stx (cons #'one*
|
[(rest* ...) (fix-template #'(rest ...))])
|
||||||
#'(rest* ...))
|
(datum->syntax stx (cons #'one*
|
||||||
stx)
|
#'(rest* ...))
|
||||||
#;
|
stx)
|
||||||
#'(one* rest* ...))]
|
#;
|
||||||
[else stx]))
|
#'(one* rest* ...))]
|
||||||
|
[else stx]))
|
||||||
|
;; removes commas from a pattern
|
||||||
|
(define (fix-commas stx)
|
||||||
|
(syntax-parse stx
|
||||||
|
#:literals (honu-comma
|
||||||
|
[ellipses ...])
|
||||||
|
[(a honu-comma ellipses rest ...)
|
||||||
|
(with-syntax ([a* (fix-commas #'a)]
|
||||||
|
[(rest* ...) (fix-commas #'(rest ...))])
|
||||||
|
(datum->syntax stx
|
||||||
|
`((~seq ,#'a* (~optional |,|)) ... ,@#'(rest* ...))
|
||||||
|
stx stx)
|
||||||
|
#;
|
||||||
|
(datum->syntax stx
|
||||||
|
(cons
|
||||||
|
#'a*
|
||||||
|
(cons
|
||||||
|
#'(... ...)
|
||||||
|
#'(rest* ...)))
|
||||||
|
stx stx))]
|
||||||
|
[(z rest ...)
|
||||||
|
(with-syntax ([z* (fix-commas #'z)]
|
||||||
|
[(rest* ...) (fix-commas #'(rest ...))])
|
||||||
|
(datum->syntax stx
|
||||||
|
(cons #'z* #'(rest* ...))
|
||||||
|
stx stx))]
|
||||||
|
[else stx]))
|
||||||
|
(define all-fixes (compose fix-commas fix-classes))
|
||||||
|
(all-fixes stx))
|
||||||
|
|
||||||
#|
|
#|
|
||||||
(define-for-syntax (fix-template stx)
|
(define-for-syntax (fix-template stx)
|
||||||
|
@ -466,11 +495,25 @@
|
||||||
|
|
||||||
(define foobar 0)
|
(define foobar 0)
|
||||||
|
|
||||||
|
|
||||||
(define-honu-syntax honu-macro
|
(define-honu-syntax honu-macro
|
||||||
(lambda (stx ctx)
|
(lambda (stx ctx)
|
||||||
(define-splicing-syntax-class patterns
|
(define-splicing-syntax-class patterns
|
||||||
#:literal-sets ([cruft #:phase (syntax-local-phase-level)])
|
#:literal-sets ([cruft #:phase (syntax-local-phase-level)])
|
||||||
|
#;
|
||||||
|
[pattern (~seq x ...)
|
||||||
|
#:with (template ...) '()
|
||||||
|
#:with (code ...) '()
|
||||||
|
#:with (fixed ...) '()
|
||||||
|
#:when (begin
|
||||||
|
(printf "Trying to parse ~a\n" (syntax->datum #'(x ...)))
|
||||||
|
#f)]
|
||||||
|
#;
|
||||||
|
[pattern (~seq (#%braces template ...)
|
||||||
|
(#%braces code ...))
|
||||||
|
#:with (fixed ...) '()
|
||||||
|
#:when (begin
|
||||||
|
(printf "Got template as ~a. Code is ~a\n" (syntax->datum #'(template ...)) (syntax->datum #'(code ...)))
|
||||||
|
#f)]
|
||||||
[pattern (~seq (#%braces template ...)
|
[pattern (~seq (#%braces template ...)
|
||||||
(#%braces code ...))
|
(#%braces code ...))
|
||||||
#:with (fixed ...) (fix-template #'(template ...))])
|
#:with (fixed ...) (fix-template #'(template ...))])
|
||||||
|
@ -495,7 +538,7 @@
|
||||||
[your-braces (datum->syntax #'name '#%braces #'name)]
|
[your-braces (datum->syntax #'name '#%braces #'name)]
|
||||||
#;
|
#;
|
||||||
[your-parens (datum->syntax #'name '#%parens #'name)])
|
[your-parens (datum->syntax #'name '#%parens #'name)])
|
||||||
|
;;(printf "Ok macro3 go!\n")
|
||||||
#;
|
#;
|
||||||
#'(define-honu-syntax name
|
#'(define-honu-syntax name
|
||||||
(lambda (stx ctx)
|
(lambda (stx ctx)
|
||||||
|
@ -517,6 +560,7 @@
|
||||||
(lambda (stx ctx)
|
(lambda (stx ctx)
|
||||||
#;
|
#;
|
||||||
(printf "Executing macro `~a' on input `~a'\n" 'name (syntax->datum stx))
|
(printf "Executing macro `~a' on input `~a'\n" 'name (syntax->datum stx))
|
||||||
|
(printf "~a pattern is ~a\n" 'name '(pattern.fixed ... ...))
|
||||||
(syntax-parse stx
|
(syntax-parse stx
|
||||||
#:literal-sets ([cruft #:at name])
|
#:literal-sets ([cruft #:at name])
|
||||||
#:literals (foobar literals ...)
|
#:literals (foobar literals ...)
|
||||||
|
|
|
@ -149,7 +149,7 @@
|
||||||
#f)]
|
#f)]
|
||||||
|
|
||||||
[pattern (~seq (~var e (expression-simple context))
|
[pattern (~seq (~var e (expression-simple context))
|
||||||
(~var dx (debug-here (format "call 1 ~a" #'e)))
|
(~var dx (debug-here (format "call 1 ~a" (syntax->datum #'e))))
|
||||||
(#%parens
|
(#%parens
|
||||||
(~seq (~var dz (debug-here (format "call 2")))
|
(~seq (~var dz (debug-here (format "call 2")))
|
||||||
(~var arg (ternary context))
|
(~var arg (ternary context))
|
||||||
|
@ -346,7 +346,7 @@
|
||||||
(define-splicing-syntax-class (ternary context)
|
(define-splicing-syntax-class (ternary context)
|
||||||
#:literals (honu-? honu-:)
|
#:literals (honu-? honu-:)
|
||||||
[pattern (~seq (~var condition (expression-1 context))
|
[pattern (~seq (~var condition (expression-1 context))
|
||||||
(~var x1 (debug-here (format "ternary 1 ~a\n" #'condition.result)))
|
(~var x1 (debug-here (format "ternary 1 ~a\n" (syntax->datum #'condition.result))))
|
||||||
(~optional (~seq honu-? (~var on-true (ternary context))
|
(~optional (~seq honu-? (~var on-true (ternary context))
|
||||||
honu-: (~var on-false (ternary context))))
|
honu-: (~var on-false (ternary context))))
|
||||||
(~var x2 (debug-here "ternary 2"))
|
(~var x2 (debug-here "ternary 2"))
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
provide function;
|
provide function;
|
||||||
macro function ()
|
macro function ()
|
||||||
{ _ name:identifier (args:identifier ...) { body ... } }
|
{ _ name:identifier (args:identifier , ...) { body ... } }
|
||||||
{ #sx scheme:syntax #sx(define (name_result args_result ...)
|
{ #sx scheme:syntax #sx(define (name_result args_result ...)
|
||||||
(honu-unparsed-begin body ...)) }
|
(honu-unparsed-begin body ...)) }
|
||||||
{ _ (args:identifier ...) { body ... }}
|
{ _ (args:identifier , ...) { body ... }}
|
||||||
{ #sx scheme:syntax #sx(lambda (args_result ...) (honu-unparsed-begin body ...)) }
|
{ #sx scheme:syntax #sx(lambda (args_result ...) (honu-unparsed-begin body ...)) }
|
||||||
|
|
|
@ -4,7 +4,7 @@ require (forSyntax "struct-patterns.honu");
|
||||||
|
|
||||||
provide struct;
|
provide struct;
|
||||||
macro struct () {
|
macro struct () {
|
||||||
_ name {field:structField ...} ;
|
_ name {field:structField, ...} ;
|
||||||
} {
|
} {
|
||||||
schemeSyntax#sx(honu-struct name (field_name_result ...))
|
schemeSyntax#sx(honu-struct name (field_name_result ...))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user