aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/collection/tree/rose/parser.lux
blob: 3696266dd4493803073cca64fe803d7674c93f96 (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
(.module:
  lux
  (lux (control ["p" parser]
                ["ex" exception (#+ exception:)])
       (data ["E" error]))
  [// (#+ Tree)]
  (// [zipper (#+ Zipper)]))

(type: #export (Parser t a)
  (p.Parser (Zipper t) a))

(def: #export (run-zipper zipper parser)
  (All [t a] (-> (Zipper t) (Parser t a) (E.Error a)))
  (case (p.run zipper parser)
    (#E.Success [zipper output])
    (#E.Success output)

    (#E.Error error)
    (#E.Error error)))

(def: #export (run tree parser)
  (All [t a] (-> (Tree t) (Parser t a) (E.Error a)))
  (run-zipper (zipper.zip tree) parser))

(def: #export value
  (All [t] (Parser t t))
  (function (_ zipper)
    (#E.Success [zipper (zipper.value zipper)])))

(exception: #export cannot-move-further)

(do-template [<name> <direction>]
  [(def: #export <name>
     (All [t] (Parser t []))
     (function (_ zipper)
       (let [next (<direction> zipper)]
         (if (is? zipper next)
           (ex.throw cannot-move-further [])
           (#E.Success [next []])))))]

  [up        zipper.up]
  [down      zipper.down]
  [left      zipper.left]
  [right     zipper.right]
  [root      zipper.root]
  [rightmost zipper.rightmost]
  [leftmost  zipper.leftmost]
  [next      zipper.next]
  [prev      zipper.prev]
  )