(dlang/whitespace in))
; Comment
- ((char=? (charobj-char ch) #\#)
+ ((char=? (chobj-char ch) #\#)
(dlang/comment in))
; Number
((or
- (and (char=? (charobj-char ch) #\-) (dlang/integer? (buf-lookahead! in 2)))
- (char-numeric? (charobj-char ch)))
+ (and (char=? (chobj-char ch) #\-) (dlang/integer? (buf-lookahead! in 2)))
+ (char-numeric? (chobj-char ch)))
(dlang/number in))
; Character
- ((char=? (charobj-char ch) #\') (dlang/character in))
+ ((char=? (chobj-char ch) #\') (dlang/character in))
; String
- ((char=? (charobj-char ch) #\") (dlang/string in))
+ ((char=? (chobj-char ch) #\") (dlang/string in))
; Symbol
- ((char=? (charobj-char ch) #\$) (dlang/symbol in))
+ ((char=? (chobj-char ch) #\$) (dlang/symbol in))
; Punctuation and Parens
- ((char=? (charobj-char ch) #\()
- (token 'lpar (string (charobj-char (buf-consume! in))) location))
- ((char=? (charobj-char ch) #\))
- (token 'rpar (string (charobj-char (buf-consume! in))) location))
- ((char=? (charobj-char ch) #\,)
- (token 'comma (string (charobj-char (buf-consume! in))) location))
- ((char=? (charobj-char ch) #\;)
- (token 'term (string (charobj-char (buf-consume! in))) location))
+ ((char=? (chobj-char ch) #\()
+ (token 'lpar (string (chobj-char (buf-consume! in))) location))
+ ((char=? (chobj-char ch) #\))
+ (token 'rpar (string (chobj-char (buf-consume! in))) location))
+ ((char=? (chobj-char ch) #\,)
+ (token 'comma (string (chobj-char (buf-consume! in))) location))
+ ((char=? (chobj-char ch) #\;)
+ (token 'term (string (chobj-char (buf-consume! in))) location))
; Id
(else
(define (dlang/whitespace? in)
(and (not (eof-object? (buf-lookahead! in 1)))
- (char-whitespace? (charobj-char (buf-lookahead! in 1)))))
+ (char-whitespace? (chobj-char (buf-lookahead! in 1)))))
(define (dlang/comment in)
(char-match in #\#)
(define (dlang/comment? in)
(and (not (eof-object? (buf-lookahead! in 1)))
- (not (char=? (charobj-char (buf-lookahead! in 1)) #\newline))))
+ (not (char=? (chobj-char (buf-lookahead! in 1)) #\newline))))
(define (dlang/number in)
(define location (buf-posdata in))
(token 'number
(string-append
(if (and (not (eof-object? (buf-lookahead! in 1)))
- (char=? #\- (charobj-char (buf-lookahead! in 1))))
- (string (charobj-char (buf-consume! in))) "")
+ (char=? #\- (chobj-char (buf-lookahead! in 1))))
+ (string (chobj-char (buf-consume! in))) "")
(dlang/integer in)
(if (and (not (eof-object? (buf-lookahead! in 1)))
- (char=? (charobj-char (buf-lookahead! in 1)) #\.))
+ (char=? (chobj-char (buf-lookahead! in 1)) #\.))
(dlang/decimal in) "")
(if (and (not (eof-object? (buf-lookahead! in 1)))
- (or (char=? (charobj-char (buf-lookahead! in 1)) #\e)
- (char=? (charobj-char (buf-lookahead! in 1)) #\E)))
+ (or (char=? (chobj-char (buf-lookahead! in 1)) #\e)
+ (char=? (chobj-char (buf-lookahead! in 1)) #\E)))
(dlang/exponent in) ""))
location))
(define (dlang/integer? in)
(and (not (eof-object? (buf-lookahead! in 1)))
- (char-numeric? (charobj-char (buf-lookahead! in 1)))))
+ (char-numeric? (chobj-char (buf-lookahead! in 1)))))
(define (dlang/decimal in)
(string-append
(string-append
(string
(if (and (not (eof-object? (buf-lookahead! in 1)))
- (char=? #\e (charobj-char (buf-lookahead! in 1))))
+ (char=? #\e (chobj-char (buf-lookahead! in 1))))
(char-match in #\e)
(char-match in #\E)))
(if (and (not (eof-object? (buf-lookahead! in 1)))
- (char=? #\- (charobj-char (buf-lookahead! in 1))))
- (string (charobj-char (buf-consume! in))) "")
+ (char=? #\- (chobj-char (buf-lookahead! in 1))))
+ (string (chobj-char (buf-consume! in))) "")
(dlang/integer in)))
(define (dlang/character in)
(string (char-match in #\'))
(if (eof-object? (buf-lookahead! in 1))
(abort "Unexpected EOF while parsing character literal")
- (string (charobj-char (buf-consume! in))))
+ (string (chobj-char (buf-consume! in))))
(string (char-match in #\')))
location))
(define (dlang/string-char? in)
(define ch (buf-lookahead! in 1))
(and (not (eof-object? ch))
- (not (char=? #\newline (charobj-char ch)))
- (not (char=? #\" (charobj-char ch)))))
+ (not (char=? #\newline (chobj-char ch)))
+ (not (char=? #\" (chobj-char ch)))))
(define (dlang/symbol in)
(define location (buf-posdata in))
(define (dlang/id-char? in)
(define ch (buf-lookahead! in 1))
(and (not (eof-object? ch))
- (not (char-whitespace? (charobj-char ch)))
- (case (charobj-char ch)
+ (not (char-whitespace? (chobj-char ch)))
+ (case (chobj-char ch)
((#\( #\) #\; #\, #\' #\" #\$ #\#) #f)
(else #t))))
(syntree-children=? (syntree-children tr1) (syntree-children tr2))))
(define (syntree-children=? ch1 ch2)
- (and
- (or
- (and (null? ch1) (null? ch2))
- (and (not (null? ch1)) (not (null? ch2))))
- (if (null? ch1)
- #t ; If we got here and one is null then BOTH must be, hence equal
- (and
- (syntree=? (car ch1) (car ch2))
- (syntree-children=? (cdr ch1) (cdr ch2))))))
-
-(define-record charobj char pos)
-(define charobj make-charobj)
-
-(define (charobj=? cho1 cho2)
- (and (char=? (charobj-char cho1) (charobj-char cho2))
- (posdata=? (charobj-pos cho1) (charobj-pos cho2))))
+ (and (or (and (null? ch1) (null? ch2))
+ (and (not (null? ch1)) (not (null? ch2))))
+ (if (null? ch1)
+ #t ; If we got here and one is null then BOTH must be, hence equal
+ (and
+ (syntree=? (car ch1) (car ch2))
+ (syntree-children=? (cdr ch1) (cdr ch2))))))
+
+(define-record chobj char pos)
+(define chobj make-chobj)
+
+(define (chobj=? cho1 cho2)
+ (and (char=? (chobj-char cho1) (chobj-char cho2))
+ (posdata=? (chobj-pos cho1) (chobj-pos cho2))))
(define (charport-read chprt)
(define ch (read-char (charport-port chprt)))
(charport-column-set! chprt 1))
(else
(charport-column-set! chprt (+ 1 (charport-column chprt)))))
- (if (eof-object? ch) ch (charobj ch (charport-posdata chprt))))
+ (if (eof-object? ch) ch (chobj ch (charport-posdata chprt))))
(define (charport-posdata chprt)
(posdata
(if (eof-object? actual)
(abort
(string-append "Expected '" (string expect) "', received EOF instead"))
- (if (equal? expect (charobj-char actual))
+ (if (equal? expect (chobj-char actual))
(buf-consume! buf)
(abort
(string-append
"Expected '" (string expect)
- "', received '" (string (charobj-char actual)) "' instead"))))
- (charobj-char actual))
+ "', received '" (string (chobj-char actual)) "' instead"))))
+ (chobj-char actual))
(define (token-match buf expect)
(define actual (buf-lookahead! buf 1))
(not (null? result)))
(define (collect-char in predfn)
- (list->string (map charobj-char (collect in predfn buf-consume!))))
+ (list->string (map chobj-char (collect in predfn buf-consume!))))
(define (consume-all in predfn)
(when (predfn in)
(call-with-input-string "a"
(lambda (input)
(define port (charport input))
- (define chobj (charport-read port))
- (and (charobj=? (charobj #\a (posdata "(string)" 1 2)) chobj)
+ (define result (charport-read port))
+ (and (chobj=? (chobj #\a (posdata "(string)" 1 2)) result)
(equal? 1 (charport-line port))
(equal? 2 (charport-column port))))))
(call-with-input-string "\n"
(lambda (input)
(define port (charport input))
- (define chobj (charport-read port))
- (and (charobj=? (charobj #\newline (posdata "(string)" 2 1)) chobj)
+ (define result (charport-read port))
+ (and (chobj=? (chobj #\newline (posdata "(string)" 2 1)) result)
(equal? 2 (charport-line port))
(equal? 1 (charport-column port))))))
(lambda (input)
(define buffer (buf (charport input) charport-read))
(consume-all buffer dlang/integer?)
- (charobj=? (charobj #\a (posdata "(string)" 1 2))
+ (chobj=? (chobj #\a (posdata "(string)" 1 2))
(buf-lookahead! buffer 1)))))
(def-test "should consume an item at a time until predicate returns false"
(lambda (input)
(define buffer (buf (charport input) charport-read))
(consume-all buffer dlang/integer?)
- (charobj=? (charobj #\a (posdata "(string)" 1 5))
+ (chobj=? (chobj #\a (posdata "(string)" 1 5))
(buf-lookahead! buffer 1)))))
; collect
(define buffer (buf (charport input) charport-read))
(define result (collect buffer dlang/integer? buf-consume!))
(equal? '(#\1 #\2 #\3)
- (map charobj-char result)))))
+ (map chobj-char result)))))