aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/parser/tree.lux
blob: 6828da45aaedb2fd3d8759bc064404bd807339b0 (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
(.using
  [library
   [lux "*"
    [abstract
     [monad {"+" do}]]
    [control
     ["[0]" try {"+" Try}]
     ["[0]" exception {"+" exception:}]]
    [data
     [collection
      [tree {"+" Tree}
       ["[0]" zipper {"+" Zipper}]]]]]]
  ["[0]" //])

(type: .public (Parser t a)
  (//.Parser (Zipper t) a))

(def: .public (result' parser zipper)
  (All (_ t a) (-> (Parser t a) (Zipper t) (Try a)))
  (do try.monad
    [[zipper output] (//.result parser zipper)]
    (in output)))

(def: .public (result parser tree)
  (All (_ t a) (-> (Parser t a) (Tree t) (Try a)))
  (result' parser (zipper.zipper tree)))

(def: .public value
  (All (_ t) (Parser t t))
  (function (_ zipper)
    {try.#Success [zipper (zipper.value zipper)]}))

(exception: .public cannot_move_further)

(template [<name> <direction>]
  [(def: .public <name>
     (All (_ t) (Parser t []))
     (function (_ zipper)
       (case (<direction> zipper)
         {.#None}
         (exception.except ..cannot_move_further [])

         {.#Some next}
         {try.#Success [next []]})))]

  [down      zipper.down]
  [up        zipper.up]

  [right     zipper.right]
  [rightmost zipper.rightmost]

  [left      zipper.left]
  [leftmost  zipper.leftmost]
  
  [next      zipper.next]
  [end       zipper.end]
  
  [previous  zipper.previous]
  [start     zipper.start]
  )