aboutsummaryrefslogtreecommitdiff
path: root/Toks.hs
blob: 29fa3813a05fa37783818f5933bd208eba0c6f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Toks where

import Data.Char
import Data.Function
import Data.List

escape '\\' = "\\\\"
escape '\n' = "\\n"
escape x = pure x

unescape [] = []
unescape ('\\':'\\':xs) = '\\' : unescape xs
unescape ('\\':'n':xs) = '\n' : unescape xs
unescape ('\\':_) = error "bad escape?"
unescape (x:xs) = x : unescape xs

markSpace [] = error "wat"
markSpace s@(c:_)
  | isSpace c = '.' : s
  | otherwise = '|' : s

unmarkSpace ('.':s) = s
unmarkSpace ('|':s) = s
unmarkSpace x = error "unwat"

space ('.':_) = True
space _ = False

joinSpaces [] = []
joinSpaces (a@('.':as):xs) =
  case joinSpaces xs of
    (('.':bs):xs') -> ('.' : (as ++ bs)) : xs'
    xs' -> a : xs'
joinSpaces (x:xs) = x : joinSpaces xs

splitCategory = make . groupBy ((==) `on` generalCategory)

simpleCategory c
  | isSpace c = 0
  | isAlpha c = 1
  | isNumber c = 2
  | otherwise = 3

splitSimple = make . groupBy ((==) `on` simpleCategory)

make = map (concatMap escape . markSpace)

glue :: [String] -> String
glue = concatMap (unmarkSpace . unescape)

fromFile = lines

toFile = unlines