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

How to "override" previously set `as`'s to make things simpler. #204

Closed phallstrom closed 4 years ago

phallstrom commented 4 years ago

I'm not sure how to explain this so will provide an example of what I want. Can't figure out if what I want is possible or not...

class Test < Parslet::Parser
  rule(:space) { str(' ') }
  rule(:sign) { str('-').as(:sign) }
  rule(:alpha) { sign.maybe >> match('[a-z]') }
  rule(:digit) { sign.maybe >> match('[0-9]') }
  rule(:query) { ((alpha | digit).as(:variable) >> space.maybe).repeat }

  root :query
end

Test.new.parse("-a b")

When run the output is:

[
  {:variable=>{:sign=>"-"}},
  {:variable=>"b"}
]

What I'd like is for it to be this.

[
  {:variable=>"-a"},
  {:variable=>"b"}
]

Some way to replace that as(:variable) with a ignore any as's inside this thing and just use the whole matched string as :variable. Is this possible, without removing the .as(:sign) ?

kschiess commented 4 years ago

How is this a parslet issue?

phallstrom commented 4 years ago

@kschiess I'm confused. I'm using the parslet gem and have a question/issue about how to capture some of the results -- specifically if a smaller rule uses as it doesn't seem possible to use as at a higher level.

kschiess commented 4 years ago

It is possible. Add more .as in the lower level, as described here: http://kschiess.github.io/parslet/get-started.html (Tree output).

Or maybe ask the question on Stack Overflow?

phallstrom commented 4 years ago

Yeah, for my case, I'm extending an existing parser so do not want to touch the lower levels. I really want a override all the lower level stuff and use my :as instead. Sounds like that isn't a feature of parslet right now.