www

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

test-rest.rkt (2515B)


      1 #lang racket
      2 
      3 (require extensible-parser-specifications
      4          extensible-parser-specifications/private/no-order
      5          racket/require
      6          syntax/parse
      7          (subtract-in syntax/stx phc-toolkit/untyped)
      8          rackunit
      9          racket/format
     10          phc-toolkit/untyped
     11          (for-syntax syntax/parse
     12                      syntax/stx
     13                      racket/format))
     14 
     15 (check-equal?
     16  (syntax-parse #'(1 "ab" . #:kw)
     17    [(~no-order {~or {~lift-rest {~and k #:kw}}
     18                     {~once n:nat}}
     19                {~once s:str})
     20     (syntax->datum #'(k n s))])
     21  '(#:kw 1 "ab"))
     22 
     23 
     24 (check-equal?
     25  (syntax-parse #'(1 "ab" . #:kw)
     26    [(~no-order {~lift-rest {~and k #:kw}}
     27                {~once n:nat}
     28                {~once s:str})
     29     (syntax->datum #'(k n s))])
     30  '(#:kw 1 "ab"))
     31 
     32 (check-equal?
     33  (syntax-parse #'(1 "ab" . #:kw)
     34    [(~no-order {~once {~and n:nat
     35                             {~lift-rest {~and k #:kw}}}}
     36                {~once s:str})
     37     (syntax->datum #'(k n s))])
     38  '(#:kw 1 "ab"))
     39 
     40 (check-equal?
     41  (syntax-parse #'(1 "ab" . #:kw)
     42    [(~no-order {~once n:nat}
     43                {~lift-rest {~and k #:kw}}
     44                {~once s:str})
     45     (syntax->datum #'(k n s))]
     46    [_ #f])
     47  '(#:kw 1 "ab"))
     48 
     49 (test-begin
     50  "Exactly the same as above, but with the post-fail"
     51  (check-false
     52   (syntax-parse #'(1 "ab" . #:kw)
     53     [(~no-order {~once n:nat}
     54                 {~lift-rest {~and k #:kw
     55                                   {~post-fail "e" #:when (= (syntax-e #'n) 1)}}}
     56                 {~once s:str})
     57      (syntax->datum #'(k n s))]
     58     [_ #f])))
     59 
     60 (test-begin
     61  "Exactly the same as above, but with a different value (2 instead of 1)"
     62  (check-equal?
     63   (syntax-parse #'(2 "ab" . #:kw)
     64     [(~no-order {~once n:nat}
     65                 {~lift-rest {~and k #:kw
     66                                   {~post-fail "e" #:when (= (syntax-e #'n) 1)}}}
     67                 {~once s:str})
     68      (syntax->datum #'(k n s))]
     69     [_ #f])
     70   '(#:kw 2 "ab")))
     71 
     72 (define p
     73   (syntax-parser
     74     [(~no-order {~and {~literal x}
     75                       {~lift-rest rn:nat}
     76                       {~lift-rest ri:id}}
     77                 {~and {~literal y}
     78                       {~lift-rest rs:str}
     79                       {~lift-rest rj:id}})
     80      'match]))
     81 
     82 (check-equal? (p #'(x . 1)) 'match)
     83 (check-equal? (p #'(x . z)) 'match)
     84 (check-equal? (p #'(y . "a")) 'match)
     85 (check-equal? (p #'(y . z)) 'match)
     86 (check-equal? (p #'(x y . 1)) 'match)
     87 (check-exn #px"more than one of the lifted rest patterns matched"
     88            (λ () (p #'(x y . z))))