Skip to content

Commit 48fda59

Browse files
committed
get package name
1 parent 2a1ab48 commit 48fda59

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

app/Main.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ main :: IO ()
99
main = do
1010
args <- getArgs
1111
file <- T.readFile $ head args
12-
case parseOnly parseComponents file of
13-
Right r -> do
14-
putStrLn $ show (length r) <> " components"
15-
mapM_ print r
12+
case parseOnly parseSec file of
13+
Right r -> print r
1614
_ -> error "Could not parse *.cabal file"

src/Lib.hs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,29 @@ type Path = Text
1515

1616
type Indent = Int
1717

18+
data Package = Package Name [Component]
19+
deriving (Show, Eq, Ord)
20+
1821
data Component = Lib Path | Exe Name Path | Test Name Path
1922
deriving (Show, Eq, Ord)
2023

21-
parseComponents :: Parser [Component]
22-
parseComponents =
24+
parseName :: Parser Text
25+
parseName = "name" >> skipSpace >> char ':' >> parseString
26+
27+
parseSec :: Parser Package
28+
parseSec =
2329
( do
24-
h <- parseComponent 0
25-
t <- parseComponents
26-
pure $ h : t
30+
n <- parseName
31+
(Package _ t) <- parseSec
32+
pure $ Package n t
2733
)
28-
<|> (skipToNextLine >> parseComponents)
29-
<|> pure []
34+
<|> ( do
35+
h <- parseComponent 0
36+
(Package n t) <- parseSec
37+
pure $ Package n (h : t)
38+
)
39+
<|> (skipToNextLine >> parseSec)
40+
<|> pure (Package "" [])
3041

3142
parseComponent :: Indent -> Parser Component
3243
parseComponent i =
@@ -41,18 +52,23 @@ parseLib i =
4152
>> skipToNextLine
4253
>> Lib <$> parsePath (i + 1)
4354

44-
parseComponentName :: Parser Name
45-
parseComponentName = do
55+
parseQuoted :: Parser Text
56+
parseQuoted = do
57+
q <- char '"' <|> char '\''
58+
takeTill (== q)
59+
60+
parseString :: Parser Name
61+
parseString = do
4662
skipSpace
47-
takeWhile1 (not . isSpace)
63+
parseQuoted <|> takeWhile1 (not . isSpace)
4864

4965
parseNamed :: Indent -> Text -> (Name -> Path -> Component) -> Parser Component
5066
parseNamed i compType compCon =
5167
do
5268
indent i
5369
_ <- asciiCI compType <?> "asciiCI " <> T.unpack compType
5470
_ <- skipSpace <?> "skipSpace"
55-
n <- parseComponentName <?> "N"
71+
n <- parseString <?> "N"
5672
skipToNextLine
5773
compCon n <$> parsePath (i + 1)
5874
<?> T.unpack ("parseNamed " <> compType)
@@ -78,7 +94,7 @@ parsePath i =
7894
skipSpace
7995
_ <- char ':'
8096
-- FIXME paths can be in quotes
81-
p <- parseComponentName
97+
p <- parseString
8298
skipToNextLine
8399
skipBlock i
84100
pure p

test/Spec.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ spec = do
2525
`shouldParse` Lib "src"
2626
describe "Should Succeed"
2727
$ it "successfully parses library section"
28-
$ (exeSection <> testSection <> libSection) ~> parseComponents
29-
`shouldParse` [ Exe "implicit-hie-exe" "app",
30-
Test "implicit-hie-test" "test",
31-
Lib "src"
32-
]
28+
$ ("name: implicit-hie\n" <> exeSection <> testSection <> libSection)
29+
~> parseSec
30+
`shouldParse` Package
31+
"implicit-hie"
32+
[ Exe "implicit-hie-exe" "app",
33+
Test "implicit-hie-test" "test",
34+
Lib "src"
35+
]
3336
describe "Should Succeed"
3437
$ it
3538
"successfully parses library section"

0 commit comments

Comments
 (0)