tools/PascalBasics.hs
author unc0rr
Fri, 25 Nov 2011 18:36:12 +0300
changeset 6425 1ef4192aa80d
parent 6412 4b9a59116535
child 6450 14224c9b4594
permissions -rw-r--r--
- Parse unions, sets, function type, packed arrays and some more imporvements to the parser. Now it parses uVariable, uConsts and even SDLh.pas - Improve rendering of C code - Fix preprocessor issues, define "FPC" - Make pas2c convert unit along with its dependencies into corresponding .c files, so you just call it for hwengine.pas to convert the whole engine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     1
{-# LANGUAGE FlexibleContexts #-}
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     2
module PascalBasics where
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     3
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     4
import Text.Parsec.Combinator
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     5
import Text.Parsec.Char
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     6
import Text.Parsec.Prim
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     7
import Text.Parsec.Token
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     8
import Text.Parsec.Language
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
     9
import Data.Char
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    10
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    11
builtin = ["succ", "pred", "low", "high"]
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    12
    
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    13
pascalLanguageDef
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    14
    = emptyDef
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    15
    { commentStart   = "(*"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    16
    , commentEnd     = "*)"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    17
    , commentLine    = "//"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    18
    , nestedComments = False
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    19
    , identStart     = letter <|> oneOf "_"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    20
    , identLetter    = alphaNum <|> oneOf "_."
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    21
    , reservedNames  = [
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    22
            "begin", "end", "program", "unit", "interface"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    23
            , "implementation", "and", "or", "xor", "shl"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    24
            , "shr", "while", "do", "repeat", "until", "case", "of"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    25
            , "type", "var", "const", "out", "array", "packed"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    26
            , "procedure", "function", "with", "for", "to"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    27
            , "downto", "div", "mod", "record", "set", "nil"
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: 6412
diff changeset
    28
            , "string", "shortstring", "cdecl", "external"
6412
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    29
            ] ++ builtin
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    30
    , reservedOpNames= [] 
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    31
    , caseSensitive  = False   
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    32
    }
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    33
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    34
preprocessorSwitch :: Stream s m Char => ParsecT s u m String
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    35
preprocessorSwitch = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    36
    try $ string "{$"
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    37
    s <- manyTill (noneOf "\n") $ char '}'
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    38
    return s
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    39
        
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    40
caseInsensitiveString s = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    41
    mapM_ (\a -> satisfy (\b -> toUpper a == toUpper b)) s <?> s
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    42
    return s
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    43
    
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    44
pas = patch $ makeTokenParser pascalLanguageDef
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    45
    where
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    46
    patch tp = tp {stringLiteral = stringL}
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    47
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    48
comment = choice [
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    49
        char '{' >> notFollowedBy (char '$') >> manyTill anyChar (try $ char '}')
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    50
        , (try $ string "(*") >> manyTill anyChar (try $ string "*)")
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    51
        , (try $ string "//") >> manyTill anyChar (try newline)
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    52
        ]
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    53
    
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    54
comments = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    55
    spaces
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    56
    skipMany $ do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    57
        preprocessorSwitch <|> comment
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    58
        spaces
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    59
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    60
stringL = do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    61
    (char '\'')
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    62
    s <- (many $ noneOf "'")
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    63
    (char '\'')
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    64
    ss <- many $ do
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    65
        (char '\'')
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    66
        s' <- (many $ noneOf "'")
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    67
        (char '\'')
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    68
        return $ '\'' : s'
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    69
    comments    
4b9a59116535 - Split PascalParser into modules
unc0rr
parents:
diff changeset
    70
    return $ concat (s:ss)