tools/pas2c/PascalBasics.hs
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 10120 b7f632c12784
child 15958 24545642473f
permissions -rw-r--r--
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
     1
{-# LANGUAGE FlexibleContexts, NoMonomorphismRestriction #-}
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     2
module PascalBasics where
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     3
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     4
import Text.Parsec.Combinator
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     5
import Text.Parsec.Char
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     6
import Text.Parsec.Prim
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     7
import Text.Parsec.Token
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     8
import Text.Parsec.Language
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
     9
import Data.Char
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    10
import Control.Monad
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    11
import Data.Functor.Identity
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    12
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    13
char' :: Char -> Parsec String u ()
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    14
char' = void . char
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    15
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    16
string' :: String -> Parsec String u ()
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    17
string' = void . string
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    18
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    19
builtin :: [String]
10120
b7f632c12784 Pas2C recognizes ansistrings
unc0rr
parents: 10113
diff changeset
    20
builtin = ["succ", "pred", "low", "high", "ord", "inc", "dec", "exit", "break", "continue", "length", "copy"]
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    21
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    22
pascalLanguageDef :: GenLanguageDef String u Identity
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    23
pascalLanguageDef
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    24
    = emptyDef
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    25
    { commentStart   = "(*"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    26
    , commentEnd     = "*)"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    27
    , commentLine    = "//"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    28
    , nestedComments = False
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    29
    , identStart     = letter <|> oneOf "_"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    30
    , identLetter    = alphaNum <|> oneOf "_"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    31
    , opLetter       = letter
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    32
    , reservedNames  = [
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    33
            "begin", "end", "program", "unit", "interface"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    34
            , "implementation", "and", "or", "xor", "shl"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    35
            , "shr", "while", "do", "repeat", "until", "case", "of"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    36
            , "type", "var", "const", "out", "array", "packed"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    37
            , "procedure", "function", "with", "for", "to"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    38
            , "downto", "div", "mod", "record", "set", "nil"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    39
            , "cdecl", "external", "if", "then", "else"
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    40
            ] -- ++ builtin
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    41
    , caseSensitive  = False
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    42
    }
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    43
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    44
preprocessorSwitch :: Stream String Identity Char => Parsec String u String
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    45
preprocessorSwitch = do
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    46
    try $ string' "{$"
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    47
    s <- manyTill (noneOf "\n") $ char '}'
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    48
    return s
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    49
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    50
caseInsensitiveString :: Stream String Identity Char => String -> Parsec String u String
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    51
caseInsensitiveString s = do
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    52
    mapM_ (\a -> satisfy (\b -> toUpper a == toUpper b)) s <?> s
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    53
    return s
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    54
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    55
pas :: GenTokenParser String u Identity
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    56
pas = patch $ makeTokenParser pascalLanguageDef
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    57
    where
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    58
    patch tp = tp {stringLiteral = stringL}
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    59
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    60
comment :: Stream String Identity Char => Parsec String u String
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    61
comment = choice [
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    62
        char '{' >> notFollowedBy (char '$') >> manyTill anyChar (try $ char '}')
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    63
        , (try $ string "(*") >> manyTill anyChar (try $ string "*)")
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    64
        , (try $ string "//") >> manyTill anyChar (try newline)
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    65
        ]
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    66
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    67
comments :: Parsec String u ()
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    68
comments = do
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    69
    spaces
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    70
    skipMany $ do
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    71
        void $ preprocessorSwitch <|> comment
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    72
        spaces
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    73
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    74
stringL :: Parsec String u String
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    75
stringL = do
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    76
    char' '\''
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    77
    s <- (many $ noneOf "'")
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    78
    char' '\''
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    79
    ss <- many $ do
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    80
        char' '\''
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    81
        s' <- (many $ noneOf "'")
10113
b26c2772e754 Fix tons and tons of pas2c warnings (but still not all of them)
unc0rr
parents: 10015
diff changeset
    82
        char' '\''
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    83
        return $ '\'' : s'
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    84
    comments
4feced261c68 partial merge of the webgl branch
koda
parents: 8020
diff changeset
    85
    return $ concat (s:ss)