/
reader.coffee
277 lines (238 loc) · 8.04 KB
/
reader.coffee
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# reader
# might be worthwhile rewriting all methods on
# the reader object into class methods to make
# them easier to call from compiled code
class Position
constructor: (@offset, @line, @column, @origin) ->
@offset ||= 0
@line ||= 1
@column ||= 1
@origin ||= "unknown origin"
update: (c) ->
if /[\n\r\f]/.test(c)
new Position(@offset+1, @line+1, 1, @origin)
else
new Position(@offset+1, @line, @column+1, @origin)
toString: () ->
"at line #{@line}, column #{@column}, in #{@origin}"
class Reader
constructor: (input, pos, macros, dispatchMacros) ->
if !(this instanceof Reader)
return new Reader(input, pos, macros, dispatchMacros)
else
@input = input || ""
@pos = pos || new Position()
@lastPos = null
@macros = macros || Reader.defaultMacros
@dispatchMacros = dispatchMacros || Reader.defaultDispatchMacros
peekChar: () ->
if @pos.offset < @input.length
return @input[@pos.offset]
else
return Reader.EOF
peekChars: (n) ->
@input.substring(@pos.offset, @pos.offset+n)
readChar: () ->
c = @peekChar()
if c != Reader.EOF
@lastPos = @pos
@pos = @pos.update(c)
c
readChars: (n) ->
r = ""
while n--
c = @readChar()
if c == Reader.EOF then raise('EOF')
r += c
return r
readConstituents: () ->
r = ""
loop
c = @peekChar()
if c == Reader.EOF || /\s|;/.test(c) || contains(@macros, c) then return r
r += @readChar()
unreadChar: () ->
@pos = @lastPos
@lastPos = null
skipWhitespace: () ->
inComment = false
loop
switch @readChar()
when Reader.EOF then return
when ' ' then null
when '\t' then null
when '\n' then inComment = false
when '\r' then inComment = false
when '\f' then inComment = false
when ';' then inComment = true
else
if !inComment
@unreadChar()
return
escape: (c) ->
if c == 'u'
code = @readChars(4)
/[a-zA-Z0-9]{4}/.test(code) || raise('invalid unicode value: ' + code ' ' + @pos)
return String.fromCharCode(parseInt(code, 16))
else
Reader.escapeMap[c] || raise('invalid escape character: ' + c + ' ' + @pos)
readAllSexps: () ->
sexps = []
while !@isEmpty()
sexps.push(@readSexp())
sexps
readSexp: () ->
@skipWhitespace()
c = @peekChar()
if c == Reader.EOF
return null
macro = get(@macros, c)
if macro then macro(this) else @readAtom()
readAtom: () ->
s = @readConstituents()
if Reader.isFloat(s) then return parseFloat(s)
if Reader.isBinary(s) then return parseInt(s.substring(2), 2)
if Reader.isOctal(s) then return parseInt(s, 8)
if Reader.isInt(s) then return parseInt(s, 10)
if Reader.isHex(s) then return parseInt(s, 16)
return Reader.parseAtom(s)
isEmpty: () ->
@skipWhitespace()
@peekChar() == Reader.EOF
@dottedRegex: /^[^\.]+(\.[^\.]+)+/
@parseAtom: (s) ->
if Reader.dottedRegex.test(s)
segs = s.split(".")
root = new Symbol(segs[0])
for seg in segs.slice(1)
root = List.create(baseSymbol('.'), root, seg)
root
else
new Symbol(s)
@isBinary: (s) -> /^(\+|-)?0[bB](0|1)+$/.test(s)
@isOctal: (s) -> /^(\+|-)?0[0-7]+$/.test(s)
@isInt: (s) -> /^(\+|-)?(0|([1-9][0-9]*))$/.test(s)
@isHex: (s) -> /^(\+|-)?0[xX][0-9a-fA-F]+$/.test(s)
@isFloat: (s) -> /^(\+|-)?(0|([1-9][0-9]*))\.[0-9]+$/.test(s)
@escapeMap: {
'n' : '\n'
'r' : '\r'
'f' : '\f'
'b' : '\b'
't' : '\t'
'"' : '"'
'\\' : '\\'
}
@readString: (r) ->
chars = []
pos = @pos
r.readChar()
loop
c = r.peekChar()
if c == '\r' && r.peekChars(2) == '\r\n'
r.readChars(2)
chars.push('\n')
continue
switch c
when Reader.EOF then raise('unclosed string literal ' + pos)
when '"' then r.readChar(); return chars.join('')
when '\\' then r.readChar(); chars.push(r.escape(r.readChar()))
else chars.push(r.readChar())
r.readString()
@EOF: {}
@mismatchedDelimiter: (r) ->
raise('unmatched delimiter ' + r.pos)
@readList: (r) ->
pos = r.pos
elts = []
r.readChar()
loop
r.skipWhitespace()
c = r.peekChar()
switch c
when ')' then r.readChar(); return List.fromArray(elts)
when Reader.EOF then raise('unclosed list ' + pos)
else elts.push(r.readSexp())
@readArray: (r) ->
pos = r.pos
elts = []
r.readChar()
loop
r.skipWhitespace()
c = r.peekChar()
switch c
when ']' then r.readChar(); return elts
when Reader.EOF then raise('unclosed array ' + pos)
else elts.push(r.readSexp())
@readDict: (r) ->
pos = r.pos
elts = []
r.readChar()
loop
r.skipWhitespace()
c = r.peekChar()
switch c
when '}' then r.readChar(); return Dict.fromArray(elts)
when Reader.EOF then raise('unclosed array ' + pos)
else elts.push(r.readSexp())
@readQuote: (r) ->
r.readChar()
List.create(baseSymbol('quote'), r.readSexp())
@readQuasiquote: (r) ->
r.readChar()
List.create(baseSymbol('quasiquote'), r.readSexp())
@readUnquote: (r) ->
r.readChar()
if r.peekChar() == '@'
r.readChar()
prefix = baseSymbol('unquote-splicing')
else
prefix = baseSymbol('unquote')
List.create(prefix, r.readSexp())
@readDispatchMacro: (r) ->
pos = r.pos
r.readChar()
c = r.peekChar()
macro = get(r.dispatchMacros, c)
if macro then macro(r) else raise('no dispatch macro for ' + c + ' ' + pos)
@readQualifiedSymbol: (r) ->
pos = r.pos
r.readChar()
ns = r.readConstituents()
if r.readChar() != "#" then throw ('malformed qualifed symbol ' + pos)
name = r.readConstituents()
new Symbol(name, ns)
@makeLiteralReader: (lit, val) ->
(reader) ->
s = reader.readChars(lit.length)
if s == lit
return val
else
raise("invalid dispatch macro: " + s)
@defaultMacros: Map.create(
')', Reader.mismatchedDelimiter,
']', Reader.mismatchedDelimiter,
'}', Reader.mismatchedDelimiter,
'(', Reader.readList,
'[', Reader.readArray,
'{', Reader.readDict,
"'", Reader.readQuote,
'"', Reader.readString,
'`', Reader.readQuasiquote,
',', Reader.readUnquote,
'#', Reader.readDispatchMacro
)
@defaultDispatchMacros: Map.create(
't', Reader.makeLiteralReader('t', true),
'f', Reader.makeLiteralReader('f', false),
'n', Reader.makeLiteralReader('nil', null),
'v', Reader.makeLiteralReader('void', undefined),
'#', Reader.readQualifiedSymbol
)
@create: (input, options) ->
options ||= {}
origin = options.origin || null
macros = options.macros || Reader.defaultMacros
dispatchMacros = options.dispatchMacros || Reader.defaultDispatchMacros
position =
new Reader(input, new Position(null, null, null, origin), macros, dispatchMacros)