[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/xr 0d4e7d8 3/5: Refactor: add local and reformat xr--pa
From: |
Mattias Engdegård |
Subject: |
[elpa] externals/xr 0d4e7d8 3/5: Refactor: add local and reformat xr--parse-seq |
Date: |
Thu, 30 Jan 2020 10:53:28 -0500 (EST) |
branch: externals/xr
commit 0d4e7d8f493b173b68153c106760d980d47bcff3
Author: Mattias Engdegård <address@hidden>
Commit: Mattias Engdegård <address@hidden>
Refactor: add local and reformat xr--parse-seq
This only adds the (yet unused) local _item-start and reindents the
code in scope, to make future changes easier to read.
---
xr.el | 445 +++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 224 insertions(+), 221 deletions(-)
diff --git a/xr.el b/xr.el
index 98ffc69..4802b76 100644
--- a/xr.el
+++ b/xr.el
@@ -467,231 +467,234 @@ UPPER may be nil, meaning infinity."
(defun xr--parse-seq (warnings)
(let ((sequence nil)) ; reversed
(while (not (looking-at (rx (or "\\|" "\\)" eos))))
- (cond
- ;; ^ - only special at beginning of sequence
- ((looking-at (rx "^"))
- (forward-char 1)
- (if (null sequence)
- (push 'bol sequence)
- (xr--report warnings (match-beginning 0) "Unescaped literal `^'")
- (push "^" sequence)))
-
- ;; $ - only special at end of sequence
- ((looking-at (rx "$"))
- (forward-char 1)
- (if (looking-at (rx (or "\\|" "\\)" eos)))
- (push 'eol sequence)
- (xr--report warnings (match-beginning 0) "Unescaped literal `$'")
- (push "$" sequence)))
-
- ;; * ? + (and non-greedy variants)
- ;; - not special at beginning of sequence or after ^
- ((looking-at (rx (group (any "*?+")) (opt "?")))
- (if (and sequence
- (not (and (eq (car sequence) 'bol) (eq (preceding-char) ?^))))
- (let ((operator (match-string 0))
- (operand (car sequence)))
- (when warnings
- (cond
- ;; (* (* X)), for any repetitions *
- ((and (consp operand)
- (memq (car operand)
- '(opt zero-or-more one-or-more +? *? ??)))
- (xr--report warnings (match-beginning 0)
- "Repetition of repetition"))
- ;; (* (group (* X))), for any repetitions *
- ((and (consp operand)
- (eq (car operand) 'group)
- (null (cddr operand))
- (let ((inner (cadr operand)))
- (and (consp inner)
- (memq (car inner)
- '(opt zero-or-more one-or-more +? *? ??))
- ;; Except (? (group (+ X))), since that may
- ;; be legitimate.
- (not (and (equal operator "?")
- (memq (car inner)
- '(one-or-more +?)))))))
- (xr--report warnings (match-beginning 0)
- "Repetition of repetition"))
- ((memq operand xr--zero-width-assertions)
- (xr--report warnings (match-beginning 0)
- "Repetition of zero-width assertion"))
- ((and (xr--matches-empty-p operand)
- ;; Rejecting repetition of the empty string
- ;; suppresses some false positives.
- (not (equal operand "")))
- (xr--report
- warnings (match-beginning 0)
- "Repetition of expression matching an empty string"))))
- (goto-char (match-end 0))
- (setq sequence (cons (xr--postfix operator operand)
- (cdr sequence))))
- (let ((literal (match-string 1)))
- (goto-char (match-end 1))
- (xr--report warnings (match-beginning 0)
- (format "Unescaped literal `%s'" literal))
- (push literal sequence))))
-
- ;; \{..\} - not special at beginning of sequence or after ^
- ((and (looking-at (rx "\\{"))
- sequence
- (not (and (eq (car sequence) 'bol) (eq (preceding-char) ?^))))
- (forward-char 2)
- (let ((operand (car sequence)))
- (when warnings
- (cond
- ;; (** N M (* X)), for any repetition *
- ((and (consp operand)
- (memq (car operand)
- '(opt zero-or-more one-or-more +? *? ??)))
- (xr--report warnings (match-beginning 0)
- "Repetition of repetition"))
- ;; (** N M (group (* X))), for any repetition *
- ((and (consp operand)
- (eq (car operand) 'group)
- (null (cddr operand))
- (let ((inner (cadr operand)))
- (and (consp inner)
- (memq (car inner)
- '(opt zero-or-more one-or-more +? *? ??)))))
- (xr--report warnings (match-beginning 0)
- "Repetition of repetition"))
- ((memq operand xr--zero-width-assertions)
- (xr--report warnings (match-beginning 0)
- "Repetition of zero-width assertion"))
- ((and (xr--matches-empty-p operand)
- ;; Rejecting repetition of the empty string
- ;; suppresses some false positives.
- (not (equal operand "")))
- (xr--report
- warnings (match-beginning 0)
- "Repetition of expression matching an empty string"))))
- (if (looking-at (rx (opt (group (one-or-more digit)))
- (opt (group ",")
- (opt (group (one-or-more digit))))
- "\\}"))
- (let ((lower (if (match-string 1)
- (string-to-number (match-string 1))
- 0))
- (comma (match-string 2))
- (upper (and (match-string 3)
- (string-to-number (match-string 3)))))
- (unless (or (match-beginning 1) (match-string 3))
- (xr--report warnings (- (match-beginning 0) 2)
- (if comma
- "Uncounted repetition"
- "Implicit zero repetition")))
+ (let ((_item-start (point)))
+ (cond
+ ;; ^ - only special at beginning of sequence
+ ((looking-at (rx "^"))
+ (forward-char 1)
+ (if (null sequence)
+ (push 'bol sequence)
+ (xr--report warnings (match-beginning 0) "Unescaped literal `^'")
+ (push "^" sequence)))
+
+ ;; $ - only special at end of sequence
+ ((looking-at (rx "$"))
+ (forward-char 1)
+ (if (looking-at (rx (or "\\|" "\\)" eos)))
+ (push 'eol sequence)
+ (xr--report warnings (match-beginning 0) "Unescaped literal `$'")
+ (push "$" sequence)))
+
+ ;; * ? + (and non-greedy variants)
+ ;; - not special at beginning of sequence or after ^
+ ((looking-at (rx (group (any "*?+")) (opt "?")))
+ (if (and sequence
+ (not (and (eq (car sequence) 'bol)
+ (eq (preceding-char) ?^))))
+ (let ((operator (match-string 0))
+ (operand (car sequence)))
+ (when warnings
+ (cond
+ ;; (* (* X)), for any repetitions *
+ ((and (consp operand)
+ (memq (car operand)
+ '(opt zero-or-more one-or-more +? *? ??)))
+ (xr--report warnings (match-beginning 0)
+ "Repetition of repetition"))
+ ;; (* (group (* X))), for any repetitions *
+ ((and (consp operand)
+ (eq (car operand) 'group)
+ (null (cddr operand))
+ (let ((inner (cadr operand)))
+ (and (consp inner)
+ (memq (car inner)
+ '(opt zero-or-more one-or-more +? *? ??))
+ ;; Except (? (group (+ X))), since that may
+ ;; be legitimate.
+ (not (and (equal operator "?")
+ (memq (car inner)
+ '(one-or-more +?)))))))
+ (xr--report warnings (match-beginning 0)
+ "Repetition of repetition"))
+ ((memq operand xr--zero-width-assertions)
+ (xr--report warnings (match-beginning 0)
+ "Repetition of zero-width assertion"))
+ ((and (xr--matches-empty-p operand)
+ ;; Rejecting repetition of the empty string
+ ;; suppresses some false positives.
+ (not (equal operand "")))
+ (xr--report
+ warnings (match-beginning 0)
+ "Repetition of expression matching an empty string"))))
(goto-char (match-end 0))
- (setq sequence (cons (xr--repeat lower
- (if comma upper lower)
- operand)
+ (setq sequence (cons (xr--postfix operator operand)
(cdr sequence))))
- (error "Invalid \\{\\} syntax"))))
-
- ;; nonspecial character
- ((looking-at (rx (not (any "\\.["))))
- (forward-char 1)
- (push (match-string 0) sequence))
-
- ;; character alternative
- ((looking-at (rx "[" (opt (group "^"))))
- (goto-char (match-end 0))
- (let ((negated (match-string 1)))
- (push (xr--parse-char-alt negated warnings) sequence)))
-
- ;; group
- ((looking-at (rx "\\(" (opt (group "?")
- (opt (opt (group (any "1-9")
- (zero-or-more digit)))
- (group ":")))))
- (let ((question (match-string 1))
- (number (match-string 2))
- (colon (match-string 3)))
- (when (and question (not colon))
- (error "Invalid \\(? syntax"))
- (goto-char (match-end 0))
- (let* ((group (xr--parse-alt warnings))
- ;; simplify - group has an implicit seq
- (operand (if (and (listp group) (eq (car group) 'seq))
- (cdr group)
- (list group))))
- (when (not (looking-at (rx "\\)")))
- (error "Missing \\)"))
- (forward-char 2)
- (let ((item (cond (number ; numbered group
- (append (list 'group-n (string-to-number
number))
- operand))
- (question ; shy group
- group)
- (t ; plain group
- (cons 'group operand)))))
- (push item sequence)))))
-
- ;; back-reference
- ((looking-at (rx "\\" (group (any "1-9"))))
- (forward-char 2)
- (push (list 'backref (string-to-number (match-string 1)))
- sequence))
-
- ;; various simple substitutions
- ((looking-at (rx (or "." "\\w" "\\W" "\\`" "\\'" "\\="
- "\\b" "\\B" "\\<" "\\>")))
- (goto-char (match-end 0))
- (let ((sym (cdr (assoc
- (match-string 0)
- '(("." . nonl)
- ("\\w" . wordchar) ("\\W" . not-wordchar)
- ("\\`" . bos) ("\\'" . eos)
- ("\\=" . point)
- ("\\b" . word-boundary) ("\\B" . not-word-boundary)
- ("\\<" . bow) ("\\>" . eow))))))
- (push sym sequence)))
-
- ;; symbol-start, symbol-end
- ((looking-at (rx "\\_" (opt (group (any "<>")))))
- (let ((arg (match-string 1)))
- (unless arg
- (error "Invalid \\_ sequence"))
- (forward-char 3)
- (push (if (string-equal arg "<") 'symbol-start 'symbol-end)
- sequence)))
-
- ;; character syntax
- ((looking-at (rx "\\" (group (any "sS")) (opt (group anything))))
- (let ((negated (string-equal (match-string 1) "S"))
- (syntax-code (match-string 2)))
- (unless syntax-code
- (error "Incomplete \\%s sequence" (match-string 1)))
+ (let ((literal (match-string 1)))
+ (goto-char (match-end 1))
+ (xr--report warnings (match-beginning 0)
+ (format "Unescaped literal `%s'" literal))
+ (push literal sequence))))
+
+ ;; \{..\} - not special at beginning of sequence or after ^
+ ((and (looking-at (rx "\\{"))
+ sequence
+ (not (and (eq (car sequence) 'bol) (eq (preceding-char) ?^))))
+ (forward-char 2)
+ (let ((operand (car sequence)))
+ (when warnings
+ (cond
+ ;; (** N M (* X)), for any repetition *
+ ((and (consp operand)
+ (memq (car operand)
+ '(opt zero-or-more one-or-more +? *? ??)))
+ (xr--report warnings (match-beginning 0)
+ "Repetition of repetition"))
+ ;; (** N M (group (* X))), for any repetition *
+ ((and (consp operand)
+ (eq (car operand) 'group)
+ (null (cddr operand))
+ (let ((inner (cadr operand)))
+ (and (consp inner)
+ (memq (car inner)
+ '(opt zero-or-more one-or-more +? *? ??)))))
+ (xr--report warnings (match-beginning 0)
+ "Repetition of repetition"))
+ ((memq operand xr--zero-width-assertions)
+ (xr--report warnings (match-beginning 0)
+ "Repetition of zero-width assertion"))
+ ((and (xr--matches-empty-p operand)
+ ;; Rejecting repetition of the empty string
+ ;; suppresses some false positives.
+ (not (equal operand "")))
+ (xr--report
+ warnings (match-beginning 0)
+ "Repetition of expression matching an empty string"))))
+ (if (looking-at (rx (opt (group (one-or-more digit)))
+ (opt (group ",")
+ (opt (group (one-or-more digit))))
+ "\\}"))
+ (let ((lower (if (match-string 1)
+ (string-to-number (match-string 1))
+ 0))
+ (comma (match-string 2))
+ (upper (and (match-string 3)
+ (string-to-number (match-string 3)))))
+ (unless (or (match-beginning 1) (match-string 3))
+ (xr--report warnings (- (match-beginning 0) 2)
+ (if comma
+ "Uncounted repetition"
+ "Implicit zero repetition")))
+ (goto-char (match-end 0))
+ (setq sequence (cons (xr--repeat lower
+ (if comma upper lower)
+ operand)
+ (cdr sequence))))
+ (error "Invalid \\{\\} syntax"))))
+
+ ;; nonspecial character
+ ((looking-at (rx (not (any "\\.["))))
+ (forward-char 1)
+ (push (match-string 0) sequence))
+
+ ;; character alternative
+ ((looking-at (rx "[" (opt (group "^"))))
(goto-char (match-end 0))
- (push (xr--char-syntax negated (string-to-char syntax-code))
- sequence)))
-
- ;; character categories
- ((looking-at (rx "\\" (group (any "cC")) (opt (group anything))))
- (let ((negated (string-equal (match-string 1) "C"))
- (category-code (match-string 2)))
- (unless category-code
- (error "Incomplete \\%s sequence" (match-string 1)))
+ (let ((negated (match-string 1)))
+ (push (xr--parse-char-alt negated warnings) sequence)))
+
+ ;; group
+ ((looking-at (rx "\\(" (opt (group "?")
+ (opt (opt (group (any "1-9")
+ (zero-or-more digit)))
+ (group ":")))))
+ (let ((question (match-string 1))
+ (number (match-string 2))
+ (colon (match-string 3)))
+ (when (and question (not colon))
+ (error "Invalid \\(? syntax"))
+ (goto-char (match-end 0))
+ (let* ((group (xr--parse-alt warnings))
+ ;; simplify - group has an implicit seq
+ (operand (if (and (listp group) (eq (car group) 'seq))
+ (cdr group)
+ (list group))))
+ (when (not (looking-at (rx "\\)")))
+ (error "Missing \\)"))
+ (forward-char 2)
+ (let ((item (cond
+ (number ; numbered group
+ (append (list 'group-n (string-to-number number))
+ operand))
+ (question ; shy group
+ group)
+ (t ; plain group
+ (cons 'group operand)))))
+ (push item sequence)))))
+
+ ;; back-reference
+ ((looking-at (rx "\\" (group (any "1-9"))))
+ (forward-char 2)
+ (push (list 'backref (string-to-number (match-string 1)))
+ sequence))
+
+ ;; various simple substitutions
+ ((looking-at (rx (or "." "\\w" "\\W" "\\`" "\\'" "\\="
+ "\\b" "\\B" "\\<" "\\>")))
(goto-char (match-end 0))
- (push (xr--char-category negated (string-to-char category-code))
- sequence)))
-
- ;; Escaped character. Only \*+?.^$[ really need escaping, but we accept
- ;; any not otherwise handled character after the backslash since
- ;; such sequences are found in the wild.
- ((looking-at (rx "\\" (group (or (any "\\*+?.^$[]")
- (group anything)))))
- (forward-char 2)
- (push (match-string 1) sequence)
- (when (match-beginning 2)
- ;; Note that we do not warn about \\], since the symmetry with \\[
- ;; makes it unlikely to be a serious error.
- (xr--report warnings (match-beginning 0)
- (format "Escaped non-special character `%s'"
- (xr--escape-string (match-string 2) nil)))))
-
- (t (error "Backslash at end of regexp"))))
+ (let ((sym (cdr (assoc
+ (match-string 0)
+ '(("." . nonl)
+ ("\\w" . wordchar) ("\\W" . not-wordchar)
+ ("\\`" . bos) ("\\'" . eos)
+ ("\\=" . point)
+ ("\\b" . word-boundary) ("\\B" .
not-word-boundary)
+ ("\\<" . bow) ("\\>" . eow))))))
+ (push sym sequence)))
+
+ ;; symbol-start, symbol-end
+ ((looking-at (rx "\\_" (opt (group (any "<>")))))
+ (let ((arg (match-string 1)))
+ (unless arg
+ (error "Invalid \\_ sequence"))
+ (forward-char 3)
+ (push (if (string-equal arg "<") 'symbol-start 'symbol-end)
+ sequence)))
+
+ ;; character syntax
+ ((looking-at (rx "\\" (group (any "sS")) (opt (group anything))))
+ (let ((negated (string-equal (match-string 1) "S"))
+ (syntax-code (match-string 2)))
+ (unless syntax-code
+ (error "Incomplete \\%s sequence" (match-string 1)))
+ (goto-char (match-end 0))
+ (push (xr--char-syntax negated (string-to-char syntax-code))
+ sequence)))
+
+ ;; character categories
+ ((looking-at (rx "\\" (group (any "cC")) (opt (group anything))))
+ (let ((negated (string-equal (match-string 1) "C"))
+ (category-code (match-string 2)))
+ (unless category-code
+ (error "Incomplete \\%s sequence" (match-string 1)))
+ (goto-char (match-end 0))
+ (push (xr--char-category negated (string-to-char category-code))
+ sequence)))
+
+ ;; Escaped character. Only \*+?.^$[ really need escaping, but we
+ ;; accept any not otherwise handled character after the backslash
+ ;; since such sequences are found in the wild.
+ ((looking-at (rx "\\" (group (or (any "\\*+?.^$[]")
+ (group anything)))))
+ (forward-char 2)
+ (push (match-string 1) sequence)
+ (when (match-beginning 2)
+ ;; Note that we do not warn about \\], since the symmetry with \\[
+ ;; makes it unlikely to be a serious error.
+ (xr--report warnings (match-beginning 0)
+ (format "Escaped non-special character `%s'"
+ (xr--escape-string (match-string 2) nil)))))
+
+ (t (error "Backslash at end of regexp")))))
(let ((item-seq (xr--rev-join-seq sequence)))
(cond ((null item-seq)