Browse files

Add OPT?/*.

  • Loading branch information...
1 parent 7679c36 commit ab99892b5c49ce76b41b697e392df4d29263167c @Ramarren committed Jan 16, 2011
Showing with 19 additions and 1 deletion.
  1. +4 −0 greedy.lisp
  2. +2 −0 package.lisp
  3. +4 −0 parsers.lisp
  4. +9 −1 test-parsers.lisp
View
4 greedy.lisp
@@ -294,6 +294,10 @@ non-recursively and has better memory performance."
"Non-backtracking parser: Find first q"
(find-after* (item) q))
+(defun opt* (p)
+ "Non-backtracking parser: result of p or nil"
+ (choice1 p (result nil)))
+
(defun expression* (term operators &optional (bracket-left nil) (bracket-right nil))
"Non-backtracking parser: Reduce a sequence of terms with unary/binary operators with precedence.
OPERATORS is a list of (op-parser :left/:right/:unary), where OP-PARSER is a parser consuming
View
2 package.lisp
@@ -82,6 +82,8 @@
#:find*
#:find?
#:hook?
+ #:opt?
+ #:opt*
#:parse-string*
#:find-before?
#:find-before*
View
4 parsers.lisp
@@ -423,6 +423,10 @@ parsers."
(with-parsers (p)
(named-seq? (<- result p) (funcall function result))))
+(defun opt? (p)
+ "Parser: result of p or nil"
+ (choice p (result nil)))
+
(defun expression? (term operators &optional (bracket-left nil) (bracket-right nil))
"Parser: Reduce a sequence of terms with unary/binary operators with precedence.
OPERATORS is a list of (op-parser :left/:right/:unary), where OP-PARSER is a parser consuming
View
10 test-parsers.lisp
@@ -263,4 +263,12 @@
(defparsertest test-hook? (hook? #'1+ (nat*))
("5" 6 "28" 29)
- ("aa"))
+ ("aa"))
+
+(defparsertest test-opt? (named-seq? (<- a "a") (<- b (opt? "b")) (<- c "c") (list a b c))
+ ("abc" '(#\a #\b #\c) "ac" '(#\a nil #\c))
+ ("abbc" "aabc"))
+
+(defparsertest test-opt* (named-seq* (<- a "a") (<- b (opt* "b")) (<- c "c") (list a b c))
+ ("abc" '(#\a #\b #\c) "ac" '(#\a nil #\c))
+ ("abbc" "aabc"))

0 comments on commit ab99892

Please sign in to comment.