From ea1ef4c6ad6788bafe232f91d9a1bf03d48e4ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 30 Oct 2022 17:56:01 -0300 Subject: Add a number parser Most of the spec regarding numbers is covered but currently numbers can have leading 0's and + sign. --- json.lisp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'json.lisp') diff --git a/json.lisp b/json.lisp index b9ea796..2bc94db 100644 --- a/json.lisp +++ b/json.lisp @@ -1 +1,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)))) -- cgit v1.2.3