www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

parse.rkt (56571B)


      1 #lang racket/base
      2 (require (for-syntax racket/base
      3                      syntax/stx
      4                      syntax/private/id-table
      5                      syntax/keyword
      6                      racket/syntax
      7                      syntax/parse/private/minimatch
      8                      syntax/parse/private/rep-attrs
      9                      syntax/parse/private/rep-data
     10                      syntax/parse/private/rep-patterns
     11                      "rep.rkt"
     12                      syntax/parse/private/kws
     13                      "opt.rkt"
     14                      "txlift.rkt")
     15          syntax/parse/private/keywords
     16          racket/syntax
     17          racket/stxparam
     18          syntax/stx
     19          stxparse-info/parse/private/residual ;; keep abs. path
     20          "runtime.rkt"
     21          stxparse-info/parse/private/runtime-reflect) ;; keep abs. path
     22 
     23 ;; ============================================================
     24 
     25 (provide define-syntax-class
     26          define-splicing-syntax-class
     27          define-integrable-syntax-class
     28          syntax-parse
     29          syntax-parser
     30          define/syntax-parse
     31          syntax-parser/template
     32          parser/rhs
     33          define-eh-alternative-set
     34          (for-syntax rhs->parser))
     35 
     36 (begin-for-syntax
     37  ;; constant-desc : Syntax -> String/#f
     38  (define (constant-desc stx)
     39    (syntax-case stx (quote)
     40      [(quote datum)
     41       (let ([d (syntax-e #'datum)])
     42         (and (string? d) d))]
     43      [expr
     44       (let ([d (syntax-e #'expr)])
     45         (and (string? d)
     46              (free-identifier=? #'#%datum (datum->syntax #'expr '#%datum))
     47              d))]))
     48 
     49  (define (tx:define-*-syntax-class stx splicing?)
     50    (syntax-case stx ()
     51      [(_ header . rhss)
     52       (parameterize ((current-syntax-context stx))
     53         (let-values ([(name formals arity)
     54                       (let ([p (check-stxclass-header #'header stx)])
     55                         (values (car p) (cadr p) (caddr p)))])
     56           (let ([the-rhs (parse-rhs #'rhss #f splicing? #:context stx)])
     57             (with-syntax ([name name]
     58                           [formals formals]
     59                           [desc (cond [(rhs-description the-rhs) => constant-desc]
     60                                       [else (symbol->string (syntax-e name))])]
     61                           [parser (generate-temporary (format-symbol "parse-~a" name))]
     62                           [arity arity]
     63                           [attrs (rhs-attrs the-rhs)]
     64                           [commit? (rhs-commit? the-rhs)]
     65                           [delimit-cut? (rhs-delimit-cut? the-rhs)])
     66               #`(begin (define-syntax name
     67                          (stxclass 'name 'arity
     68                                    'attrs
     69                                    (quote-syntax parser)
     70                                    '#,splicing?
     71                                    (scopts (length 'attrs) 'commit? 'delimit-cut? desc)
     72                                    #f))
     73                        (define-values (parser)
     74                          (parser/rhs name formals attrs rhss #,splicing? #,stx)))))))])))
     75 
     76 (define-syntax define-syntax-class
     77   (lambda (stx) (tx:define-*-syntax-class stx #f)))
     78 (define-syntax define-splicing-syntax-class
     79   (lambda (stx) (tx:define-*-syntax-class stx #t)))
     80 
     81 (define-syntax (define-integrable-syntax-class stx)
     82   (syntax-case stx (quote)
     83     [(_ name (quote description) predicate)
     84      (with-syntax ([parser (generate-temporary (format-symbol "parse-~a" (syntax-e #'name)))]
     85                    [no-arity no-arity])
     86        #'(begin (define-syntax name
     87                   (stxclass 'name no-arity '()
     88                             (quote-syntax parser)
     89                             #f
     90                             (scopts 0 #t #t 'description)
     91                             (quote-syntax predicate)))
     92                 (define (parser x cx pr es undos fh0 cp0 rl success)
     93                   (if (predicate x)
     94                       (success fh0 undos)
     95                       (let ([es (es-add-thing pr 'description #t rl es)])
     96                         (fh0 undos (failure* pr es)))))))]))
     97 
     98 (define-syntax (parser/rhs stx)
     99   (syntax-case stx ()
    100     [(parser/rhs name formals relsattrs rhss splicing? ctx)
    101      (with-disappeared-uses
    102       (let ()
    103         (define the-rhs
    104           (parameterize ((current-syntax-context #'ctx))
    105             (parse-rhs #'rhss (syntax->datum #'relsattrs) (syntax-e #'splicing?)
    106                        #:context #'ctx)))
    107         (rhs->parser #'name #'formals #'relsattrs the-rhs (syntax-e #'splicing?) #'ctx)))]))
    108 
    109 (begin-for-syntax
    110  (define (rhs->parser name formals relsattrs the-rhs splicing? [ctx #f])
    111    (define-values (transparent? description variants defs commit? delimit-cut?)
    112      (match the-rhs
    113        [(rhs _ transparent? description variants defs commit? delimit-cut?)
    114         (values transparent? description variants defs commit? delimit-cut?)]))
    115    (define vdefss (map variant-definitions variants))
    116    (define formals* (rewrite-formals formals #'x #'rl))
    117    (define patterns (map variant-pattern variants))
    118    (define no-fail?
    119      (and (not splicing?) ;; FIXME: commit? needed?
    120           (patterns-cannot-fail? patterns)))
    121    (when no-fail? (log-syntax-parse-debug "(stxclass) cannot fail: ~e" ctx))
    122    (define body
    123      (cond [(null? patterns)
    124             #'(fail (failure* pr es))]
    125            [splicing?
    126             (with-syntax ([(alternative ...)
    127                            (for/list ([pattern (in-list patterns)])
    128                              (with-syntax ([pattern pattern]
    129                                            [relsattrs relsattrs]
    130                                            [iattrs (pattern-attrs pattern)]
    131                                            [commit? commit?]
    132                                            [result-pr
    133                                             (if transparent?
    134                                                 #'rest-pr
    135                                                 #'(ps-pop-opaque rest-pr))])
    136                                #'(parse:H x cx rest-x rest-cx rest-pr pattern pr es
    137                                           (variant-success relsattrs iattrs (rest-x rest-cx result-pr)
    138                                                            success cp0 commit?))))])
    139               #'(try alternative ...))]
    140            [else
    141             (with-syntax ([matrix
    142                            (optimize-matrix
    143                             (for/list ([pattern (in-list patterns)])
    144                               (with-syntax ([iattrs (pattern-attrs pattern)]
    145                                             [relsattrs relsattrs]
    146                                             [commit? commit?])
    147                                 (pk1 (list pattern)
    148                                      #'(variant-success relsattrs iattrs ()
    149                                                         success cp0 commit?)))))])
    150               #'(parse:matrix ((x cx pr es)) matrix))]))
    151    (with-syntax ([formals* formals*]
    152                  [(def ...) defs]
    153                  [((vdef ...) ...) vdefss]
    154                  [description (or description (symbol->string (syntax-e name)))]
    155                  [transparent? transparent?]
    156                  [delimit-cut? delimit-cut?]
    157                  [body body])
    158      #`(lambda (x cx pr es undos fh0 cp0 rl success . formals*)
    159          (with ([this-syntax x]
    160                 [this-role rl])
    161                def ...
    162                vdef ... ...
    163                (#%expression
    164                 (syntax-parameterize ((this-context-syntax
    165                                        (syntax-rules ()
    166                                          [(tbs) (ps-context-syntax pr)])))
    167                   (let ([es (es-add-thing pr description 'transparent? rl
    168                                           #,(if no-fail? #'#f #'es))]
    169                         [pr (if 'transparent? pr (ps-add-opaque pr))])
    170                     (with ([fail-handler fh0]
    171                            [cut-prompt cp0]
    172                            [undo-stack undos])
    173                       ;; Update the prompt, if required
    174                       ;; FIXME: can be optimized away if no cut exposed within variants
    175                       (with-maybe-delimit-cut delimit-cut?
    176                         body))))))))))
    177 
    178 (define-syntax (syntax-parse stx)
    179   (syntax-case stx ()
    180     [(syntax-parse stx-expr . clauses)
    181      (quasisyntax/loc stx
    182        (let ([x (datum->syntax #f stx-expr)])
    183          (with ([this-syntax x])
    184            (parse:clauses x clauses body-sequence #,((make-syntax-introducer) stx)))))]))
    185 
    186 (define-syntax (syntax-parser stx)
    187   (syntax-case stx ()
    188     [(syntax-parser . clauses)
    189      (quasisyntax/loc stx
    190        (lambda (x)
    191          (let ([x (datum->syntax #f x)])
    192            (with ([this-syntax x])
    193              (parse:clauses x clauses body-sequence #,((make-syntax-introducer) stx))))))]))
    194 
    195 (define-syntax (syntax-parser/template stx)
    196   (syntax-case stx ()
    197     [(syntax-parser/template ctx . clauses)
    198      (quasisyntax/loc stx
    199        (lambda (x)
    200          (let ([x (datum->syntax #f x)])
    201            (with ([this-syntax x])
    202              (parse:clauses x clauses one-template ctx)))))]))
    203 
    204 (define-syntax (define/syntax-parse stx)
    205   (syntax-case stx ()
    206     [(define/syntax-parse pattern . rest)
    207      (with-disappeared-uses
    208       (let-values ([(rest pattern defs)
    209                     (parse-pattern+sides #'pattern
    210                                          #'rest
    211                                          #:splicing? #f
    212                                          #:decls (new-declenv null)
    213                                          #:context stx)])
    214         (let ([expr
    215                (syntax-case rest ()
    216                  [( expr ) #'expr]
    217                  [_ (raise-syntax-error #f "bad syntax" stx)])]
    218               [attrs (pattern-attrs pattern)])
    219           (with-syntax ([(a ...) attrs]
    220                         [(#s(attr name _ _) ...) attrs]
    221                         [pattern pattern]
    222                         [(def ...) defs]
    223                         [expr expr])
    224             #'(defattrs/unpack (a ...)
    225                 (let* ([x (datum->syntax #f expr)]
    226                        [cx x]
    227                        [pr (ps-empty x x)]
    228                        [es #f]
    229                        [fh0 (syntax-patterns-fail x)])
    230                   (parameterize ((current-syntax-context x))
    231                     def ...
    232                     (#%expression
    233                      (with ([fail-handler fh0]
    234                             [cut-prompt fh0]
    235                             [undo-stack null])
    236                            (parse:S x cx pattern pr es
    237                                     (list (attribute name) ...)))))))))))]))
    238 
    239 ;; ============================================================
    240 
    241 #|
    242 Parsing protocols:
    243 
    244 (parse:<X> <X-args> pr es success-expr) : Ans
    245 
    246   <S-args> : x cx
    247   <H-args> : x cx rest-x rest-cx rest-pr
    248   <EH-args> : x cx ???
    249   <A-args> : x cx
    250 
    251   x is term to parse, usually syntax but can be pair/null (stx-list?) in cdr patterns
    252   cx is most recent syntax object: if x must be coerced to syntax, use cx as lexctx and src
    253   pr, es are progress and expectstack, respectively
    254   rest-x, rest-cx, rest-pr are variable names to bind in context of success-expr
    255 
    256 (stxclass-parser x cx pr es undos fail-handler cut-prompt role success-proc arg ...) : Ans
    257 
    258   success-proc:
    259     for stxclass, is (fail-handler undos attr-value ... -> Ans)
    260     for splicing-stxclass, is (undos fail-handler rest-x rest-cx rest-pr attr-value -> Ans)
    261   fail-handler, cut-prompt : undos failure -> Ans
    262 
    263 Fail-handler is normally represented with stxparam 'fail-handler', but must be
    264 threaded through stxclass calls (in through stxclass-parser, out through
    265 success-proc) to support backtracking. Cut-prompt is never changed within
    266 stxclass or within alternative, so no threading needed.
    267 
    268 The undo stack is normally represented with stxparam 'undo-stack', but must be
    269 threaded through stxclass calls (like fail-handler). A failure handler closes
    270 over a base undo stack and receives an extended current undo stack; the failure
    271 handler unwinds effects by performing every action in the difference between
    272 them and then restores the saved undo stack.
    273 
    274 Usually sub-patterns processed in tail position, but *can* do non-tail calls for:
    275   - ~commit
    276   - var of stxclass with ~commit
    277 It is also safe to keep normal tail-call protocol and just adjust fail-handler.
    278 There is no real benefit to specializing ~commit, since it does not involve
    279 creating a success closure.
    280 
    281 Some optimizations:
    282   - commit protocol for stxclasses (but not ~commit, no point)
    283   - avoid continue-vs-end choice point in (EH ... . ()) by eager pair check
    284   - integrable stxclasses, specialize ellipses of integrable stxclasses
    285   - pattern lists that cannot fail set es=#f to disable ExpectStack allocation
    286 |#
    287 
    288 ;; ----
    289 
    290 (begin-for-syntax
    291  (define (wash stx)
    292    (syntax-e stx))
    293  (define (wash-list washer stx)
    294    (let ([l (stx->list stx)])
    295      (unless l (raise-type-error 'wash-list "stx-list" stx))
    296      (map washer l)))
    297  (define (wash-iattr stx)
    298    (with-syntax ([#s(attr name depth syntax?) stx])
    299      (attr #'name (wash #'depth) (wash #'syntax?))))
    300  (define (wash-sattr stx)
    301    (with-syntax ([#s(attr name depth syntax?) stx])
    302      (attr (wash #'name) (wash #'depth) (wash #'syntax?))))
    303  (define (wash-iattrs stx)
    304    (wash-list wash-iattr stx))
    305  (define (wash-sattrs stx)
    306    (wash-list wash-sattr stx))
    307  (define (generate-n-temporaries n)
    308    (generate-temporaries
    309     (for/list ([i (in-range n)])
    310       (string->symbol (format "g~sx" i))))))
    311 
    312 ;; ----
    313 
    314 #|
    315 Conventions:
    316   - rhs : RHS
    317   - iattr : IAttr
    318   - relsattr : SAttr
    319   - splicing? : bool
    320   - x : id (var)
    321   - cx : id (var, may be shadowed)
    322   - pr : id (var, may be shadowed)
    323   - es : id (var, may be shadowed)
    324   - success : var (bound to success procedure)
    325   - k : expr
    326   - rest-x, rest-cx, rest-pr : id (to be bound)
    327   - fh, cp, rl : id (var)
    328 |#
    329 
    330 (begin-for-syntax
    331  (define (rewrite-formals fstx x-id rl-id)
    332    (with-syntax ([x x-id]
    333                  [rl rl-id])
    334      (let loop ([fstx fstx])
    335        (syntax-case fstx ()
    336          [([kw arg default] . more)
    337           (keyword? (syntax-e #'kw))
    338           (cons #'(kw arg (with ([this-syntax x] [this-role rl]) default))
    339                 (loop #'more))]
    340          [([arg default] . more)
    341           (not (keyword? (syntax-e #'kw)))
    342           (cons #'(arg (with ([this-syntax x] [this-role rl]) default))
    343                 (loop #'more))]
    344          [(formal . more)
    345           (cons #'formal (loop #'more))]
    346          [_ fstx])))))
    347 
    348 ;; (with-maybe-delimit-cut bool expr)
    349 (define-syntax with-maybe-delimit-cut
    350   (syntax-rules ()
    351     [(wmdc #t k)
    352      (with ([cut-prompt fail-handler]) k)]
    353     [(wmdc #f k)
    354      k]))
    355 
    356 ;; (variant-success relsattrs variant (also:id ...) success bool) : expr[Ans]
    357 (define-syntax (variant-success stx)
    358   (syntax-case stx ()
    359     [(variant-success relsattrs iattrs (also ...) success cp0 commit?)
    360      #`(with-maybe-reset-fail commit? cp0
    361          (base-success-expr iattrs relsattrs (also ...) success))]))
    362 
    363 ;; (with-maybe-reset-fail bool id expr)
    364 (define-syntax with-maybe-reset-fail
    365   (syntax-rules ()
    366     [(wmrs #t cp0 k)
    367      (with ([fail-handler cp0]) k)]
    368     [(wmrs #f cp0 k)
    369      k]))
    370 
    371 ;; (base-success-expr iattrs relsattrs (also:id ...) success) : expr[Ans]
    372 (define-syntax (base-success-expr stx)
    373   (syntax-case stx ()
    374     [(base-success-expr iattrs relsattrs (also ...) success)
    375      (let ([reliattrs
    376             (reorder-iattrs (wash-sattrs #'relsattrs)
    377                             (wash-iattrs #'iattrs))])
    378        (with-syntax ([(#s(attr name _ _) ...) reliattrs])
    379          #'(success fail-handler undo-stack also ... (attribute name) ...)))]))
    380 
    381 ;; ----
    382 
    383 ;; (parse:clauses x clauses ctx)
    384 (define-syntax (parse:clauses stx)
    385   (syntax-case stx ()
    386     [(parse:clauses x clauses body-mode ctx)
    387      ;; if templates? is true, expect one form after kwargs in clause, wrap it with syntax
    388      ;; otherwise, expect non-empty body sequence (defs and exprs)
    389      (with-disappeared-uses
    390       (with-txlifts
    391        (lambda ()
    392         (define who
    393           (syntax-case #'ctx ()
    394             [(m . _) (identifier? #'m) #'m]
    395             [_ 'syntax-parse]))
    396         (define-values (chunks clauses-stx)
    397           (parse-keyword-options #'clauses parse-directive-table
    398                                  #:context #'ctx
    399                                  #:no-duplicates? #t))
    400         (define context
    401           (options-select-value chunks '#:context #:default #'x))
    402         (define colon-notation?
    403           (not (assq '#:disable-colon-notation chunks)))
    404         (define-values (decls0 defs)
    405           (get-decls+defs chunks #t #:context #'ctx))
    406         ;; for-clause : stx -> (values pattern stx (listof stx))
    407         (define (for-clause clause)
    408           (syntax-case clause ()
    409             [[p . rest]
    410              (let-values ([(rest pattern defs2)
    411                            (parameterize ((stxclass-colon-notation? colon-notation?))
    412                              (parse-pattern+sides #'p #'rest
    413                                                   #:splicing? #f
    414                                                   #:decls decls0
    415                                                   #:context #'ctx))])
    416                (let ([body-expr
    417                       (case (syntax-e #'body-mode)
    418                         ((one-template)
    419                          (syntax-case rest ()
    420                            [(template)
    421                             #'(syntax template)]
    422                            [_ (raise-syntax-error #f "expected exactly one template" #'ctx)]))
    423                         ((body-sequence)
    424                          (syntax-case rest ()
    425                            [(e0 e ...)
    426                             ;; Should we use a shadower (works on the whole file, unhygienically),
    427                             ;; or use the context of the syntax-parse identifier?
    428                             (let ([the-#%intdef-begin (datum->syntax #'ctx '#%intdef-begin)])
    429                               (if (syntax-local-value the-#%intdef-begin (λ () #f)) ;; Defined as a macro
    430                                   #`(let () (#,the-#%intdef-begin e0 e ...))
    431                                   #'(let () e0 e ...)))]
    432                            [_ (raise-syntax-error #f "expected non-empty clause body"
    433                                                   #'ctx clause)]))
    434                         (else
    435                          (raise-syntax-error #f "internal error: unknown body mode" #'ctx #'body-mode)))])
    436                  (values pattern body-expr defs2)))]
    437             [_ (raise-syntax-error #f "expected clause" #'ctx clause)]))
    438         (unless (stx-list? clauses-stx)
    439           (raise-syntax-error #f "expected sequence of clauses" #'ctx))
    440         (define-values (patterns body-exprs defs2s)
    441           (for/lists (patterns body-exprs defs2s) ([clause (in-list (stx->list clauses-stx))])
    442             (for-clause clause)))
    443         (define no-fail? (patterns-cannot-fail? patterns))
    444         (when no-fail? (log-syntax-parse-debug "cannot fail: ~e" #'ctx))
    445         (with-syntax ([(def ...) (apply append (get-txlifts-as-definitions) defs defs2s)])
    446           #`(let* ([ctx0 (normalize-context '#,who #,context x)]
    447                    [pr (ps-empty x (cadr ctx0))]
    448                    [es #,(if no-fail? #'#f #'#t)]
    449                    [cx x]
    450                    [fh0 (syntax-patterns-fail ctx0)])
    451               def ...
    452               (parameterize ((current-syntax-context (cadr ctx0))
    453                              (current-state '#hasheq())
    454                              (current-state-writable? #f))
    455                 (with ([fail-handler fh0]
    456                        [cut-prompt fh0]
    457                        [undo-stack null])
    458                   #,(cond [(pair? patterns)
    459                            (with-syntax ([matrix
    460                                           (optimize-matrix
    461                                            (for/list ([pattern (in-list patterns)]
    462                                                       [body-expr (in-list body-exprs)])
    463                                              (pk1 (list pattern) body-expr)))])
    464                              #'(parse:matrix ((x cx pr es)) matrix))
    465                            #|
    466                            (with-syntax ([(alternative ...)
    467                                           (for/list ([pattern (in-list patterns)]
    468                                                      [body-expr (in-list body-exprs)])
    469                                             #`(parse:S x cx #,pattern pr es #,body-expr))])
    470                              #`(try alternative ...))
    471                            |#]
    472                           [else
    473                            #`(fail (failure* pr es))]))))))))]))
    474 
    475 ;; ----
    476 
    477 ;; (parse:matrix ((x cx pr es) ...) (PK ...)) : expr[Ans]
    478 ;; (parse:matrix (in1 ... inN) (#s(pk1 (P11 ... P1N) e1) ... #s(pk1 (PM1 ... PMN) eM)))
    479 ;; represents the matching matrix
    480 ;;   [_in1_..._inN_|____]
    481 ;;   [ P11 ... P1N | e1 ]
    482 ;;   [  ⋮       ⋮  |  ⋮ ]
    483 ;;   [ PM1 ... PMN | eM ]
    484 
    485 (define-syntax (parse:matrix stx)
    486   (syntax-case stx ()
    487     [(parse:matrix ins (pk ...))
    488      #'(try (parse:pk ins pk) ...)]))
    489 
    490 (define-syntax (parse:pk stx)
    491   (syntax-case stx ()
    492     [(parse:pk () #s(pk1 () k))
    493      #'k]
    494     [(parse:pk ((x cx pr es) . ins) #s(pk1 (pat1 . pats) k))
    495      #'(parse:S x cx pat1 pr es (parse:pk ins #s(pk1 pats k)))]
    496     [(parse:pk ((x cx pr es) . ins) #s(pk/same pat1 inner))
    497      #'(parse:S x cx pat1 pr es (parse:matrix ins inner))]
    498     [(parse:pk ((x cx pr es) . ins) #s(pk/pair inner))
    499      #'(let-values ([(datum tcx)
    500                      (if (syntax? x)
    501                          (values (syntax-e x) x)
    502                          (values x cx))])
    503          (if (pair? datum)
    504              (let ([hx (car datum)]
    505                    [hcx (car datum)]
    506                    [hpr (ps-add-car pr)]
    507                    [tx (cdr datum)]
    508                    [tpr (ps-add-cdr pr)])
    509                (parse:matrix ((hx hcx hpr es) (tx tcx tpr es) . ins) inner))
    510              (let ([es* (if (null? datum) (es-add-proper-pair (first-desc:matrix inner) es) es)])
    511                (fail (failure* pr es*)))))]
    512     [(parse:pk (in1 . ins) #s(pk/and inner))
    513      #'(parse:matrix (in1 in1 . ins) inner)]))
    514 
    515 (define-syntax (first-desc:matrix stx)
    516   (syntax-case stx ()
    517     [(fdm (#s(pk1 (pat1 . pats) k)))
    518      #'(first-desc:S pat1)]
    519     [(fdm (#s(pk/same pat1 pks)))
    520      #'(first-desc:S pat1)]
    521     [(fdm (pk ...)) ;; FIXME
    522      #'#f]))
    523 
    524 ;; ----
    525 
    526 ;; (parse:S x cx S-pattern pr es k) : expr[Ans]
    527 ;; In k: attrs(S-pattern) are bound.
    528 (define-syntax (parse:S stx)
    529   (syntax-case stx ()
    530     [(parse:S x cx pattern0 pr es k)
    531      (syntax-case #'pattern0 ()
    532        [#s(internal-rest-pattern rest-x rest-cx rest-pr)
    533         #`(let ([rest-x x]
    534                 [rest-cx cx]
    535                 [rest-pr pr])
    536             k)]
    537        [#s(pat:any)
    538         #'k]
    539        [#s(pat:svar name)
    540         #'(let-attributes ([#s(attr name 0 #t) (datum->syntax cx x cx)])
    541             k)]
    542        [#s(pat:var/p name parser argu (nested-a ...) role
    543                      #s(scopts attr-count commit? _delimit? _desc))
    544         (with-syntax ([(av ...) (generate-n-temporaries (syntax-e #'attr-count))]
    545                       [(name-attr ...)
    546                        (if (identifier? #'name)
    547                            #'([#s(attr name 0 #t) (datum->syntax cx x cx)])
    548                            #'())])
    549           (if (not (syntax-e #'commit?))
    550               ;; The normal protocol
    551               #'(app-argu parser x cx pr es undo-stack fail-handler cut-prompt role
    552                           (lambda (fh undos av ...)
    553                             (let-attributes (name-attr ...)
    554                               (let-attributes* ((nested-a ...) (av ...))
    555                                 (with ([fail-handler fh] [undo-stack undos])
    556                                   k))))
    557                           argu)
    558               ;; The commit protocol
    559               ;; (Avoids putting k in procedure)
    560               #'(let-values ([(fs undos av ...)
    561                               (with ([fail-handler
    562                                       (lambda (undos fs)
    563                                         (unwind-to undos undo-stack)
    564                                         (values fs undo-stack (let ([av #f]) av) ...))])
    565                                 (with ([cut-prompt fail-handler])
    566                                   (app-argu parser x cx pr es undo-stack
    567                                             fail-handler cut-prompt role
    568                                             (lambda (fh undos av ...) (values #f undos av ...))
    569                                             argu)))])
    570                   (if fs
    571                       (fail fs)
    572                       (let-attributes (name-attr ...)
    573                         (let-attributes* ((nested-a ...) (av ...))
    574                           (with ([undo-stack undos])
    575                             k)))))))]
    576        [#s(pat:reflect obj argu attr-decls name (nested-a ...))
    577         (with-syntax ([(name-attr ...)
    578                        (if (identifier? #'name)
    579                            #'([#s(attr name 0 #t) (datum->syntax cx x cx)])
    580                            #'())])
    581           (with-syntax ([arity (arguments->arity (syntax->datum #'argu))])
    582             #'(let ([parser (reflect-parser obj 'arity 'attr-decls #f)])
    583                 (app-argu parser x cx pr es undo-stack fail-handler cut-prompt #f
    584                           (lambda (fh undos . result)
    585                             (let-attributes (name-attr ...)
    586                               (let/unpack ((nested-a ...) result)
    587                                 (with ([fail-handler fh] [undo-stack undos])
    588                                   k))))
    589                           argu))))]
    590        [#s(pat:datum datum)
    591         (with-syntax ([unwrap-x
    592                        (if (atomic-datum-stx? #'datum)
    593                            #'(if (syntax? x) (syntax-e x) x)
    594                            #'(syntax->datum (datum->syntax #f x)))])
    595           #`(let ([d unwrap-x])
    596               (if (equal? d (quote datum))
    597                   k
    598                   (fail (failure* pr (es-add-atom 'datum es))))))]
    599        [#s(pat:literal literal input-phase lit-phase)
    600         #`(if (and (identifier? x)
    601                    (free-identifier=? x (quote-syntax literal) input-phase lit-phase))
    602               (with ([undo-stack (cons (current-state) undo-stack)])
    603                 (state-cons! 'literals x)
    604                 k)
    605               (fail (failure* pr (es-add-literal (quote-syntax literal) es))))]
    606        [#s(pat:action action subpattern)
    607         #'(parse:A x cx action pr es (parse:S x cx subpattern pr es k))]
    608        [#s(pat:head head tail)
    609         #`(parse:H x cx rest-x rest-cx rest-pr head pr es
    610                    (parse:S rest-x rest-cx tail rest-pr es k))]
    611        [#s(pat:dots head tail)
    612         #`(parse:dots x cx head tail pr es k)]
    613        [#s(pat:and subpatterns)
    614         (for/fold ([k #'k]) ([subpattern (in-list (reverse (syntax->list #'subpatterns)))])
    615           #`(parse:S x cx #,subpattern pr es #,k))]
    616        [#s(pat:or (a ...) (subpattern ...) (subattrs ...))
    617         (with-syntax ([(#s(attr id _ _) ...) #'(a ...)])
    618           #`(let ([success
    619                    (lambda (fh undos id ...)
    620                      (let-attributes ([a id] ...)
    621                        (with ([fail-handler fh] [undo-stack undos])
    622                          k)))])
    623               (try (parse:S x cx subpattern pr es
    624                             (disjunct subattrs success () (id ...)))
    625                    ...)))]
    626        [#s(pat:not subpattern)
    627         #`(let* ([fh0 fail-handler]
    628                  [pr0 pr]
    629                  [es0 es]
    630                  [fail-to-succeed
    631                   (lambda (undos fs) (unwind-to undos undo-stack) k)])
    632             ;; ~not implicitly prompts to be safe,
    633             ;; but ~! not allowed within ~not (unless within ~delimit-cut, etc)
    634             ;; (statically checked!)
    635             (with ([fail-handler fail-to-succeed]
    636                    [cut-prompt fail-to-succeed]) ;; to be safe
    637               (parse:S x cx subpattern pr es
    638                        (fh0 undo-stack (failure* pr0 es0)))))]
    639        [#s(pat:pair head tail)
    640         #`(let ([datum (if (syntax? x) (syntax-e x) x)]
    641                 [cx (if (syntax? x) x cx)])  ;; FIXME: shadowing cx?!
    642             (if (pair? datum)
    643                 (let ([hx (car datum)]
    644                       [hcx (car datum)]
    645                       [hpr (ps-add-car pr)]
    646                       [tx (cdr datum)]
    647                       [tpr (ps-add-cdr pr)])
    648                   (parse:S hx hcx head hpr es
    649                            (parse:S tx cx tail tpr es k)))
    650                 (let ([es* (if (null? datum) (es-add-proper-pair (first-desc:S head) es) es)])
    651                   (fail (failure* pr es*)))))]
    652        [#s(pat:vector subpattern)
    653         #`(let ([datum (if (syntax? x) (syntax-e x) x)])
    654             (if (vector? datum)
    655                 (let ([datum (vector->list datum)]
    656                       [vcx (if (syntax? x) x cx)] ;; FIXME: (vector? datum) => (syntax? x) ???
    657                       [pr* (ps-add-unvector pr)])
    658                   (parse:S datum vcx subpattern pr* es k))
    659                 (fail (failure* pr es))))]
    660        [#s(pat:box subpattern)
    661         #`(let ([datum (if (syntax? x) (syntax-e x) x)])
    662             (if (box? datum)
    663                 (let ([datum (unbox datum)]
    664                       [bcx (if (syntax? x) x cx)] ;; FIXME: (box? datum) => (syntax? x) ???
    665                       [pr* (ps-add-unbox pr)])
    666                   (parse:S datum bcx subpattern pr* es k))
    667                 (fail (failure* pr es))))]
    668        [#s(pat:pstruct key subpattern)
    669         #`(let ([datum (if (syntax? x) (syntax-e x) x)])
    670             (if (let ([xkey (prefab-struct-key datum)])
    671                   (and xkey (equal? xkey 'key)))
    672                 (let ([datum (cdr (vector->list (struct->vector datum)))]
    673                       [scx (if (syntax? x) x cx)] ;; FIXME: (struct? datum) => (syntax? x) ???
    674                       [pr* (ps-add-unpstruct pr)])
    675                   (parse:S datum scx subpattern pr* es k))
    676                 (fail (failure* pr es))))]
    677        [#s(pat:describe pattern description transparent? role)
    678         #`(let ([es* (es-add-thing pr description transparent? role es)]
    679                 [pr* (if 'transparent? pr (ps-add-opaque pr))])
    680             (parse:S x cx pattern pr* es* k))]
    681        [#s(pat:delimit pattern)
    682         #`(let ([cp0 cut-prompt])
    683             (with ([cut-prompt fail-handler])
    684               (parse:S x cx pattern pr es (with ([cut-prompt cp0]) k))))]
    685        [#s(pat:commit pattern)
    686         #`(let ([fh0 fail-handler]
    687                 [cp0 cut-prompt])
    688             (with ([cut-prompt fh0])
    689               (parse:S x cx pattern pr es
    690                        (with ([cut-prompt cp0]
    691                               [fail-handler fh0])
    692                              k))))]
    693        [#s(pat:ord pattern group index)
    694         #`(let ([pr* (ps-add pr '#s(ord group index))])
    695             (parse:S x cx pattern pr* es k))]
    696        [#s(pat:post pattern)
    697         #`(let ([pr* (ps-add-post pr)])
    698             (parse:S x cx pattern pr* es k))]
    699        [#s(pat:integrated name predicate description role)
    700         (with-syntax ([(name-attr ...)
    701                        (if (identifier? #'name)
    702                            #'([#s(attr name 0 #t) x*])
    703                            #'())])
    704           #'(let ([x* (datum->syntax cx x cx)])
    705               (if (predicate x*)
    706                   (let-attributes (name-attr ...) k)
    707                   (let ([es* (es-add-thing pr 'description #t role es)])
    708                     (fail (failure* pr es*))))))])]))
    709 
    710 ;; (first-desc:S S-pattern) : expr[FirstDesc]
    711 (define-syntax (first-desc:S stx)
    712   (syntax-case stx ()
    713     [(fds p)
    714      (syntax-case #'p ()
    715        [#s(pat:any)
    716         #''(any)]
    717        [#s(pat:svar name)
    718         #''(any)]
    719        [#s(pat:var/p _ _ _ _ _ #s(scopts _ _ _ desc))
    720         #'(quote desc)]
    721        [#s(pat:datum d)
    722         #''(datum d)]
    723        [#s(pat:literal id _ip _lp)
    724         #''(literal id)]
    725        [#s(pat:describe _p desc _t? _role)
    726         #`(quote #,(or (constant-desc #'desc) #'#f))]
    727        [#s(pat:delimit pattern)
    728         #'(first-desc:S pattern)]
    729        [#s(pat:commit pattern)
    730         #'(first-desc:S pattern)]
    731        [#s(pat:ord pattern _ _)
    732         #'(first-desc:S pattern)]
    733        [#s(pat:post pattern)
    734         #'(first-desc:S pattern)]
    735        [#s(pat:integrated _name _pred description _role)
    736         #''description]
    737        [_ #'#f])]))
    738 
    739 ;; (first-desc:H HeadPattern) : Expr
    740 (define-syntax (first-desc:H stx)
    741   (syntax-case stx ()
    742     [(fdh hpat)
    743      (syntax-case #'hpat ()
    744        [#s(hpat:var/p _ _ _ _ _ #s(scopts _ _ _ desc)) #'(quote desc)]
    745        [#s(hpat:seq lp) #'(first-desc:L lp)]
    746        [#s(hpat:describe _hp desc _t? _r)
    747         #`(quote #,(or (constant-desc #'desc) #'#f))]
    748        [#s(hpat:delimit hp) #'(first-desc:H hp)]
    749        [#s(hpat:commit hp) #'(first-desc:H hp)]
    750        [#s(hpat:ord hp _ _) #'(first-desc:H hp)]
    751        [#s(hpat:post hp) #'(first-desc:H hp)]
    752        [_ #'(first-desc:S hpat)])]))
    753 
    754 (define-syntax (first-desc:L stx)
    755   (syntax-case stx ()
    756     [(fdl lpat)
    757      (syntax-case #'lpat ()
    758        [#s(pat:pair sp lp) #'(first-desc:S sp)]
    759        [_ #'#f])]))
    760 
    761 ;; (disjunct (iattr ...) success (pre:expr ...) (id:id ...)) : expr[Ans]
    762 (define-syntax (disjunct stx)
    763   (syntax-case stx ()
    764     [(disjunct (#s(attr sub-id _ _) ...) success (pre ...) (id ...))
    765      (with-syntax ([(alt-sub-id ...) (generate-temporaries #'(sub-id ...))])
    766        #`(let ([alt-sub-id (attribute sub-id)] ...)
    767            (let ([id #f] ...)
    768              (let ([sub-id alt-sub-id] ...)
    769                (success fail-handler undo-stack pre ... id ...)))))]))
    770 
    771 ;; (parse:A x cx A-pattern pr es k) : expr[Ans]
    772 ;; In k: attrs(A-pattern) are bound.
    773 (define-syntax (parse:A stx)
    774   (syntax-case stx ()
    775     [(parse:A x cx pattern0 pr es k)
    776      (syntax-case #'pattern0 ()
    777        [#s(action:and (action ...))
    778         (for/fold ([k #'k]) ([action (in-list (reverse (syntax->list #'(action ...))))])
    779           #`(parse:A x cx #,action pr es #,k))]
    780        [#s(action:cut)
    781         #'(with ([fail-handler cut-prompt]) k)]
    782        [#s(action:bind a expr)
    783         #'(let-attributes ([a (wrap-user-code expr)]) k)]
    784        [#s(action:fail condition message)
    785         #`(let ([c (wrap-user-code condition)])
    786             (if c
    787                 (let ([pr* (if (syntax? c) (ps-add-stx pr c) pr)]
    788                       [es* (es-add-message message es)])
    789                   (fail (failure* pr* es*)))
    790                 k))]
    791        [#s(action:parse pattern expr)
    792         #`(let* ([y (datum->syntax/with-clause (wrap-user-code expr))]
    793                  [cy y]
    794                  [pr* (ps-add-stx pr y)])
    795             (parse:S y cy pattern pr* es k))]
    796        [#s(action:do (stmt ...))
    797         #'(parameterize ((current-state-writable? #t))
    798             (let ([init-state (current-state)])
    799               (no-shadow stmt) ...
    800               (parameterize ((current-state-writable? #f))
    801                 (with ([undo-stack (maybe-add-state-undo init-state (current-state) undo-stack)])
    802                   (#%expression k)))))]
    803        [#s(action:undo (stmt ...))
    804         #'(with ([undo-stack (cons (lambda () stmt ... (void)) undo-stack)]
    805                  [cut-prompt illegal-cut-error])
    806             k)]
    807        [#s(action:ord pattern group index)
    808         #'(let ([pr* (ps-add pr '#s(ord group index))])
    809             (parse:A x cx pattern pr* es k))]
    810        [#s(action:post pattern)
    811         #'(let ([pr* (ps-add-post pr)])
    812             (parse:A x cx pattern pr* es k))])]))
    813 
    814 (begin-for-syntax
    815  ;; convert-list-pattern : ListPattern id -> SinglePattern
    816  ;; Converts '() datum pattern at end of list to bind (cons stx index)
    817  ;; to rest-var.
    818  (define (convert-list-pattern pattern end-pattern)
    819    (syntax-case pattern ()
    820      [#s(pat:datum ())
    821       end-pattern]
    822      [#s(pat:action action tail)
    823       (with-syntax ([tail (convert-list-pattern #'tail end-pattern)])
    824         #'#s(pat:action action tail))]
    825      [#s(pat:head head tail)
    826       (with-syntax ([tail (convert-list-pattern #'tail end-pattern)])
    827         #'#s(pat:head head tail))]
    828      [#s(pat:dots head tail)
    829       (with-syntax ([tail (convert-list-pattern #'tail end-pattern)])
    830         #'#s(pat:dots head tail))]
    831      [#s(pat:pair head-part tail-part)
    832       (with-syntax ([tail-part (convert-list-pattern #'tail-part end-pattern)])
    833         #'#s(pat:pair head-part tail-part))])))
    834 
    835 ;; (parse:H x cx rest-x rest-cx rest-pr H-pattern pr es k)
    836 ;; In k: rest, rest-pr, attrs(H-pattern) are bound.
    837 (define-syntax (parse:H stx)
    838   (syntax-case stx ()
    839     [(parse:H x cx rest-x rest-cx rest-pr head pr es k)
    840      (syntax-case #'head ()
    841        [#s(hpat:describe pattern description transparent? role)
    842         #`(let ([es* (es-add-thing pr description transparent? role es)]
    843                 [pr* (if 'transparent? pr (ps-add-opaque pr))])
    844             (parse:H x cx rest-x rest-cx rest-pr pattern pr* es*
    845                      (let ([rest-pr (if 'transparent? rest-pr (ps-pop-opaque rest-pr))])
    846                        k)))]
    847        [#s(hpat:var/p name parser argu (nested-a ...) role
    848                       #s(scopts attr-count commit? _delimit? _desc))
    849         (with-syntax ([(av ...) (generate-n-temporaries (syntax-e #'attr-count))]
    850                       [(name-attr ...)
    851                        (if (identifier? #'name)
    852                            #'([#s(attr name 0 #t)
    853                                (stx-list-take x (ps-difference pr rest-pr))])
    854                            #'())])
    855           (if (not (syntax-e #'commit?))
    856               ;; The normal protocol
    857               #`(app-argu parser x cx pr es undo-stack fail-handler cut-prompt role
    858                           (lambda (fh undos rest-x rest-cx rest-pr av ...)
    859                             (let-attributes (name-attr ...)
    860                               (let-attributes* ((nested-a ...) (av ...))
    861                                 (with ([fail-handler fh] [undo-stack undos])
    862                                   k))))
    863                           argu)
    864               ;; The commit protocol
    865               ;; (Avoids putting k in procedure)
    866               #'(let-values ([(fs undos rest-x rest-cx rest-pr av ...)
    867                               (with ([fail-handler
    868                                       (lambda (undos fs)
    869                                         (unwind-to undos undo-stack)
    870                                         (values fs undo-stack #f #f #f (let ([av #f]) av) ...))])
    871                                 (with ([cut-prompt fail-handler])
    872                                   (app-argu parser x cx pr es undo-stack
    873                                             fail-handler cut-prompt role
    874                                             (lambda (fh undos rest-x rest-cx rest-pr av ...)
    875                                               (values #f undos rest-x rest-cx rest-pr av ...))
    876                                             argu)))])
    877                   (if fs
    878                       (fail fs)
    879                       (let-attributes (name-attr ...)
    880                         (let-attributes* ((nested-a ...) (av ...))
    881                           (with ([undo-stack undos])
    882                             k)))))))]
    883        [#s(hpat:reflect obj argu attr-decls name (nested-a ...))
    884         (with-syntax ([(name-attr ...)
    885                        (if (identifier? #'name)
    886                            #'([#s(attr name 0 #t)
    887                                (stx-list-take x (ps-difference pr rest-pr))])
    888                            #'())])
    889           (with-syntax ([arity (arguments->arity (syntax->datum #'argu))])
    890             #'(let ([parser (reflect-parser obj 'arity 'attr-decls #t)])
    891                 (app-argu parser x cx pr es undo-stack fail-handler cut-prompt #f
    892                           (lambda (fh undos rest-x rest-cx rest-pr . result)
    893                             (let-attributes (name-attr ...)
    894                               (let/unpack ((nested-a ...) result)
    895                                  (with ([fail-handler fh] [undo-stack undos])
    896                                    k))))
    897                           argu))))]
    898        [#s(hpat:and head single)
    899         #`(let ([cx0 cx])
    900             (parse:H x cx rest-x rest-cx rest-pr head pr es
    901                      (let ([lst (stx-list-take x (ps-difference pr rest-pr))])
    902                        (parse:S lst cx0 single pr es k))))]
    903        [#s(hpat:or (a ...) (subpattern ...) (subattrs ...))
    904         (with-syntax ([(#s(attr id _ _) ...) #'(a ...)])
    905           #`(let ([success
    906                    (lambda (fh undos rest-x rest-cx rest-pr id ...)
    907                      (let-attributes ([a id] ...)
    908                        (with ([fail-handler fh] [undo-stack undos])
    909                          k)))])
    910               (try (parse:H x cx rest-x rest-cx rest-pr subpattern pr es
    911                             (disjunct subattrs success (rest-x rest-cx rest-pr) (id ...)))
    912                    ...)))]
    913        [#s(hpat:seq pattern)
    914         (with-syntax ([pattern
    915                        (convert-list-pattern
    916                         #'pattern
    917                         #'#s(internal-rest-pattern rest-x rest-cx rest-pr))])
    918           #'(parse:S x cx pattern pr es k))]
    919        [#s(hpat:action action subpattern)
    920         #'(parse:A x cx action pr es (parse:H x cx rest-x rest-cx rest-pr subpattern pr es k))]
    921        [#s(hpat:delimit pattern)
    922         #'(let ([cp0 cut-prompt])
    923             (with ([cut-prompt fail-handler])
    924               (parse:H x cx rest-x rest-cx rest-pr pattern pr es
    925                        (with ([cut-prompt cp0]) k))))]
    926        [#s(hpat:commit pattern)
    927         #`(let ([fh0 fail-handler]
    928                 [cp0 cut-prompt])
    929             (with ([cut-prompt fh0])
    930               (parse:H x cx rest-x rest-cx rest-pr pattern pr es
    931                        (with ([cut-prompt cp0]
    932                               [fail-handler fh0])
    933                              k))))]
    934        [#s(hpat:ord pattern group index)
    935         #`(let ([pr* (ps-add pr '#s(ord group index))])
    936             (parse:H x cx rest-x rest-cx rest-pr pattern pr* es
    937                      (let ([rest-pr (ps-pop-ord rest-pr)]) k)))]
    938        [#s(hpat:post pattern)
    939         #'(let ([pr* (ps-add-post pr)])
    940             (parse:H x cx rest-x rest-cx rest-pr pattern pr* es
    941                      (let ([rest-pr (ps-pop-post rest-pr)]) k)))]
    942        [#s(hpat:peek pattern)
    943         #`(let ([saved-x x] [saved-cx cx] [saved-pr pr])
    944             (parse:H x cx dummy-x dummy-cx dummy-pr pattern pr es
    945                      (let ([rest-x saved-x] [rest-cx saved-cx] [rest-pr saved-pr])
    946                        k)))]
    947        [#s(hpat:peek-not subpattern)
    948         #`(let* ([fh0 fail-handler]
    949                  [pr0 pr]
    950                  [es0 es]
    951                  [fail-to-succeed
    952                   (lambda (undos fs)
    953                     (unwind-to undos undo-stack)
    954                     (let ([rest-x x]
    955                           [rest-cx cx]
    956                           [rest-pr pr])
    957                       k))])
    958             ;; ~not implicitly prompts to be safe,
    959             ;; but ~! not allowed within ~not (unless within ~delimit-cut, etc)
    960             ;; (statically checked!)
    961             (with ([fail-handler fail-to-succeed]
    962                    [cut-prompt fail-to-succeed]) ;; to be safe
    963               (parse:H x cx rest-x rest-cx rest-pr subpattern pr es
    964                        (fh0 undo-stack (failure* pr0 es0)))))]
    965        [_
    966         #'(parse:S x cx
    967                    ;; FIXME: consider proper-list-pattern? (yes is consistent with ~seq)
    968                    #s(pat:pair head #s(internal-rest-pattern rest-x rest-cx rest-pr))
    969                    pr es k)])]))
    970 
    971 ;; (parse:dots x cx EH-pattern S-pattern pr es k) : expr[Ans]
    972 ;; In k: attrs(EH-pattern, S-pattern) are bound.
    973 (define-syntax (parse:dots stx)
    974   (syntax-case stx ()
    975     ;; == Specialized cases
    976     ;; -- (x ... . ())
    977     [(parse:dots x cx (#s(ehpat (attr0) #s(pat:svar name) #f #f))
    978                  #s(pat:datum ()) pr es k)
    979      #'(let-values ([(status result) (predicate-ellipsis-parser x cx pr es void #f #f)])
    980          (case status
    981            ((ok) (let-attributes ([attr0 result]) k))
    982            (else (fail result))))]
    983     ;; -- (x:sc ... . ()) where sc is an integrable stxclass like id or expr
    984     [(parse:dots x cx (#s(ehpat (attr0) #s(pat:integrated _name pred? desc role) #f #f))
    985                  #s(pat:datum ()) pr es k)
    986      #'(let-values ([(status result) (predicate-ellipsis-parser x cx pr es pred? desc role)])
    987          (case status
    988            ((ok) (let-attributes ([attr0 result]) k))
    989            (else (fail result))))]
    990     ;; -- (x:sc ... . ()) where sc is a stxclass with commit
    991     ;; Since head pattern does commit, no need to thread fail-handler, cut-prompt through.
    992     ;; Microbenchmark suggests this isn't a useful specialization
    993     ;; (probably try-or-pair/null-check already does the useful part)
    994     ;; == General case
    995     [(parse:dots x cx (#s(ehpat head-attrs head head-repc check-null?) ...) tail pr es k)
    996      (let ()
    997        (define repcs (wash-list wash #'(head-repc ...)))
    998        (define rep-ids (for/list ([repc (in-list repcs)])
    999                          (and repc (generate-temporary 'rep))))
   1000        (define rel-repcs (filter values repcs))
   1001        (define rel-rep-ids (filter values rep-ids))
   1002        (define rel-heads (for/list ([head (in-list (syntax->list #'(head ...)))]
   1003                                     [repc (in-list repcs)]
   1004                                     #:when repc)
   1005                            head))
   1006        (define aattrs
   1007          (for/list ([head-attrs (in-list (syntax->list #'(head-attrs ...)))]
   1008                     [repc (in-list repcs)]
   1009                     #:when #t
   1010                     [a (in-list (wash-iattrs head-attrs))])
   1011            (cons a repc)))
   1012        (define attrs (map car aattrs))
   1013        (define attr-repcs (map cdr aattrs))
   1014        (define ids (map attr-name attrs))
   1015        (define tail-pattern-is-null? (equal? (syntax->datum #'tail) '#s(pat:datum ())))
   1016        (with-syntax ([(id ...) ids]
   1017                      [(alt-id ...) (generate-temporaries ids)]
   1018                      [reps rel-rep-ids]
   1019                      [(head-rep ...) rep-ids]
   1020                      [(rel-rep ...) rel-rep-ids]
   1021                      [(rel-repc ...) rel-repcs]
   1022                      [(rel-head ...) rel-heads]
   1023                      [(a ...) attrs]
   1024                      [(attr-repc ...) attr-repcs]
   1025                      [do-pair/null?
   1026                       ;; FIXME: do pair/null check only if no nullable head patterns
   1027                       ;; (and tail-pattern-is-null? (andmap not (syntax->datum #'(nullable? ...))))
   1028                       tail-pattern-is-null?])
   1029          (define/with-syntax alt-map #'((id . alt-id) ...))
   1030          (define/with-syntax loop-k
   1031            #'(dots-loop dx* dcx* loop-pr* undo-stack fail-handler rel-rep ... alt-id ...))
   1032          #`(let ()
   1033              ;; dots-loop : stx progress rel-rep ... alt-id ... -> Ans
   1034              (define (dots-loop dx dcx loop-pr undos fh rel-rep ... alt-id ...)
   1035                (with ([fail-handler fh] [undo-stack undos])
   1036                  (try-or-pair/null-check do-pair/null? dx dcx loop-pr es
   1037                    (try (parse:EH dx dcx loop-pr head-attrs check-null? head-repc dx* dcx* loop-pr* 
   1038                                   alt-map head-rep head es loop-k)
   1039                         ...)
   1040                    (cond [(< rel-rep (rep:min-number rel-repc))
   1041                           (let ([es (expectation-of-reps/too-few es rel-rep rel-repc rel-head)])
   1042                             (fail (failure* loop-pr es)))]
   1043                          ...
   1044                          [else
   1045                           (let-attributes ([a (rep:finalize a attr-repc alt-id)] ...)
   1046                             (parse:S dx dcx tail loop-pr es k))]))))
   1047              (let ([rel-rep 0] ...
   1048                    [alt-id (rep:initial-value attr-repc)] ...)
   1049                (dots-loop x cx pr undo-stack fail-handler rel-rep ... alt-id ...)))))]))
   1050 
   1051 ;; (try-or-pair/null-check bool x cx es pr pair-alt maybe-null-alt)
   1052 (define-syntax try-or-pair/null-check
   1053   (syntax-rules ()
   1054     [(topc #t x cx pr es pair-alt null-alt)
   1055      (cond [(stx-pair? x) pair-alt]
   1056            [(stx-null? x) null-alt]
   1057            [else (fail (failure* pr es))])]
   1058     [(topc _ x cx pr es alt1 alt2)
   1059      (try alt1 alt2)]))
   1060 
   1061 ;; (parse:EH x cx pr repc x* cx* pr* alts rep H-pattern es k) : expr[Ans]
   1062 ;; In k: x*, cx*, pr*, alts`attrs(H-pattern) are bound and rep is shadowed.
   1063 (define-syntax (parse:EH stx)
   1064   (syntax-case stx ()
   1065     [(parse:EH x cx pr attrs check-null? repc x* cx* pr* alts rep head es k)
   1066      (let ()
   1067        (define/with-syntax k*
   1068          (let* ([main-attrs (wash-iattrs #'attrs)]
   1069                 [ids (map attr-name main-attrs)]
   1070                 [alt-ids
   1071                  (let ([table (make-bound-id-table)])
   1072                    (for ([entry (in-list (syntax->list #'alts))])
   1073                      (let ([entry (syntax-e entry)])
   1074                        (bound-id-table-set! table (car entry) (cdr entry))))
   1075                    (for/list ([id (in-list ids)]) (bound-id-table-ref table id)))])
   1076            (with-syntax ([(id ...) ids]
   1077                          [(alt-id ...) alt-ids])
   1078              #`(let ([alt-id (rep:combine repc (attribute id) alt-id)] ...)
   1079                  #,(if (syntax->datum #'check-null?)
   1080                        #'(if (zero? (ps-difference pr pr*)) (error/null-eh-match) k)
   1081                        #'k)))))
   1082        (syntax-case #'repc ()
   1083          [#f #`(parse:H x cx x* cx* pr* head pr es k*)]
   1084          [_  #`(parse:H x cx x* cx* pr* head pr es
   1085                         (if (< rep (rep:max-number repc))
   1086                             (let ([rep (add1 rep)]) k*)
   1087                             (let ([es* (expectation-of-reps/too-many es rep repc)])
   1088                               (fail (failure* pr* es*)))))]))]))
   1089 
   1090 ;; (rep:initial-value RepConstraint) : expr
   1091 (define-syntax (rep:initial-value stx)
   1092   (syntax-case stx ()
   1093     [(_ #s(rep:once _ _ _)) #'#f]
   1094     [(_ #s(rep:optional _ _ _)) #'#f]
   1095     [(_ _) #'null]))
   1096 
   1097 ;; (rep:finalize RepConstraint expr) : expr
   1098 (define-syntax (rep:finalize stx)
   1099   (syntax-case stx ()
   1100     [(_ a #s(rep:optional _ _ defaults) v)
   1101      (with-syntax ([#s(attr name _ _) #'a]
   1102                    [(#s(action:bind da de) ...) #'defaults])
   1103        (let ([default
   1104               (for/or ([da (in-list (syntax->list #'(da ...)))]
   1105                        [de (in-list (syntax->list #'(de ...)))])
   1106                 (with-syntax ([#s(attr dname _ _) da])
   1107                   (and (bound-identifier=? #'name #'dname) de)))])
   1108          (if default
   1109              #`(or v #,default)
   1110              #'v)))]
   1111     [(_ a #s(rep:once _ _ _) v) #'v]
   1112     [(_ a _ v) #'(reverse v)]))
   1113 
   1114 ;; (rep:min-number RepConstraint) : expr
   1115 (define-syntax (rep:min-number stx)
   1116   (syntax-case stx ()
   1117     [(_ #s(rep:once _ _ _)) #'1]
   1118     [(_ #s(rep:optional _ _ _)) #'0]
   1119     [(_ #s(rep:bounds min max _ _ _)) #'min]))
   1120 
   1121 ;; (rep:max-number RepConstraint) : expr
   1122 (define-syntax (rep:max-number stx)
   1123   (syntax-case stx ()
   1124     [(_ #s(rep:once _ _ _)) #'1]
   1125     [(_ #s(rep:optional _ _ _)) #'1]
   1126     [(_ #s(rep:bounds min max _ _ _)) #'max]))
   1127 
   1128 ;; (rep:combine RepConstraint expr expr) : expr
   1129 (define-syntax (rep:combine stx)
   1130   (syntax-case stx ()
   1131     [(_ #s(rep:once _ _ _) a b) #'a]
   1132     [(_ #s(rep:optional _ _ _) a b) #'a]
   1133     [(_ _ a b) #'(cons a b)]))
   1134 
   1135 ;; ----
   1136 
   1137 (define-syntax expectation-of-reps/too-few
   1138   (syntax-rules ()
   1139     [(_ es rep #s(rep:once name too-few-msg too-many-msg) hpat)
   1140      (cond [(or too-few-msg (name->too-few/once name))
   1141             => (lambda (msg) (es-add-message msg es))]
   1142            [(first-desc:H hpat) => (lambda (fd) (es-add-proper-pair fd es))]
   1143            [else es])]
   1144     [(_ es rep #s(rep:optional name too-many-msg _) hpat)
   1145      (error 'syntax-parse "INTERNAL ERROR: impossible (too-few)")]
   1146     [(_ es rep #s(rep:bounds min max name too-few-msg too-many-msg) hpat)
   1147      (cond [(or too-few-msg (name->too-few name))
   1148             => (lambda (msg) (es-add-message msg es))]
   1149            [(first-desc:H hpat) => (lambda (fd) (es-add-proper-pair fd es))]
   1150            [else es])]))
   1151 
   1152 (define-syntax expectation-of-reps/too-many
   1153   (syntax-rules ()
   1154     [(_ es rep #s(rep:once name too-few-msg too-many-msg))
   1155      (es-add-message (or too-many-msg (name->too-many name)) es)]
   1156     [(_ es rep #s(rep:optional name too-many-msg _))
   1157      (es-add-message (or too-many-msg (name->too-many name)) es)]
   1158     [(_ es rep #s(rep:bounds min max name too-few-msg too-many-msg))
   1159      (es-add-message (or too-many-msg (name->too-many name)) es)]))
   1160 
   1161 ;; ====
   1162 
   1163 (define-syntax (define-eh-alternative-set stx)
   1164   (define (parse-alt x)
   1165     (syntax-case x (pattern)
   1166       [(pattern alt)
   1167        #'alt]
   1168       [else
   1169        (wrong-syntax x "expected eh-alternative-set alternative")]))
   1170   (parameterize ((current-syntax-context stx))
   1171     (syntax-case stx ()
   1172       [(_ name a ...)
   1173        (unless (identifier? #'name)
   1174          (wrong-syntax #'name "expected identifier"))
   1175        (let* ([alts (map parse-alt (syntax->list #'(a ...)))]
   1176               [decls (new-declenv null #:conventions null)]
   1177               [ehpat+hstx-list
   1178                (apply append
   1179                       (for/list ([alt (in-list alts)])
   1180                         (parse*-ellipsis-head-pattern alt decls #t #:context stx)))]
   1181               [eh-alt+defs-list
   1182                (for/list ([ehpat+hstx (in-list ehpat+hstx-list)])
   1183                  (let ([ehpat (car ehpat+hstx)]
   1184                        [hstx (cadr ehpat+hstx)])
   1185                    (cond [(syntax? hstx)
   1186                           (with-syntax ([(parser) (generate-temporaries '(eh-alt-parser))])
   1187                             (let ([attrs (iattrs->sattrs (pattern-attrs (ehpat-head ehpat)))])
   1188                               (list (eh-alternative (ehpat-repc ehpat) attrs #'parser)
   1189                                     (list #`(define parser
   1190                                               (parser/rhs parser () #,attrs
   1191                                                           [#:description #f (pattern #,hstx)]
   1192                                                           #t
   1193                                                           #,stx))))))]
   1194                          [(eh-alternative? hstx)
   1195                           (list hstx null)]
   1196                          [else
   1197                           (error 'define-eh-alternative-set "internal error: unexpected ~e"
   1198                                  hstx)])))]
   1199               [eh-alts (map car eh-alt+defs-list)]
   1200               [defs (apply append (map cadr eh-alt+defs-list))])
   1201          (with-syntax ([(def ...) defs]
   1202                        [(alt-expr ...)
   1203                         (for/list ([alt (in-list eh-alts)])
   1204                           (with-syntax ([repc-expr
   1205                                          ;; repc structs are prefab; recreate using prefab
   1206                                          ;; quasiquote exprs to avoid moving constructors
   1207                                          ;; to residual module
   1208                                          (syntax-case (eh-alternative-repc alt) ()
   1209                                            [#f
   1210                                             #''#f]
   1211                                            [#s(rep:once n u o)
   1212                                             #'`#s(rep:once ,(quote-syntax n)
   1213                                                            ,(quote-syntax u)
   1214                                                            ,(quote-syntax o))]
   1215                                            [#s(rep:optional n o d)
   1216                                             #'`#s(rep:optional ,(quote-syntax n)
   1217                                                                ,(quote-syntax o)
   1218                                                                ,(quote-syntax d))]
   1219                                            [#s(rep:bounds min max n u o)
   1220                                             #'`#s(rep:bounds ,(quote min)
   1221                                                              ,(quote max)
   1222                                                              ,(quote-syntax n)
   1223                                                              ,(quote-syntax u)
   1224                                                              ,(quote-syntax o))])]
   1225                                         [attrs-expr
   1226                                          #`(quote #,(eh-alternative-attrs alt))]
   1227                                         [parser-expr
   1228                                          #`(quote-syntax #,(eh-alternative-parser alt))])
   1229                             #'(eh-alternative repc-expr attrs-expr parser-expr)))])
   1230            #'(begin def ...
   1231                     (define-syntax name
   1232                       (eh-alternative-set (list alt-expr ...))))))])))