为什么解析器中的 `optional` 会出错

Why `optional` in a Parser can err out

https://github.com/complyue/dcp 是重现此错误的最小工作示例

$ 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
$ 

我认为是 optionalSemicolon 导致了失败:

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

它的定义是这样的: https://github.com/complyue/dcp/blob/1df7ad590d78d4fa9a017eb53f9f265e291bdfa7/src/Parser.hs#L31-L32

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

我无法解释为什么它会像这样失败。

原来是因为 optionalSemicolon 中的 symbol 引用了 sc 而它不应该引用。像这样解决:

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 ";")