aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/parser/tree.lux
blob: b45e4044917b0d2aee88d10ff9c7d03bf9a26de2 (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 (.except left right)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" try (.only Try)]
    ["[0]" exception (.only exception:)]]
   [data
    [collection
     [tree (.only Tree)
      ["[0]" zipper (.only 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]
  )