[Top][All Lists]

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

[elpa] externals/peg 231f501: * peg.el (peg-parse-p): New macro

From: Stefan Monnier
Subject: [elpa] externals/peg 231f501: * peg.el (peg-parse-p): New macro
Date: Wed, 13 Mar 2019 15:12:47 -0400 (EDT)

branch: externals/peg
commit 231f5014b108445d8d7dec574735ab9dd87fd82e
Author: Stefan Monnier <address@hidden>
Commit: Stefan Monnier <address@hidden>

    * peg.el (peg-parse-p): New macro
    (peg--parse-no-error): New function.
    (peg-search-failed): New error.
    (peg-parse-at-point): Use it.
    * peg-tests.el (peg-test): Add test for peg-parse-p.
 peg-tests.el |  7 +++++++
 peg.el       | 22 ++++++++++++++++++----
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/peg-tests.el b/peg-tests.el
index a48dc9e..98dbe43 100644
--- a/peg-tests.el
+++ b/peg-tests.el
@@ -132,6 +132,13 @@
                 '(nil "axyf")))
+  (with-temp-buffer
+    (insert "toro")
+    (goto-char (point-min))
+    (should (peg-parse-p "to"))
+    (should-not (peg-parse-p "to"))
+    (should (peg-parse-p "ro"))
+    (should (eobp)))
 ;;; Examples:
diff --git a/peg.el b/peg.el
index f425e8c..0b38f6d 100644
--- a/peg.el
+++ b/peg.el
@@ -174,6 +174,13 @@ moving point along the way."
       `(with-peg-rules ,pexs (peg-parse-at-point (peg-rule-ref ,(caar pexs))))
     `(peg-parse-at-point (peg-rule-ref ,@pexs))))
+(defmacro peg-parse-p (&rest pexs)
+  "Match PEXS at point.
+Like `peg-parse' but returns a boolean indicating success, instead of
+signaling an error on failure.
+PEXS is a sequence of PEG expressions, implicitly combined with `and'."
+  `(peg--parse-no-error (peg-rule-ref ,@pexs)))
 (defmacro define-peg-rule (name args &rest pexs)
   "Define PEG rule NAME as equivalent to PEXS.
 The PEG expressions in PEXS are implicitly combined with the
@@ -253,17 +260,24 @@ executed in a postprocessing step, not during parsing.")
     (`(call ,name) `#',(peg--rule-id name))
     (exp `(lambda () ,(peg-translate-exp exp)))))
+(define-error 'peg-search-failed "Parse error at %d (expecting %S)")
 (defun peg-parse-at-point (rule-ref)
   "Parse text at point according to the PEG rule RULE-REF."
-  (defvar peg--errors) (defvar peg--actions)
   (let ((peg--actions '()) (peg--errors '(-1)))
     (if (funcall rule-ref)
         ;; Found a parse: run the actions collected along the way.
         (peg-postprocess peg--actions)
       (goto-char (car peg--errors))
-      (error "Parse error at %d (expecting %S)"
-            (car peg--errors)
-            (peg-merge-errors (cdr peg--errors))))))
+      (signal 'peg-search-failed
+              (list (car peg--errors)
+                   (peg-merge-errors (cdr peg--errors)))))))
+(defun peg--parse-no-error (rule-ref)
+  "Parse text at point according to the PEG rule RULE-REF.
+Only moves point and returns a boolean."
+  (let ((peg--actions '()) (peg--errors '(-1)))
+    (funcall rule-ref)))
 ;; Internally we use a regularized syntax, e.g. we only have binary OR
 ;; nodes.  Regularized nodes are lists of the form (OP ARGS...).

reply via email to

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