aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/parser/environment.lux
blob: c0ced37c2cfd31ec4d222f43b38754702511d3a0 (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
(.module:
  [library
   [lux #*
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]]
    [data
     ["." product]
     ["." text
      ["%" format (#+ format)]]
     [collection
      ["." dictionary (#+ Dictionary)]]]]]
  ["." //])

(type: #export Property
  Text)

(type: #export Environment
  (Dictionary Property Text))

(exception: #export (unknown {property Property})
  (exception.report
   ["Property" (%.text property)]))

(type: #export (Parser a)
  (//.Parser Environment a))

(def: #export empty
  Environment
  (dictionary.new text.hash))

(def: #export (property name)
  (-> Text (Parser Text))
  (function (_ environment)
    (case (dictionary.get name environment)
      (#.Some value)
      (exception.return [environment value])
      
      #.None
      (exception.throw ..unknown name))))

(def: #export (run parser environment)
  (All [a] (-> (Parser a) Environment (Try a)))
  (\ try.monad map product.right (parser environment)))