blob: 25f9af46aae2d8235367540e995adb45b5992529 (
plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
(;module:
lux
(lux (control monad)
(data [bool]
[char]
[text]
[number]
(text ["l" lexer #+ Lexer Monad<Lexer> "l/" Monad<Lexer>]
format)
[product]
(coll [list "L/" Functor<List> Fold<List>]
["V" vector]))))
(def: (space^ where)
(-> Cursor (Lexer [Text Cursor]))
(do Monad<Lexer>
[head (l;some' (l;one-of "\t\v \r\f"))]
(l;either (l;after (l;one-of "\n")
(do @
[[tail end] (space^ (|> where
(update@ #;line n.inc)
(set@ #;column +0)))]
(wrap [(format head tail)
end])))
(wrap [head
(|> where
(update@ #;column (n.+ (text;size head))))]))))
(def: (single-line-comment^ where)
(-> Cursor (Lexer [Text Cursor]))
(l;enclosed ["##" "\n"]
(do Monad<Lexer>
[comment (l;some' (l;none-of "\n"))]
(wrap [comment
(|> where
(update@ #;line n.inc)
(set@ #;column +0))]))))
(def: comment-bound^
(Lexer Text)
($_ l;either
(l;text "\n")
(l;text ")#")
(l;text "#(")))
(def: (multi-line-comment^ where)
(-> Cursor (Lexer [Text Cursor]))
(do Monad<Lexer>
[_ (l;text "#(")]
(loop [comment ""
where (|> where
(update@ #;column (n.+ +2)))]
($_ l;either
(do @
[chunk (l;many' (l;not comment-bound^))]
(recur (format comment chunk)
(|> where
(update@ #;column (n.+ (text;size chunk))))))
(do @
[_ (l;one-of "\n")]
(recur (format comment "\n")
(|> where
(update@ #;line n.inc)
(set@ #;column +0))))
(do @
[[sub-comment sub-where] (multi-line-comment^ where)]
(recur (format comment "#(" sub-comment ")#")
sub-where))
(do @
[_ (l;text ")#")]
(wrap [comment
(|> where
(update@ #;column (n.+ +2)))]))
))))
(def: (comment^ where)
(-> Cursor (Lexer [Text Cursor]))
(l;either (single-line-comment^ where)
(multi-line-comment^ where)))
(def: (padding^ where)
(-> Cursor (Lexer Cursor))
(l;either (do Monad<Lexer>
[[comment where] (comment^ where)]
(padding^ where))
(do Monad<Lexer>
[[white-space where] (space^ where)]
(wrap where))
))
(def: escaped-char^
(Lexer [Text Char])
(l;after (l;char #"\\")
(do Monad<Lexer>
[code l;any]
(case code
#"t" (wrap ["\\t" #"\t"])
#"v" (wrap ["\\v" #"\v"])
#"b" (wrap ["\\b" #"\b"])
#"n" (wrap ["\\n" #"\n"])
#"r" (wrap ["\\r" #"\r"])
#"f" (wrap ["\\f" #"\f"])
#"\"" (wrap ["\\\"" #"\""])
#"\\" (wrap ["\\\\" #"\\"])
#"u"
(do Monad<Lexer>
[code (l;between' +1 +4 l;hex-digit)]
(wrap (case (:: number;Hex@Codec<Text,Nat> decode
(format "+" code))
(#;Right value)
[(format "\\u" code) (char;char value)]
_
(undefined))))
_
(l;fail (format "Invalid escaping syntax: " (%c code)))))))
(def: raw-char^
(Lexer [Text Char])
(l;either (do Monad<Lexer>
[char (l;none-of "\\\"\n")]
(wrap [(char;as-text char) char]))
escaped-char^))
(do-template [<name> <tag> <lexer> <codec>]
[(def: (<name> where)
(-> Cursor (Lexer [AST Cursor]))
(do Monad<Lexer>
[chunk <lexer>]
(case (:: <codec> decode chunk)
(#;Left error)
(l;fail error)
(#;Right value)
(wrap [[where (<tag> value)]
(|> where
(update@ #;column (n.+ (text;size chunk))))]))))]
[bool^ #;BoolS
(l;either (l;text "true") (l;text "false"))
bool;Codec<Text,Bool>]
[nat^ #;NatS
(l;seq' (l;text "+") (l;many' l;digit))
number;Codec<Text,Nat>]
[int^ #;IntS
(l;seq' (l;default "" (l;text "-"))
(l;many' l;digit))
number;Codec<Text,Int>]
[real^ #;RealS
($_ l;seq'
(l;default "" (l;text "-"))
(l;many' l;digit)
(l;text ".")
(l;many' l;digit))
number;Codec<Text,Real>]
[deg^ #;DegS
(l;seq' (l;text ".")
(l;many' l;digit))
number;Codec<Text,Deg>]
)
(def: (char^ where)
(-> Cursor (Lexer [AST Cursor]))
(do Monad<Lexer>
[[chunk value] (l;enclosed ["#\"" "\""]
raw-char^)]
(wrap [[where (#;CharS value)]
(|> where
(update@ #;column (|>. ($_ n.+ +3 (text;size chunk)))))])))
(def: (text^ where)
(-> Cursor (Lexer [AST Cursor]))
(do Monad<Lexer>
[_ (l;text "\"")
#let [offset-column (n.inc (get@ #;column where))]
[text-read where'] (: (Lexer [Text Cursor])
(loop [text-read ""
where (|> where
(update@ #;column n.inc))
next-line-start? false]
(let [next-line (do @
[_ (l;text "\n")]
(recur (format text-read "\n")
(|> where
(update@ #;line n.inc)
(set@ #;column +0))
true))]
(if next-line-start?
(l;either next-line
(do @
[offset (l;many' (l;char #" "))
#let [offset-size (text;size offset)]]
(if (n.>= offset-column offset-size)
(recur (|> offset
(text;split offset-column)
(default (undefined))
product;right
(format text-read))
(|> where
(update@ #;column (n.+ offset-size)))
false)
(l;fail (format "Each line of a multi-line text must have an appropriate offset!\n"
"Expected: " (%i (nat-to-int offset-column)) " columns.\n"
" Actual: " (%i (nat-to-int offset-size)) " columns.\n")))))
($_ l;either
(do @
[normal (l;many' (l;none-of "\\\"\n"))]
(recur (format text-read normal)
(|> where
(update@ #;column (n.+ (text;size normal))))
false))
(do @
[[chunk char] escaped-char^]
(recur (format text-read (char;as-text char))
(|> where
(update@ #;column (n.+ (text;size chunk))))
false))
next-line
(do @
[_ (l;text "\"")]
(wrap [text-read
(|> where
(update@ #;column n.inc))])))))))]
(wrap [[where (#;TextS text-read)]
where'])))
(do-template [<name> <tag> <open> <close>]
[(def: (<name> where ast^)
(-> Cursor
(-> Cursor (Lexer [AST Cursor]))
(Lexer [AST Cursor]))
(do Monad<Lexer>
[_ (l;text <open>)
[elems where'] (loop [elems (: (V;Vector AST)
V;empty)
where where]
(l;either (do @
[[elem where'] (ast^ where)]
(recur (V;add elem elems)
where'))
(do @
[where' (padding^ where)
_ (l;text <close>)]
(wrap [(V;to-list elems)
(|> where'
(update@ #;column n.inc))]))))]
(wrap [[where (<tag> elems)]
where'])))]
[form^ #;FormS "(" ")"]
[tuple^ #;TupleS "[" "]"]
)
(def: (record^ where ast^)
(-> Cursor
(-> Cursor (Lexer [AST Cursor]))
(Lexer [AST Cursor]))
(do Monad<Lexer>
[_ (l;text "{")
[elems where'] (loop [elems (: (V;Vector [AST AST])
V;empty)
where where]
(l;either (do @
[[key where] (ast^ where)
[val where'] (ast^ where)]
(recur (V;add [key val] elems)
where'))
(do @
[where' (padding^ where)
_ (l;text "}")]
(wrap [(V;to-list elems)
(|> where'
(update@ #;column n.inc))]))))]
(wrap [[where (#;RecordS elems)]
where'])))
(def: ident-part^
(Lexer Text)
(do Monad<Lexer>
[#let [digits "0123456789"
delimiters "()[]{}#;\""
space "\t\v \n\r\f"
head-lexer (l;none-of (format digits delimiters space))
tail-lexer (l;some' (l;none-of (format delimiters space)))]
head head-lexer
tail tail-lexer]
(wrap (format (char;as-text head)
tail))))
(def: ident^
(Lexer [Ident Nat])
($_ l;either
(do Monad<Lexer>
[_ (l;text ";;")
def-name ident-part^]
(l;fail "Cannot handle ;; syntax for identifiers."))
(do Monad<Lexer>
[_ (l;text ";")
def-name ident-part^]
(wrap [["lux" def-name]
(n.inc (text;size def-name))]))
(do Monad<Lexer>
[first-part ident-part^]
(l;either (do @
[_ (l;char #";")
second-part ident-part^]
(wrap [[first-part second-part]
($_ n.+
(text;size first-part)
+1
(text;size second-part))]))
(wrap [["" first-part]
(text;size first-part)])))))
(do-template [<name> <tag> <lexer> <extra>]
[(def: (<name> where)
(-> Cursor (Lexer [AST Cursor]))
(do Monad<Lexer>
[[value length] <lexer>]
(wrap [[where (<tag> value)]
(|> where
(update@ #;column (|>. ($_ n.+ <extra> length))))])))]
[symbol^ #;SymbolS ident^ +0]
[tag^ #;TagS (l;after (l;char #"#") ident^) +1]
)
(def: #export (ast^ where)
(-> Cursor (Lexer [AST Cursor]))
(do Monad<Lexer>
[where (padding^ where)]
($_ l;either
(form^ where ast^)
(tuple^ where ast^)
(record^ where ast^)
(bool^ where)
(nat^ where)
(real^ where)
(int^ where)
(deg^ where)
(symbol^ where)
(tag^ where)
(char^ where)
(text^ where)
)))
|