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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
module TransICode where
import Directory ( getAbsolutePath, getDirectoryContents )
import FilePath ( (</>), joinPath, splitDirectories, takeDirectory )
import IO ( stderr, hPutStrLn )
import List
import System
import ParseTypes
import qualified CIParser
import DummyParser as DummyParser
import FormatParser as FormatParser
import RegexParser as RegexParser
import MLTranslate as MLTranslate
import SQLConverter as SQLParser
ciparser :: Filename -> String -> IO (PM [StandardToken])
ciparser = CIParser.parse
parsers :: Maybe Langtag -> Either String ParserInfo -> LangParser
parsers = maybe iden pars
where
iden _ _ s = return $ cleanPM s
pars :: Langtag -> Either String ParserInfo -> LangParser
pars l model p =
case l of
"sql" -> case model of
Left err -> const (return $ throwPM p err)
_ -> SQLParser.parseSQL True model p
"sql*" -> case model of
Left err -> const (return $ throwPM p err)
_ -> SQLParser.parseSQL False model p
"dummy" -> DummyParser.parse p
"format" -> FormatParser.parse "" p
"printf" -> FormatParser.parse "putStr" p
"regex" -> RegexParser.parse p
"html" -> liftIO (mapWarnsPM (addRealFname (getFilename p))) .
MLTranslate.translate l p
"xml" -> liftIO (mapWarnsPM (addRealFname (getFilename p))) .
MLTranslate.translate l p
_ -> (\_ -> return $ throwPM p ("Bad langtag: " ++ l))
addRealFname :: Filename -> Warning -> Warning
addRealFname f w = setWarnPos w (setFilename (getWarnPos w) f)
formatErrors :: [PError] -> IO _
formatErrors [] =
error "Internal error in 'TransICode.formatErrors': No errors in list!"
formatErrors es@(e1:_) = do
hPutStrLn stderr $ "\nERRORS in " ++ getFilename (getPErrorPos e1) ++ ":"
++ concatMap formatErr es
error "Failure during preprocessing of Curry source file!"
where
formatErr :: PError -> String
formatErr e = "\n" ++ "Line " ++ show (getLn (getPErrorPos e))
++ " Col " ++ show (getCol (getPErrorPos e))
++ ": " ++ getPErrorMsg e
formatWarnings :: [Warning] -> String
formatWarnings [] = ""
formatWarnings ws@((p,_):_) = "\nWARNINGS in " ++ getFilename p ++ ":"
++ foldr (++) "" (map formatW ws)
++ "\n\n"
where
formatW :: Warning -> String
formatW w = "\n" ++ "Line " ++ show (getLn (getWarnPos w))
++ " Col " ++ show (getCol (getWarnPos w))
++ " | " ++ getWarnMsg w
translateIntCode :: Int -> String -> String -> String -> IO String
translateIntCode verb model fname s = do
pinfo <- tryReadParserInfoFile verb model fname
stw <- concatAllIOPM $ applyLangParsers pinfo
$ ciparser fname s
putStr (formatWarnings (getWarnings stw))
escapePR (discardWarnings stw) formatErrors
tryReadParserInfoFile :: Int -> String -> String
-> IO (Either String ParserInfo)
tryReadParserInfoFile verb model orgfname = do
if null model
then do orgdir <- getAbsolutePath (takeDirectory orgfname)
fresult <- findParserInfoFile (splitDirectories orgdir)
case fresult of
Left err -> return (Left err)
Right fname -> readParserInfo verb (orgdir </> fname)
else readParserInfo verb model
findParserInfoFile :: [String] -> IO (Either String String)
findParserInfoFile dirpath = do
let dir = joinPath dirpath
dirfiles <- getDirectoryContents dir
case filter ("_SQLCode.info" `isSuffixOf`) dirfiles of
[] -> let uppath = init dirpath
in if null uppath
then return (Left "No .info file provided or found!")
else findParserInfoFile uppath
[m] -> return (Right $ dir </> m)
ms -> return (Left $ "Multiple .info files found in directory '" ++ dir ++
"':\n" ++ unwords ms)
concatAllIOPM :: IO (PM [StandardToken]) -> IO (PM String)
concatAllIOPM ioprpt =
do prpt <- ioprpt
return $ liftPM (\pt -> concatAll pt) prpt
concatAll :: [StandardToken] -> String
concatAll [] = ""
concatAll (t1:tks) = getCode t1 ++ (concatAllHelper
(getIdentPos t1)
(containsDSL t1)
tks)
where
concatAllHelper :: Pos -> Bool -> [StandardToken] -> String
concatAllHelper _ _ [] = ""
concatAllHelper op b (t:toks) =
let s = getCode t
p = getIdentPos t
in if b
then
let lnDiff = lnDifference op p
in
if (null s)
then genLines lnDiff ++ concatAllHelper p (containsDSL t) toks
else
if (head s == '\n')
then (genLines lnDiff ++ s
++ concatAllHelper p (containsDSL t) toks)
else
let (headLine,restOfCurry) = splitByLine s
in
headLine ++ genLines lnDiff ++ restOfCurry
++ concatAllHelper p (containsDSL t) toks
else (s ++ concatAllHelper p (containsDSL t) toks)
genLines :: Int -> String
genLines = flip replicate '\n'
splitByLine :: String -> (String,String)
splitByLine s = splitByLineIter "" s
where
splitByLineIter acc "" = (reverse acc,"")
splitByLineIter acc (c:cs) | c == '\n' = (reverse ('\n':acc),cs)
| otherwise = splitByLineIter (c:acc) cs
applyLangParsers :: Either String ParserInfo
-> IO (PM [StandardToken])
-> IO (PM [StandardToken])
applyLangParsers model iotks = do
prtks <- iotks
prpr <- swapIOPM (liftPM (mapIO (applyLangParser model)) prtks)
return (crumplePM (liftPM (\prpt -> sequencePM prpt) prpr))
applyLangParser :: Either String ParserInfo
-> StandardToken
-> IO (PM StandardToken)
applyLangParser model (StTk p pexp l c) =
do parsedStringNoIO <- (parsers l model) pexp c
return (bindPM parsedStringNoIO (\s -> cleanPM (StTk p pexp l s)))
|