[Haskell-cafe] Reason about a Megaparsec error
YueCompl
compl.yue at icloud.com
Sun Oct 25 11:00:49 UTC 2020
During the parsing of doc comments, backtracking is off on purpose, as it won't consume syntactic contents, merely whitespaces (while doc comment is a special form of block comment), the desired result is `Nothing` or `Just docCmt` without backtracking of whitespaces.
> On 2020-10-25, at 18:53, Jaro Reinders <jaro.reinders at gmail.com> wrote:
>
> I think you have to insert a 'try' manually inside the optional, because megaparsec doesn't do backtracking by default.
>
> On 10/25/20 11:27 AM, YueCompl via Haskell-Cafe wrote:
>> Dear Cafe,
>> I find myself unable to reason about an error that `optional` in a parser err out instead of return Nothing, I asked the question at StackOverflow, and would like to seek your help here as well.
>> https://stackoverflow.com/questions/64522568/why-optional-in-a-parser-can-err-out <https://stackoverflow.com/questions/64522568/why-optional-in-a-parser-can-err-out>
>> https://github.com/complyue/dcp <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 <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 <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.
>> Best regards,
>> Compl
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
More information about the Haskell-Cafe
mailing list