aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/parser/json.lux
blob: 12fb90dd39f2fc68594ab6599cad064d29100f94 (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
199
200
201
202
203
204
205
206
207
(.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: #export (Parser a)
  {#.doc "JSON parser."}
  (//.Parser (List JSON) a))

(exception: #export (unconsumed_input {input (List JSON)})
  (exception.report
   ["Input" (exception.enumerate /.format input)]))

(exception: #export empty_input)

(def: #export (run parser json)
  (All [a] (-> (Parser a) JSON (Try a)))
  (case (//.run parser (list json))
    (#try.Success [remainder output])
    (case remainder
      #.Nil
      (#try.Success output)

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

(def: #export any
  {#.doc "Just returns the JSON input without applying any logic."}
  (Parser JSON)
  (<| (function (_ inputs))
      (case inputs
        #.Nil
        (exception.throw ..empty_input [])
        
        (#.Cons head tail)
        (#try.Success [tail head]))))

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

(template [<name> <type> <tag> <desc>]
  [(def: #export <name>
     {#.doc (code.text ($_ text\compose "Reads a JSON value as " <desc> "."))}
     (Parser <type>)
     (do //.monad
       [head ..any]
       (case head
         (<tag> value)
         (wrap value)

         _
         (//.fail (exception.construct ..unexpected_value [head])))))]

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

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

(template [<test> <check> <type> <equivalence> <tag> <desc>]
  [(def: #export (<test> test)
     {#.doc (code.text ($_ text\compose "Asks whether a JSON value is a " <desc> "."))}
     (-> <type> (Parser Bit))
     (do //.monad
       [head ..any]
       (case head
         (<tag> value)
         (wrap (\ <equivalence> = test value))

         _
         (//.fail (exception.construct ..unexpected_value [head])))))

   (def: #export (<check> test)
     {#.doc (code.text ($_ text\compose "Ensures a JSON value is a " <desc> "."))}
     (-> <type> (Parser Any))
     (do //.monad
       [head ..any]
       (case head
         (<tag> value)
         (if (\ <equivalence> = test value)
           (wrap [])
           (//.fail (exception.construct ..value_mismatch [(<tag> test) (<tag> value)])))

         _
         (//.fail (exception.construct ..unexpected_value [head])))))]

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

(def: #export (nullable parser)
  (All [a] (-> (Parser a) (Parser (Maybe a))))
  (//.or ..null
         parser))

(def: #export (array parser)
  {#.doc "Parses a JSON array."}
  (All [a] (-> (Parser a) (Parser a)))
  (do //.monad
    [head ..any]
    (case head
      (#/.Array values)
      (case (//.run parser (row.to_list values))
        (#try.Failure error)
        (//.fail error)

        (#try.Success [remainder output])
        (case remainder
          #.Nil
          (wrap output)

          _
          (//.fail (exception.construct ..unconsumed_input remainder))))

      _
      (//.fail (exception.construct ..unexpected_value [head])))))

(def: #export (object parser)
  {#.doc "Parses a JSON object. Use this with the 'field' combinator."}
  (All [a] (-> (Parser a) (Parser a)))
  (do //.monad
    [head ..any]
    (case head
      (#/.Object kvs)
      (case (|> kvs
                dictionary.entries
                (list\map (function (_ [key value])
                            (list (#/.String key) value)))
                list.concat
                (//.run parser))
        (#try.Failure error)
        (//.fail error)

        (#try.Success [remainder output])
        (case remainder
          #.Nil
          (wrap output)

          _
          (//.fail (exception.construct ..unconsumed_input remainder))))
      
      _
      (//.fail (exception.construct ..unexpected_value [head])))))

(def: #export (field field_name parser)
  {#.doc "Parses a field inside a JSON object. Use this inside the 'object' combinator."}
  (All [a] (-> Text (Parser a) (Parser a)))
  (function (recur inputs)
    (case inputs
      (^ (list& (#/.String key) value inputs'))
      (if (text\= key field_name)
        (case (//.run parser (list value))
          (#try.Success [#.Nil output])
          (#try.Success [inputs' output])

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

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

      #.Nil
      (exception.throw ..empty_input [])

      _
      (exception.throw ..unconsumed_input inputs))))

(def: #export dictionary
  {#.doc "Parses a dictionary-like JSON object."}
  (All [a] (-> (Parser a) (Parser (Dictionary Text a))))
  (|>> (//.and ..string)
       //.some
       ..object
       (//\map (dictionary.from_list text.hash))))