emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/parseclj 517a371616 166/185: Use map-elt instead of parsec


From: ELPA Syncer
Subject: [nongnu] elpa/parseclj 517a371616 166/185: Use map-elt instead of parseclj-alist-get
Date: Tue, 28 Dec 2021 14:05:33 -0500 (EST)

branch: elpa/parseclj
commit 517a3716160546aa55d037f2055296e77f92f1b4
Author: Arne Brasseur <arne@arnebrasseur.net>
Commit: Bozhidar Batsov <bozhidar@batsov.dev>

    Use map-elt instead of parseclj-alist-get
---
 parseclj-ast.el           | 32 ++++++++++++++++----------------
 parseclj-parser.el        | 18 +++++++++---------
 parseclj.el               | 19 +++++--------------
 test/parseclj-ast-test.el | 10 +++++-----
 4 files changed, 35 insertions(+), 44 deletions(-)

diff --git a/parseclj-ast.el b/parseclj-ast.el
index 3070b74750..578901b2aa 100644
--- a/parseclj-ast.el
+++ b/parseclj-ast.el
@@ -47,19 +47,19 @@ Other ATTRIBUTES can be given as a flat list of key-value 
pairs."
 
 (defun parseclj-ast-node-attr (node attr)
   "Return NODE's ATTR, or nil."
-  (parseclj-alist-get node attr))
+  (map-elt node attr))
 
 (defun parseclj-ast-node-type (node)
   "Return the type of the AST node NODE."
-  (parseclj-alist-get node :node-type))
+  (map-elt node :node-type))
 
 (defun parseclj-ast-children (node)
   "Return children for the AST NODE."
-  (parseclj-alist-get node :children))
+  (map-elt node :children))
 
 (defun parseclj-ast-value (node)
   "Return the value of NODE as another AST node."
-  (parseclj-alist-get node :value))
+  (map-elt node :value))
 
 (defun parseclj-ast-leaf-node-p (node)
   "Return t if the given ast NODE is a leaf node."
@@ -82,8 +82,8 @@ on available options."
       stack
     (cons
      (parseclj-ast-node (parseclj-lex-token-type token)
-                        (parseclj-alist-get token :pos)
-                        :form (parseclj-alist-get token :form)
+                        (map-elt token :pos)
+                        :form (map-elt token :form)
                         :value (parseclj-lex--leaf-token-value token))
      stack)))
 
@@ -100,12 +100,12 @@ on available options."
         (top (car stack)))
     (if (member token-type '(:whitespace :comment))
         ;; merge consecutive whitespace or comment tokens
-        (if (eq token-type (parseclj-alist-get top :node-type))
-            (cons (parseclj-alist-update top :form #'concat 
(parseclj-alist-get token :form))
+        (if (eq token-type (map-elt top :node-type))
+            (cons (parseclj-alist-update top :form #'concat (map-elt token 
:form))
                   (cdr stack))
           (cons (parseclj-ast-node (parseclj-lex-token-type token)
-                                   (parseclj-alist-get token :pos)
-                                   :form (parseclj-alist-get token :form))
+                                   (map-elt token :pos)
+                                   :form (map-elt token :form))
                 stack))
       (parseclj-ast--reduce-leaf stack token options))))
 
@@ -118,7 +118,7 @@ brace.
 CHILDREN is the collection of nodes to be reduced into the AST branch node.
 OPTIONS is an association list.  See `parseclj-parse' for more information
 on available options."
-  (let* ((pos (parseclj-alist-get opening-token :pos))
+  (let* ((pos (map-elt opening-token :pos))
          (type (parseclj-lex-token-type opening-token))
          (type (cl-case type
                  (:lparen :list)
@@ -130,7 +130,7 @@ on available options."
       (:discard stack)
       (:tag (cons (parseclj-ast-node :tag
                                      pos
-                                     :tag (intern (substring 
(parseclj-alist-get opening-token :form) 1))
+                                     :tag (intern (substring (map-elt 
opening-token :form) 1))
                                      :children children)
                   stack))
       (:metadata (cons (parseclj-ast-node :with-meta
@@ -157,7 +157,7 @@ node.
 OPTIONS is an association list.  See `parseclj-parse' for more information
 on available options."
   (if (eq :discard (parseclj-lex-token-type opening-token))
-      (cons (parseclj-ast-node :discard (parseclj-alist-get opening-token 
:pos) :children children) stack)
+      (cons (parseclj-ast-node :discard (map-elt opening-token :pos) :children 
children) stack)
     (let* ((stack (funcall #'parseclj-ast--reduce-branch stack opening-token 
children options))
            (top (car stack)))
       (if (parseclj-ast-node-p top)
@@ -187,7 +187,7 @@ on available options."
       (when-let (node (car nodes))
         (parseclj-unparse-clojure node))
       (seq-doseq (child (cdr nodes))
-        (when (not (parseclj-alist-get node :lexical-preservation))
+        (when (not (map-elt node :lexical-preservation))
           (insert " "))
         (parseclj-unparse-clojure child)))
     (insert (cdr delimiters))))
@@ -196,9 +196,9 @@ on available options."
   "Insert a string representation of the given AST tag NODE into buffer."
   (progn
     (insert "#")
-    (insert (symbol-name (parseclj-alist-get node :tag)))
+    (insert (symbol-name (map-elt node :tag)))
     (insert " ")
-    (parseclj-unparse-clojure (car (parseclj-alist-get node :children)))))
+    (parseclj-unparse-clojure (car (map-elt node :children)))))
 
 (provide 'parseclj-ast)
 
diff --git a/parseclj-parser.el b/parseclj-parser.el
index 9f078c6a8f..6af6d6407c 100644
--- a/parseclj-parser.el
+++ b/parseclj-parser.el
@@ -71,12 +71,12 @@ OPTIONS is an association list.  This list is also passed 
down to the
 REDUCE-BRANCH function.  See `parseclj-parser' for more information on
 available options."
   (let ((opening-token-type (parseclj--find-opening-token stack closing-token))
-        (fail-fast (parseclj-alist-get options :fail-fast t))
+        (fail-fast (map-elt options :fail-fast t))
         (collection nil))
     (if (not opening-token-type)
         (if fail-fast
             (parseclj--error "At position %s, unmatched %S"
-                             (parseclj-alist-get closing-token :pos)
+                             (map-elt closing-token :pos)
                              (parseclj-lex-token-type closing-token))
 
           stack)
@@ -93,7 +93,7 @@ available options."
                 ;; any unreduced tokens left: bail early
                 (when-let ((token (seq-find #'parseclj-lex-token-p 
collection)))
                   (parseclj--error "At position %s, unmatched %S"
-                                   (parseclj-alist-get token :pos)
+                                   (map-elt token :pos)
                                    (parseclj-lex-token-type token))))
 
               ;; all good, call the reducer so it can return an updated stack 
with a
@@ -105,7 +105,7 @@ available options."
           ;; or return the original stack and continue parsing
           (if fail-fast
               (parseclj--error "At position %s, unmatched %S"
-                               (parseclj-alist-get closing-token :pos)
+                               (map-elt closing-token :pos)
                                (parseclj-lex-token-type closing-token))
 
             (reverse collection)))))))
@@ -209,9 +209,9 @@ functions. Additionally the following options are recognized
   information, please refer to its documentation.
 - `:read-one'
   Return as soon as a single complete value has been read."
-  (let ((fail-fast (parseclj-alist-get options :fail-fast t))
-        (read-one (parseclj-alist-get options :read-one))
-        (value-p (parseclj-alist-get options :value-p (lambda (e) (not 
(parseclj-lex-token-p e)))))
+  (let ((fail-fast (map-elt options :fail-fast t))
+        (read-one (map-elt options :read-one))
+        (value-p (map-elt options :value-p (lambda (e) (not 
(parseclj-lex-token-p e)))))
         (stack nil)
         (token (parseclj-lex-next)))
 
@@ -222,7 +222,7 @@ functions. Additionally the following options are recognized
 
       (when (and fail-fast (parseclj-lex-error-p token))
         (parseclj--error "Invalid token at %s: %S"
-                         (parseclj-alist-get token :pos)
+                         (map-elt token :pos)
                          (parseclj-lex-token-form token)))
 
       ;; Reduce based on the top item on the stack (collections)
@@ -273,7 +273,7 @@ functions. Additionally the following options are recognized
     (when fail-fast
       (when-let ((token (seq-find #'parseclj-lex-token-p stack)))
         (parseclj--error "At position %s, unmatched %S"
-                         (parseclj-alist-get token :pos)
+                         (map-elt token :pos)
                          (parseclj-lex-token-type token))))
 
     (if read-one
diff --git a/parseclj.el b/parseclj.el
index 0cd293f366..3896c6b9ba 100644
--- a/parseclj.el
+++ b/parseclj.el
@@ -30,6 +30,7 @@
 
 ;;; Code:
 
+(require 'map)
 (require 'parseclj-parser)
 (require 'parseclj-ast)
 
@@ -54,19 +55,9 @@ For example: (a-hash-table :foo 123 :bar 456)"
             kv-pairs)
     hash-map))
 
-(defun parseclj-alist-get (map key &optional not-found)
-  "Like alist-get, but uses equal instead of eq to look up in map MAP key KEY.
-Returns NOT-FOUND if the key is not present, or `nil' if
-NOT-FOUND is not specified."
-  (cl-block nil
-    (seq-doseq (pair map)
-      (when (equal (car pair) key)
-        (cl-return (cdr pair))))
-    not-found))
-
 (defun parseclj-alist-has-key? (coll k)
   "Check if the given association list COLL has a certain key K."
-  (not (eq (parseclj-alist-get coll k :not-found) :not-found)))
+  (not (eq (map-elt coll k :not-found) :not-found)))
 
 (defun parseclj-alist-assoc (coll k v)
   (if (parseclj-alist-has-key? coll k)
@@ -86,7 +77,7 @@ structure. If the key does not exist, nil is passed as the old
 value."
   (parseclj-alist-assoc coll
                         key
-                        (apply #'funcall fn (parseclj-alist-get coll key) 
args)))
+                        (apply #'funcall fn (map-elt coll key) args)))
 
 (defun parseclj-parse-clojure (&rest string-and-options)
   "Parse Clojure source to AST.
@@ -112,7 +103,7 @@ key-value pairs to specify parsing options.
                       (and (parseclj-ast-node-p e)
                            (not (member (parseclj-ast-node-type e) 
'(:whitespace :comment :discard))))))
            (options (apply 'parseclj-alist :value-p value-p 
string-and-options))
-           (lexical? (parseclj-alist-get options :lexical-preservation)))
+           (lexical? (map-elt options :lexical-preservation)))
       (parseclj-parser (if lexical?
                            
#'parseclj-ast--reduce-leaf-with-lexical-preservation
                          #'parseclj-ast--reduce-leaf)
@@ -128,7 +119,7 @@ Given an abstract syntax tree AST (as returned by
 `parseclj-parse-clojure'), turn it back into source code, and
 insert it into the current buffer."
   (if (parseclj-ast-leaf-node-p ast)
-      (insert (parseclj-alist-get ast :form))
+      (insert (map-elt ast :form))
     (if (eql (parseclj-ast-node-type ast) :tag)
         (parseclj-ast--unparse-tag ast)
       (parseclj-ast--unparse-collection ast))))
diff --git a/test/parseclj-ast-test.el b/test/parseclj-ast-test.el
index 72d6674b63..5707500359 100644
--- a/test/parseclj-ast-test.el
+++ b/test/parseclj-ast-test.el
@@ -38,14 +38,14 @@
         (lambda (pair)
           (let ((name (car pair))
                 (data (cdr pair)))
-            (if (and (parseclj-alist-get data :source) (parseclj-alist-get 
data :ast))
+            (if (and (map-elt data :source) (map-elt data :ast))
                 (let ((test-name (intern (concat "parseclj-parse-clojure:" 
name))))
                   `(ert-deftest ,test-name ()
                      :tags '(parseclj-ast)
                      (with-temp-buffer
-                       (insert ,(parseclj-alist-get data :source))
+                       (insert ,(map-elt data :source))
                        (goto-char 1)
-                       (should (a-equal (parseclj-parse-clojure) 
',(parseclj-alist-get data :ast)))))))))
+                       (should (a-equal (parseclj-parse-clojure) ',(map-elt 
data :ast)))))))))
         parseclj-test-data)))
 
 (defmacro define-parseclj-ast-roundtrip-tests ()
@@ -54,11 +54,11 @@
         (lambda (pair)
           (let ((name (car pair))
                 (data (cdr pair)))
-            (if (and (parseclj-alist-get data :ast) (parseclj-alist-get data 
:source))
+            (if (and (map-elt data :ast) (map-elt data :source))
                 (let ((test-name (intern (concat "parseclj-ast-rountrip:" 
name))))
                   `(ert-deftest ,test-name ()
                      :tags '(parseclj-ast-rountrip)
-                     (should (a-equal (parseclj-parse-clojure 
(parseclj-unparse-clojure-to-string ',(parseclj-alist-get data :ast))) 
',(parseclj-alist-get data :ast))))))))
+                     (should (a-equal (parseclj-parse-clojure 
(parseclj-unparse-clojure-to-string ',(map-elt data :ast))) ',(map-elt data 
:ast))))))))
         parseclj-test-data)))
 
 (define-parseclj-ast-roundtrip-tests)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]