Handle main-is

This commit is contained in:
Avi Dessauer 2020-05-02 23:34:16 -04:00
parent ea43db6bc6
commit 6ae40b0dc6
2 changed files with 65 additions and 38 deletions

View File

@ -42,7 +42,7 @@ parsePackage =
parseComponent :: Indent -> Parser Component
parseComponent i =
parseLib i
<|> parseNamed i "executable" Exe
<|> parseExe i
<|> parseNamed i "test-suite" Test
parseLib :: Indent -> Parser Component
@ -50,7 +50,7 @@ parseLib i =
indent i
>> asciiCI "library"
>> skipToNextLine
>> Lib <$> parsePath (i + 1)
>> Lib <$> extractPath (i + 1)
parseQuoted :: Parser Text
parseQuoted = do
@ -60,7 +60,25 @@ parseQuoted = do
parseString :: Parser Name
parseString = do
skipSpace
parseQuoted <|> takeWhile1 (not . isSpace)
parseQuoted <|> takeWhile1 (not . (\c -> isSpace c || c == ','))
parseExe :: Indent -> Parser Component
parseExe i =
do
indent i
_ <- asciiCI "executable"
_ <- skipSpace
n <- parseString <?> "Exe Name"
skipToNextLine
Exe n <$> pathMain (i + 1) "." ""
<?> T.unpack "parseExe"
pathMain :: Indent -> Text -> Text -> Parser Text
pathMain i p m =
(field i "hs-source-dirs" >>= (\p' -> pathMain i p' m))
<|> (field i "main-is" >>= pathMain i p)
<|> (skipBlockLine i >> pathMain i p m)
<|> pure (p <> "/" <> m)
parseNamed :: Indent -> Text -> (Name -> Path -> Component) -> Parser Component
parseNamed i compType compCon =
@ -70,43 +88,52 @@ parseNamed i compType compCon =
_ <- skipSpace <?> "skipSpace"
n <- parseString <?> "N"
skipToNextLine
compCon n <$> parsePath (i + 1)
compCon n <$> extractPath (i + 1)
<?> T.unpack ("parseNamed " <> compType)
skipToNextLine :: Parser ()
skipToNextLine = skipWhile (not . isEndOfLine) >> endOfLine
skipBlock :: Indent -> Parser ()
skipBlock i =
skipMany $
(indent i >> skipToNextLine)
<|> (skipMany tabOrSpace >> endOfLine)
<|> (skipSpace >> "--" >> skipToNextLine)
skipBlock i = skipMany $ skipBlockLine i
skipBlockLine :: Indent -> Parser ()
skipBlockLine i =
(indent i >> skipToNextLine)
<|> (skipMany tabOrSpace >> endOfLine)
<|> (skipSpace >> "--" >> skipToNextLine)
tabOrSpace :: Parser Char
tabOrSpace = char ' ' <|> char '\t'
parsePath :: Indent -> Parser Path
parsePath i =
field :: Indent -> Text -> Parser Text
field i f =
do
indent i
_ <- asciiCI f
skipSpace
_ <- char ':'
p <- parseString
skipToNextLine
pure p
parseMainIs :: Indent -> Parser Path
parseMainIs i =
do
p <- field i "main-is"
skipBlock i
pure p
<?> "hs-source-dirs"
extractPath :: Indent -> Parser Path
extractPath i =
( do
indent i
_ <- "hs-source-dirs"
skipSpace
_ <- char ':'
-- FIXME paths can be in quotes
p <- parseString
skipToNextLine
p <- field i "hs-source-dirs"
skipBlock i
pure p
<?> "hs-source-dirs"
)
<|> ( do
indent i
skipToNextLine
parsePath i
<?> "skip line"
)
<|> (pure "." <?> "not found") <?> "parsePath"
<|> (skipBlockLine i >> extractPath i <?> "skip line")
<|> (pure "." <?> "not found") <?> "extractPath"
-- | Skip at least n spaces
indent :: Indent -> Parser ()

View File

@ -15,8 +15,8 @@ spec :: Spec
spec = do
describe "Should Succeed"
$ it "successfully parses executable section"
$ exeSection ~> parseNamed 0 "executable" Exe
`shouldParse` Exe "implicit-hie-exe" "app"
$ exeSection ~> parseExe 0
`shouldParse` Exe "implicit-hie-exe" "app/Main.hs"
describe "Should Succeed"
$ it "successfully parses test section"
$ testSection ~> parseNamed 0 "test-suite" Test
@ -27,17 +27,17 @@ spec = do
`shouldParse` Lib "src"
describe "Should Succeed"
$ it "successfully parses package"
$ fullFile ~> parsePackage
`shouldParse` Package
"implicit-hie"
[ Lib "src",
Exe "implicit-hie-exe" "app",
$ fullFile ~> parsePackage
`shouldParse` Package
"implicit-hie"
[ Lib "src",
Exe "implicit-hie-exe" "app/Main.hs",
Test "implicit-hie-test" "test"
]
describe "Should Succeed"
$ it
]
describe "Should Succeed"
$ it
"skips to end of block section"
$ let r = "test\n"
$ let r = "test\n"
in (libSection <> r) ~?> parseLib 0
`leavesUnconsumed` r
describe "Should Succeed"
@ -99,7 +99,7 @@ stackHie =
\ stack:\n\
\ - path: \"src\"\n\
\ component: \"implicit-hie:lib\"\n\
\ - path: \"app\"\n\
\ - path: \"app/Main.hs\"\n\
\ component: \"implicit-hie:exe:implicit-hie-exe\"\n\
\ - path: \"test\"\n\
\ component: \"implicit-hie:test:implicit-hie-test\"\n\