You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/bf-demo/test-tokenizer.rkt

28 lines
772 B
Racket

#lang br
(require parser-tools/lex brag/support)
9 years ago
(define+provide (tokenize ip)
9 years ago
(define get-token
9 years ago
(lexer
[(char-set "><-.,+[]") lexeme]
9 years ago
;; todo: try adding support for line comments
#;[(:: "#" (:* (complement "\n")) "\n") (token 'comment #:skip? #t)]
9 years ago
[whitespace (token 'white #:skip? #t)]
9 years ago
;; treat other characters as comments
[(char-range #\nul #\~) (token 'ascii #:skip? #t)]
9 years ago
[(eof) eof]))
(define (next-token) (get-token ip))
next-token)
(module+ test
(require rackunit)
(define (test-tokenize str)
(define ip (open-input-string str))
(define token-producer (tokenize ip))
(for/list ([token (in-producer token-producer eof)])
token))
9 years ago
9 years ago
(check-equal? (test-tokenize "+") (list "+")))