author | unc0rr |
Fri, 07 Feb 2014 14:41:49 +0400 | |
changeset 10119 | 7e05a397602f |
parent 10113 | b26c2772e754 |
child 10120 | b7f632c12784 |
permissions | -rw-r--r-- |
10015 | 1 |
{-# LANGUAGE ScopedTypeVariables #-} |
2 |
module PascalPreprocessor where |
|
3 |
||
4 |
import Text.Parsec |
|
5 |
import Control.Monad.IO.Class |
|
6 |
import Control.Monad |
|
7 |
import System.IO |
|
8 |
import qualified Data.Map as Map |
|
10119 | 9 |
import qualified Control.Exception as E |
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
10 |
|
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
11 |
char' :: Char -> ParsecT String u IO () |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
12 |
char' = void . char |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
13 |
|
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
14 |
string' :: String -> ParsecT String u IO () |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
15 |
string' = void . string |
10015 | 16 |
|
17 |
-- comments are removed |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
18 |
comment :: ParsecT String u IO String |
10015 | 19 |
comment = choice [ |
20 |
char '{' >> notFollowedBy (char '$') >> manyTill anyChar (try $ char '}') >> return "" |
|
21 |
, (try $ string "(*") >> manyTill anyChar (try $ string "*)") >> return "" |
|
22 |
, (try $ string "//") >> manyTill anyChar (try newline) >> return "\n" |
|
23 |
] |
|
24 |
||
25 |
preprocess :: String -> String -> String -> [String] -> IO String |
|
26 |
preprocess inputPath alternateInputPath fn symbols = do |
|
27 |
r <- runParserT (preprocessFile (inputPath ++ fn)) (Map.fromList $ map (\s -> (s, "")) symbols, [True]) "" "" |
|
28 |
case r of |
|
29 |
(Left a) -> do |
|
30 |
hPutStrLn stderr (show a) |
|
31 |
return "" |
|
32 |
(Right a) -> return a |
|
33 |
||
34 |
where |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
35 |
preprocessFile fn' = do |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
36 |
f <- liftIO (readFile fn') |
10015 | 37 |
setInput f |
38 |
preprocessor |
|
39 |
||
40 |
preprocessor, codeBlock, switch :: ParsecT String (Map.Map String String, [Bool]) IO String |
|
41 |
||
42 |
preprocessor = chainr codeBlock (return (++)) "" |
|
43 |
||
44 |
codeBlock = do |
|
45 |
s <- choice [ |
|
46 |
switch |
|
47 |
, comment |
|
48 |
, char '\'' >> many (noneOf "'\n") >>= \s -> char '\'' >> return ('\'' : s ++ "'") |
|
49 |
, identifier >>= replace |
|
50 |
, noneOf "{" >>= \a -> return [a] |
|
51 |
] |
|
52 |
(_, ok) <- getState |
|
53 |
return $ if and ok then s else "" |
|
54 |
||
55 |
--otherChar c = c `notElem` "{/('_" && not (isAlphaNum c) |
|
56 |
identifier = do |
|
57 |
c <- letter <|> oneOf "_" |
|
58 |
s <- many (alphaNum <|> oneOf "_") |
|
59 |
return $ c:s |
|
60 |
||
61 |
switch = do |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
62 |
try $ string' "{$" |
10015 | 63 |
s <- choice [ |
64 |
include |
|
65 |
, ifdef |
|
66 |
, if' |
|
67 |
, elseSwitch |
|
68 |
, endIf |
|
69 |
, define |
|
70 |
, unknown |
|
71 |
] |
|
72 |
return s |
|
73 |
||
74 |
include = do |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
75 |
try $ string' "INCLUDE" |
10015 | 76 |
spaces |
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
77 |
(char' '"') |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
78 |
ifn <- many1 $ noneOf "\"\n" |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
79 |
char' '"' |
10015 | 80 |
spaces |
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
81 |
char' '}' |
10119 | 82 |
f <- liftIO (readFile (inputPath ++ ifn) |
83 |
`E.catch` (\(_ :: E.IOException) -> readFile (alternateInputPath ++ ifn) |
|
84 |
`E.catch` (\(_ :: E.IOException) -> error ("File not found: " ++ fn)))) |
|
10015 | 85 |
c <- getInput |
86 |
setInput $ f ++ c |
|
87 |
return "" |
|
88 |
||
89 |
ifdef = do |
|
90 |
s <- try (string "IFDEF") <|> try (string "IFNDEF") |
|
91 |
let f = if s == "IFNDEF" then not else id |
|
92 |
||
93 |
spaces |
|
94 |
d <- identifier |
|
95 |
spaces |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
96 |
char' '}' |
10015 | 97 |
|
98 |
updateState $ \(m, b) -> |
|
99 |
(m, (f $ d `Map.member` m) : b) |
|
100 |
||
101 |
return "" |
|
102 |
||
103 |
if' = do |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
104 |
try (string' "IF" >> notFollowedBy alphaNum) |
10015 | 105 |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
106 |
void $ manyTill anyChar (char' '}') |
10015 | 107 |
--char '}' |
108 |
||
109 |
updateState $ \(m, b) -> |
|
110 |
(m, False : b) |
|
111 |
||
112 |
return "" |
|
113 |
||
114 |
elseSwitch = do |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
115 |
try $ string' "ELSE}" |
10015 | 116 |
updateState $ \(m, b:bs) -> (m, (not b):bs) |
117 |
return "" |
|
118 |
endIf = do |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
119 |
try $ string' "ENDIF}" |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
120 |
updateState $ \(m, _:bs) -> (m, bs) |
10015 | 121 |
return "" |
122 |
define = do |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
123 |
try $ string' "DEFINE" |
10015 | 124 |
spaces |
125 |
i <- identifier |
|
126 |
d <- ((string ":=" >> return ()) <|> spaces) >> many (noneOf "}") |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
127 |
char' '}' |
10015 | 128 |
updateState $ \(m, b) -> (if (and b) && (head i /= '_') then Map.insert i d m else m, b) |
129 |
return "" |
|
130 |
replace s = do |
|
131 |
(m, _) <- getState |
|
132 |
return $ Map.findWithDefault s s m |
|
133 |
||
134 |
unknown = do |
|
10113
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
135 |
un <- many1 $ noneOf "}\n" |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
136 |
char' '}' |
b26c2772e754
Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents:
10015
diff
changeset
|
137 |
return $ "{$" ++ un ++ "}" |