summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2022-10-30 17:56:01 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2022-10-30 17:56:01 -0300
commitea1ef4c6ad6788bafe232f91d9a1bf03d48e4ff3 (patch)
treeb9601f4983e1de06fd9848672ee4cbe1e0d67dac
parent2afa93a7a96df494267a0846ae4bc13d0dd538ea (diff)
downloadjson-ea1ef4c6ad6788bafe232f91d9a1bf03d48e4ff3.tar.gz
json-ea1ef4c6ad6788bafe232f91d9a1bf03d48e4ff3.zip
Add a number parser
Most of the spec regarding numbers is covered but currently numbers can have leading 0's and + sign.
-rw-r--r--json.asd2
-rw-r--r--json.lisp21
-rw-r--r--package.lisp2
3 files changed, 23 insertions, 2 deletions
diff --git a/json.asd b/json.asd
index 598f9c5..19479bb 100644
--- a/json.asd
+++ b/json.asd
@@ -1,6 +1,6 @@
(asdf:defsystem #:json
:serial t
- :depends-on ()
+ :depends-on (#:monparser)
:components
((:file "package")
(:file "json")))
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))))
diff --git a/package.lisp b/package.lisp
index df3e37c..609b6b4 100644
--- a/package.lisp
+++ b/package.lisp
@@ -1,2 +1,2 @@
(defpackage #:json
- (:use #:cl))
+ (:use #:cl #:parser))