1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2module Parse.Module (moduleDecl, header, getModuleName) where
4import Text.Parsec hiding (newline, spaces)
7import qualified AST.Module as Module
8import qualified AST.Module.Name as ModuleName
9import qualified AST.Variable as Var
10import qualified Reporting.Annotation as A
11import qualified Reporting.Region as R
14getModuleName :: String -> Maybe String
19 (ModuleDecl _ names _ _) <- moduleDecl
20 return (ModuleName.toString names)
22 case iParse minimalParser source of
30header :: IParser (Module.Header [Module.UserImport])
33 (ModuleDecl tag names exports settings) <-
35 (ModuleDecl Module.Normal ["Main"] Var.openListing Module.emptySettings)
36 (moduleDecl `followedBy` freshLine)
40 [ addLocation (Just <$> docComment) `followedBy` freshLine
41 , addLocation (return Nothing)
46 return (Module.Header tag names exports settings docs imports')
51 { _tag :: Module.SourceTag
53 , _exports :: Var.Listing (A.Located Var.Value)
54 , _settings :: Module.SourceSettings
58moduleDecl :: IParser ModuleDecl
60 expecting "a module declaration" $
65 names <- dotSep1 capVar <?> "the name of this module"
71 parseSetting <* whitespace
74 return Module.emptySettings
76 reserved "exposing" <?> "something like `exposing (..)` which replaced `where` in 0.17"
79 exports <- listing (addLocation value)
81 return (ModuleDecl tag names exports settings)
84parseTag :: IParser Module.SourceTag
88 do try (reserved "module")
91 do start <- getMyPosition
92 try (reserved "effect")
96 return (Module.Effect (R.Region start end))
98 do start <- getMyPosition
105 return (Module.Foreign (R.Region start end))
109parseSetting :: IParser Module.SourceSettings
111 do try (reserved "where")
113 addLocation $ brackets $ commaSep $
114 do name <- addLocation lowVar
116 typeName <- addLocation capVar
117 return (name, typeName)
120imports :: IParser [Module.UserImport]
122 many (import' `followedBy` freshLine)
125import' :: IParser Module.UserImport
127 expecting "an import" $
129 do try (reserved "import")
131 names <- dotSep1 capVar
132 (,) names <$> method (ModuleName.toString names)
134 method :: String -> IParser Module.ImportMethod
135 method defaultAlias =
137 <$> option (Just defaultAlias) (Just <$> as' defaultAlias)
138 <*> option Var.closedListing exposing
140 as' :: String -> IParser String
142 do try (whitespace >> reserved "as")
144 capVar <?> ("an alias for module `" ++ moduleName ++ "`")
146 exposing :: IParser (Var.Listing Var.Value)
148 do try (whitespace >> reserved "exposing")
153listing :: IParser a -> IParser (Var.Listing a)
155 expecting "a listing of values and types to expose, like (..)" $
156 do try (whitespace >> char '(')
160 [ const Var.openListing <$> string ".."
161 , Var.Listing <$> commaSep1 item <*> return False
168value :: IParser Var.Value
170 val <|> tipe <?> "a value or type to expose"
173 Var.Value <$> (lowVar <|> parens symOp)
177 maybeCtors <- optionMaybe (listing capVar)
179 Nothing -> return (Var.Alias name)
180 Just ctors -> return (Var.Union name ctors)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list