Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add radix not natural number parser and make backtracking one actuall…

…y backtracking.
  • Loading branch information...
commit 90ae404d57ec55a4dcab0907757d3c581cf8dbee 1 parent 8dfc8fd
@Ramarren authored
Showing with 11 additions and 8 deletions.
  1. +3 −6 greedy.lisp
  2. +4 −0 test-parsers.lisp
  3. +4 −2 token-parsers.lisp
View
9 greedy.lisp
@@ -112,13 +112,10 @@ non-recursively and has better memory performance."
:tree init-x :suffix inp))))))
(bind p #'rest-chain))))
-(defun nat* ()
+(defun nat* (&optional (radix 10))
"Non-backtracking parser: accept natural number, consuming as many digits as possible"
- (chainl1* (named-seq* (<- x (digit?))
- (digit-char-p x))
- (result
- #'(lambda (x y)
- (+ (* 10 x) y)))))
+ (named-seq* (<- number (gather-if* (rcurry #'digit-char-p radix) :result-type 'string))
+ (parse-integer number :radix radix)))
(defun int* ()
"Non-backtracking parser: accept integer, consuming as many digits as possible"
View
4 test-parsers.lisp
@@ -97,6 +97,10 @@
("1" 1 "23" 23)
("-1"))
+(defparsertest test-nat?2 (seq-list? (nat?) "15")
+ ("1015" '(10 "15"))
+ ())
+
(defparsertest test-chainl1? (chainl1? (digit?) (result #'list))
("123" (list (list #\1 #\2) #\3))
(""))
View
6 token-parsers.lisp
@@ -43,8 +43,10 @@
(def-cached-arg-parser nat? (&optional (radix 10))
"Parser: accept natural numbers"
- (named-seq* (<- number (gather-if* #'digit-char-p :result-type 'string))
- (parse-integer number)))
+ (named-seq? (<- number (between? (sat (rcurry #'digit-char-p radix))
+ 1 nil
+ 'string))
+ (parse-integer number :radix radix)))
(def-cached-arg-parser int? (&optional (radix 10))
"Parser: accept an integer, return as integer."
Please sign in to comment.
Something went wrong with that request. Please try again.