Re: pegs: match without guessing openarray size
Not part of the stdlib, but NPeg shoild be able to do this for you at compile time: import npeg static: let p = peg rule: rule <- "cd" * +Space * >Alpha * ":": echo $1 discard p.match("cd c:\blue\monkey\bike") Run
Re: pegs: match without guessing openarray size
But sadly, it also doesn't work at compile-time.
Re: pegs: match without guessing openarray size
@sky_khan That's it! Perfect, thanks!
Re: pegs: match without guessing openarray size
To be honest, I've never used pegs in Nim before but looking at it's source, it has MaxSubpatterns = 20. So, I guess either you can define matches as newSeq[string](20) or you can use this, if fixedString =~ peg"cd\s+{\D}\:": echo matches[0] Run without defining matches at all, because it injects matches variable itself, if there is none at current scope.
Re: pegs: match without guessing openarray size
This code: import pegs static: var matches = newSeq[string](10) let fixedString = "mkdir D:\...\nimgen_test\build\libmodbus" if fixedString.match(peg"cd\s+{\D}\:", matches): # <-- Error in this line echo matches[0] Run throws /playground/nim/lib/pure/pegs.nim(1661, 13) Error: index 11 not in 0 .. 10. Notice the static statement. Is this a bug?
Re: pegs: match without guessing openarray size
Thanks @sky_khan, But how do I get the capture from the result? This now gives @["cd C:"], I need it to return just the captured substring {D} ("C" in this case), like match does.
Re: pegs: match without guessing openarray size
let fixedString = "cd C:\\something\\sometimes\\someone" result = fixedString.findAll(peg"cd\s+{\D}\:") echo result Run