forked from lep/jassdoc
/
mkdocs.hs
220 lines (178 loc) · 6.94 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
213
214
215
216
217
218
219
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE PatternSynonyms #-}
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)
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 (Maybe String) Toplevel -> L8.ByteString
handleToplevel file toplevel =
case toplevel of
Typedef (P doc) (N name) _ -> L8.unlines [ delete name, handle doc name, attachFile file name]
Native (P doc) _ (N name) (P2 params) r ->
L8.unlines [ delete name
, handle doc name
, paramOrdering name params
, attachFile file name
, returnType name r
]
Function (P doc) _ (N name) (P2 params) r _ ->
L8.unlines [ delete name
, handle doc name
, paramOrdering name params
, attachFile file name
, returnType name r
]
_ -> ""
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 (Just doc) name =
let (descr, anns) = parseDocstring doc
(params, annotations) = first (map $ extractParam . snd) $ split isParam anns
in L8.unlines [ insertDescr name descr
, L8.unlines $ map (uncurry $ insertParam name) params
, L8.unlines $ map (uncurry $ insertAnn name) annotations
]
handle _ _ = ""
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 pred xs = foldr ins mempty xs
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
insertDescr name descr
| L8.null descr = L8.empty
| otherwise = L8.unwords
[ "insert into annotations (fnname, anname, value) values ("
, L8.intercalate "," [t name, t "comment", t descr]
, ");"
]
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 ]
, ");"
]
t x = "'" <> escape x <> "'"
escape = L8.pack . concatMap e . L8.unpack
e '\'' = "''"
e x = [x]
trimWhitespace =
{- L8.reverse
. L8.dropWhile (\x -> x=='\r' || x == '\n') -- drop back newline
. L8.reverse
. -} L8.dropWhile (\x -> x=='\r' || x == '\n') -- drop front newline
type Accumulator = ([L8.ByteString], [(L8.ByteString, [L8.ByteString])])
myUnlines = L8.intercalate "\n"
parseDocstring = first (trimWhitespace . myUnlines . reverse)
. second (map (second $ myUnlines . reverse ) )
. flip go mempty
. L8.lines
where
go :: [L8.ByteString] -> Accumulator -> Accumulator
go [] acc = acc
go (x:xs) (descr, annotations)
| Just ann <- getAnn x = go xs (descr, ann:annotations)
| null annotations = go xs (x:descr, annotations)
| otherwise =
let (anTag, anLines):as = annotations
in go xs (descr, (anTag, x:anLines):as)
getAnn line =
let words = map L8.unpack $ L8.words line
in case words of
('@':ann):text -> Just (L8.pack ann, [trimWhitespace . L8.pack $ unwords text])
_ -> Nothing
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); "
]
main = do
args <- getArgs
L8.putStrLn 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 :: [Ast (Maybe String) Toplevel]
toplevel = case x of
Right (Programm y) -> y
Left err -> error $ errorBundlePretty err
L8.putStrLn "BEGIN TRANSACTION;"
mapM_ L8.putStrLn . filter (not . emptyLine) $ map (handleToplevel file) toplevel
L8.putStrLn "END TRANSACTION;"