aboutsummaryrefslogtreecommitdiff
path: root/lib/fonts/parseUnicodeMapping.hs
blob: 4f7ff692b77dbda7e67094967c0e88b51cdc4a81 (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
import System.FilePath
import Text.Parsec
import Data.Char
import System.Environment
import Control.Applicative hiding (many)
import Data.List

main :: IO ()
main = (head <$> getArgs) >>= parseUnicodeMapping


parseUnicodeMapping :: FilePath -> IO ()
parseUnicodeMapping fname = do
  fin <- readFile fname
  let mapname = dropExtension . takeFileName $ fname
  let res = runParse fin
  let header = "-- Generated from " ++ fname ++ "\n" ++
                mapname ++ " :: [(Char, Char)]\n" ++ mapname ++" =\n  [ "
  let footer = "]"
  writeFile (replaceExtension fname ".hs")
    (header ++ (concat $ intersperse "\n  , " (map show res)) ++ footer)

type Unicode = Char

runParse :: String -> [(Char, Unicode)]
runParse s=  either (error . show) id (parse parseMap "" s)

anyline = manyTill anyChar newline

getHexChar :: Parsec String () Char
getHexChar = do
  [(c,_)] <- readLitChar . ("\\x" ++) <$> many1 hexDigit
  return c

parseMap :: Parsec String () [(Char, Unicode)]
parseMap = do
  skipMany (char '#' >> anyline)
  many (flip (,) <$> getHexChar <* tab <*> getHexChar <* anyline)