File size: 4,405 Bytes
5316c4e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | -- SPDX-License-Identifier: AGPL-3.0-or-later
-- | Just enough Haskell lexing to audit sources: strip comments and literals,
-- split identifiers and operators, and find the conformance IDs that tests
-- are tagged with.
--
-- Every forbidden name below is assembled from halves. A gate that reads
-- source must survive reading its own, and exempting this file would put a
-- hole exactly where a defect parked in the gate would hide.
module MP.Gate.Scan
( codeOnly
, identifiers
, operators
, deferralMarkers
, stubNames
, partialNames
, partialOperators
, testTags
, isConformanceId
) where
import Data.Char (isAlphaNum, isDigit, isUpper)
import Data.List (isPrefixOf)
-- | Comment and work-in-progress markers that mean "not finished".
deferralMarkers :: [String]
deferralMarkers = ["TO" <> "DO", "FIX" <> "ME", "XX" <> "X"]
-- | A placeholder standing in for code.
stubNames :: [String]
stubNames = ["unde" <> "fined"]
-- | Functions that are partial, or that defeat the type system.
partialNames :: [String]
partialNames =
[ "err" <> "or"
, "errorWithout" <> "StackTrace"
, "he" <> "ad"
, "ta" <> "il"
, "in" <> "it"
, "la" <> "st"
, "from" <> "Just"
, "re" <> "ad"
, "maxi" <> "mum"
, "mini" <> "mum"
, "foldr" <> "1"
, "foldl" <> "1"
, "unsafePerform" <> "IO"
, "unsafe" <> "Coerce"
, "unsafeInterleave" <> "IO"
]
partialOperators :: [String]
partialOperators = ["!" <> "!"]
isIdChar :: Char -> Bool
isIdChar c = isAlphaNum c || c == '_' || c == '\''
isSymbolChar :: Char -> Bool
isSymbolChar c = c `elem` ("!#$%&*+./<=>?@\\^|-~:" :: String)
-- | The source with comments, string literals and character literals
-- replaced by spaces, so line structure survives.
codeOnly :: String -> String
codeOnly = go ' '
where
go _ [] = []
go prev s@(c : rest)
| "{-" `isPrefixOf` s = blockComment (1 :: Int) (drop 2 s)
| "--" `isPrefixOf` s && not (isSymbolChar prev) && lineCommentStart s =
let (_, after) = break (== '\n') s in go '\n' after
| c == '"' = stringLit rest
| c == '\'' && not (isIdChar prev) = charLit s
| otherwise = c : go c rest
lineCommentStart s = case dropWhile (== '-') s of
(d : _) -> not (isSymbolChar d)
[] -> True
blockComment _ [] = []
blockComment depth s@(c : rest)
| "-}" `isPrefixOf` s = if depth == 1 then ' ' : go ' ' (drop 2 s) else blockComment (depth - 1) (drop 2 s)
| "{-" `isPrefixOf` s = blockComment (depth + 1) (drop 2 s)
| c == '\n' = '\n' : blockComment depth rest
| otherwise = blockComment depth rest
stringLit ('\\' : _ : rest) = stringLit rest
stringLit ('"' : rest) = ' ' : go ' ' rest
stringLit ('\n' : rest) = '\n' : go '\n' rest
stringLit (_ : rest) = stringLit rest
stringLit [] = []
-- 'x' or '\n' or '\''; anything else (a promoted constructor) is code.
charLit s = case s of
('\'' : '\\' : _ : '\'' : rest) -> ' ' : go ' ' rest
('\'' : _ : '\'' : rest) -> ' ' : go ' ' rest
('\'' : rest) -> '\'' : go '\'' rest
[] -> []
(c : rest) -> c : go c rest
-- | Maximal identifier tokens, with qualifiers split off (@Set.member@ gives
-- @Set@ and @member@).
identifiers :: String -> [String]
identifiers s = case dropWhile (not . isIdStart) s of
[] -> []
s' -> let (tok, rest) = span isIdChar s' in tok : identifiers rest
where
isIdStart c = isAlphaNum c || c == '_'
-- | Maximal operator tokens.
operators :: String -> [String]
operators s = case dropWhile (not . isSymbolChar) s of
[] -> []
s' -> let (tok, rest) = span isSymbolChar s' in tok : operators rest
-- | @XX-00@: two capitals, a hyphen, two digits.
isConformanceId :: String -> Bool
isConformanceId i = case i of
[a, b, '-', c, d] -> isUpper a && isUpper b && isDigit c && isDigit d
_ -> False
-- | The IDs in @unit "XX-00"@, @unitIO@, @prop@ and @propN@ calls.
testTags :: String -> [String]
testTags = go ' '
where
heads = ["unit ", "unitIO ", "prop ", "propN "]
go _ [] = []
go prev s@(c : rest)
| not (isIdChar prev), Just after <- firstPrefix s = case after of
('"' : more) -> takeWhile (/= '"') more : go '"' (drop 1 (dropWhile (/= '"') more))
_ -> go c rest
| otherwise = go c rest
firstPrefix s = case [drop (length h) s | h <- heads, h `isPrefixOf` s] of
(a : _) -> Just a
[] -> Nothing
|