diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2023-03-16 13:18:41 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2023-03-16 19:05:09 -0700 |
commit | 4143535ae5eb9385a86240e152be873cc4348e03 (patch) | |
tree | 13cf54b103ef4d8a4540dc87aad7998797cb41e0 /src/reader.lisp | |
parent | a2f0f9376e110d6c930b0cd3cf533c7fd3b7b917 (diff) | |
download | consfigurator-4143535ae5eb9385a86240e152be873cc4348e03.tar.gz |
add regexp trailing option to attempt to parse matches as numbers
Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 'src/reader.lisp')
-rw-r--r-- | src/reader.lisp | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/reader.lisp b/src/reader.lisp index 3d73046..f961362 100644 --- a/src/reader.lisp +++ b/src/reader.lisp @@ -151,6 +151,7 @@ indented heredoc; see perlop(1)." (modes (loop for next = (read-char stream t :eof t) while (alpha-char-p next) collect next finally (unread-char next stream))) + (try-parse (find #\p modes)) (scanner-args (list first-arg :case-insensitive-mode (and (find #\i modes) t) @@ -167,7 +168,13 @@ indented heredoc; see perlop(1)." (ecase op (#\m (cond ((member #\g modes) - `(re:all-matches-as-strings ,scanner target-string)) + (let ((form + `(re:all-matches-as-strings ,scanner + target-string))) + (if try-parse + `(aand ,form + (map-into it #'try-parse-number it)) + form))) ((not arg) ;; The number of capture groups is constant if ;; FIRST-ARG is constant, so could we self-replace @@ -177,14 +184,31 @@ indented heredoc; see perlop(1)." ;; We could (coerce rest 'list) for use with ;; DESTRUCTURING-BIND. But there is already ;; CL-PPCRE:REGISTER-GROUPS-BIND. - (if (zerop (length rest)) zeroth rest))) + (if (zerop (length rest)) + ,(if try-parse + '(try-parse-number zeroth) + 'zeroth) + ,(if try-parse + '(map-into rest #'try-parse-number rest) + 'rest)))) ((zerop arg) - `(re:scan-to-strings ,scanner target-string)) + (let ((form `(re:scan-to-strings ,scanner + target-string))) + (if try-parse + `(multiple-value-bind (match groups) ,form + (values (try-parse-number match) + (map-into groups #'try-parse-number + groups))) + form))) (t ; ARG is a positive integer `(aand (nth-value 1 (re:scan-to-strings ,scanner target-string)) - (values (aref it ,(1- arg)) it))))) + ,(if try-parse + `(values (try-parse-number + (aref it ,(1- arg))) + (map-into it #'try-parse-number it)) + `(values (aref it ,(1- arg)) it)))))) (#\s `(,(if (member #\g modes) 're:regex-replace-all 're:regex-replace) |