tools/PascalPreprocessor.hs
changeset 6412 4b9a59116535
child 6413 6714531e7bd2
equal deleted inserted replaced
6411:3cb15ca5319f 6412:4b9a59116535
       
     1 module PascalPreprocessor where
       
     2 
       
     3 import Text.Parsec
       
     4 import Control.Monad.IO.Class
       
     5 import System.IO
       
     6 import qualified Data.Map as Map
       
     7 
       
     8 preprocess :: String -> IO String
       
     9 preprocess fn = do
       
    10     r <- runParserT (preprocessFile fn) Map.empty "" ""
       
    11     case r of
       
    12          (Left a) -> do
       
    13              hPutStrLn stderr (show a)
       
    14              return ""
       
    15          (Right a) -> return a
       
    16     
       
    17     where
       
    18     preprocessFile :: String -> ParsecT String (Map.Map String String) IO String
       
    19     preprocessFile fn = do
       
    20         f <- liftIO (readFile fn)
       
    21         setInput f
       
    22         preprocessor
       
    23     preprocessor, codeBlock, switch :: ParsecT String (Map.Map String String) IO String
       
    24     preprocessor = chainl codeBlock (return (++)) ""
       
    25     codeBlock = choice [
       
    26             switch
       
    27             --, comment
       
    28             , char '\'' >> many (noneOf "'") >>= \s -> char '\'' >> return ('\'' : s ++ "'")
       
    29             , many1 $ noneOf "{'"
       
    30             ]
       
    31     switch = do
       
    32         try $ string "{$"
       
    33         s <- choice [
       
    34             include
       
    35             , unknown
       
    36             ]
       
    37         return s
       
    38     include = do
       
    39         try $ string "INCLUDE"
       
    40         spaces
       
    41         (char '"')
       
    42         fn <- many1 $ noneOf "\"\n"
       
    43         char '"'
       
    44         spaces
       
    45         char '}'
       
    46         f <- liftIO (readFile fn)
       
    47         c <- getInput
       
    48         setInput $ f ++ c
       
    49         return ""
       
    50 
       
    51     unknown = do
       
    52         fn <- many1 $ noneOf "}\n"
       
    53         char '}'
       
    54         return ""
       
    55