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