[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/ftable 63fb0ac 04/14: Add checks for parsing
From: |
Stefan Monnier |
Subject: |
[elpa] externals/ftable 63fb0ac 04/14: Add checks for parsing |
Date: |
Mon, 2 Aug 2021 18:27:31 -0400 (EDT) |
branch: externals/ftable
commit 63fb0acc8409888b65b9206a7b0ec5d36ebb29b1
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Add checks for parsing
And removes an assertion.
* ftable.el (ftable-parse-error): New error.
(ftable--parse-to): Add dimension check.
(ftable--check-dimension): New function
(ftable--tokenize-line): Remove assertion.
---
ftable.el | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/ftable.el b/ftable.el
index 57c0beb..84e3a3e 100644
--- a/ftable.el
+++ b/ftable.el
@@ -131,6 +131,8 @@ Examples:
;; 7 h 8 9 n))
;; (insert (ftable-box-char code (cdadr ftable-box-charset-alist))))
+(define-error 'ftable-parse-error "Error parsing table")
+
(cl-defmethod ftable--parse-to ((table-type (eql ftable)) text
&optional box-charset)
"Parse TEXT into a table of TABLE-TYPE.
@@ -166,6 +168,9 @@ It defaults to the first charset."
(setq buffer nil))
(push (ftable--tokenize-line line charset) buffer)))
(setq matrix (reverse matrix))
+ ;; Sanity check.
+ (when (not (ftable--check-dimension matrix))
+ (signal 'ftable-parse-error '("Dimension mismatch")))
;; MATRIX:
;; ((("ab" "c")) (("de" "f") ("gh" "i")))
;;
@@ -183,6 +188,17 @@ It defaults to the first charset."
(ftable--min-column-width matrix)
:matrix matrix)))
+(defun ftable--check-dimension (matrix)
+ "Check that the dimension of MATRIX is correct.
+Correct dimension means each row has the same number of columns.
+Return t if the dimension is correct, nil if not."
+ (let* ((matrix (apply #'append matrix))
+ (first-row-column-count (length (car matrix))))
+ (cl-loop for row in (cdr matrix)
+ if (not (eq first-row-column-count (length row)))
+ return nil
+ finally return t)))
+
(defun ftable--tokenize-line (text-line box-charset)
"Tokenize TEXT-LINE into a list of tokens.
@@ -194,8 +210,6 @@ BOX-CHARSET is the same as in `ftable--parse-to'.
Assumes each line begines with box drawing characters, i.e., no
white space characters."
- (cl-assert (string-prefix-p (ftable-box-char 'v box-charset) text-line)
- t "TEXT-LINE doesn’t begin with box drawing char")
(mapcar #'string-trim
(split-string (string-trim
text-line
- [elpa] branch externals/ftable created (now 96a0475), Stefan Monnier, 2021/08/02
- [elpa] externals/ftable ec019c2 06/14: Add customization, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 2b08dc9 01/14: Init, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 63fb0ac 04/14: Add checks for parsing,
Stefan Monnier <=
- [elpa] externals/ftable 9c13bfc 07/14: Add table.el intergration, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 71dcbd0 12/14: Fix beginning- and end-of-table function, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 939cc1b 13/14: Fix typo, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 1ea20be 11/14: Allow non-table lines before and after table, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 96a0475 14/14: Prepare for ELPA, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable a647832 03/14: xx, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 8ff305b 02/14: Not use forward-paragraph, Stefan Monnier, 2021/08/02
- [elpa] externals/ftable e029b2f 10/14: * ftable.el: Add a workaround., Stefan Monnier, 2021/08/02
- [elpa] externals/ftable d2385e3 05/14: * ftable.el (ftable-misc-test): Fix test., Stefan Monnier, 2021/08/02
- [elpa] externals/ftable 9f75340 08/14: Make ftable work with CJK characters, Stefan Monnier, 2021/08/02