mirror of
https://github.com/github/semantic.git
synced 2024-12-22 06:11:49 +03:00
Rename Jump to Choose.
This commit is contained in:
parent
7c01239a0d
commit
ca16c9c0c3
@ -133,7 +133,7 @@ data AssignmentF ast grammar a where
|
||||
Source :: AssignmentF ast grammar ByteString
|
||||
Children :: Assignment ast grammar a -> AssignmentF ast grammar a
|
||||
Advance :: AssignmentF ast grammar ()
|
||||
Jump :: [grammar] -> Array grammar (Maybe a) -> Maybe a -> AssignmentF ast grammar a
|
||||
Choose :: [grammar] -> Array grammar (Maybe a) -> Maybe a -> AssignmentF ast grammar a
|
||||
Many :: Assignment ast grammar a -> AssignmentF ast grammar [a]
|
||||
Alt :: [a] -> AssignmentF ast grammar a
|
||||
Throw :: Error (Either String grammar) -> AssignmentF ast grammar a
|
||||
@ -164,7 +164,7 @@ currentNode = tracing CurrentNode `Then` return
|
||||
|
||||
-- | Zero-width match of a node with the given symbol, producing the current node’s location.
|
||||
symbol :: (Bounded grammar, Ix grammar, HasCallStack) => grammar -> Assignment ast grammar (Record Location)
|
||||
symbol s = tracing (Jump [s] (array (s, s) [(s, Just location)]) Nothing) `Then` id
|
||||
symbol s = tracing (Choose [s] (array (s, s) [(s, Just location)]) Nothing) `Then` id
|
||||
|
||||
-- | A rule to produce a node’s source as a ByteString.
|
||||
source :: HasCallStack => Assignment ast grammar ByteString
|
||||
@ -182,11 +182,11 @@ choice :: (Bounded grammar, Enum grammar, Eq (ast (AST ast grammar)), Ix grammar
|
||||
choice [] = empty
|
||||
choice alternatives
|
||||
| null symbols = asum alternatives
|
||||
| otherwise = tracing (Jump symbols (accumArray merge Nothing bounds choices) (wrap . tracing . Alt . toList <$> nonEmpty atEnd)) `Then` id
|
||||
| otherwise = tracing (Choose symbols (accumArray merge Nothing bounds choices) (wrap . tracing . Alt . toList <$> nonEmpty atEnd)) `Then` id
|
||||
where (symbols, choices, atEnd) = foldMap toChoices alternatives
|
||||
toChoices :: (Enum grammar, Ix grammar) => Assignment ast grammar a -> ([grammar], [(grammar, Assignment ast grammar a)], [Assignment ast grammar a])
|
||||
toChoices rule = case rule of
|
||||
Tracing _ (Jump s c a) `Then` continue -> (s, ((id &&& (c !)) <$> s) >>= \ (sym, a) -> (,) sym . continue <$> toList a, toList (fmap continue a))
|
||||
Tracing _ (Choose s c a) `Then` continue -> (s, ((id &&& (c !)) <$> s) >>= \ (sym, a) -> (,) sym . continue <$> toList a, toList (fmap continue a))
|
||||
Tracing _ (Many child) `Then` _ -> let (s, c, _) = toChoices child in (s, fmap (rule <$) c, [rule])
|
||||
Tracing _ (Catch child _) `Then` _ -> let (s, c, _) = toChoices child in (s, fmap (rule <$) c, [rule])
|
||||
Tracing _ (Label child _) `Then` _ -> let (s, c, _) = toChoices child in (s, fmap (rule <$) c, [rule])
|
||||
@ -239,7 +239,7 @@ nodeError expected Node{..} = Error nodeSpan expected (Just (Right nodeSymbol))
|
||||
|
||||
firstSet :: Assignment ast grammar a -> [grammar]
|
||||
firstSet = iterFreer (\ (Tracing _ assignment) _ -> case assignment of
|
||||
Jump symbols _ _ -> symbols
|
||||
Choose symbols _ _ -> symbols
|
||||
Catch during _ -> firstSet during
|
||||
Label child _ -> firstSet child
|
||||
_ -> []) . ([] <$)
|
||||
@ -279,7 +279,7 @@ runAssignment source = \ assignment state -> go assignment state >>= requireExha
|
||||
(a, state') <- go child state { stateNodes = toList f, stateCallSites = maybe id (:) (tracingCallSite t) stateCallSites } >>= requireExhaustive (tracingCallSite t)
|
||||
yield a (advanceState state' { stateNodes = stateNodes, stateCallSites = stateCallSites })
|
||||
Advance -> yield () (advanceState state)
|
||||
Jump _ choices _ | bounds choices `inRange` nodeSymbol node, Just choice <- choices ! nodeSymbol node -> yield choice state
|
||||
Choose _ choices _ | bounds choices `inRange` nodeSymbol node, Just choice <- choices ! nodeSymbol node -> yield choice state
|
||||
Catch during handler -> go during state `catchError` (flip go state . handler) >>= uncurry yield
|
||||
_ -> anywhere (Just node)
|
||||
|
||||
@ -291,7 +291,7 @@ runAssignment source = \ assignment state -> go assignment state >>= requireExha
|
||||
Throw e -> Left e
|
||||
Catch during _ -> go during state >>= uncurry yield
|
||||
Label child label -> go child state `catchError` (\ err -> throwError err { errorExpected = [Left label] }) >>= uncurry yield
|
||||
Jump _ _ (Just atEnd) | Nothing <- node -> yield atEnd state
|
||||
Choose _ _ (Just atEnd) | Nothing <- node -> yield atEnd state
|
||||
_ -> Left (makeError node)
|
||||
|
||||
state@State{..} = if not (null expectedSymbols) && all ((== Regular) . symbolType) expectedSymbols then skipTokens initialState else initialState
|
||||
@ -399,7 +399,7 @@ instance (Ix grammar, Show grammar, Show (ast (AST ast grammar))) => Show1 (Assi
|
||||
CurrentNode -> showString "CurrentNode"
|
||||
Source -> showString "Source" . showChar ' ' . sp d ""
|
||||
Children a -> showsUnaryWith (liftShowsPrec sp sl) "Children" d a
|
||||
Jump symbols choices atEnd -> showsTernaryWith showsPrec (const (liftShowList sp sl)) (liftShowsPrec sp sl) "Jump" d symbols (assocs choices >>= \ (sym, a) -> (,) sym <$> toList a) atEnd
|
||||
Choose symbols choices atEnd -> showsTernaryWith showsPrec (const (liftShowList sp sl)) (liftShowsPrec sp sl) "Choose" d symbols (assocs choices >>= \ (sym, a) -> (,) sym <$> toList a) atEnd
|
||||
Many a -> showsUnaryWith (liftShowsPrec (\ d a -> sp d [a]) (sl . pure)) "Many" d a
|
||||
Alt as -> showsUnaryWith (const sl) "Alt" d (toList as)
|
||||
Throw e -> showsUnaryWith showsPrec "Throw" d e
|
||||
|
Loading…
Reference in New Issue
Block a user