scmdoc

Automatically generate documentation from comments in R7RS Scheme code

git clone https://git.8pit.net/scmdoc.git

 1module SchemeDoc.Parser.Util where
 2
 3import Control.Monad (void)
 4import Numeric (readHex)
 5import Text.ParserCombinators.Parsec
 6
 7-- Parse a hexadecimal number without a prefix.
 8hex :: Parser Int
 9hex = fmap readHex' hexDigits
10  where
11    -- Due to hexDigits, readHex should always read the full string.
12    -- XXX: Technically R⁷RS does not allow upper case hex letters.
13    readHex' = fst . head . readHex
14    hexDigits = many1 (oneOf "0123456789abcdefABCDEF")
15
16-- Bind a given character to the given result.
17bind :: String -> a -> Parser a
18bind str val = val <$ string str
19
20-- Like skipMany but without the many part.
21skip :: Parser a -> Parser ()
22skip = void
23
24-- Runs both parsers and returns the result of the first.
25terminatedBy :: Parser a -> Parser b -> Parser a
26terminatedBy p1 p2 = do
27    r <- p1
28    _ <- p2
29    return r
30
31-- Like manyTill but preserve the terminator in the return value.
32manyTill' :: Parser a -> Parser a -> Parser [a]
33manyTill' p end = scan
34  where
35    scan =
36        do x <- end; return [x]
37        <|> do x <- p; xs <- scan; return (x : xs)
38
39------------------------------------------------------------------------
40
41-- Remove all Nothing value from a List of Maybe monads.
42filterJust :: [Maybe a] -> [a]
43filterJust =
44    foldr
45        ( \x acc -> case x of
46            Nothing -> acc
47            Just x' -> x' : acc
48        )
49        []