forked from GaloisInc/cryptol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryHtml.hs
executable file
·64 lines (56 loc) · 1.78 KB
/
CryHtml.hs
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
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env runhaskell
-- |
-- Module : $Header$
-- Copyright : (c) 2013-2015 Galois, Inc.
-- License : BSD3
-- Maintainer : [email protected]
-- Stability : provisional
-- Portability : portable
import Cryptol.Parser.Lexer
import Cryptol.Utils.PP
main :: IO ()
main = interact (wrap . concat . map toHTML . fst . primLexer defaultConfig)
wrap :: String -> String
wrap txt = "<html><head>" ++ sty ++ "</head><body>" ++ txt ++ "</body>"
toHTML :: Located Token -> String
toHTML tok = "<span class=\"" ++ cl ++ "\" title=\"" ++ pos ++ "\">"
++ concatMap esc (tokenText (thing tok))
++ "</span>"
where
pos = show (pp (srcRange tok)) ++ " " ++ show (tokenType (thing tok))
cl = case tokenType (thing tok) of
Num {} -> "number"
Ident {} -> "identifier"
KW {} -> "keyword"
Op {} -> "operator"
Sym {} -> "symbol"
Virt {} -> "virtual"
White Space -> "white"
White _ -> "comment"
Err {} -> "error"
EOF -> "eof"
StrLit {} -> "text"
ChrLit {} -> "text"
esc c = case c of
'<' -> "<"
'>' -> ">"
'&' -> "&"
' ' -> " "
'\n' -> "<br>"
_ -> [c]
sty :: String
sty = unlines
[ "<style>"
, "body { font-family: monospace }"
, ".number { color: #cc00cc }"
, ".identifier { }"
, ".keyword { color: blue; }"
, ".operator { color: #cc00cc }"
, ".symbol { color: blue }"
, ".white { }"
, ".virtual { background-color: red }"
, ".comment { color: green }"
, ".error { color: red }"
, ".text { color: #cc00cc }"
, "</style>"
]