aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/macro/syntax.lux
blob: c32d5d1055d8cf1267ae1e40ad4a182328dd3812 (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
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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;module:
  [lux #- not default]
  (lux [compiler #+ Monad<Lux> with-gensyms]
       (control functor
                applicative
                monad
                eq)
       (data [bool]
             [char]
             [number]
             [text "Text/" Monoid<Text>]
             [ident]
             (struct [list #* "" Functor<List> Fold<List> "List/" Monoid<List>])
             [product]
             error))
  (.. [ast]))

## [Utils]
(def: (join-pairs pairs)
  (All [a] (-> (List [a a]) (List a)))
  (case pairs
    #;Nil                   #;Nil
    (#;Cons [[x y] pairs']) (list& x y (join-pairs pairs'))))

## [Types]
(type: #export (Syntax a)
  (-> (List AST) (Error [(List AST) a])))

## [Structures]
(struct: #export _ (Functor Syntax)
  (def: (map f ma)
    (lambda [tokens]
      (case (ma tokens)
        (#;Left msg)
        (#;Left msg)

        (#;Right [tokens' a])
        (#;Right [tokens' (f a)])))))

(struct: #export _ (Applicative Syntax)
  (def: functor Functor<Syntax>)

  (def: (wrap x tokens)
    (#;Right [tokens x]))

  (def: (apply ff fa)
    (lambda [tokens]
      (case (ff tokens)
        (#;Right [tokens' f])
        (case (fa tokens')
          (#;Right [tokens'' a])
          (#;Right [tokens'' (f a)])

          (#;Left msg)
          (#;Left msg))

        (#;Left msg)
        (#;Left msg)))))

(struct: #export _ (Monad Syntax)
  (def: applicative Applicative<Syntax>)

  (def: (join mma)
    (lambda [tokens]
      (case (mma tokens)
        (#;Left msg)
        (#;Left msg)

        (#;Right [tokens' ma])
        (ma tokens')))))

## [Utils]
(def: (remaining-inputs asts)
  (-> (List AST) Text)
  ($_ Text/append " | Remaining input: "
      (|> asts (map ast;ast-to-text) (interpose " ") (text;join-with ""))))

## [Syntaxs]
(def: #export any
  {#;doc "Just returns the next input without applying any logic."}
  (Syntax AST)
  (lambda [tokens]
    (case tokens
      #;Nil                (#;Left "There are no tokens to parse!")
      (#;Cons [t tokens']) (#;Right [tokens' t]))))

(do-template [<get-name> <ask-name> <demand-name> <type> <tag> <eq> <desc>]
  [(def: #export <get-name>
     (Syntax <type>)
     (lambda [tokens]
       (case tokens
         (#;Cons [[_ (<tag> x)] tokens'])
         (#;Right [tokens' x])

         _
         (#;Left ($_ Text/append "Can't parse " <desc> (remaining-inputs tokens))))))

   (def: #export (<ask-name> v)
     (-> <type> (Syntax Bool))
     (lambda [tokens]
       (case tokens
         (#;Cons [[_ (<tag> x)] tokens'])
         (let [is-it? (:: <eq> = v x)
               remaining (if is-it?
                           tokens'
                           tokens)]
           (#;Right [remaining is-it?]))

         _
         (#;Right [tokens false]))))

   (def: #export (<demand-name> v)
     (-> <type> (Syntax Unit))
     (lambda [tokens]
       (case tokens
         (#;Cons [[_ (<tag> x)] tokens'])
         (if (:: <eq> = v x)
           (#;Right [tokens' []])
           (#;Left ($_ Text/append "Expected a " <desc> " but instead got " (ast;ast-to-text [_ (<tag> x)]) (remaining-inputs tokens))))

         _
         (#;Left ($_ Text/append "Can't parse " <desc> (remaining-inputs tokens))))))]

  [  bool   bool?   bool!  Bool   #;BoolS   bool;Eq<Bool> "bool"]
  [   nat    nat?    nat!   Nat    #;NatS  number;Eq<Nat> "nat"]
  [   int    int?    int!   Int    #;IntS  number;Eq<Int> "int"]
  [  real   real?   real!  Real   #;RealS number;Eq<Real> "real"]
  [  char   char?   char!  Char   #;CharS   char;Eq<Char> "char"]
  [  text   text?   text!  Text   #;TextS   text;Eq<Text> "text"]
  [symbol symbol? symbol! Ident #;SymbolS ident;Eq<Ident> "symbol"]
  [   tag    tag?    tag! Ident    #;TagS ident;Eq<Ident> "tag"]
  )

(def: #export (assert v message)
  (-> Bool Text (Syntax Unit))
  (lambda [tokens]
    (if v
      (#;Right [tokens []])
      (#;Left ($_ Text/append message (remaining-inputs tokens))))))

(do-template [<name> <comp> <error>]
  [(def: #export <name>
     (Syntax Int)
     (do Monad<Syntax>
       [n int
        _ (assert (<comp> 0 n) <error>)]
       (wrap n)))]

  [pos-int i.> "Expected a positive integer: N > 0"]
  [neg-int i.< "Expected a negative integer: N < 0"]
  )

(do-template [<name> <tag> <desc>]
  [(def: #export <name>
     (Syntax Text)
     (lambda [tokens]
       (case tokens
         (#;Cons [[_ (<tag> ["" x])] tokens'])
         (#;Right [tokens' x])

         _
         (#;Left ($_ Text/append "Can't parse " <desc> (remaining-inputs tokens))))))]

  [local-symbol #;SymbolS "local symbol"]
  [   local-tag #;TagS    "local tag"]
  )

(do-template [<name> <tag> <desc>]
  [(def: #export (<name> p)
     (All [a]
       (-> (Syntax a) (Syntax a)))
     (lambda [tokens]
       (case tokens
         (#;Cons [[_ (<tag> members)] tokens'])
         (case (p members)
           (#;Right [#;Nil x]) (#;Right [tokens' x])
           _                   (#;Left ($_ Text/append "Syntax was expected to fully consume " <desc> (remaining-inputs tokens))))

         _
         (#;Left ($_ Text/append "Can't parse " <desc> (remaining-inputs tokens))))))]

  [ form  #;FormS "form"]
  [tuple #;TupleS "tuple"]
  )

(def: #export (record p)
  (All [a]
    (-> (Syntax a) (Syntax a)))
  (lambda [tokens]
    (case tokens
      (#;Cons [[_ (#;RecordS pairs)] tokens'])
      (case (p (join-pairs pairs))
        (#;Right [#;Nil x]) (#;Right [tokens' x])
        _                   (#;Left ($_ Text/append "Syntax was expected to fully consume record" (remaining-inputs tokens))))

      _
      (#;Left ($_ Text/append "Can't parse record" (remaining-inputs tokens))))))

(def: #export (opt p)
  {#;doc "Optionality combinator."}
  (All [a]
    (-> (Syntax a) (Syntax (Maybe a))))
  (lambda [tokens]
    (case (p tokens)
      (#;Left _)            (#;Right [tokens #;None])
      (#;Right [tokens' x]) (#;Right [tokens' (#;Some x)]))))

(def: #export (run tokens p)
  (All [a]
    (-> (List AST) (Syntax a) (Error [(List AST) a])))
  (p tokens))

(def: #export (some p)
  {#;doc "0-or-more combinator."}
  (All [a]
    (-> (Syntax a) (Syntax (List a))))
  (lambda [tokens]
    (case (p tokens)
      (#;Left _)            (#;Right [tokens (list)])
      (#;Right [tokens' x]) (run tokens'
                                 (do Monad<Syntax>
                                   [xs (some p)]
                                   (wrap (list& x xs)))
                                 ))))

(def: #export (many p)
  {#;doc "1-or-more combinator."}
  (All [a]
    (-> (Syntax a) (Syntax (List a))))
  (do Monad<Syntax>
    [x p
     xs (some p)]
    (wrap (list& x xs))))

(def: #export (seq p1 p2)
  {#;doc "Sequencing combinator."}
  (All [a b]
    (-> (Syntax a) (Syntax b) (Syntax [a b])))
  (do Monad<Syntax>
    [x1 p1
     x2 p2]
    (wrap [x1 x2])))

(def: #export (alt p1 p2)
  {#;doc "Heterogeneous alternative combinator."}
  (All [a b]
    (-> (Syntax a) (Syntax b) (Syntax (| a b))))
  (lambda [tokens]
    (case (p1 tokens)
      (#;Right [tokens' x1]) (#;Right [tokens' (+0 x1)])
      (#;Left _)             (run tokens
                                  (do Monad<Syntax>
                                    [x2 p2]
                                    (wrap (+1 x2))))
      )))

(def: #export (either pl pr)
  {#;doc "Homogeneous alternative combinator."}
  (All [a]
    (-> (Syntax a) (Syntax a) (Syntax a)))
  (lambda [tokens]
    (case (pl tokens)
      (#;Left _) (pr tokens)
      output     output
      )))

(def: #export end
  {#;doc "Ensures there are no more inputs."}
  (Syntax Unit)
  (lambda [tokens]
    (case tokens
      #;Nil (#;Right [tokens []])
      _     (#;Left ($_ Text/append "Expected list of tokens to be empty!" (remaining-inputs tokens))))))

(def: #export end?
  {#;doc "Checks whether there are no more inputs."}
  (Syntax Bool)
  (lambda [tokens]
    (case tokens
      #;Nil (#;Right [tokens true])
      _     (#;Right [tokens false]))))

(def: #export (exactly n p)
  (All [a] (-> Nat (Syntax a) (Syntax (List a))))
  (if (n.> +0 n)
    (do Monad<Syntax>
      [x p
       xs (exactly (n.dec n) p)]
      (wrap (#;Cons x xs)))
    (:: Monad<Syntax> wrap (list))))

(def: #export (at-least n p)
  (All [a] (-> Nat (Syntax a) (Syntax (List a))))
  (do Monad<Syntax>
    [min (exactly n p)
     extra (some p)]
    (wrap (List/append min extra))))

(def: #export (at-most n p)
  (All [a] (-> Nat (Syntax a) (Syntax (List a))))
  (if (n.> +0 n)
    (lambda [input]
      (case (p input)
        (#;Left msg)
        (#;Right [input (list)])

        (#;Right [input' x])
        (run input'
             (do Monad<Syntax>
               [xs (at-most (n.dec n) p)]
               (wrap (#;Cons x xs))))
        ))
    (:: Monad<Syntax> wrap (list))))

(def: #export (between from to p)
  (All [a] (-> Nat Nat (Syntax a) (Syntax (List a))))
  (do Monad<Syntax>
    [min-xs (exactly from p)
     max-xs (at-most (n.- from to) p)]
    (wrap (:: Monad<List> join (list min-xs max-xs)))))

(def: #export (sep-by sep p)
  {#;doc "Parsers instances of 'p' that are separated by instances of 'sep'."}
  (All [a b] (-> (Syntax b) (Syntax a) (Syntax (List a))))
  (do Monad<Syntax>
    [?x (opt p)]
    (case ?x
      #;None
      (wrap #;Nil)
      
      (#;Some x)
      (do @
        [xs' (some (seq sep p))]
        (wrap (#;Cons x (map product;right xs'))))
      )))

(def: #export (not p)
  (All [a] (-> (Syntax a) (Syntax Unit)))
  (lambda [input]
    (case (p input)
      (#;Left msg)
      (#;Right [input []])
      
      _
      (#;Left "Expected to fail; yet succeeded."))))

(def: #export (fail message)
  (All [a] (-> Text (Syntax a)))
  (lambda [input]
    (#;Left message)))

(def: #export (default value parser)
  {#;doc "If the given parser fails, returns the default value."}
  (All [a] (-> a (Syntax a) (Syntax a)))
  (lambda [input]
    (case (parser input)
      (#;Left error)
      (#;Right [input value])

      (#;Right [input' output])
      (#;Right [input' output]))))

(def: #export (on compiler meta)
  (All [a] (-> Compiler (Lux a) (Syntax a)))
  (lambda [input]
    (case (meta compiler)
      (#;Left error)
      (#;Left error)

      (#;Right [_ value])
      (#;Right [input value])
      )))

(def: #export (local local-inputs syntax)
  (All [a] (-> (List AST) (Syntax a) (Syntax a)))
  (lambda [real-inputs]
    (case (syntax local-inputs)
      (#;Left error)
      (#;Left error)

      (#;Right [unconsume-inputs value])
      (case unconsume-inputs
        #;Nil
        (#;Right [real-inputs value])

        _
        (#;Left "Unconsumed inputs.")))))

## [Syntax]
(def: #hidden text.join-with text;join-with)

(macro: #export (syntax: tokens)
  {#;doc (doc "A more advanced way to define macros than macro:."
              "The inputs to the macro can be parsed in complex ways through the use of syntax parsers."
              "The macro body is also (implicitly) run in the Monad<Lux>, to save some typing."
              "Also, the compiler state can be accessed through the *compiler* binding."
              (syntax: #export (object [#let [imports (class-imports *compiler*)]]
                                 [#let [class-vars (list)]]
                                 [super (opt (super-class-decl^ imports class-vars))]
                                 [interfaces (tuple (some (super-class-decl^ imports class-vars)))]
                                 [constructor-args (constructor-args^ imports class-vars)]
                                 [methods (some (overriden-method-def^ imports))])
                (let [def-code ($_ Text/append "anon-class:"
                                   (spaced (list (super-class-decl$ (;default object-super-class super))
                                                 (with-brackets (spaced (map super-class-decl$ interfaces)))
                                                 (with-brackets (spaced (map constructor-arg$ constructor-args)))
                                                 (with-brackets (spaced (map (method-def$ id) methods))))))]
                  (wrap (list (` (;_lux_proc ["jvm" (~ (ast;text def-code))] [])))))))}
  (let [[exported? tokens] (case tokens
                             (^ (list& [_ (#;TagS ["" "export"])] tokens'))
                             [true tokens']

                             _
                             [false tokens])
        ?parts (: (Maybe [Text (List AST) AST AST])
                  (case tokens
                    (^ (list [_ (#;FormS (list& [_ (#;SymbolS ["" name])] args))]
                             body))
                    (#;Some name args (` {}) body)

                    (^ (list [_ (#;FormS (list& [_ (#;SymbolS ["" name])] args))]
                             meta-data
                             body))
                    (#;Some name args meta-data body)

                    _
                    #;None))]
    (case ?parts
      (#;Some [name args meta body])
      (with-gensyms [g!tokens g!body g!msg]
        (do Monad<Lux>
          [vars+parsers (mapM Monad<Lux>
                              (: (-> AST (Lux [AST AST]))
                                 (lambda [arg]
                                   (case arg
                                     (^ [_ (#;RecordS (list [var parser]))])
                                     (wrap [var parser])

                                     [_ (#;SymbolS var-name)]
                                     (wrap [(ast;symbol var-name) (` any)])

                                     _
                                     (compiler;fail "Syntax pattern expects records or symbols."))))
                              args)
           #let [g!state (ast;symbol ["" "*compiler*"])
                 g!end (ast;symbol ["" ""])
                 error-msg (ast;text (Text/append "Wrong syntax for " name))
                 export-ast (: (List AST) (if exported? (list (' #export)) (list)))]]
          (wrap (list (` (macro: (~@ export-ast) ((~ (ast;symbol ["" name])) (~ g!tokens))
                           (~ meta)
                           (lambda [(~ g!state)]
                             (;_lux_case (run (~ g!tokens)
                                              (: (Syntax (Lux (List AST)))
                                                 (do Monad<Syntax>
                                                   [(~@ (join-pairs vars+parsers))
                                                    (~ g!end) end]
                                                   ((~' wrap) (do Monad<Lux>
                                                                []
                                                                (~ body))))))
                               (#;Right [(~ g!tokens) (~ g!body)])
                               ((~ g!body) (~ g!state))

                               (#;Left (~ g!msg))
                               (#;Left (text.join-with ": " (list (~ error-msg) (~ g!msg))))))))))))
      
      _
      (compiler;fail "Wrong syntax for syntax:"))))