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

27 lines
1.1 KiB
Racket

9 years ago
#lang br
(require parser-tools/lex parser-tools/lex-sre
brag/support
9 years ago
racket/string)
(provide tokenize)
(define (tokenize input-port)
(define (next-token)
(define get-token
9 years ago
(lexer-src-pos
9 years ago
[(eof) eof]
9 years ago
[(union
(seq "/*" (complement (seq any-string "*/" any-string)) "*/")
(seq "//" (repetition 1 +inf.0 (char-complement #\newline)) #\newline))
(token 'COMMENT lexeme #:skip? #t)]
9 years ago
[(union #\tab #\space #\newline) (get-token input-port)]
9 years ago
[(union "CHIP" "IN" "OUT" "PARTS:") lexeme]
[(char-set "[]{}(),;=.") lexeme]
9 years ago
["true" (token 'TRUE #t)]
["false" (token 'FALSE #f)]
[(repetition 1 +inf.0 numeric) (token 'NUMBER (string->number lexeme))]
8 years ago
; bugaboo: "10" is ambiguous: number or binary number?
[(repetition 1 +inf.0 (char-set "01")) (token 'BINARY-NUMBER (string->number lexeme 2))]
9 years ago
[(seq (repetition 1 1 alphabetic) (repetition 0 +inf.0 (union alphabetic numeric "-"))) (token 'ID (string->symbol lexeme))]))
9 years ago
(get-token input-port))
next-token)