Skip to content

Commit ece9648

Browse files
authored
Merge pull request #33 from clojure-emacs/arne/remove-a-from-tests
Remove the last remains of a.el, restructure requires
2 parents 1c8f833 + c7f50e3 commit ece9648

File tree

9 files changed

+108
-60
lines changed

9 files changed

+108
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Unreleased
22

3-
## 1.0.2 (2021-09-28)
3+
## 1.0.3 (2021-09-29)
44

55
- Remove remaining a.el usage (this time for real)
66

Cask

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
"parseclj-parser.el")
99

1010
(development
11-
(depends-on "a")
1211
(depends-on "ert-runner"))

parseclj-alist.el

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
;;; parseclj-alist.el --- Clojure/EDN parser -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2017-2021 Arne Brasseur
4+
5+
;; Author: Arne Brasseur <[email protected]>
6+
7+
;; This file is not part of GNU Emacs.
8+
9+
;; This file is free software; you can redistribute it and/or modify
10+
;; it under the terms of the GNU General Public License as published by
11+
;; the Free Software Foundation; either version 3, or (at your option)
12+
;; any later version.
13+
14+
;; This file is distributed in the hope that it will be useful,
15+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
;; GNU General Public License for more details.
18+
19+
;; You should have received a copy of the GNU General Public License
20+
;; along with GNU Emacs; see the file COPYING. If not, write to
21+
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
;; Boston, MA 02110-1301, USA.
23+
24+
;;; Commentary:
25+
26+
;; A shift/reduce parser for Clojure source.
27+
28+
;;; Code:
29+
30+
(defun parseclj-alist (&rest kvs)
31+
"Create an association list from the given keys and values KVS.
32+
Arguments are simply provided in sequence, rather than as lists or cons cells.
33+
For example: (parseclj-alist :foo 123 :bar 456)"
34+
;; Emacs 27:
35+
;; (map-into kvs 'alist)
36+
(mapcar (lambda (kv) (cons (car kv) (cadr kv))) (seq-partition kvs 2)))
37+
38+
(defun parseclj-alist-assoc (coll k v)
39+
"Associate a key K with a value V in the association list COLL
40+
41+
Returns a new alist (does not mutate its argument). If an entry
42+
with the same key is present it will be replaced, otherwise the
43+
new kv-pair is added to the head of the list."
44+
(if (map-contains-key coll k)
45+
(mapcar (lambda (entry)
46+
(if (equal (car entry) k)
47+
(cons k v)
48+
entry))
49+
coll)
50+
(cons (cons k v) coll)))
51+
52+
(defun parseclj-alist-update (coll key fn &rest args)
53+
"In collection COLL, at location KEY, apply FN with extra args ARGS.
54+
'Updates' a value in an associative collection COLL, where KEY is
55+
a key and FN is a function that will take the old value and any
56+
supplied args and return the new value, and returns a new
57+
structure. If the key does not exist, nil is passed as the old
58+
value."
59+
(parseclj-alist-assoc coll
60+
key
61+
(apply #'funcall fn (map-elt coll key) args)))
62+
63+
(defun parseclj-hash-table (&rest kvs)
64+
"Create a hash table from the given keys and values KVS.
65+
Arguments are simply provided in sequence, rather than as lists
66+
or cons cells. As \"test\" for the hash table, equal is used. The
67+
hash table is created without extra storage space, so with a size
68+
equal to amount of key-value pairs, since it is assumed to be
69+
treated as immutable.
70+
For example: (parseclj-hash-table :foo 123 :bar 456)"
71+
;; Emacs 27:
72+
;; (map-into kvs 'hash-table)
73+
(let* ((kv-pairs (seq-partition kvs 2))
74+
(hash-map (make-hash-table :test 'equal :size (length kv-pairs))))
75+
(seq-do (lambda (pair)
76+
(puthash (car pair) (cadr pair) hash-map))
77+
kv-pairs)
78+
hash-map))
79+
80+
(provide 'parseclj-alist)
81+
82+
;;; parseclj-alist.el ends here

parseclj-ast.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
(require 'seq)
3131
(require 'subr-x)
3232
(require 'parseclj-lex)
33+
(require 'parseclj-alist)
3334

3435
;; AST helper functions
3536

parseclj-lex.el

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
;;; Code:
2929

30+
(require 'parseclj-alist)
31+
3032
(defvar parseclj-lex--leaf-tokens '(:whitespace
3133
:comment
3234
:symbolic-value

parseclj-parser.el

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
;; Copyright (C) 2017-2021 Arne Brasseur
44

55
;; Author: Arne Brasseur <[email protected]>
6-
;; Keywords: lisp
7-
;; Package-Requires: ((emacs "25"))
8-
;; Version: 0.2.0
96

107
;; This file is not part of GNU Emacs.
118

@@ -33,6 +30,7 @@
3330
(require 'cl-lib)
3431
(require 'subr-x)
3532
(require 'parseclj-lex)
33+
(require 'parseclj-alist)
3634

3735
(define-error 'parseclj-parser-error "parseclj: Syntax error")
3836

parseclj.el

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
;; Author: Arne Brasseur <[email protected]>
66
;; Keywords: lisp clojure edn parser
77
;; Package-Requires: ((emacs "25"))
8-
;; Version: 1.0.2
8+
;; Version: 1.0.3
99

1010
;; This file is not part of GNU Emacs.
1111

@@ -33,58 +33,9 @@
3333
(require 'map)
3434
(require 'seq)
3535

36-
(defun parseclj-alist (&rest kvs)
37-
"Create an association list from the given keys and values KVS.
38-
Arguments are simply provided in sequence, rather than as lists or cons cells.
39-
For example: (parseclj-alist :foo 123 :bar 456)"
40-
;; Emacs 27:
41-
;; (map-into kvs 'alist)
42-
(mapcar (lambda (kv) (cons (car kv) (cadr kv))) (seq-partition kvs 2)))
43-
4436
(require 'parseclj-parser)
4537
(require 'parseclj-ast)
46-
47-
(defun parseclj-hash-table (&rest kvs)
48-
"Create a hash table from the given keys and values KVS.
49-
Arguments are simply provided in sequence, rather than as lists
50-
or cons cells. As \"test\" for the hash table, equal is used. The
51-
hash table is created without extra storage space, so with a size
52-
equal to amount of key-value pairs, since it is assumed to be
53-
treated as immutable.
54-
For example: (parseclj-hash-table :foo 123 :bar 456)"
55-
;; Emacs 27:
56-
;; (map-into kvs 'hash-table)
57-
(let* ((kv-pairs (seq-partition kvs 2))
58-
(hash-map (make-hash-table :test 'equal :size (length kv-pairs))))
59-
(seq-do (lambda (pair)
60-
(puthash (car pair) (cadr pair) hash-map))
61-
kv-pairs)
62-
hash-map))
63-
64-
(defun parseclj-alist-assoc (coll k v)
65-
"Associate a key K with a value V in the association list COLL
66-
67-
Returns a new alist (does not mutate its argument). If an entry
68-
with the same key is present it will be replaced, otherwise the
69-
new kv-pair is added to the head of the list."
70-
(if (map-contains-key coll k)
71-
(mapcar (lambda (entry)
72-
(if (equal (car entry) k)
73-
(cons k v)
74-
entry))
75-
coll)
76-
(cons (cons k v) coll)))
77-
78-
(defun parseclj-alist-update (coll key fn &rest args)
79-
"In collection COLL, at location KEY, apply FN with extra args ARGS.
80-
'Updates' a value in an associative collection COLL, where KEY is
81-
a key and FN is a function that will take the old value and any
82-
supplied args and return the new value, and returns a new
83-
structure. If the key does not exist, nil is passed as the old
84-
value."
85-
(parseclj-alist-assoc coll
86-
key
87-
(apply #'funcall fn (map-elt coll key) args)))
38+
(require 'parseclj-alist)
8839

8940
(defun parseclj-parse-clojure (&rest string-and-options)
9041
"Parse Clojure source to AST.

test/parseclj-ast-test.el

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
;;; Code
2929

30-
(require 'a)
3130
(require 'ert)
3231
(require 'parseclj-ast)
3332

@@ -46,7 +45,7 @@
4645
(with-temp-buffer
4746
(insert ,(map-elt data :source))
4847
(goto-char 1)
49-
(should (a-equal (parseclj-parse-clojure) ',(map-elt data :ast)))))))))
48+
(should (equal (parseclj-parse-clojure) ',(map-elt data :ast)))))))))
5049
parseclj-test-data)))
5150

5251
(defmacro define-parseclj-ast-roundtrip-tests ()
@@ -59,7 +58,10 @@
5958
(let ((test-name (intern (concat "parseclj-ast-rountrip:" name))))
6059
`(ert-deftest ,test-name ()
6160
:tags '(parseclj-ast-rountrip)
62-
(should (a-equal (parseclj-parse-clojure (parseclj-unparse-clojure-to-string ',(map-elt data :ast))) ',(map-elt data :ast))))))))
61+
(should (equal (parseclj-parse-clojure (parseclj-unparse-clojure-to-string
62+
',(map-elt data :ast)))
63+
',(or (map-elt data :roundtrip-ast)
64+
(map-elt data :ast)))))))))
6365
parseclj-test-data)))
6466

6567
(define-parseclj-ast-roundtrip-tests)

test/parseclj-test-data.el

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,20 @@
272272
((:node-type . :number)
273273
(:position . 10)
274274
(:form . "12")
275-
(:value . 12)))))))))
275+
(:value . 12))))))))
276+
;; After round-tripping the position of the "12" is no longer the same
277+
:roundtrip-ast '((:node-type . :root)
278+
(:position . 1)
279+
(:children . (((:node-type . :list)
280+
(:position . 1)
281+
(:children . (((:node-type . :number)
282+
(:position . 2)
283+
(:form . "10")
284+
(:value . 10))
285+
((:node-type . :number)
286+
(:position . 5)
287+
(:form . "12")
288+
(:value . 12)))))))))
276289

277290

278291
"tag-1"

0 commit comments

Comments
 (0)