Skip to content

Commit

Permalink
[#107] Parse nested comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-christiansen authored and tomjaguarpaw committed Dec 16, 2024
1 parent f56be07 commit 6e17153
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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 @@ -423,6 +423,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

0 comments on commit 6e17153

Please sign in to comment.