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"
68 settings <- option Module.emptySettings parseSetting
74 exports <- listing (addLocation value)
76 return (ModuleDecl tag names exports settings)
79parseTag :: IParser Module.SourceTag
83 do try (reserved "module")
86 do start <- getMyPosition
87 try (reserved "effect")
91 return (Module.Effect (R.Region start end))
93 do start <- getMyPosition
100 return (Module.Foreign (R.Region start end))
104parseSetting :: IParser Module.SourceSettings
106 do try (reserved "where")
108 addLocation $ brackets $ commaSep $
109 do name <- addLocation lowVar
111 typeName <- addLocation capVar
112 return (name, typeName)
115imports :: IParser [Module.UserImport]
117 many (import' `followedBy` freshLine)
120import' :: IParser Module.UserImport
122 expecting "an import" $
124 do try (reserved "import")
126 names <- dotSep1 capVar
127 (,) names <$> method (ModuleName.toString names)
129 method :: String -> IParser Module.ImportMethod
130 method defaultAlias =
132 <$> option (Just defaultAlias) (Just <$> as' defaultAlias)
133 <*> option Var.closedListing exposing
135 as' :: String -> IParser String
137 do try (whitespace >> reserved "as")
139 capVar <?> ("an alias for module `" ++ moduleName ++ "`")
141 exposing :: IParser (Var.Listing Var.Value)
143 do try (whitespace >> reserved "exposing")
148listing :: IParser a -> IParser (Var.Listing a)
150 expecting "a listing of values and types to expose, like (..)" $
151 do try (whitespace >> char '(')
155 [ const Var.openListing <$> string ".."
156 , Var.Listing <$> commaSep1 item <*> return False
163value :: IParser Var.Value
165 val <|> tipe <?> "a value or type to expose"
168 Var.Value <$> (lowVar <|> parens symOp)
172 maybeCtors <- optionMaybe (listing capVar)
174 Nothing -> return (Var.Alias name)
175 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