From 550204b6840d2986b1707a16d0a77c0e69d5d5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Wed, 2 Nov 2022 10:51:58 -0300 Subject: Add a string parser Doesn't have good error reporting yet. Planning on making so the monparser comp macro allows not defining a binding. That way less work is needed when ignoring stuff. --- json.lisp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'json.lisp') diff --git a/json.lisp b/json.lisp index cb94985..5f570d5 100644 --- a/json.lisp +++ b/json.lisp @@ -20,3 +20,31 @@ (fail "Malformed exponent part.")) nothing))) (list 'number base fraction exponent)))) + +(defparameter string-parser + (comp ((start (unit (lambda (x) (char= x #\")))) + (chars (zero-or-more (either (comp ((slash (unit (lambda (x) (char= x #\\)))) + (escaped (unit)) + (codepoints (if (and escaped (char= escaped #\u)) + (comp ((cp0 (unit #'digit-char-p)) + (cp1 (unit #'digit-char-p)) + (cp2 (unit #'digit-char-p)) + (cp3 (unit #'digit-char-p))) + (let ((str (make-string 4))) + (setf (char str 0) cp0) + (setf (char str 1) cp1) + (setf (char str 2) cp2) + (setf (char str 3) cp3) + str)) + nothing))) + (case escaped + (#\n + #\Newline) + (#\t + #\Tab) + (#\u + codepoints) + (t escaped))) + (unit (lambda (x) (char/= x #\")))))) + (end (unit (lambda (x) (char= x #\"))))) + (list 'string chars))) -- cgit v1.2.3