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 | |
parent | c3b8d7d3ac67a8b71e1eed8645eedfa055cfb87a (diff) |
tiny tweak to allow mutually recursive parsers
-rw-r--r-- | examples/json-parzival.lisp | 36 | ||||
-rw-r--r-- | package.lisp | 1 | ||||
-rw-r--r-- | parzival.asd | 2 | ||||
-rw-r--r-- | parzival.lisp | 4 |
4 files changed, 18 insertions, 25 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 #\:))) diff --git a/package.lisp b/package.lisp index 6942708..ed0cee9 100644 --- a/package.lisp +++ b/package.lisp @@ -4,6 +4,7 @@ (:use #:cl #:replay-streams) (:export #:parse + #:<<def #:<<result #:<fail< #:<peek< diff --git a/parzival.asd b/parzival.asd index de329a9..1814260 100644 --- a/parzival.asd +++ b/parzival.asd @@ -4,7 +4,7 @@ :description "Streaming parser language in Common Lisp." :author "Boutade <thegoofist@protonmail.com>" :license "GPLv3" - :version "0.0.2" + :version "0.1.0" :serial t :depends-on (#:replay-streams) :components ((:file "package") diff --git a/parzival.lisp b/parzival.lisp index 9884068..85a6632 100644 --- a/parzival.lisp +++ b/parzival.lisp @@ -43,8 +43,8 @@ (defmacro <<def (name parser &optional docstring) `(progn - (defvar ,name ,parser) - (defun ,name (stream) ,docstring (funcall ,name stream)))) + (defvar ,name ',name) + (defun ,name (stream) ,docstring (funcall ,parser stream)))) ;;; The CORE PARSERS out of which all other parsers are built! A most solemn |