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.
35 lines
1.5 KiB
Racket
35 lines
1.5 KiB
Racket
9 years ago
|
#lang br
|
||
9 years ago
|
(require parser-tools/lex parser-tools/lex-sre
|
||
9 years ago
|
brag/support
|
||
9 years ago
|
racket/string)
|
||
|
(provide tokenize)
|
||
|
|
||
9 years ago
|
(define-lex-abbrevs
|
||
|
(natural (repetition 1 +inf.0 numeric))
|
||
9 years ago
|
;; don't lex the leading "-": muddles "-X" and "Y-X"
|
||
|
(number (union (seq natural)
|
||
|
(seq (? natural) (seq "." natural))))
|
||
9 years ago
|
(quoted-string (seq "\"" (repetition 0 +inf.0 (char-complement "\"")) "\"")))
|
||
9 years ago
|
|
||
9 years ago
|
(define (tokenize input-port)
|
||
|
(define (next-token)
|
||
|
(define get-token
|
||
9 years ago
|
(lexer-src-pos
|
||
9 years ago
|
[(eof) eof]
|
||
9 years ago
|
[(seq "/*" (complement (seq any-string "*/" any-string)) "*/") (get-token input-port)]
|
||
9 years ago
|
[(union #\tab #\space #\newline
|
||
9 years ago
|
(seq number " REM" (repetition 0 +inf.0 (char-complement #\newline)) #\newline)) (get-token input-port)]
|
||
9 years ago
|
[(union "PRINT" "print" "FOR" "for" "TO" "to" "STEP" "step" "IF" "if"
|
||
|
"GOTO" "goto" "INPUT" "input" "LET" "let" "NEXT" "next"
|
||
|
"RETURN" "return" "CLEAR" "clear" "LIST" "list" "RUN" "run"
|
||
|
"END" "end" "THEN" "then" "ELSE" "else" "GOSUB" "gosub"
|
||
9 years ago
|
"AND" "and" "OR" "or" "STOP" "stop" "LET" "let" "DEF" "def" "DIM" "dim" "ON" "on"
|
||
9 years ago
|
";" "=" "(" ")" "+" "-" "*" "/" "^"
|
||
9 years ago
|
"<=" ">=" "<>" "<" ">" "=" ":" ",") (string-downcase lexeme)]
|
||
9 years ago
|
[number (token 'NUMBER (string->number lexeme))]
|
||
9 years ago
|
[(seq upper-case (repetition 0 +inf.0 (or upper-case numeric)) (? "$")) (token 'ID (string->symbol lexeme))]
|
||
9 years ago
|
[quoted-string (token 'STRING (string-trim lexeme "\""))]))
|
||
9 years ago
|
(get-token input-port))
|
||
|
next-token)
|
||
9 years ago
|
|