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/br/demo/basic/tokenizer.rkt

37 lines
1.6 KiB
Racket

#lang br
(require parser-tools/lex
(prefix-in : parser-tools/lex-sre)
ragg/support
racket/string)
(provide tokenize)
(define (tokenize input-port)
(define (next-token)
(define get-token
(lexer
8 years ago
[(:seq "REM" (repetition 1 +inf.0 (char-complement "\n")))
(token 'REM-COMMENT (format-datum '(comment "~v") lexeme))]
[(repetition 1 +inf.0 "\n") (token 'CR "cr")]
[(union "PRINT" "FOR" "TO" "STEP" "IF" "THEN" "GOTO"
8 years ago
"INPUT" "LET" "NEXT" "GOSUB" "RETURN"
"CLEAR" "LIST" "RUN" "END") (string->symbol lexeme)]
8 years ago
;; this only matches integers
8 years ago
[(repetition 1 +inf.0 numeric) (token 'INTEGER (string->number lexeme))]
[(repetition 1 +inf.0 (union "." numeric)) (token 'REAL (string->number lexeme))]
8 years ago
;; things that get thrown out: pass through as strings,
;; because they can be matched literally in macros.
;; things that become identifiers: pass through as symbols,
;; so they can get bound by the expander.
[(union "," ":") (token 'SEPARATOR lexeme #:skip? #t)]
[(union ";" "=" "(" ")") lexeme]
[(union "+" "-" "*" "/"
"<=" ">=" "<>" "><" "<" ">" "=" ) (string->symbol lexeme)]
8 years ago
[(:seq (repetition 1 +inf.0 upper-case) (:? "$")) (token 'ID (string->symbol lexeme))]
8 years ago
[upper-case (token 'UPPERCASE (string->symbol lexeme))]
[whitespace (token 'WHITESPACE lexeme #:skip? #t)]
[(:seq "\"" (complement (:: any-string "\"" any-string)) "\"") (token 'STRING (string-trim lexeme "\""))]
[(eof) eof]))
(get-token input-port))
next-token)