diff options
author | Boutade <thegoofist@protonmail.com> | 2020-03-06 11:21:04 -0600 |
---|---|---|
committer | Boutade <thegoofist@protonmail.com> | 2020-03-06 11:21:04 -0600 |
commit | 1dcd98391c66840c1dd377eb598d9a1f808b99d8 (patch) | |
tree | 67a2f59e0a54e725715eb49009f0ee46ed48dcd4 /examples | |
parent | c3b8d7d3ac67a8b71e1eed8645eedfa055cfb87a (diff) |
tiny tweak to allow mutually recursive parsers
Diffstat (limited to 'examples')
-rw-r--r-- | examples/json-parzival.lisp | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/examples/json-parzival.lisp b/examples/json-parzival.lisp index 25dcb54..8937ab9 100644 --- a/examples/json-parzival.lisp +++ b/examples/json-parzival.lisp @@ -9,43 +9,35 @@ (in-package "json-parzival") ;;; Parses and returns JSON's null value -(defvar <json-null< (<<map (lambda (null) :null) - (<<string "null"))) + +(<<def <json-null< (<<and (<<string "null") (<<result :null))) ;;; Parses and returns JSON's boolean values, true => T, false => NIL -(defvar <json-bool< (<<map (lambda (bool) (equal bool "true")) - (<<plus (<<string "true") (<<string "false")))) +(<<def <json-bool< (<<map (lambda (bool) (equal bool "true")) + (<<plus (<<string "true") (<<string "false")))) ;;; Parses a subset of JSON's real number expressions using parzival's built-in ;;; real number parser -(defvar <json-num< <real<) +(<<def <json-num< <real<) ;;; Parses most of the strings that can be represented in JSON, excepting only ;;; those strings that contain an escaped double-quote symbol -(defvar <json-string< - (<<char-brackets #\" - (<<to-string (<<* (<<asat (not (eql it #\"))))) - #\")) - +(<<def <json-string< + (<<char-brackets #\" + (<<to-string (<<* (<<asat (not (eql it #\"))))) + #\")) -;;; For the functions, <json-array< and <json-object<, we defer completing the -;;; definition until after <json-value< has been written. This will raise -;;; warnings during compilation. The reason for this odd behavior is that -;;; <json-array< and <json-object are both defined in terms of <json-value<, -;;; which is itself defined in terms of <json-object< and <json-array<. -(defun <json-array< (stream) (funcall <json-array< stream)) -(defun <json-object< (stream) (funcall <json-object< stream)) ;;; This is the main JSON parser, and will parse any stream containing JSON values. -(defvar <json-value< - (<<or #'<json-object< #'<json-array< <json-string< <json-num< <json-bool< <json-null<)) +(<<def <json-value< + (<<or <json-object< <json-array< <json-string< <json-num< <json-bool< <json-null<)) ;;; A utility parser to match a comma surrounded by whitespace -(defvar <comma-sep< (<<strip (<<char #\,))) +(<<def <comma-sep< (<<strip (<<char #\,))) ;;; A JSON array is a comma separated list of <json-value<'s, surrounded by [ and ]. -(defvar <json-array< +(<<def <json-array< (<<brackets (<<strip (<<char #\[)) (<<sep-by <json-value< <comma-sep<) (<<strip (<<char #\])))) @@ -58,7 +50,7 @@ ;;; retunrs such a pair. Ignoring whitespace, a pair is a string followed by a : ;;; followed by any JSON value. Here, strings in the key position are ;;; transformed into KEYWORDs. The pair is returned as a (cons key value). -(defvar <json-object-pair< +(<<def <json-object-pair< (<<bind (<<brackets <whitespace< <json-string< (<<strip (<<char #\:))) |