From 07b132e631efe6b2939e7eeff55d4638f198de2f Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Sat, 22 Jul 2017 13:44:51 -0400 Subject: [PATCH] Define a combinator requiring exhaustiveness. --- src/Data/Syntax/Assignment.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Data/Syntax/Assignment.hs b/src/Data/Syntax/Assignment.hs index a1f7ed41d..602e32062 100644 --- a/src/Data/Syntax/Assignment.hs +++ b/src/Data/Syntax/Assignment.hs @@ -308,6 +308,13 @@ runAssignment source toNode = go | otherwise -> ([a], state') {-# INLINE runMany #-} +requireExhaustive :: (Symbol grammar, Recursive ast) => (forall x. Base ast x -> Node grammar) -> (a, AssignmentState ast grammar) -> Either (Error grammar) (a, AssignmentState ast grammar) +requireExhaustive toNode (a, state) = case stateNodes (dropAnonymous toNode state) of + [] -> Right (a, state) + node : _ | Node nodeSymbol _ (Info.Span spanStart _) <- toNode (F.project node) -> + Left $ fromMaybe (Error spanStart (UnexpectedSymbol [] nodeSymbol)) (stateError state) + + dropAnonymous :: (Symbol grammar, Recursive ast) => (forall x. Base ast x -> Node grammar) -> AssignmentState ast grammar -> AssignmentState ast grammar dropAnonymous toNode state = state { stateNodes = dropWhile ((/= Regular) . symbolType . nodeSymbol . toNode . F.project) (stateNodes state) }