Implement origami-parser-string

This commit is contained in:
Greg Sexton 2014-05-06 20:37:17 +01:00
parent 503b160ba0
commit 9ab8de98ba

View File

@ -294,11 +294,16 @@ contains point, or null."
(defun origami-parser-get () (defun origami-parser-get ()
(lambda (s) (cons s s))) (lambda (s) (cons s s)))
(defun origami-parser-item () (defun origami-parser-take (n)
(lambda (content) (lambda (content)
(let ((content-str (origami-content-string content))) (let ((content-str (origami-content-string content)))
(unless (s-blank? content-str) (unless (s-blank? content-str)
(cons (substring content-str 0 1) (origami-content-from content 1)))))) (let* ((len (length content-str))
(n (if (> n len) len n)))
(cons (substring content-str 0 n) (origami-content-from content n)))))))
(defun origami-parser-item ()
(origami-parser-take 1))
(defun origami-parser-position () (defun origami-parser-position ()
"Returns the point position, which is 1 more than the current "Returns the point position, which is 1 more than the current
@ -316,6 +321,14 @@ consumed count."
(defun origami-parser-char (x) (defun origami-parser-char (x)
(origami-parser-sat (lambda (c) (equal x c)))) (origami-parser-sat (lambda (c) (equal x c))))
(defun origami-parser-string (str)
;; take rather than recursion due to elisp
(origami-do (prefix <- (origami-parser-take (length str)))
(pos <- (origami-parser-position))
(if (equal str prefix)
(origami-parser-return pos)
(origami-parser-zero))))
(defun origami-parser-conj (p1 p2) (defun origami-parser-conj (p1 p2)
(lambda (content) (lambda (content)
(or (origami-run-parser p1 content) (or (origami-run-parser p1 content)