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.
27 lines
1.1 KiB
Racket
27 lines
1.1 KiB
Racket
9 years ago
|
#lang br
|
||
|
(require parser-tools/lex parser-tools/lex-sre
|
||
9 years ago
|
brag/support
|
||
9 years ago
|
racket/string)
|
||
|
|
||
|
(provide tokenize)
|
||
|
(define (tokenize input-port)
|
||
|
(define (next-token)
|
||
|
(define get-token
|
||
|
(lexer
|
||
|
[(eof) eof]
|
||
|
[(union
|
||
|
(seq "/*" (complement (seq any-string "*/" any-string)) "*/")
|
||
|
(seq "//" (repetition 1 +inf.0 (char-complement #\newline)) #\newline))
|
||
|
(token 'COMMENT lexeme #:skip? #t)]
|
||
|
[(union #\tab #\space #\newline) (get-token input-port)]
|
||
|
[(repetition 1 +inf.0 (union upper-case (char-set "="))) lexeme]
|
||
9 years ago
|
[(seq "\"" (complement (seq any-string "\"" any-string)) "\"") (token 'STRING (string-trim lexeme "\""))]
|
||
9 years ago
|
[(seq "---"
|
||
|
(repetition 1 +inf.0 (union alphabetic numeric punctuation))
|
||
9 years ago
|
"---") (token 'DASHED-NAME (string->symbol (string-trim lexeme "-" #:repeat? #t)))]
|
||
|
[(repetition 1 +inf.0 (union alphabetic numeric (char-set "-!?.#'")))
|
||
9 years ago
|
(token 'ID (read (open-input-string lexeme)))]
|
||
9 years ago
|
[any-char lexeme]))
|
||
9 years ago
|
(get-token input-port))
|
||
|
next-token)
|