kschiess / parslet

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

Parslet - Get Started typos #48

Closed tmcgilchrist closed 13 years ago

tmcgilchrist commented 13 years ago

Hi,

I just worked through the Get Started guide and noticed a few typos in the code. For example under the heading Making the parser complete

class MiniP < Parslet::Parser
  # Single character rules
  rule(:lparen)     { str('(') >> space? }
  rule(:rparen)     { str(')') >> space? }
  rule(:comma)      { str(',') >> space? }
  # etc
end

Looks like you need to escape the brackets to get it working.

Is there a copy of this page somewhere I could clone and provide a patch to fix theses issues?

floere commented 13 years ago

Hi Tim,

Thanks for the feedback. I can't seem to reproduce the problem, neither under 1.9.2 nor 1.8.7.

The quick example I used is:

require 'parslet'

class MiniP < Parslet::Parser
  # Single character rules
  rule(:lparen)     { str('(') >> space? }
  rule(:rparen)     { str(')') >> space? }
  rule(:comma)      { str(',') >> space? }

  rule(:space)      { match('\s').repeat(1) }
  rule(:space?)     { space.maybe }

  # Things
  rule(:integer)    { match('[0-9]').repeat(1).as(:int) >> space? }
  rule(:identifier) { match['a-z'].repeat(1) }
  rule(:operator)   { match('[+]') >> space? }

  # Grammar parts
  rule(:sum)        { integer.as(:left) >> operator.as(:op) >> expression.as(:right) }
  rule(:arglist)    { expression >> (comma >> expression).repeat }
  rule(:funcall)    { identifier.as(:funcall) >> lparen >> arglist.as(:arglist) >> rparen }

  rule(:expression) { funcall | sum | integer }
  root :expression
end

p MiniP.new.parse('puts(1 + 2)')

Cheers, Florian

tmcgilchrist commented 13 years ago

Hi Florian,

Yes indeed it does, my mistake sorry. I had my str('(') mixed up with match('(') thanks.

I was also a little confused about the full code listing at the bottom of the article.

class Addition < Struct.new(:left, :right) 
  def eval; left.eval + right.eval; end
end
class FunCall < Struct.new(:name, :args); 
  def eval
    p args.map { |s| s.eval }
  end
end

notice what appears to be a stray semi-colon at the end of the class FunCall. It doesn't break anything if you have it, and it still works if you remove it.

Is there a reason I'm missing for it to be there?

Cheers, Tim

floere commented 13 years ago

Hi Tim,

No worries.

My assumption is that Kaspar originally had the "class Funcall" thing on one line, using ; to help Ruby parse. And then, when formatting it, forgot to remove it. Or perhaps he had a flashback to his time as a Java (TM) programmer.

It isn't the most beautiful thing to look at, or necessary, but isn't a problem either.

So, in a nutshell: No, there is no important reason for it to be there, so don't be confused, please;

I'm closing this issue since all seems to be fine.

Cheers & thanks for using Parslet, Florian

kschiess commented 13 years ago

Thanks @floere for solving / answering this.

@tmcgilchrist: Thanks for joining and taking interest! If you find any real bugs in the documentation, you can check out the 'gh-pages' branch of parslet to correct the source. Hint: it's in the .source subdirectory of that branch.

tmcgilchrist commented 13 years ago

@floere @kschiess Thanks for the responses guys. I'll be sure to contribute anything back if I find real bugs. And thanks for the great library it's lots of fun to use.