[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/parseclj dc0d165b0a 152/185: Merge pull request #23 from c
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/parseclj dc0d165b0a 152/185: Merge pull request #23 from clojure-emacs/read-one |
Date: |
Tue, 28 Dec 2021 14:05:31 -0500 (EST) |
branch: elpa/parseclj
commit dc0d165b0a8633f5b11ed9175a6e421c52f4d314
Merge: 93a0f43529 ff0443c27c
Author: Arne Brasseur <arne.brasseur@gmail.com>
Commit: GitHub <noreply@github.com>
Merge pull request #23 from clojure-emacs/read-one
Add :read-one option
---
parseclj-parser.el | 28 +++++++++++++++++++++++-----
parseclj.el | 4 +++-
test/parseclj-test.el | 17 +++++++++++++++++
3 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/parseclj-parser.el b/parseclj-parser.el
index ef04e16ef7..d0e5d24a21 100644
--- a/parseclj-parser.el
+++ b/parseclj-parser.el
@@ -149,6 +149,18 @@ TOKEN-TYPES are the token types to look for."
(t
(push (pop stack) result)))))))
+(defun parseclj-single-value-p (stack value-p)
+ "Return t if STACK only has a single node for which VALUE-P is true.
+
+This checks if the stack contains a single, fully reduced value, and no
+dangling unmatched tokens. When parsing with `:read-one' this indicates a
+form can be returned."
+ (and (not (cl-reduce (lambda (bool node)
+ (or bool (parseclj-lex-token-p node)))
+ stack
+ :initial-value nil))
+ (parseclj--take-value stack value-p)))
+
(defun parseclj-parser (reduce-leaf reduce-branch &optional options)
"Clojure/EDN stack-based shift-reduce parser.
@@ -186,13 +198,17 @@ functions. Additionally the following options are
recognized
- `:tag-readers'
An association list that describes tag handler functions for any possible
tag. This options in only available in `parseedn-read', for more
- information, please refer to its documentation."
+ information, please refer to its documentation.
+- `:read-one'
+ Return as soon as a single complete value has been read."
(let ((fail-fast (a-get options :fail-fast t))
+ (read-one (a-get options :read-one))
(value-p (a-get options :value-p (lambda (e) (not
(parseclj-lex-token-p e)))))
(stack nil)
(token (parseclj-lex-next)))
- (while (not (eq (parseclj-lex-token-type token) :eof))
+ (while (not (or (and read-one (parseclj-single-value-p stack value-p))
+ (eq (parseclj-lex-token-type token) :eof)))
;; (message "STACK: %S" stack)
;; (message "TOKEN: %S\n" token)
@@ -252,9 +268,11 @@ functions. Additionally the following options are
recognized
(a-get token :pos)
(parseclj-lex-token-type token))))
- (car (funcall reduce-branch nil (parseclj-lex-token :root "" 1)
- (reverse stack)
- options))))
+ (if read-one
+ (car (parseclj--take-value stack value-p))
+ (car (funcall reduce-branch nil (parseclj-lex-token :root "" 1)
+ (reverse stack)
+ options)))))
(provide 'parseclj-parser)
;;; parseclj-parser.el ends here
diff --git a/parseclj.el b/parseclj.el
index 6a4849dd6c..5a0743141e 100644
--- a/parseclj.el
+++ b/parseclj.el
@@ -45,7 +45,9 @@ key-value pairs to specify parsing options.
- `:lexical-preservation' Retain whitespace, comments, and
discards. Defaults to nil.
- `:fail-fast' Raise an error when encountering invalid syntax.
- Defaults to t."
+ Defaults to t.
+- `:read-one'
+ Read a single form. Defaults to false: parse the complete input."
(if (stringp (car string-and-options))
(with-temp-buffer
(insert (car string-and-options))
diff --git a/test/parseclj-test.el b/test/parseclj-test.el
index a25d2e4e3b..a60ad9407a 100644
--- a/test/parseclj-test.el
+++ b/test/parseclj-test.el
@@ -319,6 +319,23 @@
(new-stack (nthcdr (+ (length top-value) (length opening-token))
stack)))
top-value)))
+(ert-deftest parseclj---read-one-test ()
+ (equal (parseclj-parse-clojure "(+ 1 1) foo bar" :read-one t)
+ '((:node-type . :list)
+ (:position . 1)
+ (:children ((:node-type . :symbol)
+ (:position . 2)
+ (:form . "+")
+ (:value . +))
+ ((:node-type . :number)
+ (:position . 4)
+ (:form . "1")
+ (:value . 1))
+ ((:node-type . :number)
+ (:position . 6)
+ (:form . "1")
+ (:value . 1))))))
+
(provide 'parseclj-test)
;;; parseclj-test.el ends here
- [nongnu] elpa/parseclj a71e57df4d 080/185: Rename clj-lex to parseclj-lex, (continued)
- [nongnu] elpa/parseclj a71e57df4d 080/185: Rename clj-lex to parseclj-lex, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 168027fed5 094/185: Merge pull request #7 from lambdaisland/reorganize-package, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 45bd6a7431 109/185: Clean up tests, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 1f8e449897 105/185: Simplify error messages, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj f362018ff1 112/185: Silence the byte-compiler about some unused vars., ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 70804992ee 122/185: Merge pull request #14 from lambdaisland/parseclj-lex-symbol-fix, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 19ca5f5bd0 121/185: Stick to the previous Travis Trusty image, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj b2550e6456 133/185: Add docstring for `parseclj-lex--string-value`, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 13059d8529 138/185: Minor checkdoc fixes, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 49c715ed22 148/185: For prefix-2 elements: pass children to reduce in right order, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj dc0d165b0a 152/185: Merge pull request #23 from clojure-emacs/read-one,
ELPA Syncer <=
- [nongnu] elpa/parseclj 62c9bf9126 177/185: Remove the last remains of a.el, restructure requires, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 517a371616 166/185: Use map-elt instead of parseclj-alist-get, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj ba04dbe334 171/185: Remove remaining a-list call, move a.el to be test-only, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 48abe456c6 155/185: Update the installation instructions, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj b234692020 157/185: error on unmatched closing paren/brace, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 1bb3800f8f 162/185: Update CHANGELOG, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj fcebf65075 180/185: Provide parseclj-alist-merge for older Emacsen, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj eff9411268 161/185: Merge pull request #26 from ikappaki/master, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 689ebddbdd 158/185: Add support for shebang and symbolic values, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 1e46607912 015/185: Split files into packages, More test setup, ELPA Syncer, 2021/12/28