Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#107] Parse nested comments #109

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Extensions/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Data.ByteString (ByteString)
import Data.Char (toLower, toUpper)
import Data.Either (partitionEithers)
import Data.Foldable (traverse_)
import Data.Functor ((<&>))
import Data.Functor ((<&>), void)
import Data.List (nub)
import Data.List.NonEmpty (NonEmpty (..))
import System.Directory (doesFileExist)
Expand Down Expand Up @@ -109,8 +109,8 @@ extensionsP :: Parser [ParsedExtension]
extensionsP = concat <$>
( newLines *>
manyTill
(try singleExtensionsP <|> try optionsGhcP <|> try commentP <|> try cppP)
(eof <|> (() <$ manyTill endOfLine letter))
(try singleExtensionsP <|> try optionsGhcP <|> [] <$ try commentP <|> try cppP)
(eof <|> void (manyTill endOfLine letter))
)

{- | Single LANGUAGE pragma parser.
Expand Down Expand Up @@ -186,16 +186,16 @@ and multi-line comments:
\-\}
@
-}
commentP :: Parser [a]
commentP :: Parser String
commentP = newLines *> (try singleLineCommentP <|> try multiLineCommentP) <* newLines
where
singleLineCommentP :: Parser [a]
singleLineCommentP = [] <$
(string "--" *> manyTill anyChar (try (() <$ endOfLine) <|> eof))
singleLineCommentP :: Parser String
singleLineCommentP =
string "--" *> manyTill anyChar (try (void endOfLine) <|> eof)

multiLineCommentP :: Parser [a]
multiLineCommentP = [] <$
(string "{-" *> manyTill anyChar (try $ string "-}"))
multiLineCommentP :: Parser String
multiLineCommentP =
concat <$> (string "{-" *> manyTill (try multiLineCommentP <|> (: []) <$> anyChar) (try (string "-}")))

{- | CPP syntax parser.

Expand Down
3 changes: 3 additions & 0 deletions test/Test/Extensions/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ mixSpec = describe "Parsing combinations of different parts" $ do
{ parsedExtensionsAll = [On Cpp]
, parsedExtensionsSafe = Just Trustworthy
}
itShouldParse "{- {- -} -}" []
itShouldParse "{- {-# LANGUAGE LambdaCase #-} -}" []
itShouldParse "{-# LANGUAGE LambdaCase {- -} #-}" [LambdaCase]

itShouldParse :: String -> [Extension] -> SpecWith (Arg Expectation)
itShouldParse s = itShouldParseOnOff s . map On
Expand Down
Loading