Permalink
Browse files

Fixes, and attempts to make line counting work.

  • Loading branch information...
1 parent fd9f0f0 commit a7800921d496da1607a51111fa58b2011e0b7e1e @samth committed Sep 4, 2012
Showing with 29 additions and 18 deletions.
  1. +29 −18 main.rkt
View
@@ -15,45 +15,56 @@
(lambda (n in . args)
(apply p n (filter-port in) args)))
- (define <-byte 62)
+ (define >-byte (bytes-ref #">" 0))
+ (define nl-byte (bytes-ref #"\n" 0))
(define (filter-port in)
- (make-input-port
- (cons 'semilit (object-name in))
- (let ([remain #f] [remain-start 0])
+ (let ([remain #f] [remain-start 0] [lines 0] [pos 0])
+ (make-input-port
+ (cons 'semilit (object-name in))
(lambda (bs)
(define len (bytes-length bs))
(let outer ()
- (cond [(and remain (<= (- (bytes-length remain) remain-start) len))
+ (cond [(and remain (< (- (bytes-length remain) remain-start) len))
(bytes-copy! bs 0 remain remain-start)
- (begin0
- (- (bytes-length remain) remain-start)
- (set! remain-start 0)
- (set! remain #f))]
+ (bytes-set! bs (sub1 len) nl-byte)
+ (set! pos (+ pos (add1 (- (bytes-length remain) remain-start))))
+ (begin0 (add1 (- (bytes-length remain) remain-start))
+ (set! remain-start 0)
+ (set! remain #f))]
[remain
(bytes-copy! bs 0 remain remain-start (+ remain-start len))
- (set! remain-start (+ remain-start len))
+ (set! remain-start (+ remain-start len))
+ (set! pos (+ pos len))
len]
[else
(let inner ()
(define line (read-bytes-line in))
+ (set! lines (add1 lines))
(cond
[(eof-object? line) line]
[(zero? (bytes-length line))
- (bytes-copy! bs 0 (string->bytes/utf-8 "\n"))
+ (bytes-set! bs 0 nl-byte)
+ (set! pos (add1 pos))
1]
- [(equal? (subbytes line 0 1) (string->bytes/utf-8 ">"))
+ [(equal? (bytes-ref line 0) >-byte)
(set! remain line) (set! remain-start 1)
+ (set! pos (add1 pos))
(outer)]
- [else (inner)]))]))))
- #f ;; peek
- (lambda () (close-input-port in))))
+ [else (set! pos (+ pos 1 (bytes-length line))) (inner)]))])))
+ #f ;; peek
+ (λ () (close-input-port in))
+ #f
+ #f
+ (λ () (values lines remain-start pos))
+ (λ () (port-count-lines! in))
+ 7)))
(define-values (sl-read sl-read-syntax sl-get-info)
(make-meta-reader
'semilit
"language path"
- (lambda (bstr)
+ (λ (bstr)
(let* ([str (bytes->string/utf-8 bstr)]
[sym (string->symbol str)])
(and (module-path? sym)
@@ -64,7 +75,7 @@
(string->symbol (string-append str "/lang/reader"))))))
wrap-read
wrap-read-syntax
- (lambda (proc)
- (lambda (key defval)
+ (λ (proc)
+ (λ (key defval)
(case key
[else (if proc (proc key defval) defval)]))))))

0 comments on commit a780092

Please sign in to comment.