aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/parser/lux/data/format/json.lux
blob: aff96d9b76215edc347732a1bca40dec2a0d0eab (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
(.require
 [library
  [lux (.except symbol)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["//" parser (.use "[1]#[0]" functor)]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" bit]
    ["[0]" text (.use "[1]#[0]" equivalence monoid)]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]
     ["[0]" sequence]
     ["[0]" dictionary (.only Dictionary)]]]
   [math
    [number
     ["[0]" frac]]]
   [meta
    ["[0]" code]]]]
 [\\library
  ["[0]" / (.only JSON)]])

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

(exception.def .public (unconsumed_input input)
  (Exception (List JSON))
  (exception.report
   (list ["Input" (exception.listing /.format input)])))

(exception.def .public empty_input)

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

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

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

(exception.def .public (unexpected_value value)
  (Exception JSON)
  (exception.report
   (list ["Value" (/.format value)])))

(with_template [<name> <type> <tag>]
  [(def .public <name>
     (Parser <type>)
     (do //.monad
       [head ..any]
       (when head
         {<tag> value}
         (in value)

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

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

(exception.def .public (value_mismatch [reference sample])
  (Exception [JSON JSON])
  (exception.report
   (list ["Reference" (/.format reference)]
         ["Sample" (/.format sample)])))

(with_template [<test> <check> <type> <equivalence> <tag>]
  [(def .public (<test> test)
     (-> <type> (Parser Bit))
     (do //.monad
       [head ..any]
       (when head
         {<tag> value}
         (in (at <equivalence> = test value))

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

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

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

  [boolean? this_boolean /.Boolean bit.equivalence  /.#Boolean]
  [number?  this_number  /.Number  frac.equivalence /.#Number]
  [string?  this_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]
    (when head
      {/.#Array values}
      (when (//.result parser (sequence.list values))
        {try.#Failure error}
        (//.failure error)

        {try.#Success [remainder output]}
        (when 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]
    (when head
      {/.#Object kvs}
      (when (|> 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]}
        (when 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 (again inputs)
    (when inputs
      (list.partial {/.#String key} value inputs')
      (if (text#= key field_name)
        (when (//.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] (again inputs')]
          (in [(list.partial {/.#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))))