tools/PascalPreprocessor.hs
author unc0rr
Tue, 22 Nov 2011 22:48:02 +0300
changeset 6413 6714531e7bd2
parent 6412 4b9a59116535
child 6414 8474b7fa84d6
permissions -rw-r--r--
Preprocessor strips comments
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
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
     8
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
     9
-- comments are removed
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    10
comment = choice [
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    11
        char '{' >> notFollowedBy (char '$') >> manyTill anyChar (try $ char '}') >> return ""
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    12
        , (try $ string "(*") >> manyTill anyChar (try $ string "*)") >> return ""
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    13
        , (try $ string "//") >> manyTill anyChar (try newline) >> return "\n"
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    14
        ]
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    15
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    16
preprocess :: String -> IO String
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    17
preprocess fn = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    18
    r <- runParserT (preprocessFile fn) Map.empty "" ""
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    19
    case r of
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    20
         (Left a) -> do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    21
             hPutStrLn stderr (show a)
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    22
             return ""
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    23
         (Right a) -> return a
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    24
    
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    25
    where
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    26
    preprocessFile :: String -> ParsecT String (Map.Map String String) IO String
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    27
    preprocessFile fn = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    28
        f <- liftIO (readFile fn)
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    29
        setInput f
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    30
        preprocessor
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    31
        
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    32
    preprocessor, codeBlock, switch :: ParsecT String (Map.Map String String) IO String
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    33
    
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    34
    preprocessor = chainl codeBlock (return (++)) ""
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    35
    
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    36
    codeBlock = choice [
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    37
            switch
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    38
            , comment
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    39
            , char '\'' >> many (noneOf "'") >>= \s -> char '\'' >> return ('\'' : s ++ "'")
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    40
            , many1 $ noneOf "{'/("
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    41
            , char '/' >> notFollowedBy (char '/') >> return "/"
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    42
            , char '(' >> notFollowedBy (char '*') >> return "("
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    43
            ]
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    44
            
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    45
    switch = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    46
        try $ string "{$"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    47
        s <- choice [
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    48
            include
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    49
            , unknown
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    50
            ]
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    51
        return s
6413
6714531e7bd2 Preprocessor strips comments
unc0rr
parents: 6412
diff changeset
    52
        
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    53
    include = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    54
        try $ string "INCLUDE"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    55
        spaces
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    56
        (char '"')
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    57
        fn <- many1 $ noneOf "\"\n"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    58
        char '"'
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    59
        spaces
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    60
        char '}'
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    61
        f <- liftIO (readFile fn)
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    62
        c <- getInput
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    63
        setInput $ f ++ c
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    64
        return ""
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    65
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    66
    unknown = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    67
        fn <- many1 $ noneOf "}\n"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    68
        char '}'
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    69
        return ""
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    70