aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/parser/json.lux
blob: f73bf4ce85b694a19386422616a0b0b1653ef23e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
(.module:
  [library
   [lux #*
    [abstract
     ["." monad (#+ do)]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]]
    [data
     ["." bit]
     ["." text ("#\." equivalence monoid)]
     [collection
      ["." list ("#\." functor)]
      ["." row]
      ["." dictionary (#+ Dictionary)]]
     [format
      ["/" json (#+ JSON)]]]
    [macro
     ["." code]]
    [math
     [number
      ["." frac]]]]]
  ["." // ("#\." functor)])

(type: .public (Parser a)
  (//.Parser (List JSON) a))

(exception: .public (unconsumed_input {input (List JSON)})
  (exception.report
   ["Input" (exception.listing /.format input)]))

(exception: .public empty_input)

(def: .public (result parser json)
  (All (_ a) (-> (Parser a) JSON (Try a)))
  (case (//.result parser (list json))
    (#try.Success [remainder output])
    (case remainder
      #.End
      (#try.Success output)

      _
      (exception.except ..unconsumed_input remainder))
    
    (#try.Failure error)
    (#try.Failure error)))

(def: .public any
  (Parser JSON)
  (<| (function (_ inputs))
      (case inputs
        #.End
        (exception.except ..empty_input [])
        
        (#.Item head tail)
        (#try.Success [tail head]))))

(exception: .public (unexpected_value {value JSON})
  (exception.report
   ["Value" (/.format value)]))

(template [<name> <type> <tag>]
  [(def: .public <name>
     (Parser <type>)
     (do //.monad
       [head ..any]
       (case head
         (<tag> value)
         (in value)

         _
         (//.failure (exception.error ..unexpected_value [head])))))]

  [null    /.Null    #/.Null]
  [boolean /.Boolean #/.Boolean]
  [number  /.Number  #/.Number]
  [string  /.String  #/.String]
  )

(exception: .public [a] (value_mismatch {reference JSON} {sample JSON})
  (exception.report
   ["Reference" (/.format reference)]
   ["Sample" (/.format sample)]))

(template [<test> <check> <type> <equivalence> <tag>]
  [(def: .public (<test> test)
     (-> <type> (Parser Bit))
     (do //.monad
       [head ..any]
       (case head
         (<tag> value)
         (in (\ <equivalence> = test value))

         _
         (//.failure (exception.error ..unexpected_value [head])))))

   (def: .public (<check> test)
     (-> <type> (Parser Any))
     (do //.monad
       [head ..any]
       (case head
         (<tag> value)
         (if (\ <equivalence> = test value)
           (in [])
           (//.failure (exception.error ..value_mismatch [(<tag> test) (<tag> value)])))

         _
         (//.failure (exception.error ..unexpected_value [head])))))]

  [boolean? boolean! /.Boolean bit.equivalence  #/.Boolean]
  [number?  number!  /.Number  frac.equivalence #/.Number]
  [string?  string!  /.String  text.equivalence #/.String]
  )

(def: .public (nullable parser)
  (All (_ a) (-> (Parser a) (Parser (Maybe a))))
  (//.or ..null
         parser))

(def: .public (array parser)
  (All (_ a) (-> (Parser a) (Parser a)))
  (do //.monad
    [head ..any]
    (case head
      (#/.Array values)
      (case (//.result parser (row.list values))
        (#try.Failure error)
        (//.failure error)

        (#try.Success [remainder output])
        (case remainder
          #.End
          (in output)

          _
          (//.failure (exception.error ..unconsumed_input remainder))))

      _
      (//.failure (exception.error ..unexpected_value [head])))))

(def: .public (object parser)
  (All (_ a) (-> (Parser a) (Parser a)))
  (do //.monad
    [head ..any]
    (case head
      (#/.Object kvs)
      (case (|> kvs
                dictionary.entries
                (list\each (function (_ [key value])
                             (list (#/.String key) value)))
                list.together
                (//.result parser))
        (#try.Failure error)
        (//.failure error)

        (#try.Success [remainder output])
        (case remainder
          #.End
          (in output)

          _
          (//.failure (exception.error ..unconsumed_input remainder))))
      
      _
      (//.failure (exception.error ..unexpected_value [head])))))

(def: .public (field field_name parser)
  (All (_ a) (-> Text (Parser a) (Parser a)))
  (function (recur inputs)
    (case inputs
      (^ (list& (#/.String key) value inputs'))
      (if (text\= key field_name)
        (case (//.result parser (list value))
          (#try.Success [#.End output])
          (#try.Success [inputs' output])

          (#try.Success [inputs'' _])
          (exception.except ..unconsumed_input inputs'')

          (#try.Failure error)
          (#try.Failure error))
        (do try.monad
          [[inputs'' output] (recur inputs')]
          (in [(list& (#/.String key) value inputs'')
               output])))

      #.End
      (exception.except ..empty_input [])

      _
      (exception.except ..unconsumed_input inputs))))

(def: .public dictionary
  (All (_ a) (-> (Parser a) (Parser (Dictionary Text a))))
  (|>> (//.and ..string)
       //.some
       ..object
       (//\each (dictionary.of_list text.hash))))