Automatically generate documentation from comments in R7RS Scheme code
git clone https://git.8pit.net/scmdoc.git
1-- | Data types for the SchemeDoc library. 2module SchemeDoc.Types (Documented, Sexp (..), Walk (..), walk) 3where 4 5import Data.Complex 6import qualified Data.Text as T 7 8-- | A documented S-expression, i.e. an S-expression which is preceeded 9-- by a 'DocComment`.10type Documented = (T.Text, Sexp)1112-- | Algebraic data type representing Scheme S-expressions.13data Sexp14 = Str T.Text -- "foo"15 | Id T.Text -- foo16 | Symbol T.Text -- 'foo17 | Char Char -- #\f18 | Boolean Bool -- #t19 | List [Sexp] -- ["foo" "bar"]20 | Number Integer21 | Float Double22 | Complex (Complex Double)23 | Rational Rational24 | DocComment T.Text25 deriving (Eq)2627instance Show Sexp where28 show (Str s) = "\"" ++ T.unpack s ++ "\"" -- TODO: Perform escaping29 show (Id i) = T.unpack i30 show (Symbol s) = "'" ++ T.unpack s31 show (Char c) = "#\\" ++ [c]32 show (Boolean b) = if b then "#t" else "#f"33 show (List a) = "(" ++ unwords (map show a) ++ ")"34 show (Number n) = show n35 show (Float n) = show n36 show (Complex n) = show n37 show (Rational n) = show n38 show (DocComment c) = ";;> " ++ T.unpack c3940-- | Type used for the return value of the closure passed to 'walk'.41-- The type is used to indicate whether 'walk' should recurse deeper42-- into a 'List' S-expression.43data Walk a = Recur a | Rise a4445getValue :: Walk a -> a46getValue (Recur v) = v47getValue (Rise v) = v4849-- | Traverse a Scheme source, i.e. a list of S-expressions.50--51-- For `List`s, the function used for traversal will first52-- be passed the `List` expression itself and then each53-- element of the list from left to right. If `proc` returns54-- 'False' as a first tuple element for a list, then the `List`55-- wont't be iterated over.56walk :: (b -> Sexp -> Walk b) -> b -> [Sexp] -> b57walk proc =58 foldl59 ( \a x -> case x of60 List exprs -> case proc a x of61 Recur r -> walk proc r exprs62 Rise r -> r63 expr -> getValue $ proc a expr64 )