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

(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)))