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