-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.rkt
61 lines (59 loc) · 2.21 KB
/
tokenizer.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#lang racket
(require brag/support)
(provide tokenize)
(define (tokenize ip [path #f])
(port-count-lines! ip)
(lexer-file-path path)
(define (next-token)
(let ([bs-lexer
(lexer-srcloc
;; whitespace
[whitespace
(next-token)]
[(:seq "OP_PUSHDATA" (char-range #\1 #\4))
(token 'OPPUSHDATA (string->symbol lexeme)
#:position (pos lexeme-start)
#:line (line lexeme-start)
#:column (col lexeme-start)
#:span (- (pos lexeme-end)
(pos lexeme-start)))]
;; opcode
[(:seq "OP_"
(:* (:or (char-range #\0 #\9)
(char-range #\A #\Z)
(char-range #\a #\z))))
(token 'OPCODE (string->symbol lexeme)
#:position (pos lexeme-start)
#:line (line lexeme-start)
#:column (col lexeme-start)
#:span (- (pos lexeme-end)
(pos lexeme-start)))]
;; decimal data
[(:+ (char-range #\0 #\9))
(token 'DEC (string->number lexeme)
#:position (pos lexeme-start)
#:line (line lexeme-start)
#:column (col lexeme-start)
#:span (- (pos lexeme-end)
(pos lexeme-start)))]
;; hexadecimal data
[(:seq "0x" (:+ (:or (char-range #\0 #\9)
(char-range #\A #\F)
(char-range #\a #\f))))
(token 'HEX (substring lexeme 2)
#:position (pos lexeme-start)
#:line (line lexeme-start)
#:column (col lexeme-start)
#:span (- (pos lexeme-end)
(pos lexeme-start)))]
;; single line # comment
[(:: "#" (:* (:~ #\newline)))
(next-token)]
;; end of file
[(from/to "<" ">")
(next-token)]
#;
[(eof)
(void)])])
(bs-lexer ip)))
next-token)