Add ‘skipBlockCommentNested’ function

Close #96.
This commit is contained in:
mrkkrp 2016-03-30 14:50:35 +06:00
parent 18d192ba70
commit 747993e0bb
3 changed files with 29 additions and 0 deletions

View File

@ -28,6 +28,9 @@
makes `signed` parser more natural and general, because we do not need makes `signed` parser more natural and general, because we do not need
ad-hoc `Signed` type class anymore. ad-hoc `Signed` type class anymore.
* Added `skipBlockCommentNested` function that should help parse possibly
nested block comments.
## Megaparsec 4.4.0 ## Megaparsec 4.4.0
* Now state returned on failure is the exact state of parser at the moment * Now state returned on failure is the exact state of parser at the moment

View File

@ -26,6 +26,7 @@ module Text.Megaparsec.Lexer
, symbol' , symbol'
, skipLineComment , skipLineComment
, skipBlockComment , skipBlockComment
, skipBlockCommentNested
-- * Indentation -- * Indentation
, indentLevel , indentLevel
, indentGuard , indentGuard
@ -162,6 +163,20 @@ skipBlockComment start end = p >> void (manyTill C.anyChar n)
where p = C.string start where p = C.string start
n = C.string end n = C.string end
-- | @skipBlockCommentNested start end@ skips possibly nested block comment
-- starting with @start@ and ending with @end@.
--
-- @since 5.0.0
skipBlockCommentNested :: MonadParsec s m Char
=> String -- ^ Start of block comment
-> String -- ^ End of block comment
-> m ()
skipBlockCommentNested start end = p >> void (manyTill e n)
where e = skipBlockCommentNested start end <|> void C.anyChar
p = C.string start
n = C.string end
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
-- Indentation -- Indentation

View File

@ -45,7 +45,9 @@ import Data.Scientific (fromFloatDigits)
import Numeric (showInt, showHex, showOct, showSigned) import Numeric (showInt, showHex, showOct, showSigned)
import Test.Framework import Test.Framework
import Test.Framework.Providers.HUnit (testCase)
import Test.Framework.Providers.QuickCheck2 (testProperty) import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.HUnit (Assertion)
import Test.QuickCheck import Test.QuickCheck
import Text.Megaparsec.Error import Text.Megaparsec.Error
@ -67,6 +69,7 @@ tests = testGroup "Lexer"
[ testProperty "space combinator" prop_space [ testProperty "space combinator" prop_space
, testProperty "symbol combinator" prop_symbol , testProperty "symbol combinator" prop_symbol
, testProperty "symbol' combinator" prop_symbol' , testProperty "symbol' combinator" prop_symbol'
, testCase "skipBlockCommentNested" prop_skipBlockCommentNested
, testProperty "indentLevel" prop_indentLevel , testProperty "indentLevel" prop_indentLevel
, testProperty "indentGuard combinator" prop_indentGuard , testProperty "indentGuard combinator" prop_indentGuard
, testProperty "nonIndented combinator" prop_nonIndented , testProperty "nonIndented combinator" prop_nonIndented
@ -154,6 +157,14 @@ parseSymbol p' f s' t = checkParser p r s
g = takeWhile (not . isSpace) s g = takeWhile (not . isSpace) s
s = s' ++ maybeToList t s = s' ++ maybeToList t
prop_skipBlockCommentNested :: Assertion
prop_skipBlockCommentNested = checkCase p r s
where p :: MonadParsec s m Char => m ()
p = space (void C.spaceChar) empty
(skipBlockCommentNested "/*" "*/") <* eof
r = Right ()
s = " /* foo bar /* baz */ quux */ "
-- Indentation -- Indentation
prop_indentLevel :: SourcePos -> Property prop_indentLevel :: SourcePos -> Property