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/jsonic-2/tokenizer.rkt

62 lines
1.8 KiB
Racket

8 years ago
#lang br/quicklang
(require brag/support)
8 years ago
8 years ago
(module+ test
(require rackunit))
8 years ago
(define (token? x)
(or (eof-object? x) (string? x) (token-struct? x)))
8 years ago
(module+ test
(check-true (token? eof))
(check-true (token? "a string"))
(check-true (token? (token 'A-TOKEN-STRUCT "hi")))
(check-false (token? 42)))
8 years ago
(define/contract (tokenize port)
(input-port? . -> . (-> token?))
8 years ago
(port-count-lines! port)
8 years ago
(define/contract (next-token)
(-> token?)
8 years ago
(define our-lexer
(lexer
[(eof) eof]
[(from/to "//" "\n") (next-token)]
[(from/to "@$" "$@")
8 years ago
(token 'SEXP-TOK (trim-ends "@$" lexeme "$@")
8 years ago
#:position (+ (pos lexeme-start) 2)
8 years ago
#:line (line lexeme-start)
#:column (+ (col lexeme-start) 2)
#:span (- (pos lexeme-end)
(pos lexeme-start) 4))]
8 years ago
[any-char (token 'CHAR-TOK lexeme
8 years ago
#:position (pos lexeme-start)
8 years ago
#:line (line lexeme-start)
8 years ago
#:column (col lexeme-start)
#:span (- (pos lexeme-end)
(pos lexeme-start)))]))
8 years ago
(our-lexer port))
8 years ago
next-token)
8 years ago
(provide tokenize)
(module+ test
(check-equal? (apply-tokenizer tokenize "// comment\n") empty)
(check-equal?
(apply-tokenizer tokenize "@$ (+ 6 7) $@")
8 years ago
(list (token 'SEXP-TOK " (+ 6 7) "
#:position 3
8 years ago
#:line 1
#:column 2
8 years ago
#:span 9)))
8 years ago
(check-equal?
(apply-tokenizer tokenize "hi")
8 years ago
(list (token 'CHAR-TOK "h"
8 years ago
#:position 1
8 years ago
#:line 1
#:column 0
#:span 1)
(token 'CHAR-TOK "i"
8 years ago
#:position 2
8 years ago
#:line 1
#:column 1
#:span 1))))