1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2module Parse.Module (moduleDecl, header, getModuleName) where
4import Text.Parsec hiding (newline, parse, spaces)
6import Parse.Binop (infixOp)
8 ( IParser, addLocation, brackets, capVar, commaSep, commaSep1
9 , docComment, dotSep1, equals, expecting, followedBy, freshLine
10 , getMyPosition, lowVar, padded, parens, parse, reserved, whitespace
12import qualified AST.Module as Module
13import qualified AST.Module.Name as ModuleName
14import qualified AST.Variable as Var
15import qualified Reporting.Annotation as A
16import qualified Reporting.Region as R
19getModuleName :: String -> Maybe String
24 (ModuleDecl _ names _ _) <- moduleDecl
25 return (ModuleName.toString names)
27 case parse minimalParser source of
35header :: IParser (Module.Header [Module.UserImport])
38 (ModuleDecl tag names exports settings) <-
40 (ModuleDecl Module.Normal ["Main"] Var.openListing Module.emptySettings)
41 (moduleDecl `followedBy` freshLine)
45 [ addLocation (Just <$> docComment) `followedBy` freshLine
46 , addLocation (return Nothing)
51 return (Module.Header tag names exports settings docs imports')
56 { _tag :: Module.SourceTag
58 , _exports :: Var.Listing (A.Located Var.Value)
59 , _settings :: Module.SourceSettings
63moduleDecl :: IParser ModuleDecl
65 expecting "a module declaration" $
70 names <- dotSep1 capVar <?> "the name of this module"
76 parseSetting <* whitespace
79 return Module.emptySettings
81 reserved "exposing" <?> "something like `exposing (..)` which replaced `where` in 0.17"
84 exports <- listing (addLocation value)
86 return (ModuleDecl tag names exports settings)
89parseTag :: IParser Module.SourceTag
93 do try (reserved "module")
96 do start <- getMyPosition
97 try (reserved "effect")
101 return (Module.Effect (R.Region start end))
103 do start <- getMyPosition
104 try (reserved "port")
108 return (Module.Port (R.Region start end))
112parseSetting :: IParser Module.SourceSettings
114 do try (reserved "where")
116 addLocation $ brackets $ commaSep $
117 do name <- addLocation lowVar
119 typeName <- addLocation capVar
120 return (name, typeName)
123imports :: IParser [Module.UserImport]
125 many (import' `followedBy` freshLine)
128import' :: IParser Module.UserImport
130 expecting "an import" $
132 do try (reserved "import")
134 names <- dotSep1 capVar
135 (,) names <$> method (ModuleName.toString names)
137 method :: String -> IParser Module.ImportMethod
138 method defaultAlias =
140 <$> option (Just defaultAlias) (Just <$> as' defaultAlias)
141 <*> option Var.closedListing exposing
143 as' :: String -> IParser String
145 do try (whitespace >> reserved "as")
147 capVar <?> ("an alias for module `" ++ moduleName ++ "`")
149 exposing :: IParser (Var.Listing Var.Value)
151 do try (whitespace >> reserved "exposing")
156listing :: IParser a -> IParser (Var.Listing a)
158 expecting "a listing of values and types to expose, like (..)" $
159 do try (whitespace >> char '(')
163 [ const Var.openListing <$> string ".."
164 , Var.Listing <$> commaSep1 item <*> return False
171value :: IParser Var.Value
173 val <|> tipe <?> "a value or type to expose"
176 Var.Value <$> (lowVar <|> parens infixOp)
180 maybeCtors <- optionMaybe (listing capVar)
182 Nothing -> return (Var.Alias name)
183 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