module Config.Tokens
( Token(..)
, Located(..)
, Position(..)
, layoutPass
) where
import Data.Text (Text)
data Position = Position
{ posLine, posColumn :: !Int }
deriving (Read, Show)
data Located a = Located
{ locPosition :: !Position
, locThing :: !a
}
deriving (Read, Show)
instance Functor Located where
fmap f (Located p x) = Located p (f x)
data Token
= Section Text
| String Text
| Bullet
| Comma
| Number Int Integer
| OpenList
| CloseList
| OpenMap
| CloseMap
| Yes
| No
| Error
| LayoutSep
| LayoutEnd
| EOF
deriving (Show)
layoutPass ::
[Located Token] ->
[Located Token]
layoutPass toks = foldr step (\_ -> []) toks [0]
step :: Located Token -> ([Int] -> [Located Token]) -> ([Int] -> [Located Token])
step t next (col:cols)
| tokenCol > col && usesLayout t = t : next (tokenCol:col:cols)
| tokenCol == col && usesLayout t = t{locThing=LayoutSep} : t : next (col:cols)
| tokenCol <= col = t{locThing=LayoutEnd} : step t next cols
where
tokenCol = posColumn (locPosition t)
step t next cols = t : next cols
usesLayout :: Located Token -> Bool
usesLayout t
| Section{} <- locThing t = True
| Bullet <- locThing t = True
| otherwise = False