为什么Parser中的"optional"会出错



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=统一#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 ";")

相关内容

  • 没有找到相关文章

最新更新