Why doesn't Parsec return when one part of the analyzer completes successfully and the rest fails?

I have a parser parser:

a = optionMaybe $ do {try $ spaceornull *> string "hello";string "No"}                        

Where spaceornullis((:[]) <$> try space) <|> string ""

When I test a with the input "", I get:

Left (row 1, column 2):
  unexpected end of input
  waiting for "hello"

I don’t understand this, I spaceornull *> string "hello"have to fail because there is no “hello”, then with trybacktracks parse, and now there is no input consumed, but it tryfails anyway, therefore the parser switched to optionMaybe(inside inside do) does not work completely, it should not try to consume a newer input, so we get an unsuccessful parser without consuming any input, so I have to get it Right Nothing.

But the error message says this, space is consumed, so tryit doesn’t actually recede, does tryn’t return when part of the parser is completed successfully? and how to make it return using the above?

+1
source share
1 answer

try , . , . <|> :

a = optionMaybe $ (try $ spaceornull *> string "hello") <|> string "No"

OTOH,

a = optionMaybe $ (try $ spaceornull *> string "hello") >> string "No"

>> ( , *>) parsec, LHS , , RHS. , :

a = optionMaybe $ do
       s <- try $ spaceornull *> string "hello"
       string $ "No"++s

( , <- - ) , . , , , !


, <|> , LHS t try. , , parsec , ckecked.

+4

Source: https://habr.com/ru/post/1696224/


All Articles