aboutsummaryrefslogtreecommitdiff
path: root/Toks.hs
blob: 3a27715081672af134f85a036617a154fea61b69 (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
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 _ = error "wat"

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

split =
  unlines
    . map (concatMap escape . markSpace)
    . groupBy ((==) `on` generalCategory)

glueToks = concatMap (unmarkSpace . unescape)

glue = glueToks . lines