Hi Ant,
I copied your solution into the Factor2 rule and it showed the same result.
But the result I want is:
transform:
{
:result => {
:term => "a"
:plus => "+ ",
:term=>"b"
}
}
Any idea how to achieve that?
Cheers,
Thiel
ps If your are busy you may answer later.
Op 5-5-2011 14:15, Ant Skelton schreef:
I'll have another crack at it ;)
I think you're problem is that you're only matching on :factor,
whereas your hash contains :plus and :term too, so you need to match
those also. The logic seems to be that you have to explicitly match
all labelled terms that are present. Of course, just because you'ved
matched them, you don't have to use them.
For example:
#!/usr/bin/env ruby
require 'parslet'
require 'ap'
t = {
:result => {
:term => {
:factor => "a",
:plus => "+ ",
:term=>"b"
}
}
}
transform = Parslet::Transform.new do
rule(:factor => simple(:str), :plus => simple(:str2), :term =>
simple(:str3)) { str }
end
puts "original:"
ap t
puts "transform:"
ap transform.apply(t) # matches
will give the result:
transform:
{
:result => {
:term => "a"
}
}
Which is closer to what you wanted?
cheers
ant