-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.ss
147 lines (133 loc) · 4.64 KB
/
parse.ss
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(define (parse-next-token input)
(define (skip? char)
(char-whitespace? char))
(define operator?
(lambda (char) (memq char '(#\+ #\- #\/ #\% #\( #\) #\{ #\} #\= #\; #\, #\* #\! #\& #\| #\> #\<))))
(define quote?
(lambda (char) (memq char '(#\"))))
(define escape?
(lambda (char) (eq? char #\\)))
(define decimal-point?
(lambda (char) (eq? char #\.)))
(define name?
(lambda (char) (or (char-alphabetic? char) (eq? char #\_))))
(define parse-next-float
(lambda (a i)
(if (char-numeric? (car i))
(parse-next-float (string-append a (string (car i))) (cdr i))
(list i 'token-float-number (string->number a)))))
(define parse-next-numerical
(lambda (a i)
(cond
((char-numeric? (car i))
(parse-next-numerical (string-append a (string (car i))) (cdr i)))
((decimal-point? (car i))
(parse-next-float (string-append a (string (car i))) (cdr i)))
(else
(list i 'token-number (string->number a))))))
(define parse-next-alphanum
(lambda (a i)
(if (or (char-numeric? (car i)) (name? (car i)))
(parse-next-alphanum (string-append a (string (car i))) (cdr i))
(list i 'token-symbol (string->symbol a)))))
(define parse-next-escape
(lambda (a i)
(cond
((or (eq? (car i) #\\) (eq? (car i) #\")) (parse-next-quoted (string-append a (string (car i))) (cdr i)))
((eq? (car i) #\n) (parse-next-quoted (string-append a (string #\newline)) (cdr i)))
(else error "unknown escaped char" (car i)))))
(define parse-next-quoted
(lambda (a i)
(cond ((quote? (car i))
(list (cdr i) 'token-string a))
((escape? (car i))
(parse-next-escape a (cdr i)))
(else
(parse-next-quoted (string-append a (string (car i))) (cdr i))))))
;they can be compound in theory, like operator -> in C
(define parse-next-operator
(lambda (a i )
(cond ( (eq? a #\( ) (list i 'token-left-bracket))
( (eq? a #\) ) (list i 'token-right-bracket))
( (eq? a #\{ ) (list i 'token-left-curly-bracket))
( (eq? a #\} ) (list i 'token-right-curly-bracket))
( (eq? a #\+ ) (list i 'token-plus))
( (eq? a #\- ) (if (eq? (car i) #\>)
(list (cdr i) 'token-pointer)
(list i 'token-minus)))
( (eq? a #\/ ) (list i 'token-divide))
( (eq? a #\% ) (list i 'token-percent))
( (eq? a #\= ) (list i 'token-equals))
( (eq? a #\; ) (list i 'token-semicolon))
( (eq? a #\, ) (list i 'token-comma))
( (eq? a #\* ) (list i 'token-star))
( (eq? a #\! ) (if (eq? (car i) #\=)
(list (cdr i) 'token-not-equals)
(list i 'token-bang)))
( (eq? a #\< ) (if (eq? (car i) #\=)
(list (cdr i) 'token-less-equals)
(list i 'token-less)))
( (eq? a #\> ) (if (eq? (car i) #\=)
(list (cdr i) 'token-greater-equals)
(list i 'token-greater)))
( (eq? a #\&) (if (eq? (car i) #\&)
(list (cdr i) 'token-logical-and)
(list (cdr i) 'token-bitwise-and)))
( (eq? a #\|) (if (eq? (car i) #\|)
(list (cdr i) 'token-logical-or)
(list (cdr i) 'token-bitwise-or)))
(else (error "bad operator" a))
)
))
(cond ((null? input)
'())
((skip? (car input))
(parse-next-token (cdr input)))
((name? (car input))
(parse-next-alphanum (string (car input)) (cdr input)))
((char-numeric? (car input))
(parse-next-numerical (string (car input)) (cdr input)))
((quote? (car input))
(parse-next-quoted "" (cdr input)))
((operator? (car input))
(parse-next-operator (car input) (cdr input)))
(else (error "unknown token " (car input)))))
(define tokenize
(lambda (input)
(let f ((token (parse-next-token input)))
(if (null? token)
'()
(cons (cdr token) (f (parse-next-token (car token))))))))
(define (make-token-provider tokens)
(let [(hold tokens)
(tmp #f)
(save-stack (make-stack))]
(define (get-next)
(set! tmp (car hold))
(advance)
tmp)
(define (advance)
(set! hold (cdr hold)))
(define (has-next arg)
(cond
((null? hold) '())
((eq? (car (peek)) arg) (get-next))
(else '())))
(define (peek)
(car hold))
(define (accept)
((save-stack 'pop!)))
(define (save)
((save-stack 'push!) (list hold)))
(define (restore)
(set! hold ((save-stack 'pop!))))
(define (dispatch m)
(cond ((eq? m 'get-next) get-next)
((eq? m 'accept) accept)
((eq? m 'save) save)
((eq? m 'restore) restore)
((eq? m 'has-next) has-next)
((eq? m 'advance) advance)
((eq? m 'peek) peek)
(else (error "unknown call" m))))
dispatch))