Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concatinated .maybe statements match "" as "" instead of nil #155

Closed
postmodern opened this issue Apr 5, 2016 · 3 comments
Closed

Concatinated .maybe statements match "" as "" instead of nil #155

postmodern opened this issue Apr 5, 2016 · 3 comments

Comments

@postmodern
Copy link
Contributor

Example

class Parser < Parslet::Parser
  root :foo
  rule(:foo) { (str('foo=').maybe >> str('bar').maybe).as(:match) }
end
Parser.new.foo.parse("")

Actual Output

# => {:match=>""}

Expected Output

# => {:match=>nil}
@postmodern
Copy link
Contributor Author

Rewriting the expression as below seems to work:

rule(:foo) {
  (
    (str('foo=') >> str('bar').maybe) |
    (str('foo=').maybe >> str('bar'))
  ).maybe.as(:value)
}

@kschiess
Copy link
Owner

kschiess commented May 9, 2016

Should we act on this? What could be done?

Our merging algo is a bit finnicky - in theory, we would have to complicate things to cover all cases. But in practice, the heuristics work nicely apart from a few edge cases like this one. SOO ... it's a tradeoff...

@postmodern
Copy link
Contributor Author

Can we add a note about this unexpected behavior somewhere? Kind of surprised me when I stumbled across this.

rjkaes pushed a commit to rjkaes/spf-query that referenced this issue Nov 24, 2022
Prompted by: trailofbits#8

When creating a new `Mechanism`, check whether the supplied `value` is
an empty string.  If it is, convert it to `nil` as `#to_s` has logic to
avoid outputting `value` when it's nil.

This works around an issue in [Parslet](kschiess/parslet#155)
where the parser is returning an empty string when it should return
`nil`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants