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

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

[elpa] externals/xr 2a1de75 03/13: Correct parsing of group and backref


From: Mattias Engdegård
Subject: [elpa] externals/xr 2a1de75 03/13: Correct parsing of group and backref
Date: Wed, 13 Feb 2019 04:03:27 -0500 (EST)

branch: externals/xr
commit 2a1de75f192e351cd2ea7e4302756dbb9e995384
Author: Mattias Engdegård <address@hidden>
Commit: Mattias Engdegård <address@hidden>

    Correct parsing of group and backref
    
    Parse groups and backrefs more carefully. In particular, there
    is no group 0; (?0:) is illegal and \0 means 0.
---
 xr-test.el | 11 +++++++++--
 xr.el      | 26 +++++++++++++++-----------
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/xr-test.el b/xr-test.el
index ea3d9b4..b0e52d3 100644
--- a/xr-test.el
+++ b/xr-test.el
@@ -61,6 +61,13 @@
 (ert-deftest xr-backref ()
   (should (equal (xr "\\(ab\\)\\(?3:cd\\)\\1\\3")
                  '(seq (group "ab") (group-n 3 "cd") (backref 1) (backref 3))))
+  (should (equal (xr "\\01")
+                 "01"))
+  (should-error (xr "\\(?abc\\)"))
+  (should-error (xr "\\(?2\\)"))
+  (should-error (xr "\\(?0:xy\\)"))
+  (should (equal (xr "\\(?29:xy\\)")
+                 '(group-n 29 "xy")))
   )
 
 (ert-deftest xr-misc ()
@@ -191,8 +198,8 @@
                  '(or "*a" (seq "*b" (group "*c")))))
   (should (equal (xr "+a\\|+b\\(+c\\)")
                  '(or "+a" (seq "+b" (group "+c")))))
-  (should (equal (xr "?a\\|?b\\(?c\\)")
-                 '(or "?a" (seq "?b" (group "?c")))))
+  (should (equal (xr "?a\\|?b\\(^?c\\)")
+                 '(or "?a" (seq "?b" (group bol "?c")))))
   (should (equal (xr "^**")
                  '(seq bol (zero-or-more "*"))))
   (should (equal (xr "^+")
diff --git a/xr.el b/xr.el
index 6ad306f..e28b986 100644
--- a/xr.el
+++ b/xr.el
@@ -313,12 +313,16 @@
           (push (xr--parse-char-alt negated) sequence)))
 
        ;; group
-       ((looking-at (rx "\\("
-                        (opt (group "?" (group (zero-or-more digit)) ":"))))
+       ((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))
-              (end (match-end 0)))
-          (goto-char end)
+              (colon (match-string 3)))
+          (when (and question (not colon))
+            (error "Invalid \\(? syntax"))
+          (goto-char (match-end 0))
           (let* ((group (xr--parse-alt))
                  ;; simplify - group has an implicit seq
                  (operand (if (and (listp group) (eq (car group) 'seq))
@@ -327,17 +331,17 @@
             (when (not (looking-at (rx "\\)")))
               (error "Missing \\)"))
             (forward-char 2)
-            (let ((item (cond ((not question)           ; plain subgroup
-                               (cons 'group operand))
-                              ((zerop (length number))  ; shy group
-                               group)
-                              (t
+            (let ((item (cond (number   ; numbered group
                                (append (list 'group-n (string-to-number 
number))
-                                       operand)))))
+                                       operand))
+                              (question ; shy group
+                               group)
+                              (t        ; plain group
+                               (cons 'group operand)))))
               (push item sequence)))))
 
        ;; back-reference
-       ((looking-at (rx "\\" (group digit)))
+       ((looking-at (rx "\\" (group (any "1-9"))))
         (forward-char 2)
         (push (list 'backref (string-to-number (match-string 1)))
               sequence))



reply via email to

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