-
Notifications
You must be signed in to change notification settings - Fork 20
/
mkdocs.hs
213 lines (173 loc) · 6.61 KB
/
mkdocs.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE PatternSynonyms #-}
import Data.Bifunctor (bimap)
import Control.Arrow
import Control.Monad
import qualified Data.ByteString.Lazy.Char8 as L8
import Data.Char (isSpace)
import Data.Maybe (catMaybes, mapMaybe)
import Data.Monoid
import Jass.Parser
import Jass.Ast
import Jass.Types
import System.Environment (getArgs)
import System.IO
import Text.Megaparsec (parse, errorBundlePretty)
import Options.Applicative
import Annotation
pattern N x <- (L8.pack -> x)
pattern P x <- (fmap L8.pack -> x)
pattern P2 x <- (fmap (L8.pack***L8.pack) -> x)
handleToplevel :: FilePath -> Ast Annotations Toplevel -> L8.ByteString
handleToplevel file toplevel =
case toplevel of
Typedef doc (N name) _ -> L8.unlines [ delete name, handle doc name, attachFile file name]
Native doc _ (N name) (P2 params) r ->
L8.unlines [ delete name
, handle doc name
, paramOrdering name params
, attachFile file name
, returnType name r
]
Function doc _ (N name) (P2 params) r _ ->
L8.unlines [ delete name
, handle doc name
, paramOrdering name params
, attachFile file name
, returnType name r
]
Global (ADef doc (N name) (N ty)) -> L8.unlines
[ delete name
, handle doc name
, attachFile file name
]
Global (SDef doc isConst (N name) (N ty) _) -> L8.unlines
[ delete name
, handle doc name
, attachFile file name
]
_ -> ""
where
delete name = L8.unlines [
L8.unwords [ "delete from parameters where fnname = ", t name, ";" ]
, L8.unwords [ "delete from annotations where fnname = ", t name, ";" ]
, L8.unwords [ "delete from params_extra where fnname = ", t name, ";" ]
]
handle (Annotations anns) name =
let (params, annotations) = first (map $ extractParam . snd) $ split isParam anns
in L8.unlines [ L8.unlines $ map (uncurry $ insertParam name) params
, L8.unlines $ map (uncurry $ insertAnn name) annotations
]
returnType name (L8.pack -> r) =
L8.unwords [ "insert into annotations values("
, t name, ","
, t "return-type", ","
, t r
, ");"
]
attachFile (L8.pack -> file) name =
L8.unwords [ "insert into annotations values("
, t name, ","
, t "source-file", ","
, t file
, ");"
]
paramOrdering name params = L8.unlines $ zipWith (oneParam name) params [1..]
oneParam name (typ, param) idx = L8.unlines [
L8.unwords [ "insert into params_extra values ("
, t name, ","
, t param, ","
, t "param_order", ","
, L8.pack $ show idx
, ");"
]
, L8.unwords [ "insert into params_extra values ("
, t name, ","
, t param, ","
, t "param_type", ","
, t typ
, ");"
]
]
split :: (a -> Bool) -> [a] -> ([a], [a])
split pred = foldr ins mempty
where
ins elem (l, r)
| pred elem = (elem:l, r)
| otherwise = (l, elem:r)
extractParam xs =
let (name, descr) = L8.break isSpace xs
in (name, L8.dropWhile isSpace descr)
isParam = ("param" == ) . fst
insertParam name param value = L8.unwords
[ "insert into parameters (fnname, param, value) values ("
, L8.intercalate "," [t name, t param, t value ]
, ");"
]
insertAnn name ann value = L8.unwords
[ "insert into annotations (fnname, anname, value) values ("
, L8.intercalate "," [t name, t ann, t value ]
, ");"
]
chop = L8.dropWhileEnd (`elem` ['\r', '\n'])
t x = "'" <> escape (chop x) <> "'"
escape = L8.pack . concatMap e . L8.unpack
e '\'' = "''"
e x = [x]
emptyLine = L8.null . L8.filter (not . isVerticalSpace)
where
isVerticalSpace x = x `elem` [' ', '\t']
schema = L8.unlines
[ " create table if not exists parameters ( "
, " fnname text, "
, " param text, "
, " value text, "
, " primary key (fnname, param) "
, " ); "
, " create table if not exists annotations ( "
, " fnname text, "
, " anname text, "
, " value text "
, " ); "
, " create table if not exists params_extra ( "
, " fnname text, "
, " param text, "
, " anname text, "
, " value, "
, " primary key (fnname, param, anname) "
, " ); "
, " create index if not exists annotation_index "
, " on annotations(fnname); "
, " create table if not exists metadata ( "
, " key text primary key, "
, " value text "
, "); "
]
data Args = Args FilePath [FilePath]
argsParser :: Parser Args
argsParser =
Args <$> strOption (long "output" <> metavar "FILE" <> help "Write output to FILE")
<*> many (argument str (metavar "INPUTS..."))
opts :: ParserInfo Args
opts = info (argsParser <**> helper)
( fullDesc
<> progDesc "Process jassdoc'd jass files"
)
main = do
Args out args <- execParser opts
h <- openFile out WriteMode
L8.hPutStrLn h schema
forM_ args $ \file -> do
hPutStrLn stderr file
handle <- openFile file ReadMode
hSetBinaryMode handle True
x <- parse programm file <$> hGetContents handle -- closes handle
let toplevel = case x of
Right (Programm y) -> y
Left err -> error $ errorBundlePretty err
L8.hPutStrLn h "BEGIN TRANSACTION;"
mapM_ (L8.hPutStrLn h) . filter (not . emptyLine) $ map (handleToplevel file) toplevel
L8.hPutStrLn h "END TRANSACTION;"
hClose h