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
103 return (Module.Port (R.Region start end))
107parseSetting :: IParser Module.SourceSettings
109 do try (reserved "where")
111 addLocation $ brackets $ commaSep $
112 do name <- addLocation lowVar
114 typeName <- addLocation capVar
115 return (name, typeName)
118imports :: IParser [Module.UserImport]
120 many (import' `followedBy` freshLine)
123import' :: IParser Module.UserImport
125 expecting "an import" $
127 do try (reserved "import")
129 names <- dotSep1 capVar
130 (,) names <$> method (ModuleName.toString names)
132 method :: String -> IParser Module.ImportMethod
133 method defaultAlias =
135 <$> option (Just defaultAlias) (Just <$> as' defaultAlias)
136 <*> option Var.closedListing exposing
138 as' :: String -> IParser String
140 do try (whitespace >> reserved "as")
142 capVar <?> ("an alias for module `" ++ moduleName ++ "`")
144 exposing :: IParser (Var.Listing Var.Value)
146 do try (whitespace >> reserved "exposing")
151listing :: IParser a -> IParser (Var.Listing a)
153 expecting "a listing of values and types to expose, like (..)" $
154 do try (whitespace >> char '(')
158 [ const Var.openListing <$> string ".."
159 , Var.Listing <$> commaSep1 item <*> return False
166value :: IParser Var.Value
168 val <|> tipe <?> "a value or type to expose"
171 Var.Value <$> (lowVar <|> parens symOp)
175 maybeCtors <- optionMaybe (listing capVar)
177 Nothing -> return (Var.Alias name)
178 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