[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/parseclj 185ce6367b 090/185: Add :discard support for :lex
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/parseclj 185ce6367b 090/185: Add :discard support for :lexical-preservation t, and show that it's broken |
Date: |
Tue, 28 Dec 2021 14:05:21 -0500 (EST) |
branch: elpa/parseclj
commit 185ce6367b8bb37d73b6d24103da318f88de8367
Author: Arne Brasseur <arne@arnebrasseur.net>
Commit: Arne Brasseur <arne@arnebrasseur.net>
Add :discard support for :lexical-preservation t, and show that it's broken
Discard nodes were currently not being preserved, this changes handles
reductions of type :discard, but also adds a test case that demonstrates
this
breaks down in the face of whitespace (or other discard nodes).
The reason is that the parser only considers the top two elements on the
stack,
but in the case of :lexical-preservation, there could be several
:whitespace and
:comment nodes that need to be taken into account as well.
We'll have the same problem when dealing with metadata.
---
parseclj-ast.el | 20 +++++++++++---------
test/parseclj-test.el | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 9 deletions(-)
diff --git a/parseclj-ast.el b/parseclj-ast.el
index 88d6654820..2b53cf3ec8 100644
--- a/parseclj-ast.el
+++ b/parseclj-ast.el
@@ -94,15 +94,17 @@ Other ATTRIBUTES can be given as a flat list of key-value
pairs. "
(parseclj-ast-node type pos :children children)
stack)))))
-(defun parseclj-ast--reduce-branch-with-lexical-preservation (&rest args)
- (let* ((stack (apply #'parseclj-ast--reduce-branch args))
- (top (car stack)))
- (if (parseclj-ast-node? top)
- (cons (cl-list* (car top) ;; make sure :node-type remains the first
element in the list
- '(:lexical-preservation . t)
- (cdr top))
- (cdr stack))
- stack)))
+(defun parseclj-ast--reduce-branch-with-lexical-preservation (stack
opening-token children)
+ (if (eq :discard (parseclj-lex-token-type opening-token))
+ (cons (parseclj-ast-node :discard (a-get opening-token :pos) :children
children) stack)
+ (let* ((stack (funcall #'parseclj-ast--reduce-branch stack opening-token
children))
+ (top (car stack)))
+ (if (parseclj-ast-node? top)
+ (cons (cl-list* (car top) ;; make sure :node-type remains the first
element in the list
+ '(:lexical-preservation . t)
+ (cdr top))
+ (cdr stack))
+ stack))))
(provide 'parseclj-ast)
diff --git a/test/parseclj-test.el b/test/parseclj-test.el
index ee9eb51b66..a2773157df 100644
--- a/test/parseclj-test.el
+++ b/test/parseclj-test.el
@@ -144,6 +144,58 @@
;; TODO: uneven map forms
)
+(ert-deftest parseclj-parse-clojure-lexical-preservation ()
+ (should (equal
+ (parseclj-parse-clojure "#_ (1 2 3) true")
+ '((:node-type . :root) (:position . 1) (:children ((:node-type .
:true) (:position . 12) (:form . "true") (:value . t))))))
+ (should (equal
+ (parseclj-parse-clojure "#_(1 2 3) true" :lexical-preservation t)
+ '((:node-type . :root)
+ (:lexical-preservation . t)
+ (:position . 1)
+ (:children ((:node-type . :discard)
+ (:position . 1)
+ (:children ((:node-type . :list)
+ (:lexical-preservation . t)
+ (:position . 3)
+ (:children ((:node-type . :number)
(:position . 4) (:form . "1") (:value . 1))
+ ((:node-type . :whitespace)
(:position . 5) (:form . " "))
+ ((:node-type . :number)
(:position . 6) (:form . "2") (:value . 2))
+ ((:node-type . :whitespace)
(:position . 7) (:form . " "))
+ ((:node-type . :number)
(:position . 8) (:form . "3") (:value . 3))))))
+ ((:node-type . :whitespace)
+ (:position . 10)
+ (:form . " "))
+ ((:node-type . :true)
+ (:position . 11)
+ (:form . "true")
+ (:value . t))))))
+
+ (should (equal
+ (parseclj-parse-clojure "#_ (1 2 3) true" :lexical-preservation t)
+ '((:node-type . :root)
+ (:lexical-preservation . t)
+ (:position . 1)
+ (:children ((:node-type . :discard)
+ (:position . 1)
+ (:children
+ ((:node-type . :whitespace) (:position . 3) (:form .
" "))
+ ((:node-type . :list)
+ (:lexical-preservation . t)
+ (:position . 4)
+ (:children ((:node-type . :number) (:position . 5)
(:form . "1") (:value . 1))
+ ((:node-type . :whitespace) (:position .
6) (:form . " "))
+ ((:node-type . :number) (:position . 7)
(:form . "2") (:value . 2))
+ ((:node-type . :whitespace) (:position .
8) (:form . " "))
+ ((:node-type . :number) (:position . 9)
(:form . "3") (:value . 3))))))
+ ((:node-type . :whitespace)
+ (:position . 11)
+ (:form . " "))
+ ((:node-type . :true)
+ (:position . 12)
+ (:form . "true")
+ (:value . t)))))))
+
(provide 'parseclj-test)
;;; parseclj-test.el ends here
- [nongnu] elpa/parseclj 212e0dc42b 063/185: silly typo, (continued)
- [nongnu] elpa/parseclj 212e0dc42b 063/185: silly typo, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 96b8180987 087/185: Unparse ASTs that have lexical preservation., ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj da0c877940 069/185: Remove dash, using seq is good enough, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 6ae14f26ce 070/185: Work on tests and EDN printer + other things, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 496a7356bb 064/185: Add edn.el as a dependency, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 87953e44ba 092/185: Parse options on to the reducers, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 496c965edc 074/185: Make t print as true - thanks @martinklepsch, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 1cf0fb9d3f 065/185: More build related fixes, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 0644bcdbf4 085/185: Implement :fail-fast, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 6d40b39cec 082/185: Rename clj-edn to parseedn, keep it in this package for now., ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 185ce6367b 090/185: Add :discard support for :lexical-preservation t, and show that it's broken,
ELPA Syncer <=
- [nongnu] elpa/parseclj 1be462e0f2 108/185: Move parser to its own module, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj f86a3be4bf 107/185: Add last rewordings, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 388bb2bde2 115/185: Fix test case for `\u` and `\o` characters, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj d3cb78544d 106/185: Use Emacs Lisp predicate style convention, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj aeac6a1755 118/185: Fix code's organization for 2-item stack reduction, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj bad1fb8745 119/185: Merge pull request #12 from lambdaisland/nested-2-items-reduction, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj b377e12d7e 125/185: Add test case for invalid input error token, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 903d60284e 130/185: Update the Travis CI badge, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj d691df5d63 131/185: Update the copyright years, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj acf4a29778 132/185: Update the README, ELPA Syncer, 2021/12/28