1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2module Parse.Module (moduleDecl, header, getModuleName) where
4import Text.Parsec hiding (newline, spaces)
6import Parse.Binop (infixOp)
8import qualified AST.Module as Module
9import qualified AST.Module.Name as ModuleName
10import qualified AST.Variable as Var
11import qualified Reporting.Annotation as A
12import qualified Reporting.Region as R
15getModuleName :: String -> Maybe String
20 (ModuleDecl _ names _ _) <- moduleDecl
21 return (ModuleName.toString names)
23 case iParse minimalParser source of
31header :: IParser (Module.Header [Module.UserImport])
34 (ModuleDecl tag names exports settings) <-
36 (ModuleDecl Module.Normal ["Main"] Var.openListing Module.emptySettings)
37 (moduleDecl `followedBy` freshLine)
41 [ addLocation (Just <$> docComment) `followedBy` freshLine
42 , addLocation (return Nothing)
47 return (Module.Header tag names exports settings docs imports')
52 { _tag :: Module.SourceTag
54 , _exports :: Var.Listing (A.Located Var.Value)
55 , _settings :: Module.SourceSettings
59moduleDecl :: IParser ModuleDecl
61 expecting "a module declaration" $
66 names <- dotSep1 capVar <?> "the name of this module"
72 parseSetting <* whitespace
75 return Module.emptySettings
77 reserved "exposing" <?> "something like `exposing (..)` which replaced `where` in 0.17"
80 exports <- listing (addLocation value)
82 return (ModuleDecl tag names exports settings)
85parseTag :: IParser Module.SourceTag
89 do try (reserved "module")
92 do start <- getMyPosition
93 try (reserved "effect")
97 return (Module.Effect (R.Region start end))
99 do start <- getMyPosition
100 try (reserved "port")
104 return (Module.Port (R.Region start end))
108parseSetting :: IParser Module.SourceSettings
110 do try (reserved "where")
112 addLocation $ brackets $ commaSep $
113 do name <- addLocation lowVar
115 typeName <- addLocation capVar
116 return (name, typeName)
119imports :: IParser [Module.UserImport]
121 many (import' `followedBy` freshLine)
124import' :: IParser Module.UserImport
126 expecting "an import" $
128 do try (reserved "import")
130 names <- dotSep1 capVar
131 (,) names <$> method (ModuleName.toString names)
133 method :: String -> IParser Module.ImportMethod
134 method defaultAlias =
136 <$> option (Just defaultAlias) (Just <$> as' defaultAlias)
137 <*> option Var.closedListing exposing
139 as' :: String -> IParser String
141 do try (whitespace >> reserved "as")
143 capVar <?> ("an alias for module `" ++ moduleName ++ "`")
145 exposing :: IParser (Var.Listing Var.Value)
147 do try (whitespace >> reserved "exposing")
152listing :: IParser a -> IParser (Var.Listing a)
154 expecting "a listing of values and types to expose, like (..)" $
155 do try (whitespace >> char '(')
159 [ const Var.openListing <$> string ".."
160 , Var.Listing <$> commaSep1 item <*> return False
167value :: IParser Var.Value
169 val <|> tipe <?> "a value or type to expose"
172 Var.Value <$> (lowVar <|> parens infixOp)
176 maybeCtors <- optionMaybe (listing capVar)
178 Nothing -> return (Var.Alias name)
179 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