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