in reply to Re^5: Perl regexp matching is slow??
in thread Perl regexp matching is slow??
The inspiration comes from Parse::RecDescent's "autotree" directive, which basically constructs a parse tree by collecting submatches into a hash (keyed by submatch name) and blessing that into a package named according to the parent rule, e.g. A ::= B C creates an object of type A with two fields B and C.
I think a much less blessed scheme would be appropriate for the regex engine, in which numbered submatches are collected into match variables @/ and %/. For @/, each capturing group generates a capture:
$sexp = qr/ (?<sexp> \s* \( \s* (?&sexp)* \s* \) \s* | \s* (?<atom>\w+) \s* )/x; '(A (B C))' =~ /$sexp/; ## AFTERWARDS %/ = (sexp => { 0 => '(A (B C))', sexp => [{ 0 => 'A', atom => 'A' }, { 0 => '(B C)', sexp => [{ 0 => 'B', atom => 'B' }, { 0 => 'C', atom => 'C' }] }]}; @/ = ('(A (B C))', [['A', 'A'], ['(B C)', [['B', 'B'], ['C', 'C']]]]);
There are probably some obvious oversights here, but I'll try to get the Regexp::Parser version of this working to shake the bugs out.
|
|---|