on master help sign in
Revise elm-docs to copy the names of type variables in type annotations directly.
commitb359fd0b4765f6c6791bd217855139e9801cad7c authorevancz <info@elm-lang.org> dateSat 4 May 2013, 18:19 (13 years ago) parentbe72e86
6 files changed+42−48
Elm.cabal+1−3 Setup.hs+2−5 compiler/Docs.hs+25−24 compiler/Model/LoadLibraries.hs+8−10 compiler/Types/Hints.hs+5−4 libraries/Dict.elm+1−2
Elm.cabal+1−3
@@ -145,9 +145,7 @@ Executable elm-doc
145145 Context,
146146 Parse.Library,
147147 Parse.Modules,
148− Parse.Types,
149− Rename,
150− Types.Types
148+ Rename
151149
152150 Build-depends: base >=4.2 && <5,
153151 containers >= 0.3,
Setup.hs+2−5
@@ -141,9 +141,9 @@ runElm lbi file = do
141141 out_c <- canonicalizePath js -- dist/js (root folder)
142142 elm_c <- canonicalizePath (elm lbi) -- dist/build/elm/elm
143143 rtd_c <- canonicalizePath (rtsDir lbi) -- dist/data (for docs.json)
144− handle <- runProcess elm_c ["--only-js", "--no-prelude", "--runtime="++rts_c, "--output-directory="++out_c, file]
144+ handle <- runProcess elm_c ["--only-js", "--no-prelude", "--output-directory="++out_c, file]
145145 Nothing (Just [("Elm_datadir", rtd_c)]) Nothing Nothing Nothing
146− waitForProcess handle
146+ exitCode <- waitForProcess handle
147147 return $ j </> replaceExtension (takeFileName file) ".js"
148148
149149
@@ -154,6 +154,3 @@ buildRuntime lbi = do
154154 mapM_ (appendJS lbi) =<< getFiles ".js" "libraries"
155155 mapM_ (appendElm lbi) =<< getFiles ".elm" "libraries"
156156 mapM_ (appendJS lbi) =<< getFiles ".js" "runtime"
157− putStrLn "\n+------------------------------------------+\
158− \\n| Success building runtime and libraries! |\
159− \\n+------------------------------------------+\n"
compiler/Docs.hs+25−24
@@ -4,13 +4,12 @@ module Main where
44import Ast
55import Control.Applicative ((<$>), (<*>))
66import Data.List (intercalate)
7+import Data.Maybe (catMaybes)
78import Parse.Library
89import Parse.Modules (moduleDef)
9−import Parse.Types (typeAnnotation)
1010import Text.Parsec hiding (newline,spaces)
1111import System.Environment
1212import System.Exit
13−import Types.Types
1413
1514
1615main = do
@@ -29,32 +28,34 @@ toModule (name, values) =
2928
3029toValue (name, tipe, desc) =
3130 "{ \"name\" : " ++ show name ++
32− ",\n \"type\" : \"" ++ show tipe ++
33− "\",\n \"desc\" : " ++ show desc ++ "\n }"
31+ ",\n \"type\" : " ++ show tipe ++
32+ ",\n \"desc\" : " ++ show desc ++ "\n }"
3433
35−docParse :: String -> Either String (String, [(String, Type, String)])
34+docParse :: String -> Either String (String, [(String, String, String)])
3635docParse = setupParser $ do
37− optional freshLine
38− (,) <$> option "Main" moduleName <*> types
36+ optional freshLine
37+ (names, exports) <- option (["Main"],[]) (moduleDef `followedBy` freshLine)
38+ info <- many (docs exports <|> try skip <|> end)
39+ return (intercalate "." names, catMaybes info)
3940 where
40− skip = manyTill anyChar simpleNewline >> return []
41− end = many1 anyChar >> return []
42− types = concat <$> many (docs <|> try skip <|> end)
43− getName = intercalate "." . fst
44− moduleName = do optional freshLine
45− getName <$> moduleDef `followedBy` freshLine
46−
47−docs :: IParser [(String, Type, String)]
48−docs = (tipe <$> try typeAnnotation) <|> commentTipe
41+ skip = manyTill anyChar simpleNewline >> return Nothing
42+ end = many1 anyChar >> return Nothing
43+
44+docs :: [String] -> IParser (Maybe (String, String, String))
45+docs exports = export <$> try annotation
4946 where
50− clip str = case str of { ' ':rest -> rest ; _ -> str }
51− tipe stmt = case stmt of { TypeAnnotation n t -> [(n,t,"")] ; _ -> [] }
52− commentTipe = do
53− cs <- map clip <$> many1 lineComment
54− typ <- optionMaybe (try typeAnnotation)
55− return $ case typ of
56− Just (TypeAnnotation n t) -> [(n, t, intercalate "\n" cs)]
57− _ -> []
47+ tipe comment = tuple <$> name <*> tipe
48+ where name = do v <- lowVar <|> parens symOp
49+ whitespace ; hasType ; whitespace ; return v
50+ tipe = manyTill anyChar (try (freshLine >> notFollowedBy (string " ")))
51+ tuple n t = (n,t,comment)
52+
53+ annotation = tipe =<< option "" (concatMap clip <$> many1 lineComment)
54+ where clip str = case str of { ' ':rest -> rest ; _ -> str } ++ "\n"
55+
56+ export info@(name,_,_) =
57+ if null exports || name `elem` exports then Just info else Nothing
58+
5859
5960
6061setupParser p source =
compiler/Model/LoadLibraries.hs+8−10
@@ -15,22 +15,20 @@ import System.IO.Unsafe (unsafePerformIO)
1515
1616{-# NOINLINE docs #-}
1717docs :: String
18−docs = force . unsafePerformIO . safeReadDocs . getDataFileName $ "docs.json"
18+docs = force $ unsafePerformIO (safeReadDocs =<< getDataFileName "docs.json")
1919
20−safeReadDocs :: IO FilePath -> IO String
21−safeReadDocs path = E.catch (readDocs path) (emitError path)
20+safeReadDocs :: FilePath -> IO String
21+safeReadDocs name = E.catch (readDocs name) (emitError name)
2222
23−readDocs :: IO FilePath -> IO String
24−readDocs path = do
25− name <- path
23+readDocs :: FilePath -> IO String
24+readDocs name = do
2625 exists <- doesFileExist name
2726 if exists then readFile name
2827 else ioError . userError $ "File Not Found"
2928
3029
31−emitError :: IO FilePath -> IOError -> IO String
32−emitError path err = do
33− name <- path
34− putStrLn $ "Error! Types for standard library not loaded properly!\n File should be here:" ++ name ++ "\n The file is created and copied by command: cabal install"
30+emitError :: FilePath -> IOError -> IO String
31+emitError name err = do
32+ putStrLn $ "Error! Types for standard library not loaded properly!\n File should be here:" ++ name ++ "\n The file is created and copied by command: cabal install"
3533 putStrLn (show err)
3634 return "{\"modules\":[]}"
compiler/Types/Hints.hs+5−4
@@ -20,7 +20,8 @@ hints = mapM toScheme values
2020
2121 toScheme :: (String, String) -> GuidCounter (String, Scheme)
2222 toScheme (name, tipeString) =
23− case iParse (fmap toType typeExpr) "parsing types in docs.json" tipeString of
24− Left err -> error (show err)
25− Right tipe -> do scheme <- Subs.generalize [] =<< Subs.superize name tipe
26− return (name, scheme)
23+ let err = "in docs.json parsing type: " ++ tipeString in
24+ case iParse (fmap toType typeExpr) err tipeString of
25+ Left err -> error (show err)
26+ Right tipe -> do scheme <- Subs.generalize [] =<< Subs.superize name tipe
27+ return (name, scheme)
libraries/Dict.elm+1−2
@@ -375,8 +375,7 @@ union t1 t2 = foldl insert t2 t1
375375
376376-- Keep a key-value pair when its key appears in the second dictionary.
377377-- Preference is given to values in the first dictionary.
378−intersect : Dict (Comparable k) v
379− -> Dict (Comparable k) v -> Dict (Comparable k) v
378+intersect : Dict (Comparable k) v -> Dict (Comparable k) v -> Dict (Comparable k) v
380379intersect t1 t2 =
381380 let combine k v t = if k `member` t2 then insert k v t else t
382381 in foldl combine empty t1