-
Notifications
You must be signed in to change notification settings - Fork 6
/
stream.lisp
163 lines (143 loc) · 5.3 KB
/
stream.lisp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(in-package #:queen)
(defparameter *has-letter-prop* (cl-unicode:property-test "Letter"))
(defmacro with-parse-stream (input &body body)
(let ((pos (gensym "pos"))
(line (gensym "line"))
(col (gensym "col"))
(log (gensym "log"))
(-next (gensym "-next")))
`(let ((,pos 0) (,line 1) (,col 0) (,log nil))
(labels
((,-next ()
(if ,log
(pop ,log)
(read-char ,input nil 'EOF)))
(unget (ch)
(push ch ,log))
(peek ()
(if ,log
(car ,log)
(peek-char nil ,input nil nil)))
(next ()
(let ((ch (,-next)))
(when (and (eql ch #\Return)
(eql (peek) #\Newline))
(incf ,pos)
(setf ch (,-next)))
(case ch
(EOF
nil)
(#\Newline
(setf ,col 0)
(incf ,line)
(incf ,pos)
ch)
(otherwise
(incf ,col)
(incf ,pos)
ch))))
(eof? ()
(not (peek)))
(croak (msg &rest args)
(when args
(setf msg (apply #'format nil msg args)))
(error "ERROR: ~A (~A:~A)" msg ,line ,col))
(read-while (pred)
(with-output-to-string (out)
(loop for ch = (peek)
while (and ch (funcall pred ch))
do (write-char (next) out))))
(whitespace? (ch)
(case (char-code ch)
((32 10 13 9 #xA0 #xFEFF) t)))
(non-whitespace? (ch)
(not (whitespace? ch)))
(digit? (ch)
(digit-char-p ch))
(letter? (ch)
(funcall *has-letter-prop* ch))
(alnum? (ch)
(or (digit? ch)
(letter? ch)))
(look-ahead (len func)
(let ((savepos ,pos)
(saveline ,line)
(savecol ,col))
(let ((chars (loop repeat len collect (next))))
(or (funcall func chars)
(progn
(setf ,pos savepos
,line saveline
,col savecol
,log (nconc chars ,log))
nil)))))
(skip (ch &optional no-error)
(cond
((characterp ch)
(let ((curr (next)))
(if (eql ch curr)
curr
(unless no-error
(croak "Expected ~A but found ~A" ch curr)))))
((stringp ch)
(let ((match
(look-ahead (length ch)
(lambda (chars)
(unless (member nil chars)
(string= ch (coerce chars 'string)))))))
(if match match
(unless no-error
(croak "Expected ~A ch")))))
(t
(error "Unknown token in `skip'"))))
(read-number ()
(let (n d ch)
(tagbody
next
(setq ch (next))
(unless ch (go finish))
(setq d (digit? ch))
(unless d (go finish))
(setf n (+ d (* (or n 0) 10)))
(go next)
finish
(when ch (unget ch)))
n))
(read-string (&optional (quote #\") (esc #\\))
(skip quote)
(let* ((escaped nil))
(prog1 (read-while (lambda (ch)
(cond
(escaped
(setf escaped nil)
t)
((eql ch quote)
nil)
((eql ch esc)
(next)
(setf escaped t)
t)
(t t))))
(skip quote))))
(skip-whitespace ()
(read-while #'whitespace?)))
(declare (ignorable #'peek
#'next
#'unget
#'eof?
#'croak
#'read-while
#'whitespace?
#'non-whitespace?
#'digit?
#'letter?
#'alnum?
#'skip
#'read-string
#'skip-whitespace
#'look-ahead
#'read-number))
(unwind-protect
(progn ,@body)
(when ,log
(unread-char (car ,log) ,input)))))))