module DDC.Core.Lexer.Comments
( dropComments
, dropNewLines)
where
import DDC.Core.Lexer.Tokens
import DDC.Data.Token
import DDC.Data.SourcePos
dropComments
:: Eq n => [Token (Tok n)] -> [Token (Tok n)]
dropComments [] = []
dropComments (t@(Token tok sourcePos) : xs)
= case tok of
KM KCommentLineStart
-> dropComments $ dropWhile (\t' -> not $ isToken t' (KM KNewLine)) xs
KM KCommentBlockStart
-> dropComments $ dropCommentBlock sourcePos xs t
_ -> t : dropComments xs
dropCommentBlock
:: Eq n
=> SourcePos
-> [Token (Tok n)]
-> Token (Tok n)
-> [Token (Tok n)]
dropCommentBlock spStart [] _terr
= [Token (KM KCommentUnterminated) spStart]
dropCommentBlock spStart (t@(Token tok _) : xs) terr
= case tok of
KM KCommentBlockStart
-> dropCommentBlock spStart (dropCommentBlock spStart xs t) terr
KM KCommentBlockEnd
-> xs
_ -> dropCommentBlock spStart xs terr
dropNewLines :: Eq n => [Token (Tok n)] -> [Token (Tok n)]
dropNewLines [] = []
dropNewLines (t:ts)
| isToken t (KM KNewLine)
= dropNewLines ts
| otherwise
= t : dropNewLines ts
isToken :: Eq n => Token (Tok n) -> Tok n -> Bool
isToken (Token tok _) tok2 = tok == tok2