From 4daab99a09e33e9924c61322f7f6d742e18f5450 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 16 Aug 2017 12:48:39 -0400 Subject: [PATCH] Revert "Define eof in terms of state." This reverts commit b71a285f23f1a7112173f03a2041bebaae637940. --- src/Data/Syntax/Assignment.hs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Data/Syntax/Assignment.hs b/src/Data/Syntax/Assignment.hs index 6c4a250ee..ba1e63bb5 100644 --- a/src/Data/Syntax/Assignment.hs +++ b/src/Data/Syntax/Assignment.hs @@ -131,6 +131,7 @@ type Assignment ast grammar = Freer (AssignmentF ast grammar) data AssignmentF ast grammar a where Get :: AssignmentF ast grammar (State ast grammar) Put :: State ast grammar -> AssignmentF ast grammar () + End :: HasCallStack => AssignmentF ast grammar () Source :: HasCallStack => AssignmentF ast grammar ByteString Children :: HasCallStack => Assignment ast grammar a -> AssignmentF ast grammar a Choose :: HasCallStack => [grammar] -> IntMap.IntMap a -> AssignmentF ast grammar a @@ -264,6 +265,7 @@ runAssignment source = \ assignment state -> go assignment state >>= requireExha anywhere node = case assignment of Get -> yield state state Put s -> yield () s + End -> requireExhaustive ((), state) >>= uncurry yield Many rule -> fix (\ recur state -> (go rule state >>= \ (a, state') -> first (a:) <$> if state == state' then pure ([], state') else recur state') `catchError` const (pure ([], state))) state >>= uncurry yield Alt as -> sconcat (flip yield state <$> as) Throw e -> Left (fromMaybe (makeError node) e) @@ -350,7 +352,7 @@ instance (Eq grammar, Eq (ast (AST ast grammar))) => Alternative (Assignment ast many :: HasCallStack => Assignment ast grammar a -> Assignment ast grammar [a] many a = Many a `Then` return -instance (Eq grammar, Symbol grammar, Eq (ast (AST ast grammar)), Show grammar, Show (ast (AST ast grammar))) => Parsing (Assignment ast grammar) where +instance (Eq grammar, Eq (ast (AST ast grammar)), Show grammar, Show (ast (AST ast grammar))) => Parsing (Assignment ast grammar) where try :: HasCallStack => Assignment ast grammar a -> Assignment ast grammar a try = id @@ -361,9 +363,7 @@ instance (Eq grammar, Symbol grammar, Eq (ast (AST ast grammar)), Show grammar, unexpected s = location >>= \ loc -> throwError (Error (Info.sourceSpan loc) [] (Just (Left s))) eof :: HasCallStack => Assignment ast grammar () - eof = do - state <- get - guard (not (null (stateNodes (skipTokens state)))) + eof = withFrozenCallStack $ End `Then` return notFollowedBy :: (HasCallStack, Show a) => Assignment ast grammar a -> Assignment ast grammar () notFollowedBy a = a *> unexpected (show a) <|> pure () @@ -383,6 +383,7 @@ instance (Show grammar, Show (ast (AST ast grammar))) => Show1 (AssignmentF ast liftShowsPrec sp sl d a = case a of Get -> showString "Get" Put s -> showsUnaryWith showsPrec "Put" d s + End -> showString "End" . showChar ' ' . sp d () Source -> showString "Source" . showChar ' ' . sp d "" Children a -> showsUnaryWith (liftShowsPrec sp sl) "Children" d a Choose symbols choices -> showsBinaryWith showsPrec (const (liftShowList sp sl)) "Choose" d symbols (IntMap.toList choices)