Skip to content

consumeWith: propagate consumed flag when it was set #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Parsing/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ consumeWith
. (String -> Either String { value :: a, consumed :: String, remainder :: String })
-> ParserT String m a
consumeWith f = ParserT
( mkFn5 \state1@(ParseState input pos _) _ _ throw done ->
( mkFn5 \state1@(ParseState input pos oldConsumed) _ _ throw done ->
case f input of
Left err ->
runFn2 throw state1 (ParseError err pos)
Right { value, consumed, remainder } ->
runFn2 done (ParseState remainder (updatePosString pos consumed remainder) (not (String.null consumed))) value
runFn2 done (ParseState remainder (updatePosString pos consumed remainder) (oldConsumed || not (String.null consumed))) value
)

-- | Combinator which finds the first position in the input `String` where the
Expand Down
13 changes: 12 additions & 1 deletion test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import Effect.Console (log, logShow)
import Effect.Unsafe (unsafePerformEffect)
import Node.Process (lookupEnv)
import Parsing (ParseError(..), ParseState(..), Parser, ParserT, Position(..), consume, fail, getParserT, initialPos, parseErrorPosition, position, region, runParser)
import Parsing.Combinators (advance, between, chainl, chainl1, chainr, chainr1, choice, empty, endBy, endBy1, lookAhead, many, many1, many1Till, many1Till_, manyIndex, manyTill, manyTill_, notFollowedBy, optionMaybe, replicateA, sepBy, sepBy1, sepEndBy, sepEndBy1, skipMany, skipMany1, try, tryRethrow, (<?>), (<??>), (<~?>))
import Parsing.Combinators (advance, between, chainl, chainl1, chainr, chainr1, choice, empty, endBy, endBy1, lookAhead, many, many1, many1Till, many1Till_, manyIndex, manyTill, manyTill_, notFollowedBy, optional, optionMaybe, replicateA, sepBy, sepBy1, sepEndBy, sepEndBy1, skipMany, skipMany1, try, tryRethrow, (<?>), (<??>), (<~?>))
import Parsing.Combinators.Array as Combinators.Array
import Parsing.Expr (Assoc(..), Operator(..), buildExprParser)
import Parsing.Language (haskellDef, haskellStyle, javaStyle)
Expand Down Expand Up @@ -572,6 +572,15 @@ javaStyleTest = do
"hello {- comment\n -} foo"
(mkPos 7)

takeWhilePropagateFail :: TestM
takeWhilePropagateFail = do
-- `takeWhile` always succeeds, but if input was consumed prior and failure happens
-- later, then the failure with consumption should propagate past `optional` #236
parseErrorTestPosition
(optional (char 'f' <* takeWhile (const false) <* fail "failure"))
"f"
(Position { index: 1, line: 1, column: 2 })

main :: Effect Unit
main = do

Expand Down Expand Up @@ -734,6 +743,8 @@ main = do
, expected: Left $ ParseError "Expected letter" (Position { index: 0, line: 1, column: 1 })
}

takeWhilePropagateFail

log "\nTESTS number\n"

-- assert' "Number.fromString" $ Just infinity == Data.Number.fromString "Infinity"
Expand Down