branch: master commit cbd33529b83fc55967153887f273dc9c84a328c3 Author: Junpeng Qiu <qjpchm...@gmail.com> Commit: Junpeng Qiu <qjpchm...@gmail.com>
Add parsec-lookahead --- parsec-tests.el | 29 +++++++++++++++++++++++++++++ parsec.el | 7 +++++++ 2 files changed, 36 insertions(+) diff --git a/parsec-tests.el b/parsec-tests.el index fdf2055..e9c2672 100644 --- a/parsec-tests.el +++ b/parsec-tests.el @@ -229,6 +229,35 @@ (parsec-str "abc"))) "abc"))) +(ert-deftest test-parsec-lookahead () + (should + (equal + (parsec-with-input "abc" + (parsec-lookahead (parsec-str "abc")) + (point)) + (point-min))) + (should + (equal + (parsec-with-input "abc" + (parsec-start + (parsec-lookahead + (parsec-and + (parsec-ch ?a) + (parsec-ch ?c)))) + (point)) + (1+ (point-min)))) + (should + (equal + (parsec-with-input "abc" + (parsec-start + (parsec-try + (parsec-lookahead + (parsec-and + (parsec-ch ?a) + (parsec-ch ?c))))) + (point)) + (point-min)))) + (ert-deftest test-parsec-error-handles () (should (equal diff --git a/parsec.el b/parsec.el index 70209e8..f28e42c 100644 --- a/parsec.el +++ b/parsec.el @@ -730,6 +730,13 @@ point of your parsing program." (parsec-and ,parser) (goto-char ,orig-pt-sym))))) +(defmacro parsec-lookahead (parser) + "Try PARSER, and pretend that no input is consumed when it succeeds." + (let ((orig-pt-sym (make-symbol "orig-pt"))) + `(let ((,orig-pt-sym (point))) + (parsec-return ,parser + (goto-char ,orig-pt-sym))))) + (defsubst parsec--atom-tag (name) (intern (format "parsec-failed-at-half-%s" name)))