Skip to content

Commit 6ae40b0

Browse files
committed
Handle main-is
1 parent ea43db6 commit 6ae40b0

File tree

2 files changed

+65
-38
lines changed

2 files changed

+65
-38
lines changed

src/Hie/Cabal/Parser.hs

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ parsePackage =
4242
parseComponent :: Indent -> Parser Component
4343
parseComponent i =
4444
parseLib i
45-
<|> parseNamed i "executable" Exe
45+
<|> parseExe i
4646
<|> parseNamed i "test-suite" Test
4747

4848
parseLib :: Indent -> Parser Component
4949
parseLib i =
5050
indent i
5151
>> asciiCI "library"
5252
>> skipToNextLine
53-
>> Lib <$> parsePath (i + 1)
53+
>> Lib <$> extractPath (i + 1)
5454

5555
parseQuoted :: Parser Text
5656
parseQuoted = do
@@ -60,7 +60,25 @@ parseQuoted = do
6060
parseString :: Parser Name
6161
parseString = do
6262
skipSpace
63-
parseQuoted <|> takeWhile1 (not . isSpace)
63+
parseQuoted <|> takeWhile1 (not . (\c -> isSpace c || c == ','))
64+
65+
parseExe :: Indent -> Parser Component
66+
parseExe i =
67+
do
68+
indent i
69+
_ <- asciiCI "executable"
70+
_ <- skipSpace
71+
n <- parseString <?> "Exe Name"
72+
skipToNextLine
73+
Exe n <$> pathMain (i + 1) "." ""
74+
<?> T.unpack "parseExe"
75+
76+
pathMain :: Indent -> Text -> Text -> Parser Text
77+
pathMain i p m =
78+
(field i "hs-source-dirs" >>= (\p' -> pathMain i p' m))
79+
<|> (field i "main-is" >>= pathMain i p)
80+
<|> (skipBlockLine i >> pathMain i p m)
81+
<|> pure (p <> "/" <> m)
6482

6583
parseNamed :: Indent -> Text -> (Name -> Path -> Component) -> Parser Component
6684
parseNamed i compType compCon =
@@ -70,43 +88,52 @@ parseNamed i compType compCon =
7088
_ <- skipSpace <?> "skipSpace"
7189
n <- parseString <?> "N"
7290
skipToNextLine
73-
compCon n <$> parsePath (i + 1)
91+
compCon n <$> extractPath (i + 1)
7492
<?> T.unpack ("parseNamed " <> compType)
7593

7694
skipToNextLine :: Parser ()
7795
skipToNextLine = skipWhile (not . isEndOfLine) >> endOfLine
7896

7997
skipBlock :: Indent -> Parser ()
80-
skipBlock i =
81-
skipMany $
82-
(indent i >> skipToNextLine)
83-
<|> (skipMany tabOrSpace >> endOfLine)
84-
<|> (skipSpace >> "--" >> skipToNextLine)
98+
skipBlock i = skipMany $ skipBlockLine i
99+
100+
skipBlockLine :: Indent -> Parser ()
101+
skipBlockLine i =
102+
(indent i >> skipToNextLine)
103+
<|> (skipMany tabOrSpace >> endOfLine)
104+
<|> (skipSpace >> "--" >> skipToNextLine)
85105

86106
tabOrSpace :: Parser Char
87107
tabOrSpace = char ' ' <|> char '\t'
88108

89-
parsePath :: Indent -> Parser Path
90-
parsePath i =
109+
field :: Indent -> Text -> Parser Text
110+
field i f =
111+
do
112+
indent i
113+
_ <- asciiCI f
114+
skipSpace
115+
_ <- char ':'
116+
p <- parseString
117+
skipToNextLine
118+
pure p
119+
120+
parseMainIs :: Indent -> Parser Path
121+
parseMainIs i =
122+
do
123+
p <- field i "main-is"
124+
skipBlock i
125+
pure p
126+
<?> "hs-source-dirs"
127+
128+
extractPath :: Indent -> Parser Path
129+
extractPath i =
91130
( do
92-
indent i
93-
_ <- "hs-source-dirs"
94-
skipSpace
95-
_ <- char ':'
96-
-- FIXME paths can be in quotes
97-
p <- parseString
98-
skipToNextLine
131+
p <- field i "hs-source-dirs"
99132
skipBlock i
100133
pure p
101-
<?> "hs-source-dirs"
102134
)
103-
<|> ( do
104-
indent i
105-
skipToNextLine
106-
parsePath i
107-
<?> "skip line"
108-
)
109-
<|> (pure "." <?> "not found") <?> "parsePath"
135+
<|> (skipBlockLine i >> extractPath i <?> "skip line")
136+
<|> (pure "." <?> "not found") <?> "extractPath"
110137

111138
-- | Skip at least n spaces
112139
indent :: Indent -> Parser ()

test/Spec.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ spec :: Spec
1515
spec = do
1616
describe "Should Succeed"
1717
$ it "successfully parses executable section"
18-
$ exeSection ~> parseNamed 0 "executable" Exe
19-
`shouldParse` Exe "implicit-hie-exe" "app"
18+
$ exeSection ~> parseExe 0
19+
`shouldParse` Exe "implicit-hie-exe" "app/Main.hs"
2020
describe "Should Succeed"
2121
$ it "successfully parses test section"
2222
$ testSection ~> parseNamed 0 "test-suite" Test
@@ -27,17 +27,17 @@ spec = do
2727
`shouldParse` Lib "src"
2828
describe "Should Succeed"
2929
$ it "successfully parses package"
30-
$ fullFile ~> parsePackage
31-
`shouldParse` Package
32-
"implicit-hie"
33-
[ Lib "src",
34-
Exe "implicit-hie-exe" "app",
30+
$ fullFile ~> parsePackage
31+
`shouldParse` Package
32+
"implicit-hie"
33+
[ Lib "src",
34+
Exe "implicit-hie-exe" "app/Main.hs",
3535
Test "implicit-hie-test" "test"
36-
]
37-
describe "Should Succeed"
38-
$ it
36+
]
37+
describe "Should Succeed"
38+
$ it
3939
"skips to end of block section"
40-
$ let r = "test\n"
40+
$ let r = "test\n"
4141
in (libSection <> r) ~?> parseLib 0
4242
`leavesUnconsumed` r
4343
describe "Should Succeed"
@@ -99,7 +99,7 @@ stackHie =
9999
\ stack:\n\
100100
\ - path: \"src\"\n\
101101
\ component: \"implicit-hie:lib\"\n\
102-
\ - path: \"app\"\n\
102+
\ - path: \"app/Main.hs\"\n\
103103
\ component: \"implicit-hie:exe:implicit-hie-exe\"\n\
104104
\ - path: \"test\"\n\
105105
\ component: \"implicit-hie:test:implicit-hie-test\"\n\

0 commit comments

Comments
 (0)