Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add OPT?/*.

  • Loading branch information...
commit ab99892b5c49ce76b41b697e392df4d29263167c 1 parent 7679c36
Jakub Higersberger authored
4 greedy.lisp
@@ -294,6 +294,10 @@ non-recursively and has better memory performance."
294 294 "Non-backtracking parser: Find first q"
295 295 (find-after* (item) q))
296 296
  297 +(defun opt* (p)
  298 + "Non-backtracking parser: result of p or nil"
  299 + (choice1 p (result nil)))
  300 +
297 301 (defun expression* (term operators &optional (bracket-left nil) (bracket-right nil))
298 302 "Non-backtracking parser: Reduce a sequence of terms with unary/binary operators with precedence.
299 303 OPERATORS is a list of (op-parser :left/:right/:unary), where OP-PARSER is a parser consuming
2  package.lisp
@@ -82,6 +82,8 @@
82 82 #:find*
83 83 #:find?
84 84 #:hook?
  85 + #:opt?
  86 + #:opt*
85 87 #:parse-string*
86 88 #:find-before?
87 89 #:find-before*
4 parsers.lisp
@@ -423,6 +423,10 @@ parsers."
423 423 (with-parsers (p)
424 424 (named-seq? (<- result p) (funcall function result))))
425 425
  426 +(defun opt? (p)
  427 + "Parser: result of p or nil"
  428 + (choice p (result nil)))
  429 +
426 430 (defun expression? (term operators &optional (bracket-left nil) (bracket-right nil))
427 431 "Parser: Reduce a sequence of terms with unary/binary operators with precedence.
428 432 OPERATORS is a list of (op-parser :left/:right/:unary), where OP-PARSER is a parser consuming
10 test-parsers.lisp
@@ -263,4 +263,12 @@
263 263
264 264 (defparsertest test-hook? (hook? #'1+ (nat*))
265 265 ("5" 6 "28" 29)
266   - ("aa"))
  266 + ("aa"))
  267 +
  268 +(defparsertest test-opt? (named-seq? (<- a "a") (<- b (opt? "b")) (<- c "c") (list a b c))
  269 + ("abc" '(#\a #\b #\c) "ac" '(#\a nil #\c))
  270 + ("abbc" "aabc"))
  271 +
  272 +(defparsertest test-opt* (named-seq* (<- a "a") (<- b (opt* "b")) (<- c "c") (list a b c))
  273 + ("abc" '(#\a #\b #\c) "ac" '(#\a nil #\c))
  274 + ("abbc" "aabc"))

0 comments on commit ab99892

Please sign in to comment.
Something went wrong with that request. Please try again.