-
For the sake of example, let's say the number must end in |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
You can run arbitrary code at match time using repl:1:> (peg/match ~(cmt '(at-least 2 :d) ,|(if (string/has-suffix? "0" $) $)) "1230")
@["1230"]
repl:2:> (peg/match ~(cmt '(at-least 2 :d) ,|(if (string/has-suffix? "0" $) $)) "0")
nil
repl:3:> (peg/match ~(cmt '(at-least 2 :d) ,|(if (string/has-suffix? "0" $) $)) "12")
nil
repl:4:> (peg/match ~(cmt '(at-least 2 :d) ,|(if (string/has-suffix? "0" $) $)) "00")
@["00"] I think you could also do this with mutually recursive rules instead of explicit repetition? It might perform better, but ( (Also probably not worth noting that |
Beta Was this translation helpful? Give feedback.
-
As requested in this comment, here's an alternative peg: ~(sequence :d+ (look -1 "0")) I confess to have seen something like it in bakpakin's code (^^;
:long-bytes '{:delim (some "`")
:open (capture :delim :n)
:close (cmt (* (not (> -1 "`")) (-> :n) ':delim) ,=)
:main (drop (* :open (any (if-not :close 1)) :close))} via: code in spork/fmt |
Beta Was this translation helpful? Give feedback.
As requested in this comment, here's an alternative peg:
I confess to have seen something like it in bakpakin's code (^^;
>
below is an alias forlook
IIUC:via: code in spork/fmt