Skip to content

Commit

Permalink
Make whitespace? and word? backtracking, export new symbols.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramarren committed Jan 25, 2011
1 parent 653c140 commit bdf0843
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
10 changes: 10 additions & 0 deletions greedy.lisp
Expand Up @@ -112,6 +112,16 @@ non-recursively and has better memory performance."
:tree init-x :suffix inp)))))) :tree init-x :suffix inp))))))
(bind p #'rest-chain)))) (bind p #'rest-chain))))


(def-cached-arg-parser whitespace* (&key (result-type nil) (accept-empty nil))
"Non-backtracking parser: accept a sequence of whitespace characters."
(gather-if* (rcurry #'member '(#\Space #\Newline #\ ))
:result-type result-type
:accept-empty accept-empty))

(def-cached-parser word*
"Parser: accept a string of alphabetic characters"
(gather-if* #'alpha-char-p :result-type 'string))

(defun nat* (&optional (radix 10)) (defun nat* (&optional (radix 10))
"Non-backtracking parser: accept natural number, consuming as many digits as possible" "Non-backtracking parser: accept natural number, consuming as many digits as possible"
(named-seq* (<- number (gather-if* (rcurry #'digit-char-p radix) :result-type 'string)) (named-seq* (<- number (gather-if* (rcurry #'digit-char-p radix) :result-type 'string))
Expand Down
3 changes: 3 additions & 0 deletions package.lisp
Expand Up @@ -18,6 +18,9 @@
#:letter? #:letter?
#:alphanum? #:alphanum?
#:word? #:word?
#:word*
#:whitespace?
#:whitespace*
#:string? #:string?
#:many? #:many?
#:many1? #:many1?
Expand Down
9 changes: 5 additions & 4 deletions token-parsers.lisp
Expand Up @@ -26,13 +26,14 @@


(def-cached-arg-parser whitespace? (&key (result-type nil) (accept-empty nil)) (def-cached-arg-parser whitespace? (&key (result-type nil) (accept-empty nil))
"Parser: accept a sequence of whitespace characters." "Parser: accept a sequence of whitespace characters."
(gather-if* (rcurry #'member '(#\Space #\Newline #\ )) (between? (sat (rcurry #'member '(#\Space #\Newline #\ )))
:result-type result-type (if accept-empty nil 1)
:accept-empty accept-empty)) nil
result-type))


(def-cached-parser word? (def-cached-parser word?
"Parser: accept a string of alphabetic characters" "Parser: accept a string of alphabetic characters"
(gather-if* #'alpha-char-p :result-type 'string)) (between? (alphanum?) 1 nil 'string))


;; naive implementation using monadic combinators, unfortunately rather slow ;; naive implementation using monadic combinators, unfortunately rather slow
;; (defun int? () ;; (defun int? ()
Expand Down

0 comments on commit bdf0843

Please sign in to comment.