diff options
| author | Mirek Kratochvil <miroslav.kratochvil@uni.lu> | 2025-07-15 15:40:36 +0200 |
|---|---|---|
| committer | Mirek Kratochvil <miroslav.kratochvil@uni.lu> | 2025-07-15 15:40:36 +0200 |
| commit | 40dfb86e7246a40d01fb518578401702ec9f135c (patch) | |
| tree | 67baadd60eed0143bb2d22adab56376c04c6378a | |
| parent | 9dfe7b924d58dcb082c614647dda4517ffeabb23 (diff) | |
| download | werge-40dfb86e7246a40d01fb518578401702ec9f135c.tar.gz werge-40dfb86e7246a40d01fb518578401702ec9f135c.tar.bz2 | |
type toks
| -rw-r--r-- | Toks.hs | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -4,28 +4,36 @@ import Data.Char import Data.Function import Data.List +type Tok = String + +escape :: Char -> String escape '\\' = "\\\\" escape '\n' = "\\n" escape x = pure x +unescape :: String -> String unescape [] = [] unescape ('\\':'\\':xs) = '\\' : unescape xs unescape ('\\':'n':xs) = '\n' : unescape xs unescape ('\\':_) = error "bad escape?" unescape (x:xs) = x : unescape xs +markSpace :: String -> Tok markSpace [] = error "wat" markSpace s@(c:_) | isSpace c = '.' : s | otherwise = '|' : s +unmarkSpace :: Tok -> String unmarkSpace ('.':s) = s unmarkSpace ('|':s) = s unmarkSpace x = error "unwat" +space :: Tok -> Bool space ('.':_) = True space _ = False +joinSpaces :: [Tok] -> [Tok] joinSpaces [] = [] joinSpaces (a@('.':as):xs) = case joinSpaces xs of @@ -33,21 +41,27 @@ joinSpaces (a@('.':as):xs) = xs' -> a : xs' joinSpaces (x:xs) = x : joinSpaces xs +splitCategory :: String -> [Tok] splitCategory = make . groupBy ((==) `on` generalCategory) +simpleCategory :: Char -> Int simpleCategory c | isSpace c = 0 | isAlpha c = 1 | isNumber c = 2 | otherwise = 3 +splitSimple :: String -> [Tok] splitSimple = make . groupBy ((==) `on` simpleCategory) +make :: [String] -> [Tok] make = map (concatMap escape . markSpace) glue :: [String] -> String glue = concatMap (unmarkSpace . unescape) +fromFile :: String -> [Tok] fromFile = lines +toFile :: [Tok] -> String toFile = unlines |
