Hi all. I'm new to Nim, just started today in fact.
I'm confused why the following fails with an index out of bounds error:
import pegs
let pattern = peg"{\w+} '=' {\w+} ';'"
var matches : seq[string] = @[]
echo match("x=y;", pattern, matches)
echo matches
This will work if I use @["", ""] instead, but I don't see why I have to?
I understand that the procedure is simply trying to write to the array/sequence directly, but why is it designed that way? Why not simply extend the sequence?
Also, is there a way to quickly determine the number of captures?
Thanks.
This will work if I use @["", ""] instead, but I don't see why I have to?
Because match takes an openarray[string] for matches. It means that the proc works with both an array and a seq. And an array cannot be extended.
Since the number of matches in your PEG is statically known, it simply does not make sense to use a seq. Just use an array instead:
var matches: array[0..1, string]
Makes sense. I originally thought I could have a varying number of capture groups depending on what is in the string.
Thanks.