Hi Ant,
I tried to stay as close as possible with that rule to the parse tree
just to see if it would match the pattern. More as an experiment.
Can you let me know what 's the require 'ap' ? Is that a gem? I could
not find it with rubygems in my Netbeans ide.
I'll have look at your code suggestion.
Many thanks,
Cheers
Thiel
Op 6-5-2011 14:45, Ant Skelton schreef:
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