updated syntax/parse/exp./splicing, updated honu use
fixed stress.rkt
This commit is contained in:
parent
675cdfda58
commit
712a8f60e9
|
@ -4,7 +4,7 @@
|
||||||
"util.ss"
|
"util.ss"
|
||||||
(for-template "literals.ss")
|
(for-template "literals.ss")
|
||||||
syntax/parse
|
syntax/parse
|
||||||
syntax/parse/experimental
|
syntax/parse/experimental/splicing
|
||||||
scheme/splicing
|
scheme/splicing
|
||||||
syntax/stx
|
syntax/stx
|
||||||
(for-syntax "util.ss")
|
(for-syntax "util.ss")
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
[else (loop (stx-cdr start) (add1 count))]))))
|
[else (loop (stx-cdr start) (add1 count))]))))
|
||||||
|
|
||||||
(define-primitive-splicing-syntax-class (honu-expr context)
|
(define-primitive-splicing-syntax-class (honu-expr context)
|
||||||
#:attrs (result)
|
#:attributes (result)
|
||||||
#:description "honu-expr"
|
#:description "honu-expr"
|
||||||
(lambda (stx fail)
|
(lambda (stx fail)
|
||||||
(cond
|
(cond
|
||||||
|
@ -42,11 +42,11 @@
|
||||||
[(get-transformer stx) => (lambda (transformer)
|
[(get-transformer stx) => (lambda (transformer)
|
||||||
(let-values ([(used rest)
|
(let-values ([(used rest)
|
||||||
(transformer stx context)])
|
(transformer stx context)])
|
||||||
(list rest (syntax-object-position stx rest)
|
(list (syntax-object-position stx rest)
|
||||||
used)))]
|
used)))]
|
||||||
|
|
||||||
[else (syntax-case stx ()
|
[else (syntax-case stx ()
|
||||||
[(f . rest) (list #'rest 1 #'f)])])))
|
[(f . rest) (list 1 #'f)])])))
|
||||||
|
|
||||||
#;
|
#;
|
||||||
(define-splicing-syntax-class expr
|
(define-splicing-syntax-class expr
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
#lang racket/base
|
|
||||||
(require "experimental/splicing.rkt")
|
|
||||||
(provide (all-from-out "experimental/splicing.rkt"))
|
|
|
@ -1,6 +1,7 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require (for-syntax racket/base
|
(require (for-syntax racket/base
|
||||||
"../../parse.ss"
|
"../../parse.ss"
|
||||||
|
"../private/rep-attrs.rkt"
|
||||||
"../private/rep-data.rkt"
|
"../private/rep-data.rkt"
|
||||||
"../private/kws.rkt")
|
"../private/kws.rkt")
|
||||||
"../private/runtime-progress.rkt"
|
"../private/runtime-progress.rkt"
|
||||||
|
@ -16,7 +17,7 @@
|
||||||
|
|
||||||
(syntax-parse stx
|
(syntax-parse stx
|
||||||
[(dssp (name:id param:id ...)
|
[(dssp (name:id param:id ...)
|
||||||
(~or (~once (~seq #:attrs (a:attr ...))
|
(~or (~once (~seq #:attributes (a:attr ...))
|
||||||
#:name "attributes declaration")
|
#:name "attributes declaration")
|
||||||
(~once (~seq #:description description)
|
(~once (~seq #:description description)
|
||||||
#:name "description declaration")) ...
|
#:name "description declaration")) ...
|
||||||
|
@ -25,6 +26,7 @@
|
||||||
(define (get-description param ...)
|
(define (get-description param ...)
|
||||||
description)
|
description)
|
||||||
(define parser
|
(define parser
|
||||||
|
(let ([permute (mk-permute '(a.name ...))])
|
||||||
(lambda (x cx pr es fh cp success param ...)
|
(lambda (x cx pr es fh cp success param ...)
|
||||||
(let ([stx (datum->syntax cx x cx)])
|
(let ([stx (datum->syntax cx x cx)])
|
||||||
(let ([result
|
(let ([result
|
||||||
|
@ -36,36 +38,51 @@
|
||||||
(case (car result)
|
(case (car result)
|
||||||
((ok)
|
((ok)
|
||||||
(apply success
|
(apply success
|
||||||
((mk-check-result pr 'name '(a.name ...) x cx fh cp) (cdr result))))
|
((mk-check-result pr 'name (length '(a.name ...)) permute x cx fh cp)
|
||||||
|
(cdr result))))
|
||||||
((error)
|
((error)
|
||||||
(let ([es
|
(let ([es
|
||||||
(list* (cons (expect:thing (get-description param ...) #f) stx)
|
(list* (cons (expect:thing (get-description param ...) #f) stx)
|
||||||
(cons (expect:message (cadr result)) (caddr result))
|
(cons (expect:message (cadr result)) (caddr result))
|
||||||
es)])
|
es)])
|
||||||
(fh (failure pr es)))))))))
|
(fh (failure pr es))))))))))
|
||||||
(define-syntax name
|
(define-syntax name
|
||||||
(make-stxclass 'name (arity (length '(param ...)) (length '(param ...)) '() '())
|
(stxclass 'name (arity (length '(param ...)) (length '(param ...)) '() '())
|
||||||
'(#s(attr a.name a.depth #f) ...)
|
(sort-sattrs '(#s(attr a.name a.depth #f) ...))
|
||||||
(quote-syntax parser)
|
(quote-syntax parser)
|
||||||
#t
|
#t
|
||||||
#s(options #t #t)
|
#s(options #t #t)
|
||||||
#f)))]))
|
#f)))]))
|
||||||
|
|
||||||
(define (mk-check-result pr name attr-names x cx fh cp)
|
(define (mk-permute unsorted-attrs)
|
||||||
|
(let ([sorted-attrs
|
||||||
|
(sort unsorted-attrs string<? #:key symbol->string #:cache-keys? #t)])
|
||||||
|
(if (equal? unsorted-attrs sorted-attrs)
|
||||||
|
values
|
||||||
|
(let* ([pos-table
|
||||||
|
(for/hasheq ([a (in-list unsorted-attrs)] [i (in-naturals)])
|
||||||
|
(values a i))]
|
||||||
|
[indexes
|
||||||
|
(for/vector ([a (in-list sorted-attrs)])
|
||||||
|
(hash-ref pos-table a))])
|
||||||
|
(lambda (result)
|
||||||
|
(for/list ([index (in-vector indexes)])
|
||||||
|
(list-ref result index)))))))
|
||||||
|
|
||||||
|
(define (mk-check-result pr name attr-count permute x cx fh cp)
|
||||||
(lambda (result)
|
(lambda (result)
|
||||||
(unless (list? result)
|
(unless (list? result)
|
||||||
(error name "parser returned non-list"))
|
(error name "parser returned non-list"))
|
||||||
(let ([rlength (length result)])
|
(let ([rlength (length result)])
|
||||||
(unless (= rlength (+ 2 (length attr-names)))
|
(unless (= rlength (+ 1 attr-count))
|
||||||
(error name "parser returned list of wrong length; expected length ~s, got ~e"
|
(error name "parser returned list of wrong length; expected length ~s, got ~e"
|
||||||
(+ 2 (length attr-names))
|
(+ 1 attr-count)
|
||||||
result))
|
result))
|
||||||
;; Ignore (car result), supposed to be rest-x
|
(let ([skip (car result)])
|
||||||
;; Easier to recompute it and get rest-cx right, too.
|
;; Compute rest-x & rest-cx from skip
|
||||||
(let ([skip (cadr result)])
|
|
||||||
(unless (exact-nonnegative-integer? skip)
|
(unless (exact-nonnegative-integer? skip)
|
||||||
(error name "expected exact nonnegative integer for second element of result list, got ~e"
|
(error name "expected exact nonnegative integer for first element of result list, got ~e"
|
||||||
skip))
|
skip))
|
||||||
(let-values ([(rest-x rest-cx) (stx-list-drop/cx x cx skip)])
|
(let-values ([(rest-x rest-cx) (stx-list-drop/cx x cx skip)])
|
||||||
(list* fh cp rest-x rest-cx (ps-add-cdr pr skip)
|
(list* fh cp rest-x rest-cx (ps-add-cdr pr skip)
|
||||||
(cddr result)))))))
|
(permute (cdr result))))))))
|
||||||
|
|
|
@ -176,17 +176,16 @@ Like @scheme[~reflect] but for reified splicing syntax classes.
|
||||||
parser-expr)
|
parser-expr)
|
||||||
#:contracts ([parser (-> syntax?
|
#:contracts ([parser (-> syntax?
|
||||||
(->* () ((or/c string? #f) -> any))
|
(->* () ((or/c string? #f) -> any))
|
||||||
(list syntax? exact-positive-integer? any/c ...))])]{
|
(cons/c exact-positive-integer? list?))])]{
|
||||||
|
|
||||||
Defines a splicing syntax via a procedural parser.
|
Defines a splicing syntax via a procedural parser.
|
||||||
|
|
||||||
The parser procedure is given two arguments, the syntax to parse and a
|
The parser procedure is given two arguments, the syntax to parse and a
|
||||||
failure procedure. To signal a successful parse, the parser procedure
|
failure procedure. To signal a successful parse, the parser procedure
|
||||||
returns a list of 2+@scheme[N] elements, where @scheme[N] is the
|
returns a list of @scheme[N]+1 elements, where @scheme[N] is the
|
||||||
number of attributes declared by the splicing syntax class. The first
|
number of attributes declared by the splicing syntax class. The first
|
||||||
two elements are the unconsumed part of the syntax and the size of the
|
element is the size of the prefix consumed. The rest of the list
|
||||||
prefix consumed. The rest of the list contains the values of the
|
contains the values of the attributes.
|
||||||
attributes.
|
|
||||||
|
|
||||||
To indicate failure, the parser calls the failure procedure with an
|
To indicate failure, the parser calls the failure procedure with an
|
||||||
optional message argument.
|
optional message argument.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#lang racket
|
||||||
|
|
||||||
(begin
|
(begin
|
||||||
(require syntax/parse)
|
(require syntax/parse)
|
||||||
(define (mkstx n) (datum->syntax #f (for/list ([i (in-range n)]) #'hello)))
|
(define (mkstx n) (datum->syntax #f (for/list ([i (in-range n)]) #'hello)))
|
||||||
|
@ -96,10 +98,11 @@ cpu time: 2908 real time: 2901 gc time: 12
|
||||||
|#
|
|#
|
||||||
|
|
||||||
|
|
||||||
|
#|
|
||||||
given pattern (E ...) where E = A _ | A
|
given pattern (E ...) where E = A _ | A
|
||||||
the sequence (A A B A A B A A B ...)
|
the sequence (A A B A A B A A B ...)
|
||||||
causes each E to backtrack
|
causes each E to backtrack
|
||||||
|
|#
|
||||||
|
|
||||||
(begin
|
(begin
|
||||||
(define-syntax-class id/nat
|
(define-syntax-class id/nat
|
||||||
|
@ -132,6 +135,8 @@ causes each E to backtrack
|
||||||
|
|
||||||
;; (solve 35 _) and (solve 36 _) seem manageable
|
;; (solve 35 _) and (solve 36 _) seem manageable
|
||||||
|
|
||||||
|
#|
|
||||||
|
|
||||||
#| before markparams |#
|
#| before markparams |#
|
||||||
|
|
||||||
> (time (parse/trip tripstx3 100))
|
> (time (parse/trip tripstx3 100))
|
||||||
|
@ -246,3 +251,4 @@ cpu time: 588 real time: 588 gc time: 76
|
||||||
cpu time: 580 real time: 584 gc time: 68
|
cpu time: 580 real time: 584 gc time: 68
|
||||||
> (time (solve 36 20))
|
> (time (solve 36 20))
|
||||||
cpu time: 580 real time: 586 gc time: 56
|
cpu time: 580 real time: 586 gc time: 56
|
||||||
|
|#
|
||||||
|
|
Loading…
Reference in New Issue
Block a user