-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.rkt
72 lines (62 loc) · 2.38 KB
/
lexer.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
62
63
64
65
66
67
68
69
70
71
72
#lang racket
;; Modified from an example in the Racket parser tools repo
;; https://github.com/racket/parser-tools
;; Import the parser and lexer generators.
(require parser-tools/lex
(prefix-in : parser-tools/lex-sre))
(provide value-tokens op-tokens halide-lexer evaluate-halide-parser)
(define-tokens value-tokens (NUM VAR TVAR NTVAR XBROADCAST))
(define-empty-tokens op-tokens (newline OP CP COMMA + - * / % ^ < > ! EQ NEQ GE LE EOF NEG OR AND MAX MIN SELECT TRUE FALSE LII UINT1 UINT0 UINT LIKELY PROMISE LET IN ASMT RAMP BROADCAST))
(define-lex-abbrevs
(lower-letter (:/ "a" "z"))
(upper-letter (:/ #\A #\Z))
;; (:/ 0 9) would not work because the lexer does not understand numbers. (:/ #\0 #\9) is ok too.
(digit (:/ "0" "9")))
(define halide-lexer
(lexer
[(eof) 'EOF]
;; recursively call the lexer on the remaining input after a tab or space. Returning the
;; result of that operation. This effectively skips all whitespace.
[(:or #\tab #\space) (halide-lexer input-port)]
["VERIFYFAILURE" (halide-lexer input-port)] ;; throw away header
;; (token-newline) returns 'newline
[#\newline (token-newline)]
[(:: (:? "-") (:+ digit)) (token-NUM (string->number lexeme))]
;; Since (token-=) returns '=, just return the symbol directly
[(:or "+" "-" "*" "/" "<" ">" "!" "%") (string->symbol lexeme)]
["(uint1)1" 'UINT1]
["(uint1)0" 'UINT0]
["(uint1)" 'UINT]
[">=" 'GE]
["<=" 'LE]
["==" 'EQ]
["!=" 'NEQ]
["(" 'OP]
[")" 'CP]
["," 'COMMA]
["||" 'OR]
["&&" 'AND]
["max" 'MAX]
["min" 'MIN]
["select" 'SELECT]
["true" 'TRUE]
["false" 'FALSE]
["likely_if_innermost" 'LII]
["likely" 'LIKELY]
["promise_clamped" 'PROMISE]
["let" 'LET]
["=" 'ASMT]
["in" 'IN]
["ramp" 'RAMP]
["broadcast" 'BROADCAST]
;[(:+ (:or lower-letter upper-letter)) (token-VAR (string->symbol lexeme))]
[(:: "x" (:+ digit)) (token-XBROADCAST lexeme)]
[(union "x" "y" "z" "w" "u" "v") (token-VAR (string->symbol lexeme))]
[(:: "t" (:+ digit)) (token-TVAR (string->symbol lexeme))]
[(:: "n" (:+ digit)) (token-NTVAR (string->symbol lexeme))]
[(:: (union "v" "i" "c") (:+ digit)) (token-VAR (string->symbol lexeme))]
[(:: (:+ digit) #\. (:* digit)) (token-NUM (string->number lexeme))]))
(define (evaluate-halide-parser p s)
(let ([ip (open-input-string s)])
(port-count-lines! ip)
(p (λ () (halide-lexer ip)))))