[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)
- [nongnu] elpa/parseclj 45bd6a7431 109/185: Clean up tests, (continued)
- [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, 2021/12/28
- [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 <=
- [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
- [nongnu] elpa/parseclj a9dba19760 086/185: Clean up node and token types, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 9480ae09b3 068/185: push is destructive, in this case cons will do, ELPA Syncer, 2021/12/28