module Data.Focus ( Focus , toList , fromList , fromString , parse , split , strip , focus , contract , retract , unfocused ) whereimport Control.Arrow ( first )import Data.List ( intercalate )import Data.List.Split ( splitOn )import Data.String.Utils ( lstrip )data Focus = Focus { _list :: [Int] }instance Eq Focus where x == y = common x y == common y xinstance Ord Focus where xs <= ys = common xs ys <= common ys xscommon :: Focus -> Focus -> [Int]common xs ys = take enough $ toList xs where enough = min (length $ toList xs) (length $ toList ys)-- | Reading and Showinginstance Show Focus where show (Focus xs) = "<" ++ intercalate "|" (map show xs) ++ ">"instance Read Focus where readsPrec _ s = [(Focus xs, rest) | ("<", s') <- lex s , (xs, '>':rest) <- readInts s'] where readInts t = [(x : xs, rest) | (x, t') <- reads t , ("|", t'') <- lex t' , (xs, rest) <- readInts t''] ++ map (first return) (reads t)-- | Behaviorfocus :: Focus -> Focus -> Focusfocus (Focus xs) (Focus ys) = Focus $ xs ++ yscontract :: Focus -> Int -> Focuscontract (Focus xs) x = Focus $ xs ++ [x]retract :: Focus -> Focusretract (Focus []) = unfocusedretract (Focus xs) = Focus $ init xsunfocused :: Focusunfocused = fromList []-- | CreationtoList :: Focus -> [Int]toList = _listfromList :: [Int] -> FocusfromList = FocusfromString :: String -> Maybe FocusfromString = fst . split-- | Parsingseparators :: Stringseparators = " .,;:|-_"split :: String -> (Maybe Focus, String)split str = split' $ parse str where split' [] = (Nothing, str) split' ((f, (s:ss)):_) | s `elem` separators = (Just f, ss) split' ((f, s):_) = (Just f, s)strip :: String -> Stringstrip = snd . splitparse :: ReadS Focusparse s = [(Focus xs, rest) | (xs, rest) <- parseInts s]-- | Parser helpersparseInts :: ReadS [Int]parseInts s = separated ++ spaced ++ single where separated = [(x : xs, rest) | (x, s') <- parseInt s , (_, s'') <- parseSep s' , (xs, rest) <- parseInts s''] spaced = [(x : xs, rest) | (x, s') <- parseInt s , (xs, rest) <- parseInts s' ] single = [([x], rest) | (x, rest) <- parseInt s ]parseInt :: ReadS IntparseInt s = [(x, rest'++rest) | (tok, rest) <- dotlex s , (x, rest') <- reads tok]parseSep :: ReadS StringparseSep s = [(sep, rest) | (sep, rest) <- dotlex s , sep `elem` map return separators]-- A version of 'lex' that splits on dots as well, allowing us to parse-- something like "1.2.3" as multiple numbersdotlex :: ReadS Stringdotlex = dotlex' . lstrip where dotlex' ('.' : s) = [(".", s)] dotlex' s = [(a, b++rest) | (tok, rest) <- lex s , let (a:bs) = splitOn "." tok , let b = intercalate "." ("":bs)]