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

`quote >> any.repeat >> quote` does not work. #94

Closed ghost closed 10 years ago

ghost commented 11 years ago

I'm working on a shell language that has strings as a data type and can't get my algorithim to work. I chose quote >> any.repeat >> quote which should translate to QUOTE .*QUOTE and out puts in the parser as (QUOTE .{0, } QUOTE) I'm convinced this should work so I'm reporting this as a bug. Any work-around suggestions or corrections if I'm wrong?

class BangParser < Parslet::Parser
  rule(:quote) do
    str(%Q{"}) | str(%Q{'})
  end
  rule(:integer) do
    str('-').maybe >> match['0-9'].repeat(1)
  end
  rule(:float) do
    str('-').maybe >> integer.repeat(1) >> match['.'] >> integer.repeat(1)
  end
  rule(:string) do
    quote >> any.repeat >> quote
  end
  rule(:expression) do
    float | integer | string
  end
  root :expression
  def self.parse(str)
    self.new.parse str
  end
end

  if __FILE__ == $PROGRAM_NAME
      p BangParser.new.string.parse(%Q{'aaaaaa'})
  end
kschiess commented 10 years ago

PEG != regular expressions

You assume that 'any.repeat' will stop repeating before the (ending) quote, which it doesn't. It consumes all of your string, leaving nothing for the ending quote. (aka 'greedy') So no, that's not a bug. Please look at https://github.com/kschiess/parslet/blob/master/example/string_parser.rb for directions.