mrkkrp / parser-combinators

Lightweight package providing commonly useful parser combinators
Other
52 stars 15 forks source link

Permutation parsing is more lenient than I expect #38

Closed daniel-beard closed 2 years ago

daniel-beard commented 2 years ago

I'm attempting to parse permutations of flags. The behavior I want is "one or more flags in any order, without repetition".

The code I have is outputting what I want, but is too lenient on inputs. I don't understand why it's accepting multiples of the same flags. What am I doing wrong here?

pFlags :: Parser [Flag]
pFlags = runPermutation $ f <$> 
    toPermutation (optional (GroupFlag <$ char '\'')) <*> 
    toPermutation (optional (LeftJustifyFlag <$ char '-'))
    where f a b = catMaybes [a, b]

Example inputs / outputs:

"'-" = [GroupFlag, LeftJustifyFlag] -- CORRECT
"-'" = [LeftJustifyFlag, GroupFlag] -- CORRECT
"''''-" = [GroupFlag, LeftJustifyFlag] -- INCORRECT, should fail if there's more than one of the same flag.
mrkkrp commented 2 years ago

Hey @daniel-beard, I can't really help with permutation parsers because it wasn't me who wrote this functionality. Let's try to ping @recursion-ninja.

recursion-ninja commented 2 years ago

Let me take a look at this. I'm quite busy the next two days but on Wednesday or Thursday I should be able to carve out some time.

daniel-beard commented 2 years ago

I closed the ticket as I was able to get the behavior I wanted with this:

pFlags :: Parser [Flag]
pFlags = runPermutation $ removeMaybes <$>
    toPermutationWithDefault Nothing (Just GroupFlag <$ char '\'') <*>
    toPermutationWithDefault Nothing (Just LeftJustifyFlag <$ char '-') <*> 
    toPermutationWithDefault Nothing (Just ShowSignFlag <$ char '+') <*> 
    toPermutationWithDefault Nothing (Just SpaceFlag <$ char ' ') <*> 
    toPermutationWithDefault Nothing (Just AltFlag <$ char '#') <*> 
    toPermutationWithDefault Nothing (Just ZeroFlag <$ char '0')
    where removeMaybes a b c d e f = catMaybes [a,b,c,d,e,f]
mrkkrp commented 2 years ago

@daniel-beard Good to hear! It wasn't obvious to me why the issue was closed (maybe you had lost any hope of getting a response!), that's why I re-opened it. I'm going to close it now, however if @recursion-ninja finds time to look at this, I guess his comment is still welcome.