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
13getModuleName :: String -> Maybe String
18 (ModuleDecl _ names _ _) <- moduleDecl
19 return (ModuleName.toString names)
21 case iParse minimalParser source of
29header :: IParser (Module.Header [Module.UserImport])
32 (ModuleDecl tag names exports settings) <-
34 (ModuleDecl Module.SrcNormal ["Main"] Var.openListing Module.emptySettings)
35 (moduleDecl `followedBy` freshLine)
39 [ addLocation (Just <$> docComment) `followedBy` freshLine
40 , addLocation (return Nothing)
45 return (Module.Header tag names exports settings docs imports')
50 { _tag :: Module.SourceTag
52 , _exports :: Var.Listing (A.Located Var.Value)
53 , _settings :: Module.SourceSettings
57moduleDecl :: IParser ModuleDecl
59 expecting "a module declaration" $
63 names <- padded (dotSep1 capVar) <?> "the name of this module"
65 settings <- option Module.emptySettings parseSetting
67 exports <- padded (listing (addLocation value))
71 return (ModuleDecl tag names exports settings)
74parseTag :: IParser Module.SourceTag
78 do try (reserved "module")
79 return Module.SrcNormal
81 do try (reserved "effect")
84 return Module.SrcEffect
91 return Module.SrcForeign
95parseSetting :: IParser Module.SourceSettings
97 addLocation $ brackets $ commaSep $
98 do name <- addLocation lowVar
100 typeName <- addLocation capVar
101 return (name, typeName)
104imports :: IParser [Module.UserImport]
106 many (import' `followedBy` freshLine)
109import' :: IParser Module.UserImport
111 expecting "an import" $
113 do try (reserved "import")
115 names <- dotSep1 capVar
116 (,) names <$> method (ModuleName.toString names)
118 method :: String -> IParser Module.ImportMethod
119 method defaultAlias =
121 <$> option (Just defaultAlias) (Just <$> as' defaultAlias)
122 <*> option Var.closedListing exposing
124 as' :: String -> IParser String
126 do try (whitespace >> reserved "as")
128 capVar <?> ("an alias for module `" ++ moduleName ++ "`")
130 exposing :: IParser (Var.Listing Var.Value)
132 do try (whitespace >> reserved "exposing")
137listing :: IParser a -> IParser (Var.Listing a)
139 expecting "a listing of values and types to expose, like (..)" $
140 do try (whitespace >> char '(')
144 [ const Var.openListing <$> string ".."
145 , Var.Listing <$> commaSep1 item <*> return False
152value :: IParser Var.Value
154 val <|> tipe <?> "a value or type to expose"
157 Var.Value <$> (lowVar <|> parens symOp)
161 maybeCtors <- optionMaybe (listing capVar)
163 Nothing -> return (Var.Alias name)
164 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