summaryrefslogtreecommitdiff
path: root/json.lisp
blob: 2bc94dbbe752ea8f0b603f015da5aa99021d9810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(in-package #:json)

(defparameter number-parser
  (let ((signed-digits
          (comp ((sign (zero-or-one (unit (lambda (x) (or (char= x #\-)
                                                          (char= x #\+))))))
                 (natural (one-or-more (unit #'digit-char-p))))
            (cons sign natural))))
    (comp ((base (any signed-digits
                      (fail "Malformed number.")))
           (dot (zero-or-one (unit (lambda (x) (char= x #\.)))))
           (fraction (if dot
                       (any (one-or-more (unit #'digit-char-p))
                            (fail "Malformed fractional part."))
                       (new nil)))
           (e (zero-or-one (unit (lambda (x) (or (char= x #\e)
                                                 (char= x #\E))))))
           (exponent (if e
                       (any signed-digits
                            (fail "Malformed exponent part."))
                       (new nil))))
      (list 'number base fraction exponent))))