1
1
mirror of https://github.com/github/semantic.git synced 2024-12-25 07:55:12 +03:00

Add an advance rule to pass nodes.

This commit is contained in:
Rob Rix 2017-08-14 13:58:02 -04:00
parent 41d5ec6065
commit 965f716be1

View File

@ -74,6 +74,7 @@ module Data.Syntax.Assignment
, symbol , symbol
, source , source
, children , children
, advance
, while , while
, until , until
, manyThrough , manyThrough
@ -129,6 +130,7 @@ data AssignmentF ast grammar a where
Project :: HasCallStack => (forall x. F.Base ast x -> a) -> AssignmentF ast grammar a Project :: HasCallStack => (forall x. F.Base ast x -> a) -> AssignmentF ast grammar a
Source :: HasCallStack => AssignmentF ast grammar ByteString Source :: HasCallStack => AssignmentF ast grammar ByteString
Children :: HasCallStack => Assignment ast grammar a -> AssignmentF ast grammar a Children :: HasCallStack => Assignment ast grammar a -> AssignmentF ast grammar a
Advance :: HasCallStack => AssignmentF ast grammar ()
Choose :: HasCallStack => [grammar] -> IntMap.IntMap a -> AssignmentF ast grammar a Choose :: HasCallStack => [grammar] -> IntMap.IntMap a -> AssignmentF ast grammar a
Many :: HasCallStack => Assignment ast grammar a -> AssignmentF ast grammar [a] Many :: HasCallStack => Assignment ast grammar a -> AssignmentF ast grammar [a]
Alt :: HasCallStack => NonEmpty a -> AssignmentF ast grammar a Alt :: HasCallStack => NonEmpty a -> AssignmentF ast grammar a
@ -162,6 +164,8 @@ source = withFrozenCallStack $ Source `Then` return
children :: HasCallStack => Assignment ast grammar a -> Assignment ast grammar a children :: HasCallStack => Assignment ast grammar a -> Assignment ast grammar a
children forEach = withFrozenCallStack $ Children forEach `Then` return children forEach = withFrozenCallStack $ Children forEach `Then` return
advance :: HasCallStack => Assignment ast grammar ()
advance = withFrozenCallStack $ Advance `Then` return
-- | Collect a list of values passing a predicate. -- | Collect a list of values passing a predicate.
while :: (Alternative m, Monad m, HasCallStack) => (a -> Bool) -> m a -> m [a] while :: (Alternative m, Monad m, HasCallStack) => (a -> Bool) -> m a -> m [a]
@ -245,6 +249,7 @@ runAssignment toNode source = \ assignment state -> go assignment state >>= requ
Children child -> do Children child -> do
(a, state') <- go child state { stateNodes = toList node } >>= requireExhaustive (a, state') <- go child state { stateNodes = toList node } >>= requireExhaustive
yield a (advance state' { stateNodes = stateNodes }) yield a (advance state' { stateNodes = stateNodes })
Advance -> yield () (advance state)
Choose _ choices | Just choice <- IntMap.lookup (toIndex (nodeSymbol (toNode node))) choices -> yield choice state Choose _ choices | Just choice <- IntMap.lookup (toIndex (nodeSymbol (toNode node))) choices -> yield choice state
Catch during handler -> go during state `catchError` (flip go state . handler) >>= uncurry yield Catch during handler -> go during state `catchError` (flip go state . handler) >>= uncurry yield
_ -> anywhere (Just node) _ -> anywhere (Just node)
@ -261,6 +266,7 @@ runAssignment toNode source = \ assignment state -> go assignment state >>= requ
Project{} -> Left (makeError node) Project{} -> Left (makeError node)
Children{} -> Left (makeError node) Children{} -> Left (makeError node)
Source -> Left (makeError node) Source -> Left (makeError node)
Advance{} -> Left (makeError node)
Label child label -> go child state `catchError` (\ err -> throwError err { errorExpected = [Left label] }) >>= uncurry yield Label child label -> go child state `catchError` (\ err -> throwError err { errorExpected = [Left label] }) >>= uncurry yield
state@State{..} = if not (null expectedSymbols) && all ((== Regular) . symbolType) expectedSymbols then dropAnonymous initialState else initialState state@State{..} = if not (null expectedSymbols) && all ((== Regular) . symbolType) expectedSymbols then dropAnonymous initialState else initialState
@ -362,6 +368,7 @@ instance MonadError (Error (Either String grammar)) (Assignment ast grammar) whe
instance Show grammar => Show1 (AssignmentF ast grammar) where instance Show grammar => Show1 (AssignmentF ast grammar) where
liftShowsPrec sp sl d a = case a of liftShowsPrec sp sl d a = case a of
End -> showString "End" . showChar ' ' . sp d () End -> showString "End" . showChar ' ' . sp d ()
Advance -> showString "Advance" . showChar ' ' . sp d ()
Location -> showString "Location" . sp d (Info.Range 0 0 :. Info.Span (Info.Pos 1 1) (Info.Pos 1 1) :. Nil) Location -> showString "Location" . sp d (Info.Range 0 0 :. Info.Span (Info.Pos 1 1) (Info.Pos 1 1) :. Nil)
Project projection -> showsUnaryWith (const (const (showChar '_'))) "Project" d projection Project projection -> showsUnaryWith (const (const (showChar '_'))) "Project" d projection
Source -> showString "Source" . showChar ' ' . sp d "" Source -> showString "Source" . showChar ' ' . sp d ""