author | unc0rr |
Sun, 17 Jun 2012 21:45:49 +0400 | |
changeset 7258 | 722e8a0d89dc |
parent 7067 | f98ec3aecf4e |
child 7315 | 59b5b19e6604 |
permissions | -rw-r--r-- |
6412 | 1 |
module PascalPreprocessor where |
2 |
||
3 |
import Text.Parsec |
|
4 |
import Control.Monad.IO.Class |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
5 |
import Control.Monad |
6412 | 6 |
import System.IO |
7 |
import qualified Data.Map as Map |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
8 |
import Data.Char |
6412 | 9 |
|
6413 | 10 |
|
11 |
-- comments are removed |
|
12 |
comment = choice [ |
|
13 |
char '{' >> notFollowedBy (char '$') >> manyTill anyChar (try $ char '}') >> return "" |
|
14 |
, (try $ string "(*") >> manyTill anyChar (try $ string "*)") >> return "" |
|
15 |
, (try $ string "//") >> manyTill anyChar (try newline) >> return "\n" |
|
16 |
] |
|
17 |
||
7038 | 18 |
initDefines = Map.fromList [ |
19 |
("FPC", "") |
|
20 |
, ("PAS2C", "") |
|
21 |
] |
|
6425
1ef4192aa80d
- Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6414
diff
changeset
|
22 |
|
6412 | 23 |
preprocess :: String -> IO String |
24 |
preprocess fn = do |
|
6425
1ef4192aa80d
- Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6414
diff
changeset
|
25 |
r <- runParserT (preprocessFile fn) (initDefines, [True]) "" "" |
6412 | 26 |
case r of |
27 |
(Left a) -> do |
|
28 |
hPutStrLn stderr (show a) |
|
29 |
return "" |
|
30 |
(Right a) -> return a |
|
31 |
||
32 |
where |
|
33 |
preprocessFile fn = do |
|
34 |
f <- liftIO (readFile fn) |
|
35 |
setInput f |
|
36 |
preprocessor |
|
6413 | 37 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
38 |
preprocessor, codeBlock, switch :: ParsecT String (Map.Map String String, [Bool]) IO String |
6413 | 39 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
40 |
preprocessor = chainr codeBlock (return (++)) "" |
6413 | 41 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
42 |
codeBlock = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
43 |
s <- choice [ |
6412 | 44 |
switch |
6413 | 45 |
, comment |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
46 |
, char '\'' >> many (noneOf "'\n") >>= \s -> char '\'' >> return ('\'' : s ++ "'") |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
47 |
, identifier >>= replace |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
48 |
, noneOf "{" >>= \a -> return [a] |
6412 | 49 |
] |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
50 |
(_, ok) <- getState |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
51 |
return $ if and ok then s else "" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
52 |
|
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
53 |
--otherChar c = c `notElem` "{/('_" && not (isAlphaNum c) |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
54 |
identifier = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
55 |
c <- letter <|> oneOf "_" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
56 |
s <- many (alphaNum <|> oneOf "_") |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
57 |
return $ c:s |
6413 | 58 |
|
6412 | 59 |
switch = do |
60 |
try $ string "{$" |
|
61 |
s <- choice [ |
|
62 |
include |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
63 |
, ifdef |
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
64 |
, if' |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
65 |
, elseSwitch |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
66 |
, endIf |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
67 |
, define |
6412 | 68 |
, unknown |
69 |
] |
|
70 |
return s |
|
6413 | 71 |
|
6412 | 72 |
include = do |
73 |
try $ string "INCLUDE" |
|
74 |
spaces |
|
75 |
(char '"') |
|
76 |
fn <- many1 $ noneOf "\"\n" |
|
77 |
char '"' |
|
78 |
spaces |
|
79 |
char '}' |
|
6964 | 80 |
f <- liftIO (readFile fn `catch` error ("File not found: " ++ fn)) |
6412 | 81 |
c <- getInput |
82 |
setInput $ f ++ c |
|
83 |
return "" |
|
84 |
||
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
85 |
ifdef = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
86 |
s <- try (string "IFDEF") <|> try (string "IFNDEF") |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
87 |
let f = if s == "IFNDEF" then not else id |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
88 |
|
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
89 |
spaces |
6425
1ef4192aa80d
- Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6414
diff
changeset
|
90 |
d <- identifier |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
91 |
spaces |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
92 |
char '}' |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
93 |
|
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
94 |
updateState $ \(m, b) -> |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
95 |
(m, (f $ d `Map.member` m) : b) |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
96 |
|
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
97 |
return "" |
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
98 |
|
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
99 |
if' = do |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
100 |
s <- try (string "IF" >> notFollowedBy alphaNum) |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
101 |
|
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
102 |
manyTill anyChar (char '}') |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
103 |
--char '}' |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
104 |
|
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
105 |
updateState $ \(m, b) -> |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
106 |
(m, False : b) |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
107 |
|
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
108 |
return "" |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
109 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
110 |
elseSwitch = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
111 |
try $ string "ELSE}" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
112 |
updateState $ \(m, b:bs) -> (m, (not b):bs) |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
113 |
return "" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
114 |
endIf = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
115 |
try $ string "ENDIF}" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
116 |
updateState $ \(m, b:bs) -> (m, bs) |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
117 |
return "" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
118 |
define = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
119 |
try $ string "DEFINE" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
120 |
spaces |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
121 |
i <- identifier |
6425
1ef4192aa80d
- Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas
unc0rr
parents:
6414
diff
changeset
|
122 |
d <- ((string ":=" >> return ())<|> spaces) >> many (noneOf "}") |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
123 |
char '}' |
7067
f98ec3aecf4e
A solution to char vs string problem: mark single-letter strings with _S macro
unc0rr
parents:
7059
diff
changeset
|
124 |
updateState $ \(m, b) -> (if (and b) && (head i /= '_') then Map.insert i d m else m, b) |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
125 |
return "" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
126 |
replace s = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
127 |
(m, _) <- getState |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
128 |
return $ Map.findWithDefault s s m |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
129 |
|
6412 | 130 |
unknown = do |
131 |
fn <- many1 $ noneOf "}\n" |
|
132 |
char '}' |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
133 |
return $ "{$" ++ fn ++ "}" |