on master help sign in
Parse out types from ADT constructors.
d292236evancz
compiler/Docs.hs+20−9
@@ -5,6 +5,8 @@ import Ast
5import Control.Applicative ((<$>), (<*>))
5import Control.Applicative ((<$>), (<*>))
6import Data.List (intercalate)
6import Data.List (intercalate)
7import Data.Maybe (catMaybes)
7import Data.Maybe (catMaybes)
8+import Types.Types ((==>), Type ( ADT, VarT ) )
9+import Parse.Types (datatype)
8import Parse.Library
10import Parse.Library
9import Parse.Modules (moduleDef)
11import Parse.Modules (moduleDef)
10import Text.Parsec hiding (newline,spaces)
12import Text.Parsec hiding (newline,spaces)
@@ -35,15 +37,19 @@ docParse :: String -> Either String (String, [(String, String, String)])
35docParse = setupParser $ do
37docParse = setupParser $ do
36 optional freshLine
38 optional freshLine
37 (names, exports) <- option (["Main"],[]) moduleDef
39 (names, exports) <- option (["Main"],[]) moduleDef
38− info <- many (annotation exports <|> try skip <|> end)
40+ info <- many (try (annotation exports) <|> try skip <|> end)
39− return (intercalate "." names, catMaybes info)
41+ return (intercalate "." names, concat info)
40 where
42 where
41− skip = manyTill anyChar simpleNewline >> return Nothing
43+ skip = manyTill anyChar simpleNewline >> return []
42− end = many1 anyChar >> return Nothing
44+ end = many1 anyChar >> return []
43
45
44−annotation :: [String] -> IParser (Maybe (String, String, String))
46+annotation :: [String] -> IParser [(String, String, String)]
45annotation exports =
47annotation exports =
46− try ((\c n t -> export (n,t,c)) <$> comment <*> (try adt <|> name) <*> tipe)
48+ do description <- comment
49+ extras <- option [] (lookAhead adt)
50+ varName <- try typeDef <|> name
51+ varType <- tipe
52+ return $ extras ++ export varName varType description
47 where
53 where
48 comment = concatMap clip <$> many lineComment
54 comment = concatMap clip <$> many lineComment
49 clip str = case str of { ' ':rest -> rest ; _ -> str } ++ "\n"
55 clip str = case str of { ' ':rest -> rest ; _ -> str } ++ "\n"
@@ -53,11 +59,16 @@ annotation exports =
53
59
54 tipe = manyTill anyChar (try (simpleNewline >> notFollowedBy (string " ")))
60 tipe = manyTill anyChar (try (simpleNewline >> notFollowedBy (string " ")))
55
61
56− export info@(name,_,_) =
62+ export name tipe desc =
57− if null exports || name `elem` exports then Just info else Nothing
63+ if null exports || name `elem` exports then [(name,tipe,desc)] else []
58
64
59− adt = lookAhead ((string "data" <|> string "type") >> whitespace >> capVar)
65+ typeDef = lookAhead ((string "data" <|> string "type") >> whitespace >> capVar)
60
66
67+ adt = do
68+ (Datatype name tvs constructors) <- datatype
69+ let toType (cName, typeArgs) =
70+ (cName, show $ foldr (==>) (ADT name $ map VarT tvs) typeArgs, "")
71+ return $ map toType constructors
61
72
62setupParser p source =
73setupParser p source =
63 case iParse p "" source of
74 case iParse p "" source of
libraries/Either.elm+2−1
@@ -6,7 +6,8 @@ import List
6-- Represents any data that can take two different types.
6-- Represents any data that can take two different types.
7--
7--
8-- This can also be used for error handling `(Either String a)` where error
8-- This can also be used for error handling `(Either String a)` where error
9−-- messages are stored on the left, and the correct values (&ldquo;right&rdquo; values) are stored on the right.
9+-- messages are stored on the left, and the correct values (&ldquo;right&rdquo;
10+-- values) are stored on the right.
10data Either a b = Left a | Right b
11data Either a b = Left a | Right b
11
12
12-- Apply the first function to a `Left` and the second function to a `Right`.
13-- Apply the first function to a `Left` and the second function to a `Right`.