Hi all,
I'm having trouble getting Parslet 1.6.2 to parse nested tags dynamically.
Here's a baby version of my code:
-------
require 'parslet'
class SimpleNestParser < Parslet::Parser
rule(:id) { match('[a-z]') }
rule(:tag) {
str('<') >> id.capture(:tagname) >> str('>') >>
tag.maybe >>
str('</') >> dynamic { |src,ctx| str(ctx.captures[:tagname]) } >>
str('>')
}
root(:tag)
end
SimpleNestParser.new.parse "<a><b></b></a>"
-------
I would expect the parse to succeed, but it fails:
-------
/Library/Ruby/Gems/2.0.0/gems/parslet-1.6.2/lib/parslet/cause.rb:63:in
`raise': Failed to match sequence ('<' (:tagname = ID) '>' TAG? '</'
dynamic { ... } '>') at line 1 char 13. (Parslet::ParseFailed)
-------
If I change the dynamic matcher to "match('[ab]')" then the parse does
succeed, which makes it seem that I'm not trying to do anything that
Parslet can't handle. (But it's important that I match blocks exactly, so
I can't use this as a permanent solution.)
By adding "$stderr.puts ctx.captures[:tagname]" within the dynamic block, I
can see that the block is executed four times, and the value is always "b",
which seems suspicious. I would have thought to have seen at least one "a"
in there.
Am I misunderstanding how to use Parslet in general or the dynamic block in
particular?
Thanks for any guidance the list can provide on how to get my grammar
working!
Dave Townsend