@@ -8,49 +8,55 @@ import Parse.Helpers
88import qualified AST.Module as Module
99import qualified AST.Variable as Var
1010
11+
1112getModuleName :: String -> Maybe String
1213getModuleName source =
13− case iParse getModuleName source of
14− Right name -> Just name
15− Left _ -> Nothing
16− where
17− getModuleName = do
18− optional freshLine
19− (names, _) <- header
20− return (intercalate "." names)
14+ case iParse getModuleName source of
15+ Right name -> Just name
16+ Left _ -> Nothing
17+ where
18+ getModuleName =
19+ do optional freshLine
20+ (names, _) <- header
21+ return (intercalate "." names)
22+
2123
2224headerAndImports :: IParser Module.HeaderAndImports
2325headerAndImports =
24− do optional freshLine
25− (names, exports) <-
26− option (["Main"], Var.openListing) (header `followedBy` freshLine)
27− imports' <- commitToImports <|> return []
28− return $ Module.HeaderAndImports names exports imports'
29− where
30− commitToImports =
31− do try (lookAhead $ reserved "import")
32− imports `followedBy` freshLine
26+ do optional freshLine
27+ (names, exports) <-
28+ option (["Main"], Var.openListing) (header `followedBy` freshLine)
29+ imports' <- commitToImports <|> return []
30+ return $ Module.HeaderAndImports names exports imports'
31+ where
32+ commitToImports =
33+ do try (lookAhead $ reserved "import")
34+ imports `followedBy` freshLine
35+
3336
3437header :: IParser ([String], Var.Listing Var.Value)
35−header = do
36− try (reserved "module")
37− whitespace
38− names <- dotSep1 capVar <?> "name of module"
39− whitespace
40− exports <- option Var.openListing (listing value)
41− whitespace <?> "reserved word 'where'"
42− reserved "where"
43− return (names, exports)
38+header =
39+ do try (reserved "module")
40+ whitespace
41+ names <- dotSep1 capVar <?> "name of module"
42+ whitespace
43+ exports <- option Var.openListing (listing value)
44+ whitespace <?> "reserved word 'where'"
45+ reserved "where"
46+ return (names, exports)
47+
4448
4549imports :: IParser [(Module.Name, Module.ImportMethod)]
46−imports = option [] ((:) <$> import' <*> many (try (freshLine >> import')))
50+imports =
51+ option [] ((:) <$> import' <*> many (try (freshLine >> import')))
52+
4753
4854import' :: IParser (Module.Name, Module.ImportMethod)
4955import' =
50− do reserved "import"
51− whitespace
52− names <- dotSep1 capVar
53− (,) names <$> option (Module.As (intercalate "." names)) method
56+ do reserved "import"
57+ whitespace
58+ names <- dotSep1 capVar
59+ (,) names <$> option (Module.As (intercalate "." names)) method
5460 where
5561 method :: IParser Module.ImportMethod
5662 method = as' <|> importing'
@@ -64,25 +70,31 @@ import' =
6470 importing' :: IParser Module.ImportMethod
6571 importing' = Module.Open <$> listing value
6672
73+
6774listing :: IParser a -> IParser (Var.Listing a)
6875listing item =
69− do try (whitespace >> char '(')
70− whitespace
71− listing <- choice [ const Var.openListing <$> string ".."
72− , Var.Listing <$> commaSep1 item <*> return False
73− ] <?> "listing of values (x,y,z)"
74− whitespace
75− char ')'
76− return listing
76+ do try (whitespace >> char '(')
77+ whitespace
78+ listing <-
79+ choice
80+ [ const Var.openListing <$> string ".."
81+ , Var.Listing <$> commaSep1 item <*> return False
82+ ] <?> "listing of values (x,y,z)"
83+ whitespace
84+ char ')'
85+ return listing
86+
7787
7888value :: IParser Var.Value
79−value = val <|> tipe
80− where
81− val = Var.Value <$> (lowVar <|> parens symOp)
82−
83− tipe = do
84− name <- capVar
85− maybeCtors <- optionMaybe (listing capVar)
86− case maybeCtors of
87− Nothing -> return (Var.Alias name)
88− Just ctors -> return (Var.Union name ctors)
89+value =
90+ val <|> tipe
91+ where
92+ val =
93+ Var.Value <$> (lowVar <|> parens symOp)
94+
95+ tipe =
96+ do name <- capVar
97+ maybeCtors <- optionMaybe (listing capVar)
98+ case maybeCtors of
99+ Nothing -> return (Var.Alias name)
100+ Just ctors -> return (Var.Union name ctors)