author | koda |
Sun, 09 Jun 2013 12:22:53 +0200 | |
branch | webgl |
changeset 9160 | fc46e75f6b72 |
parent 8330 | aaefa587e277 |
child 9164 | d923ba9d1145 |
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 |
||
8020 | 18 |
|
7038 | 19 |
initDefines = Map.fromList [ |
20 |
("FPC", "") |
|
21 |
, ("PAS2C", "") |
|
8020 | 22 |
-- , ("WEBGL", "") |
23 |
-- , ("AI_MAINTHREAD", "") |
|
7429
fcf13e40d6b6
Changes to pas2c - unreviewed apart from cursory glance and compile test.
xymeng
parents:
7315
diff
changeset
|
24 |
, ("ENDIAN_LITTLE", "") |
7038 | 25 |
] |
7315 | 26 |
|
7961
620331af6b9a
pas2c can search for inc files in alternate folders (needed for out of source builds)
koda
parents:
7957
diff
changeset
|
27 |
preprocess :: String -> String -> String -> IO String |
620331af6b9a
pas2c can search for inc files in alternate folders (needed for out of source builds)
koda
parents:
7957
diff
changeset
|
28 |
preprocess inputPath alternateInputPath fn = do |
7957
497ec84e0c21
pas2c can read files without changing the process directory
koda
parents:
7762
diff
changeset
|
29 |
r <- runParserT (preprocessFile (inputPath ++ fn)) (initDefines, [True]) "" "" |
6412 | 30 |
case r of |
31 |
(Left a) -> do |
|
32 |
hPutStrLn stderr (show a) |
|
33 |
return "" |
|
34 |
(Right a) -> return a |
|
7315 | 35 |
|
6412 | 36 |
where |
37 |
preprocessFile fn = do |
|
38 |
f <- liftIO (readFile fn) |
|
39 |
setInput f |
|
40 |
preprocessor |
|
7315 | 41 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
42 |
preprocessor, codeBlock, switch :: ParsecT String (Map.Map String String, [Bool]) IO String |
7315 | 43 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
44 |
preprocessor = chainr codeBlock (return (++)) "" |
7315 | 45 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
46 |
codeBlock = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
47 |
s <- choice [ |
6412 | 48 |
switch |
6413 | 49 |
, comment |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
50 |
, 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
|
51 |
, identifier >>= replace |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
52 |
, noneOf "{" >>= \a -> return [a] |
6412 | 53 |
] |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
54 |
(_, ok) <- getState |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
55 |
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
|
56 |
|
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
57 |
--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
|
58 |
identifier = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
59 |
c <- letter <|> oneOf "_" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
60 |
s <- many (alphaNum <|> oneOf "_") |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
61 |
return $ c:s |
7315 | 62 |
|
6412 | 63 |
switch = do |
64 |
try $ string "{$" |
|
65 |
s <- choice [ |
|
66 |
include |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
67 |
, ifdef |
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
68 |
, if' |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
69 |
, elseSwitch |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
70 |
, endIf |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
71 |
, define |
6412 | 72 |
, unknown |
73 |
] |
|
74 |
return s |
|
7315 | 75 |
|
6412 | 76 |
include = do |
77 |
try $ string "INCLUDE" |
|
78 |
spaces |
|
79 |
(char '"') |
|
80 |
fn <- many1 $ noneOf "\"\n" |
|
81 |
char '"' |
|
82 |
spaces |
|
83 |
char '}' |
|
7961
620331af6b9a
pas2c can search for inc files in alternate folders (needed for out of source builds)
koda
parents:
7957
diff
changeset
|
84 |
f <- liftIO (readFile (inputPath ++ fn) `catch` (\exc -> readFile (alternateInputPath ++ fn) `catch` error ("File not found: " ++ fn))) |
6412 | 85 |
c <- getInput |
86 |
setInput $ f ++ c |
|
87 |
return "" |
|
88 |
||
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
89 |
ifdef = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
90 |
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
|
91 |
let f = if s == "IFNDEF" then not else id |
7315 | 92 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
93 |
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
|
94 |
d <- identifier |
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
95 |
spaces |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
96 |
char '}' |
7315 | 97 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
98 |
updateState $ \(m, b) -> |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
99 |
(m, (f $ d `Map.member` m) : b) |
7315 | 100 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
101 |
return "" |
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
102 |
|
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
103 |
if' = do |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
104 |
s <- try (string "IF" >> notFollowedBy alphaNum) |
7315 | 105 |
|
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
106 |
manyTill anyChar (char '}') |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
107 |
--char '}' |
7315 | 108 |
|
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
109 |
updateState $ \(m, b) -> |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
110 |
(m, False : b) |
7315 | 111 |
|
6453
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
112 |
return "" |
11c578d30bd3
Countless imporvements to the parser and countless help to the parser in sources.
unc0rr
parents:
6425
diff
changeset
|
113 |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
114 |
elseSwitch = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
115 |
try $ string "ELSE}" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
116 |
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
|
117 |
return "" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
118 |
endIf = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
119 |
try $ string "ENDIF}" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
120 |
updateState $ \(m, b:bs) -> (m, bs) |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
121 |
return "" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
122 |
define = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
123 |
try $ string "DEFINE" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
124 |
spaces |
7315 | 125 |
i <- identifier |
7762 | 126 |
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
|
127 |
char '}' |
7067
f98ec3aecf4e
A solution to char vs string problem: mark single-letter strings with _S macro
unc0rr
parents:
7059
diff
changeset
|
128 |
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
|
129 |
return "" |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
130 |
replace s = do |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
131 |
(m, _) <- getState |
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
132 |
return $ Map.findWithDefault s s m |
7315 | 133 |
|
6412 | 134 |
unknown = do |
135 |
fn <- many1 $ noneOf "}\n" |
|
136 |
char '}' |
|
6414
8474b7fa84d6
Finish preprocessor. Now it correctly handles $IFDEF, $IFNDEF, $ELSE, $ENDIF and $DEFINE.
unc0rr
parents:
6413
diff
changeset
|
137 |
return $ "{$" ++ fn ++ "}" |