summaryrefslogtreecommitdiff
path: root/ebus-racket/3rdparty/bzlib/parseq/reader.ss
blob: 50a5f9d3d8c00cae62e7552f86a078a3565714cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#lang scheme/base
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PARSEQ.PLT 
;; A Parser Combinator library.
;; 
;; Bonzai Lab, LLC.  All rights reserved.
;; 
;; Licensed under LGPL.
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; reader.ss - move make-reader & Reader/c here 
;; yc 1/18/2010 - first version 
;; yc 1/21/2010 - make-reader to take on additional default params 
(require "depend.ss"
         "input.ss"
         "primitive.ss"
         "combinator.ss"
         (prefix-in c: scheme/contract)
         )
;; use this to create a reader that will read the bytes if the parse succeeds.
(define (make-reader parser #:sof? (sof? #t) #:eof? (eof? #t) #:default (default #f)) 
  (lambda (in #:sof? (sof? sof?) #:eof? (eof? eof?) #:default (default default)) 
    (let-values (((v in)
                  ((seq (if sof? SOF (return #t))
                        v <- parser 
                        (if eof? EOF (return #t)) 
                        (return v)) (make-input in))))
      (unless (failed? v) (read-bytes* in))
      (if (failed? v)
          default 
          v))))

(define Reader/c (c:->* (Input/c)
                      (#:sof? boolean? #:eof? boolean? #:default c:any/c) 
                      c:any))
(provide Reader/c)
(c:provide/contract
 (make-reader (c:->* (Parser/c)
                   (#:sof? boolean? #:eof? boolean? #:default c:any/c)
                   Reader/c))
 )