kschiess / parslet

A small PEG based parser library. See the Hacking page in the Wiki as well.
kschiess.github.com/parslet
MIT License
805 stars 95 forks source link

Context set throws error #101

Closed rubydesign closed 10 years ago

rubydesign commented 10 years ago

The funny thing is the test causing this used to work, but the sad one is that i don't remember all the changes i made.

Some poking around reveals that the "set" in question is a Atom::Entity with the value "def", though the cache seems to be a hash and i don't see why any conversion should be necessary. The project is on github (https://github.com/ruby-in-ruby/crystal), has no dependencies, and the bug in this commit fdb5dd4f671bdf4eaa76b4d44561fdfe4ffcddf5

caused by running: ruby test/parser/test_root.rb

TypeError: no implicit conversion of Parslet::Atoms::Entity into Integer /Users/raisa/pi/home/crystal/lib/parslet/atoms/context.rb:88:in []=' /Users/raisa/pi/home/crystal/lib/parslet/atoms/context.rb:88:inset' /Users/raisa/pi/home/crystal/lib/parslet/atoms/context.rb:34:in try_with_cache' /Users/raisa/pi/home/crystal/lib/parslet/atoms/base.rb:83:inapply' /Users/raisa/pi/home/crystal/lib/parslet/atoms/base.rb:70:in setup_and_apply' /Users/raisa/pi/home/crystal/lib/parslet/atoms/base.rb:30:inparse' /Users/raisa/pi/home/crystal/lib/parslet/convenience.rb:28:in parse_with_debug' /Users/raisa/pi/home/crystal/test/parser/helper.rb:26:incheck_parse' /Users/raisa/pi/home/crystal/test/parser/helper.rb:58:in `block (3 levels) in runnable_methods'

rubydesign commented 10 years ago

So i poked around a bit more and it was my recent addition of compound types (thought i'd checked that)

By leaving out ComoundTypes (just uncommenting) the test will parse. Compound types "should" not be parsed in the example, and are in fact not referenced anywhere in the grammar (i just coded and tested them separately)

Seems strange, and my only guess would be that the names :array and :hash cause something ?

kschiess commented 10 years ago

Nothing to do with the bug, but...

Copyright (c) 2010-2014 Kaspar Schiess

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

Please take this seriously, if we OSS developers don't, how can we expect anyone else to?

Now working on that bug ;)

kschiess commented 10 years ago

This guy here is to blame ;)

+    rule(:hash)       { left_brace >> ((hash_pair.as(:hash_pair) >> 
+                         (comma >> space? >> hash_pair.as(:hash_pair)).repeat(0)).repeat(0,1)).as(:hash)>> 
+                         space? >> right_brace }

This essentially redefines the '#hash' method on the parser - which makes the parser impossible to treat as a normal Ruby object. This is the bad news.

The good news is that changing the context.rb around to

    def lookup(obj, pos)
      @cache[pos][obj.object_id] 
    end
    def set(obj, pos, val)
      @cache[pos][obj.object_id] = val
    end

yields a 10% speed increase and also 'fixes' the bug ('#hash' is not called anymore). I'll credit you in the commit.

My tip is to maybe consider the fact that rules define methods and be more cautious with that ;)

rubydesign commented 10 years ago

Thanks for looking at this Kasper.

And rest assured the vendoring was meant to be a temporary solution (so i have only one load directory, though i forget why that was important). If not I shall plaster your name in big letters all over the parser part. It really is great stuff.

I hope you find time to look at the project a little. Admittedly docs are sparse, but i would be happy to write more if you have questions.

And i would be very happy if you find it interesting enough to help a little :-)

Parsing ruby is not a simple thing even with a good tool.

rubydesign commented 10 years ago

Hey that was lightning quick.

And good tip. That fact had not really gelled in my mind, but certainly will now!