>
>
> I tried to match the following rule:
>
> rule( :term => { :factor => "c", :plus =>"+ ", :term=> "d" } ) { puts
> "--> Pattern Matched" } # Simple test to check the matching of the rule.
>
> in the parse tree:
>
> {:result=>
> {:term=>
> {:factor=>"a"@2,
> :plus=>"+ "@4,
> :term=>
> {:factor=>"b"@6,
> :plus=>"+ "@8,
> :term => { :factor=>"c"@10, :plus=>"+ "@12, :term=>"d"@14} } } } }
>
> But the rule did not match.
>
> Can anyone give me a clue what I did wrong?
>
I think it's the ":term => { :factor => "c", :plus =>"+ ", :term=> "d" }"
bit of your rule. As far as I know, it's not meant to.
Basically the hash (in your parse tree) "{ :factor=>"c"@10, :plus=>"+ "@12,
:term=>"d"@14}"
does not match the hash (in your rule) "{ :factor=>"c", :plus=>"+ ",
:term=>"d" }"
Whether this is because anonymous ruby hashes don't match under comparison,
or because it's the fact that your string values are ruby String objects in
one and Parslet's slicey things in the other, I don't know. Some
experimentation would probably tell you. In the mean time, have you
considered an approach like this:
#!/usr/bin/env ruby
require 'parslet'
require 'ap'
t = {:result=>
{:term=>
{:factor=>"a",
:plus=>"+ ",
:term=>
{:factor=>"b",
:plus=>"+ ",
:term => { :factor=>"c", :plus=>"+ ",
:term=>"d"} } } } }
class Thingy
def initialize(*args)
@args = *args
end
end
transform = Parslet::Transform.new do
#rule( :term => { :factor => "c", :plus =>"+ ", :term=> "d" } ) { puts
"--> Pattern Matched" }
rule(:factor => simple(:a), :plus => simple(:b), :term => simple(:c)) {
Thingy.new(a,b,c) }
rule(:term => simple(:thingy)) { puts "Matched a thingy! #{thingy.inspect}";
thingy }
end
puts "original:"
ap t
puts "transform:"
ap transform.apply(t) # matches
cheers
ant