1
1
mirror of https://github.com/github/semantic.git synced 2024-12-25 07:55:12 +03:00
This commit is contained in:
Patrick Thomson 2018-09-12 12:28:58 -04:00
parent 47f6c1beb1
commit fd4abae735

View File

@ -73,27 +73,25 @@ instance Applicative Tokenizer where
instance Monad Tokenizer where (>>=) = Bind instance Monad Tokenizer where (>>=) = Bind
data Strategy data Strategy
= Reprinting = Reprinting
| PrettyPrinting | PrettyPrinting
deriving (Eq, Show) deriving (Eq, Show)
data State = State data State = State
{ _source :: Source -- We need to be able to slice { source :: Source -- We need to be able to slice
, _history :: History -- What's the history of the term we're examining , history :: History -- What's the history of the term we're examining
, _strategy :: Strategy -- What are we doing right now? , strategy :: Strategy -- What are we doing right now?
, _cursor :: Int -- Where do we begin slices? , cursor :: Int -- Where do we begin slices?
, _enabled :: Bool -- Should we ignore ddata tokens , enabled :: Bool -- Should we ignore data tokens?
} deriving (Show, Eq) } deriving (Show, Eq)
-- Builtins -- Builtins
-- | Yield an 'Element' token in a 'Tokenizer' context. -- | Yield an 'Element' token in a 'Tokenizer' context.
yield :: Element -> Tokenizer () yield :: Element -> Tokenizer ()
yield e = do yield e = do
on <- _enabled <$> Get on <- enabled <$> Get
when on . Tell . TElement $ e when on . Tell . TElement $ e
-- | Yield a 'Control' token. -- | Yield a 'Control' token.
@ -107,9 +105,9 @@ chunk = Tell . Chunk
-- | Ensures that the final chunk is emitted -- | Ensures that the final chunk is emitted
finish :: Tokenizer () finish :: Tokenizer ()
finish = do finish = do
crs <- asks _cursor crs <- asks cursor
log ("Finishing, cursor is " <> show crs) log ("Finishing, cursor is " <> show crs)
src <- asks _source src <- asks source
chunk (dropSource crs src) chunk (dropSource crs src)
-- State handling -- State handling
@ -121,28 +119,28 @@ modify :: (State -> State) -> Tokenizer ()
modify f = Get >>= \x -> Put . f $! x modify f = Get >>= \x -> Put . f $! x
enable, disable :: Tokenizer () enable, disable :: Tokenizer ()
enable = modify (\x -> x { _enabled = True }) enable = modify (\x -> x { enabled = True })
disable = modify (\x -> x { _enabled = False}) disable = modify (\x -> x { enabled = False})
move :: Int -> Tokenizer () move :: Int -> Tokenizer ()
move c = modify (\x -> x { _cursor = c }) move c = modify (\x -> x { cursor = c })
withHistory :: (Annotated t (Record fields), HasField fields History) withHistory :: (Annotated t (Record fields), HasField fields History)
=> t => t
-> Tokenizer a -> Tokenizer a
-> Tokenizer a -> Tokenizer a
withHistory t act = do withHistory t act = do
old <- asks _history old <- asks history
modify (\x -> x { _history = getField (annotation t)}) modify (\x -> x { history = getField (annotation t)})
act <* modify (\x -> x { _history = old }) act <* modify (\x -> x { history = old })
withStrategy :: Strategy -> Tokenizer a -> Tokenizer a withStrategy :: Strategy -> Tokenizer a -> Tokenizer a
withStrategy s act = do withStrategy s act = do
old <- Get old <- Get
Put (old { _strategy = s }) Put (old { strategy = s })
res <- act res <- act
new <- Get new <- Get
Put (new { _strategy = _strategy old }) Put (new { strategy = strategy old })
pure res pure res
-- The reprinting algorithm. -- The reprinting algorithm.