module Language.C.Comments
( Comment
, CommentFormat(SingleLine,MultiLine)
, commentPosition
, commentText
, commentTextWithoutMarks
, commentFormat
, comments
, commentsFromString
) where
import Control.Arrow
import Data.Maybe (maybeToList)
import Language.C.Comments.Lexer
import Language.C.Comments.LineParser (parseLines)
import Language.C.Data.Position
convertPosn :: FilePath -> AlexPosn -> Position
convertPosn file (AlexPn offset line col) = position offset file line col
parseComments :: String -> (String,[(AlexPosn,String,CommentFormat)])
parseComments =
alexScanTokens >>> unzip >>> (concat *** concatMap maybeToList)
data Comment = Comment {
commentPosition :: Position,
commentText :: String,
commentFormat :: CommentFormat
} deriving (Eq,Show)
commentTextWithoutMarks :: Comment -> String
commentTextWithoutMarks c = stripCommentMarks fmt (commentText c)
where fmt = commentFormat c
instance Ord Comment where
compare x y = compare (commentPosition x) (commentPosition y)
stripCommentMarks :: CommentFormat -> String -> String
stripCommentMarks SingleLine = drop 2
stripCommentMarks MultiLine = reverse . drop 2 . reverse . drop 2
makeComment :: FilePath -> (AlexPosn,String,CommentFormat) -> Comment
makeComment file (pos,txt,fmt) = Comment pos' txt fmt
where pos' = convertPosn file pos
commentsInFile :: FilePath -> String -> [Comment]
commentsInFile file code = map (makeComment file) cmnts
where joinBrokenLines = unlines . parseLines
(_,cmnts) = parseComments (joinBrokenLines code)
comments :: FilePath -> IO [Comment]
comments file = do
code <- readFile file
return $ commentsInFile file code
commentsFromString :: String -> [Comment]
commentsFromString = commentsInFile ""