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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
## This is LuxC's parser.
## It takes the source code of a Lux file in raw text form and
## extracts the syntactic structure of the code from it.
## It only produces Lux Code nodes, and thus removes any white-space
## and comments while processing its inputs.
## Another important aspect of the parser is that it keeps track of
## its position within the input data.
## That is, the parser takes into account the line and column
## information in the input text (it doesn't really touch the
## file-name aspect of the cursor, leaving it intact in whatever
## base-line cursor it is given).
## This particular piece of functionality is not located in one
## function, but it is instead scattered throughout several parsers,
## since the logic for how to update the cursor varies, depending on
## what is being parsed, and the rules involved.
## You will notice that several parsers have a "where" parameter, that
## tells them the cursor position prior to the parser being run.
## They are supposed to produce some parsed output, alongside an
## updated cursor pointing to the end position, after the parser was run.
## Lux Code nodes/tokens are annotated with cursor meta-data
## [file-name, line, column] to keep track of their provenance and
## location, which is helpful for documentation and debugging.
(.module:
[lux #*
[abstract
monad]
[control
["." exception (#+ exception:)]]
[data
["." maybe]
["." error (#+ Error)]
[number
["." nat]
["." int]
["." rev]
["." frac]]
["." text
[lexer (#+ Offset)]
format]
[collection
["." list]
["." dictionary (#+ Dictionary)]]]
[macro
["." template]]])
## TODO: Implement "lux syntax char case!" as a custom extension.
## That way, it should be possible to obtain the char without wrapping
## it into a java.lang.Long, thereby improving performance.
## TODO: Make an extension to take advantage of java/lang/String::indexOf<int,int>
## to get better performance than the current "lux text index" extension.
## TODO: Instead of always keeping a "where" cursor variable, keep the
## individual components (i.e. file, line and column) separate, so
## that updated the "where" only involved updating the components, and
## producing the cursors only involved building them, without any need
## for pattern-matching and de-structuring.
(type: Char Nat)
(template [<name> <extension> <diff>]
[(template: (<name> value)
(<extension> <diff> value))]
[!inc "lux i64 +" 1]
[!inc/2 "lux i64 +" 2]
[!dec "lux i64 -" 1]
)
(template: (!clip from to text)
("lux text clip" from to text))
(template [<name> <extension>]
[(template: (<name> reference subject)
(<extension> reference subject))]
[!n/= "lux i64 ="]
[!i/< "lux int <"]
)
(template [<name> <extension>]
[(template: (<name> param subject)
(<extension> param subject))]
[!n/+ "lux i64 +"]
[!n/- "lux i64 -"]
)
(type: #export Aliases (Dictionary Text Text))
(def: #export no-aliases Aliases (dictionary.new text.hash))
(def: #export prelude "lux")
(def: #export space " ")
(def: #export text-delimiter text.double-quote)
(def: #export open-form "(")
(def: #export close-form ")")
(def: #export open-tuple "[")
(def: #export close-tuple "]")
(def: #export open-record "{")
(def: #export close-record "}")
(def: #export sigil "#")
(def: #export digit-separator ",")
(def: #export positive-sign "+")
(def: #export negative-sign "-")
(def: #export frac-separator ".")
## The parts of an name are separated by a single mark.
## E.g. module.short.
## Only one such mark may be used in an name, since there
## can only be 2 parts to an name (the module [before the
## mark], and the short [after the mark]).
## There are also some extra rules regarding name syntax,
## encoded on the parser.
(def: #export name-separator ".")
(exception: #export (end-of-file {module Text})
(exception.report
["Module" (%t module)]))
(def: amount-of-input-shown 64)
(def: (input-at start input)
(-> Offset Text Text)
(let [end (|> start (n/+ amount-of-input-shown) (n/min ("lux text size" input)))]
(!clip start end input)))
(exception: #export (unrecognized-input {[file line column] Cursor} {context Text} {input Text} {offset Offset})
(exception.report
["File" file]
["Line" (%n line)]
["Column" (%n column)]
["Context" (%t context)]
["Input" (input-at offset input)]))
(exception: #export (text-cannot-contain-new-lines {text Text})
(exception.report
["Text" (%t text)]))
(type: (Parser a)
(-> Source (Either [Source Text] [Source a])))
(template: (!with-char+ @source-code-size @source-code @offset @char @else @body)
(if (!i/< (:coerce Int @source-code-size)
(:coerce Int @offset))
(let [@char ("lux text char" @offset @source-code)]
@body)
@else))
(template: (!with-char @source-code @offset @char @else @body)
(!with-char+ ("lux text size" @source-code) @source-code @offset @char @else @body))
(template: (!letE <binding> <computation> <body>)
(case <computation>
(#.Right <binding>)
<body>
(#.Left error)
(#.Left error)))
(template: (!horizontal where offset source-code)
[(update@ #.column inc where)
(!inc offset)
source-code])
(template: (!new-line where)
## (-> Cursor Cursor)
(let [[where::file where::line where::column] where]
[where::file (!inc where::line) 0]))
(template: (!vertical where offset source-code)
[(!new-line where)
(!inc offset)
source-code])
(def: close-signal
(template.with-locals [g!close-signal]
(template.text [g!close-signal])))
(template [<name> <close> <tag> <context>]
[(def: (<name> parse source)
(-> (Parser Code) (Parser Code))
(let [[where _ _] source]
(loop [source source
stack (: (List Code) #.Nil)]
(case (parse source)
(#.Right [source' top])
(recur source' (#.Cons top stack))
(#.Left [source' error])
(if (is? <close> error)
(#.Right [source'
[where (<tag> (list.reverse stack))]])
(#.Left [source' error]))))))]
## Form and tuple syntax is mostly the same, differing only in the
## delimiters involved.
## They may have an arbitrary number of arbitrary Code nodes as elements.
[parse-form ..close-form #.Form "Form"]
[parse-tuple ..close-tuple #.Tuple "Tuple"]
)
(def: (parse-record parse source)
(-> (Parser Code) (Parser Code))
(let [[where _ _] source]
(loop [source source
stack (: (List [Code Code]) #.Nil)]
(case (parse source)
(#.Right [sourceF field])
(!letE [sourceFV value] (parse sourceF)
(recur sourceFV (#.Cons [field value] stack)))
(#.Left [source' error])
(if (is? ..close-record error)
(#.Right [source'
[where (#.Record (list.reverse stack))]])
(#.Left [source' error]))))))
(template: (!guarantee-no-new-lines where offset source-code content body)
(case ("lux text index" 0 (static text.new-line) content)
#.None
body
g!_
(#.Left [[where offset source-code]
(exception.construct ..text-cannot-contain-new-lines content)])))
(template: (!read-text where offset source-code)
(case ("lux text index" offset (static ..text-delimiter) source-code)
(#.Some g!end)
(let [g!content (!clip offset g!end source-code)]
(<| (!guarantee-no-new-lines where offset source-code g!content)
(#.Right [[(update@ #.column (n/+ (!n/- offset g!end)) where)
(!inc g!end)
source-code]
[where
(#.Text g!content)]])))
_
(#.Left [[where offset source-code]
(exception.construct unrecognized-input [where "Text" source-code offset])])))
(def: digit-bottom Nat (!dec (char "0")))
(def: digit-top Nat (!inc (char "9")))
(template: (!digit? char)
(and (!i/< (:coerce Int char) (:coerce Int (static ..digit-bottom)))
(!i/< (:coerce Int (static ..digit-top)) (:coerce Int char))))
(`` (template: (!digit?+ char)
(or (!digit? char)
("lux i64 =" (.char (~~ (static ..digit-separator))) char))))
(with-expansions [<clauses> (template [<char>]
[("lux i64 =" (.char (~~ (static <char>))) char)
#0]
[..space] [text.new-line]
[..name-separator]
[..open-form] [..close-form]
[..open-tuple] [..close-tuple]
[..open-record] [..close-record]
[..text-delimiter]
[..sigil])]
(`` (template: (!strict-name-char? char)
(cond <clauses>
## else
#1))))
(template: (!name-char?|head char)
(and (!strict-name-char? char)
(not (!digit? char))))
(template: (!name-char? char)
(or (!strict-name-char? char)
(!digit? char)))
(template: (!number-output <start> <end> <codec> <tag>)
(case (|> source-code
(!clip <start> <end>)
(text.replace-all ..digit-separator "")
(:: <codec> decode))
(#.Right output)
(#.Right [[(update@ #.column (n/+ (!n/- <start> <end>)) where)
<end>
source-code]
[where (<tag> output)]])
(#.Left error)
(#.Left [[where <start> source-code]
error])))
(def: no-exponent Offset 0)
(with-expansions [<int-output> (as-is (!number-output start end int.decimal #.Int))
<frac-output> (as-is (!number-output start end frac.decimal #.Frac))
<failure> (#.Left [[where offset source-code]
(exception.construct unrecognized-input [where "Frac" source-code offset])])]
(def: (parse-frac source-code//size start [where offset source-code])
(-> Nat Offset (Parser Code))
(loop [end offset
exponent ..no-exponent]
(<| (!with-char+ source-code//size source-code end char/0 <frac-output>)
(cond (!digit?+ char/0)
(recur (!inc end) exponent)
(and (or (!n/= (char "e") char/0)
(!n/= (char "E") char/0))
(not (is? ..no-exponent exponent)))
(<| (!with-char+ source-code//size source-code (!inc end) char/1 <failure>)
(if (or (!n/= (`` (char (~~ (static ..positive-sign)))) char/1)
(!n/= (`` (char (~~ (static ..negative-sign)))) char/1))
(<| (!with-char+ source-code//size source-code (!n/+ 2 end) char/2 <failure>)
(if (!digit?+ char/2)
(recur (!n/+ 3 end) char/0)
<failure>))
<failure>))
## else
<frac-output>))))
(def: (parse-signed start [where offset source-code])
(-> Offset (Parser Code))
(let [source-code//size ("lux text size" source-code)]
(loop [end offset]
(<| (!with-char+ source-code//size source-code end char <int-output>)
(cond (!digit?+ char)
(recur (!inc end))
(!n/= (`` (.char (~~ (static ..frac-separator))))
char)
(parse-frac source-code//size start [where (!inc end) source-code])
## else
<int-output>))))))
(template [<name> <codec> <tag>]
[(template: (<name> source-code//size start where offset source-code)
(loop [g!end offset]
(<| (!with-char+ source-code//size source-code g!end g!char (!number-output start g!end <codec> <tag>))
(if (!digit?+ g!char)
(recur (!inc g!end))
(!number-output start g!end <codec> <tag>)))))]
[!parse-nat nat.decimal #.Nat]
[!parse-rev rev.decimal #.Rev]
)
(template: (!parse-signed source-code//size offset where source-code @aliases @end)
(let [g!offset/1 (!inc offset)]
(<| (!with-char+ source-code//size source-code g!offset/1 g!char/1 @end)
(if (!digit? g!char/1)
(parse-signed offset [where (!inc/2 offset) source-code])
(!parse-full-name offset [where (!inc offset) source-code] where @aliases #.Identifier)))))
(with-expansions [<output> (#.Right [[(update@ #.column (n/+ (!n/- start end)) where)
end
source-code]
(!clip start end source-code)])]
(def: (parse-name-part start [where offset source-code])
(-> Offset (Parser Text))
(let [source-code//size ("lux text size" source-code)]
(loop [end offset]
(<| (!with-char+ source-code//size source-code end char <output>)
(if (!name-char? char)
(recur (!inc end))
<output>))))))
(template: (!failure where offset source-code)
(#.Left [[where offset source-code]
(exception.construct unrecognized-input [where "General" source-code offset])]))
(template: (!end-of-file where offset source-code current-module)
(#.Left [[where offset source-code]
(exception.construct ..end-of-file current-module)]))
(with-expansions [<consume-1> (as-is [where (!inc offset/0) source-code])
<consume-2> (as-is [where (!inc/2 offset/0) source-code])]
(template: (!parse-half-name @offset @char @module)
(cond (!name-char?|head @char)
(!letE [source' name] (..parse-name-part @offset [where (!inc @offset) source-code])
(#.Right [source' [@module name]]))
## else
(!failure where @offset source-code)))
(`` (def: (parse-short-name current-module [where offset/0 source-code])
(-> Text (Parser Name))
(<| (!with-char source-code offset/0 char/0
(!end-of-file where offset/0 source-code current-module))
(if (!n/= (char (~~ (static ..name-separator))) char/0)
(let [offset/1 (!inc offset/0)]
(<| (!with-char source-code offset/1 char/1
(!end-of-file where offset/1 source-code current-module))
(!parse-half-name offset/1 char/1 current-module)))
(!parse-half-name offset/0 char/0 ..prelude)))))
(template: (!parse-short-name @current-module @source @where @tag)
(!letE [source' name] (..parse-short-name @current-module @source)
(#.Right [source' [@where (@tag name)]])))
(with-expansions [<simple> (as-is (#.Right [source' ["" simple]]))]
(`` (def: (parse-full-name aliases start source)
(-> Aliases Offset (Parser Name))
(!letE [source' simple] (..parse-name-part start source)
(let [[where' offset' source-code'] source']
(<| (!with-char source-code' offset' char/separator <simple>)
(if (!n/= (char (~~ (static ..name-separator))) char/separator)
(let [offset'' (!inc offset')]
(!letE [source'' complex] (..parse-name-part offset'' [where' offset'' source-code'])
(#.Right [source'' [(|> aliases
(dictionary.get simple)
(maybe.default simple))
complex]])))
<simple>)))))))
(template: (!parse-full-name @offset @source @where @aliases @tag)
(!letE [source' full-name] (..parse-full-name @aliases @offset @source)
(#.Right [source' [@where (@tag full-name)]])))
## TODO: Grammar macro for specifying syntax.
## (grammar: lux-grammar
## [expression ...]
## [form "(" [#* expression] ")"])
(template: (!close closer)
(#.Left [<consume-1> closer]))
(with-expansions [<recur> (as-is (parse current-module aliases source-code//size))
<horizontal-move> (as-is (recur (!horizontal where offset/0 source-code)))]
(def: #export (parse current-module aliases source-code//size)
(-> Text Aliases Nat (Parser Code))
## The "exec []" is only there to avoid function fusion.
## This is to preserve the loop as much as possible and keep it tight.
(exec []
(function (recur [where offset/0 source-code])
(<| (!with-char+ source-code//size source-code offset/0 char/0
(!end-of-file where offset/0 source-code current-module))
## TODO: Add ..space as just another case for "lux syntax char case!" ASAP.
## It"s currently failing for some reason.
(`` (if (!n/= (char (~~ (static ..space))) char/0)
<horizontal-move>
("lux syntax char case!" char/0
[## New line
[(~~ (static text.carriage-return))]
<horizontal-move>
[(~~ (static text.new-line))]
(recur (!vertical where offset/0 source-code))
## Form
[(~~ (static ..open-form))]
(parse-form <recur> <consume-1>)
[(~~ (static ..close-form))]
(!close ..close-form)
## Tuple
[(~~ (static ..open-tuple))]
(parse-tuple <recur> <consume-1>)
[(~~ (static ..close-tuple))]
(!close ..close-tuple)
## Record
[(~~ (static ..open-record))]
(parse-record <recur> <consume-1>)
[(~~ (static ..close-record))]
(!close ..close-record)
## Text
[(~~ (static ..text-delimiter))]
(let [offset/1 (!inc offset/0)]
(!read-text where offset/1 source-code))
## Special code
[(~~ (static ..sigil))]
(let [offset/1 (!inc offset/0)]
(<| (!with-char+ source-code//size source-code offset/1 char/1
(!end-of-file where offset/1 source-code current-module))
("lux syntax char case!" char/1
[[(~~ (static ..name-separator))]
(!parse-short-name current-module <consume-2> where #.Tag)
## Single-line comment
[(~~ (static ..sigil))]
(case ("lux text index" (!inc offset/1) (static text.new-line) source-code)
(#.Some end)
(recur (!vertical where end source-code))
_
(!end-of-file where offset/1 source-code current-module))
(~~ (template [<char> <bit>]
[[<char>]
(#.Right [[(update@ #.column (|>> !inc/2) where)
(!inc offset/1)
source-code]
[where (#.Bit <bit>)]])]
["0" #0]
["1" #1]))]
## else
(cond (!name-char?|head char/1) ## Tag
(!parse-full-name offset/1 <consume-2> where aliases #.Tag)
## else
(!failure where offset/0 source-code)))))
## Coincidentally (= name-separator frac-separator)
[(~~ (static ..name-separator))]
(let [offset/1 (!inc offset/0)]
(<| (!with-char+ source-code//size source-code offset/1 char/1
(!end-of-file where offset/1 source-code current-module))
(if (!digit? char/1)
(let [offset/2 (!inc offset/1)]
(!parse-rev source-code//size offset/0 where offset/2 source-code))
(!parse-short-name current-module [where offset/1 source-code] where #.Identifier))))
[(~~ (static ..positive-sign))
(~~ (static ..negative-sign))]
(!parse-signed source-code//size offset/0 where source-code aliases
(!end-of-file where offset/0 source-code current-module))]
## else
(if (!digit? char/0)
## Natural number
(let [offset/1 (!inc offset/0)]
(!parse-nat source-code//size offset/0 where offset/1 source-code))
## Identifier
(!parse-full-name offset/0 <consume-1> where aliases #.Identifier))
)))
)))
))
)
|