Speed up test parser

Skip using regular expressions. This is less 'pure' but necessary for
speed!
This commit is contained in:
Greg Sexton 2014-05-26 21:57:21 +01:00
parent fc5db446d4
commit 26e35f4227

View File

@ -305,7 +305,7 @@ contains point, or null."
(origami-do (content <- (origami-parser-get)) (origami-do (content <- (origami-parser-get))
(origami-parser-put (origami-content-from content n)) (origami-parser-put (origami-content-from content n))
;; TODO: substring will error if n is too large, guard against this ;; TODO: substring will error if n is too large, guard against this
(origami-parser-return (substring (origami-content-string content) 0 n)))) (origami-parser-return n)))
(defun origami-parser-take (n) (defun origami-parser-take (n)
(lambda (content) (lambda (content)
@ -351,6 +351,19 @@ have to prefix with '^' if you wish to match the beginning."
(origami-parser-zero)) (origami-parser-zero))
(origami-parser-position))) (origami-parser-position)))
;;; TODO: rename? origami-parser-consume-while-not ?
(defun origami-parser-drop-until-regex (rx)
"Skip over all characters until hitting RX. If rx is not found
this will bind to zero. If rx is matched at the beginning of the
string, we bind to zero. This allows for bottoming out of
recursion. We fail if we don't consume something."
(origami-do (str <- (origami-parser-get-string))
(if (string-match rx str)
(if (> (match-beginning 0) 0)
(origami-parser-drop (match-beginning 0))
(origami-parser-zero))
(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)
@ -382,12 +395,7 @@ have to prefix with '^' if you wish to match the beginning."
nil nil
(origami-run-parser (origami-parser-item) content)))) (origami-run-parser (origami-parser-item) content))))
(defun origami-parser-consume-while (parser) (defun origami-parser-paired (start children end create)
;; TODO: this should really be 0+ but for some reason goes in to an infinte loop
(origami-do (positions <- (origami-parser-1+ parser))
(origami-parser-return nil)))
(defun origami-parser-paired (start end children create)
"CHILDREN should be a zero-arg lambda returning a parser to "CHILDREN should be a zero-arg lambda returning a parser to
allow for recursive nesting." allow for recursive nesting."
(origami-do (begin <- start) (origami-do (begin <- start)
@ -427,14 +435,16 @@ allow for recursive nesting."
(defun origami-test-parser (create) (defun origami-test-parser (create)
(origami-parser-0+ (origami-parser-conj (origami-parser-0+ (origami-parser-conj
(origami-do (origami-do
(origami-parser-consume-while (origami-parser-not (origami-parser-drop-until-regex "[{}]")
(origami-parser-conj (origami-parser-char "{") (origami-parser-1?
(origami-parser-char "}")))) (origami-parser-paired (origami-parser-char "{")
(origami-parser-paired (origami-parser-char "{") (lambda () (origami-test-parser create))
(origami-parser-char "}") (origami-parser-char "}")
(lambda () (origami-test-parser create)) create)))
create)) (origami-parser-paired (origami-parser-char "{")
(origami-parser-consume-while (origami-parser-not (origami-parser-char "}")))))) (lambda () (origami-test-parser create))
(origami-parser-char "}")
create))))
(defun origami-get-parser (buffer) (defun origami-get-parser (buffer)
;; TODO: remove hardcoding! ;; TODO: remove hardcoding!