Thanks! That did it. 

Note, the documentation on the site seems to be incorrect in a couple places. 
For example, I copied that operator code directly from the getting started page

http://kschiess.github.com/parslet/get-started.html
<Example in the section " Making the parser complete"/>
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

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf 
Of Melissa Whittington
Sent: Wednesday, August 24, 2011 8:42 AM
To: [email protected]
Subject: Re: [ruby.parslet] Parser not really eatting whitespace?

Zach,

The problem is that your operator rule is including the whitespace, so :op => 
"+ " (note space) and thus your transform rule is not matching.

You should switch these two rules:

  rule(:operator) { match('[+]') >> space? }
  rule(:sum) { integer.as(:left) >> operator.as(:op) >> expression.as(:right) }

to this instead:

  rule(:operator) { match('[+]').as(:op) >> space? }
  rule(:sum) { integer.as(:left) >> operator >> expression.as(:right) }

and I believe that should work.

-mj

On Tue, Aug 23, 2011 at 8:22 PM, Zachary Drummond - BIA 
<[email protected]> wrote:
> I followed the online docs pretty closely and got hit by the fact that 
> the parser does not seem to always eat whitespace. If you run the 
> following code, you will see that the intergers have no whitespace, 
> but the + does, which causes the transform to fail. Any ideas of what I am 
> doing wrong?
>
> Thanks,
>
> -Zach

Reply via email to