parsecparser-combinatorsattoparsecmegaparsec

Why `optional` in a Parser can err out


https://github.com/complyue/dcp is a minimum working example to reprod this error

$ cabal run dcp:dcp < samples/basic.txt 
Up to date
dcp: 10:1:
   |
10 | method doXXX() pass
   | ^
unexpected 'm'
expecting ';'

CallStack (from HasCallStack):
  error, called at src/Parser.hs:149:14 in main:Parser
$ 

I believe it's optionalSemicolon causing the failure:

https://github.com/complyue/dcp/blob/1df7ad590d78d4fa9a017eb53f9f265e291bdfa7/src/Parser.hs#L50-L54

  findIt = do
    -- ignore leading whitespaces and an optional semicolon in between
    nbsc >> optionalSemicolon >> nbsc
    -- try get a doc comment block
    getIt >>= \case

And it's defined like this: https://github.com/complyue/dcp/blob/1df7ad590d78d4fa9a017eb53f9f265e291bdfa7/src/Parser.hs#L31-L32

optionalSemicolon :: Parser Bool
optionalSemicolon = fromMaybe False <$> optional (True <$ symbol ";")

I can't reason about why it can fail like this.


Solution

  • Turns out it is because the symbol in optionalSemicolon is referencing sc while it shouldn't. Solved like this:

    https://github.com/complyue/dcp/commit/fd2df02f7218e59db2a732d5de74acedfefefaa2?branch=fd2df02f7218e59db2a732d5de74acedfefefaa2&diff=unified#diff-1785501875711a0bda12ba99505aa188659de4e35ad27d7fb819993bd8ec95bdL26-R145

     optionalComma :: Parser Bool
    -optionalComma = fromMaybe False <$> optional (True <$ symbol ",")
    +optionalComma = fromMaybe False <$> optional (True <$ string ",")
     
     optionalSemicolon :: Parser Bool
    -optionalSemicolon = fromMaybe False <$> optional (True <$ symbol ";")
    +optionalSemicolon = fromMaybe False <$> optional (True <$ string ";")