diff --git a/CHANGELOG.md b/CHANGELOG.md index e916850..5ec29e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ * Defined `displayException` for `ParseError`, so exceptions are displayed in human-friendly form now. This works with GHC 7.10 and later. +* Line comments parsed by `skipLineComment` now may end at the end of input + and do not necessarily require a newline to be parsed correctly. See #119. + ## Megaparsec 5.0.1 * Derived `NFData` instances for `Pos`, `InvalidPosException`, `SourcePos`, diff --git a/Text/Megaparsec/Lexer.hs b/Text/Megaparsec/Lexer.hs index 0f2b23f..e54cc09 100644 --- a/Text/Megaparsec/Lexer.hs +++ b/Text/Megaparsec/Lexer.hs @@ -158,7 +158,7 @@ skipLineComment :: (MonadParsec e s m, Token s ~ Char) -> m () skipLineComment prefix = p >> void (manyTill C.anyChar n) where p = C.string prefix - n = lookAhead C.newline + n = lookAhead (void C.newline) <|> eof -- | @skipBlockComment start end@ skips non-nested block comment starting -- with @start@ and ending with @end@. diff --git a/tests/Lexer.hs b/tests/Lexer.hs index 33d8915..d919377 100644 --- a/tests/Lexer.hs +++ b/tests/Lexer.hs @@ -71,6 +71,7 @@ tests = testGroup "Lexer" [ testProperty "space combinator" prop_space , testProperty "symbol combinator" prop_symbol , testProperty "symbol' combinator" prop_symbol' + , testCase "case_skipLineCommentEof" case_skipLineCommentEof , testCase "skipBlockCommentNested" case_skipBlockCommentNested , testProperty "indentLevel" prop_indentLevel , testProperty "incorrectIndent" prop_incorrectIndent @@ -165,6 +166,12 @@ parseSymbol p' f s' t = checkParser p r s g = takeWhile (not . isSpace) s s = s' ++ maybeToList t +case_skipLineCommentEof :: Assertion +case_skipLineCommentEof = checkCase p r s + where p = space (void C.spaceChar) (skipLineComment "//") empty <* eof + r = Right () + s = " // this line comment doesn't have a newline at the end " + case_skipBlockCommentNested :: Assertion case_skipBlockCommentNested = checkCase p r s where p = space (void C.spaceChar) empty