From 9f49d2f4bd7df80e884af9a61f9256b836f2a267 Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Sat, 11 Jun 2022 03:12:14 +0600 Subject: [PATCH 1/8] [#6] Add a switch for allowing comments Problem: We want the interpolator to support commentaries in arbitrary lines. Solution: Implement a switch to allow writing comments and add some tests against the implementation. --- .../Interpolation/Nyan/Core/Internal/Base.hs | 6 +++++ .../Nyan/Core/Internal/Parser.hs | 24 ++++++++++++++++++- .../Nyan/Core/Internal/Processor.hs | 8 +++++++ core/tests/Test/Customization.hs | 1 + core/tests/Test/Interpolator.hs | 14 +++++++++++ core/tests/Test/Parser.hs | 2 +- 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs index ecbcde2..eab1feb 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs @@ -32,6 +32,8 @@ data IntData = IntData data ParsedIntPiece = PipString Text -- ^ Mere text. + | PipComments Text + -- ^ Comments. | PipNewline Text -- ^ Some line feed. -- This must be preferred over 'PipString'. @@ -84,6 +86,7 @@ data PreviewLevel -- | All switches options. data SwitchesOptions = SwitchesOptions { spacesTrimming :: Bool + , commenting :: Bool , indentationStripping :: Bool , leadingNewlineStripping :: Bool , trailingSpacesStripping :: Bool @@ -100,6 +103,7 @@ data SwitchesOptions = SwitchesOptions -- mandatory for specifying in the interpolator. data DefaultSwitchesOptions = DefaultSwitchesOptions { defSpacesTrimming :: Maybe Bool + , defCommenting :: Maybe Bool , defIndentationStripping :: Maybe Bool , defLeadingNewlineStripping :: Maybe Bool , defTrailingSpacesStripping :: Maybe Bool @@ -117,6 +121,7 @@ data DefaultSwitchesOptions = DefaultSwitchesOptions basicDefaultSwitchesOptions :: DefaultSwitchesOptions basicDefaultSwitchesOptions = DefaultSwitchesOptions { defSpacesTrimming = Just False + , defCommenting = Just False , defIndentationStripping = Just False , defLeadingNewlineStripping = Just False , defTrailingSpacesStripping = Just False @@ -130,6 +135,7 @@ basicDefaultSwitchesOptions = DefaultSwitchesOptions recommendedDefaultSwitchesOptions :: DefaultSwitchesOptions recommendedDefaultSwitchesOptions = DefaultSwitchesOptions { defSpacesTrimming = Just False + , defCommenting = Just False , defIndentationStripping = Just True , defLeadingNewlineStripping = Just True , defTrailingSpacesStripping = Just True diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs index ebd2b9c..f41b444 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs @@ -17,6 +17,7 @@ import Fmt (Builder, build, fmt) import Text.Interpolation.Nyan.Core.Internal.Base import Text.Megaparsec (Parsec, customFailure, eof, errorBundlePretty, label, lookAhead, parse, single, takeWhile1P, takeWhileP) +import Text.Megaparsec.Char (string) import Text.Megaparsec.Error (ShowErrorComponent (..)) newtype OptionChanged = OptionChanged Bool @@ -25,6 +26,7 @@ newtype OptionChanged = OptionChanged Bool -- | An accumulator for switch options during parsing. data SwitchesOptionsBuilder = SwitchesOptionsBuilder { spacesTrimmingB :: (OptionChanged, Maybe Bool) + , commentingB :: (OptionChanged, Maybe Bool) , indentationStrippingB :: (OptionChanged, Maybe Bool) , leadingNewlineStrippingB :: (OptionChanged, Maybe Bool) , trailingSpacesStrippingB :: (OptionChanged, Maybe Bool) @@ -38,6 +40,7 @@ toSwitchesOptionsBuilder :: DefaultSwitchesOptions -> SwitchesOptionsBuilder toSwitchesOptionsBuilder DefaultSwitchesOptions{..} = SwitchesOptionsBuilder { spacesTrimmingB = (OptionChanged False, defSpacesTrimming) + , commentingB = (OptionChanged False, defCommenting) , indentationStrippingB = (OptionChanged False, defIndentationStripping) , leadingNewlineStrippingB = (OptionChanged False, defLeadingNewlineStripping) , trailingSpacesStrippingB = (OptionChanged False, defTrailingSpacesStripping) @@ -50,6 +53,7 @@ toSwitchesOptionsBuilder DefaultSwitchesOptions{..} = finalizeSwitchesOptions :: MonadFail m => SwitchesOptionsBuilder -> m SwitchesOptions finalizeSwitchesOptions SwitchesOptionsBuilder{..} = do spacesTrimming <- fromOptional "spaces trimming" spacesTrimmingB + commenting <- fromOptional "allow commenting" commentingB indentationStripping <- fromOptional "indentation stripping" indentationStrippingB leadingNewlineStripping <- fromOptional "leading newline stripping" leadingNewlineStrippingB trailingSpacesStripping <- fromOptional "trailing spaces stripping" trailingSpacesStrippingB @@ -73,6 +77,12 @@ setIfNew desc new (OptionChanged ch, old) | old == Just new = fail $ "Switch option `" <> desc <> "` is set redundantly" | otherwise = return (OptionChanged True, Just new) +setCommenting :: SwitchesOptionsSetter m => Bool -> m () +setCommenting enable = do + opts <- get + res <- setIfNew "allow comments" enable (commentingB opts) + put opts{ commentingB = res } + setSpacesTrimming :: SwitchesOptionsSetter m => Bool -> m () setSpacesTrimming enable = do opts <- get @@ -150,6 +160,11 @@ switchesSectionP defSOpts = , single 'S' $> False ] >>= setSpacesTrimming + , asum + [ single 'c' $> True + , single 'C' $> False + ] >>= setCommenting + , asum [ single 'd' $> True , single 'D' $> False @@ -201,6 +216,7 @@ switchesHelpMessage sopts = (error "") (error "") (error "") + (error "") -- ↑ Note: If you edit this, you may also need to update -- the help messages below. in mconcat @@ -257,8 +273,14 @@ switchesHelpMessage sopts = intPieceP :: Ord e => Parsec e Text [ParsedIntPiece] intPieceP = asum [ + + -- consume comments + string "--" >>= \prefix -> do + content <- takeWhile1P Nothing (/= '\n') + pure $ one $ PipComments (prefix <> content) + -- consume normal text - one . PipString <$> takeWhile1P Nothing (notAnyOf [(== '\\'), (== '#'), isSpace]) + , one . PipString <$> takeWhile1P Nothing (notAnyOf [(== '\\'), (== '#'), isSpace]) -- potentially interpolator case , single '#' *> do diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs index 45d1b8b..6ad6c16 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs @@ -15,6 +15,7 @@ import Text.Interpolation.Nyan.Core.Internal.Base processIntString :: SwitchesOptions -> ParsedInterpolatedString -> InterpolatedString processIntString sopts istr = istr & V.fromList + & do if commenting sopts then skipComments else id & do if leadingNewlineStripping sopts then stripLeadingNewline else id & do if trailingSpacesStripping sopts then stripTrailingLeadingWs else id & do if indentationStripping sopts then stripCommonIndentation else id @@ -30,6 +31,8 @@ processIntString sopts istr = istr where (&) = flip ($) + skipComments = V.filter \case PipComments{} -> False; _ -> True + stripLeadingNewline ps = case V.uncons ps of Just (PipNewline _, ps') -> ps' _ -> ps @@ -45,6 +48,9 @@ processIntString sopts istr = istr PipString s -> let s' = trimText s in if T.null s' then Nothing else Just (PipString s') + PipComments s -> + let s' = trimText s + in if T.null s' then Nothing else Just (PipString s') p@PipInt{} -> Just p trimLeftSpaces ps = case V.uncons ps of @@ -104,6 +110,7 @@ processIntString sopts istr = istr -- invisible spaces to break the newlines sequence. p@PipLeadingWs{} : l -> p : skipNext l p@PipString{} : l -> p : reduceNext l + p@PipComments{} : l -> p : reduceNext l p@PipInt{} : l -> p : reduceNext l [] -> [] @@ -113,6 +120,7 @@ processIntString sopts istr = istr PipNewline nl -> IpString nl PipLeadingWs n -> IpString . mconcat $ replicate (fromIntegral n) " " PipEmptyLine -> IpString mempty + PipComments s -> IpString s PipInt i -> IpInt i glueStrings :: InterpolatedString -> InterpolatedString diff --git a/core/tests/Test/Customization.hs b/core/tests/Test/Customization.hs index a9c3970..9ad99b0 100644 --- a/core/tests/Test/Customization.hs +++ b/core/tests/Test/Customization.hs @@ -28,6 +28,7 @@ _AllFieldsAreExported = (error "") (error "") (error "") + (error "") -- ↑ if you change this, also add a field to the record below in basicDefaultSwitchesOptions { defIndentationStripping = Nothing diff --git a/core/tests/Test/Interpolator.hs b/core/tests/Test/Interpolator.hs index eaae800..b3aa01c 100644 --- a/core/tests/Test/Interpolator.hs +++ b/core/tests/Test/Interpolator.hs @@ -297,6 +297,20 @@ test_DefaultInterpolator = testGroup "Default interpolator" ] + ---------------------------------- + , testGroup "Commenting" + + [ testCase "Basic comments" do + [int|tc|Abc -- this is a comment|] + @?= "Abc " + + , testCase "Comments in arbitrary lines" do + [int|tc| -- comments at the beginning + My text -- comments in the middle + -- comments at the end + |] @?= " \nMy text \n\n" + ] + ] ] diff --git a/core/tests/Test/Parser.hs b/core/tests/Test/Parser.hs index 98aae69..e6bbcd1 100644 --- a/core/tests/Test/Parser.hs +++ b/core/tests/Test/Parser.hs @@ -121,7 +121,7 @@ test_TextParser = testGroup "Main text parser" basicSwitchesOptions :: SwitchesOptions basicSwitchesOptions = - SwitchesOptions False False False False AnyFromBuilder False False PreviewNone + SwitchesOptions False False False False False AnyFromBuilder False False PreviewNone test_SwitchesParser :: TestTree test_SwitchesParser = testGroup "Switches parser" From 4b01213d36cefa5b8453bcd4d5b52b872cd9f3f2 Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Mon, 13 Jun 2022 15:14:13 +0600 Subject: [PATCH 2/8] fixup! [#6] Add a switch for allowing comments --- core/src/Text/Interpolation/Nyan/Core.hs | 1 + core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/core/src/Text/Interpolation/Nyan/Core.hs b/core/src/Text/Interpolation/Nyan/Core.hs index c580a7d..e7374ce 100644 --- a/core/src/Text/Interpolation/Nyan/Core.hs +++ b/core/src/Text/Interpolation/Nyan/Core.hs @@ -33,6 +33,7 @@ module Text.Interpolation.Nyan.Core , recommendedDefaultSwitchesOptions -- ** Field accessors for default switches options , defSpacesTrimming + , defCommenting , defIndentationStripping , defLeadingNewlineStripping , defTrailingSpacesStripping diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs index f41b444..097390e 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs @@ -226,6 +226,11 @@ switchesHelpMessage sopts = , ("S", "disable spaces trimming", Just False) ] + , helpOnOptions (defCommenting sopts) + [ ("c", "enable commenting", Just True) + , ("C", "disable commenting", Just False) + ] + , helpOnOptions (defIndentationStripping sopts) [ ("d", "enable indentation stripping", Just True) , ("D", "disable indentation stripping", Just False) From 3ed16dd66afa619101922960ba28c87fef58bd8b Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Wed, 15 Jun 2022 18:50:55 +0600 Subject: [PATCH 3/8] fixup! [#6] Add a switch for allowing comments --- .../Text/Interpolation/Nyan/Core/Internal/Base.hs | 2 -- .../Interpolation/Nyan/Core/Internal/Parser.hs | 15 ++++++--------- .../Interpolation/Nyan/Core/Internal/Processor.hs | 8 -------- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs index eab1feb..a90a7f8 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs @@ -32,8 +32,6 @@ data IntData = IntData data ParsedIntPiece = PipString Text -- ^ Mere text. - | PipComments Text - -- ^ Comments. | PipNewline Text -- ^ Some line feed. -- This must be preferred over 'PipString'. diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs index 097390e..d4c51d5 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs @@ -17,7 +17,7 @@ import Fmt (Builder, build, fmt) import Text.Interpolation.Nyan.Core.Internal.Base import Text.Megaparsec (Parsec, customFailure, eof, errorBundlePretty, label, lookAhead, parse, single, takeWhile1P, takeWhileP) -import Text.Megaparsec.Char (string) +import Text.Megaparsec.Char.Lexer (skipLineComment) import Text.Megaparsec.Error (ShowErrorComponent (..)) newtype OptionChanged = OptionChanged Bool @@ -275,14 +275,11 @@ switchesHelpMessage sopts = , val /= defVal ] -intPieceP :: Ord e => Parsec e Text [ParsedIntPiece] -intPieceP = asum - [ +intPieceP :: Ord e => SwitchesOptions -> Parsec e Text [ParsedIntPiece] +intPieceP SwitchesOptions{..} = asum [ - -- consume comments - string "--" >>= \prefix -> do - content <- takeWhile1P Nothing (/= '\n') - pure $ one $ PipComments (prefix <> content) + -- ignore comments if 'commenting' switch is on + guard commenting *> skipLineComment "--" $> [] -- consume normal text , one . PipString <$> takeWhile1P Nothing (notAnyOf [(== '\\'), (== '#'), isSpace]) @@ -362,7 +359,7 @@ intStringP intStringP sopts = do switches <- switchesSectionP sopts _ <- single '|' - pieces <- glueParsedStrings . concat <$> many intPieceP + pieces <- glueParsedStrings . concat <$> many (intPieceP switches) eof return (switches, pieces) diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs index 6ad6c16..45d1b8b 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs @@ -15,7 +15,6 @@ import Text.Interpolation.Nyan.Core.Internal.Base processIntString :: SwitchesOptions -> ParsedInterpolatedString -> InterpolatedString processIntString sopts istr = istr & V.fromList - & do if commenting sopts then skipComments else id & do if leadingNewlineStripping sopts then stripLeadingNewline else id & do if trailingSpacesStripping sopts then stripTrailingLeadingWs else id & do if indentationStripping sopts then stripCommonIndentation else id @@ -31,8 +30,6 @@ processIntString sopts istr = istr where (&) = flip ($) - skipComments = V.filter \case PipComments{} -> False; _ -> True - stripLeadingNewline ps = case V.uncons ps of Just (PipNewline _, ps') -> ps' _ -> ps @@ -48,9 +45,6 @@ processIntString sopts istr = istr PipString s -> let s' = trimText s in if T.null s' then Nothing else Just (PipString s') - PipComments s -> - let s' = trimText s - in if T.null s' then Nothing else Just (PipString s') p@PipInt{} -> Just p trimLeftSpaces ps = case V.uncons ps of @@ -110,7 +104,6 @@ processIntString sopts istr = istr -- invisible spaces to break the newlines sequence. p@PipLeadingWs{} : l -> p : skipNext l p@PipString{} : l -> p : reduceNext l - p@PipComments{} : l -> p : reduceNext l p@PipInt{} : l -> p : reduceNext l [] -> [] @@ -120,7 +113,6 @@ processIntString sopts istr = istr PipNewline nl -> IpString nl PipLeadingWs n -> IpString . mconcat $ replicate (fromIntegral n) " " PipEmptyLine -> IpString mempty - PipComments s -> IpString s PipInt i -> IpInt i glueStrings :: InterpolatedString -> InterpolatedString From f2d05abfd18afcfd78296586c8606af71d4c2d9e Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Wed, 15 Jun 2022 19:01:26 +0600 Subject: [PATCH 4/8] fixup! [#6] Add a switch for allowing comments --- core/tests/Test/Customization.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/core/tests/Test/Customization.hs b/core/tests/Test/Customization.hs index 9ad99b0..8a1bd02 100644 --- a/core/tests/Test/Customization.hs +++ b/core/tests/Test/Customization.hs @@ -33,6 +33,7 @@ _AllFieldsAreExported = in basicDefaultSwitchesOptions { defIndentationStripping = Nothing , defSpacesTrimming = Nothing + , defCommenting = Nothing , defLeadingNewlineStripping = Nothing , defTrailingSpacesStripping = Nothing , defReducedNewlines = Nothing From 2e2697f31d60c70d72108b0e12cc20d78377c1ce Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Thu, 16 Jun 2022 13:35:21 +0600 Subject: [PATCH 5/8] fixup! [#6] Add a switch for allowing comments --- .../Text/Interpolation/Nyan/Core/Internal/Parser.hs | 5 +++-- core/tests/Test/Interpolator.hs | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs index d4c51d5..cbab063 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs @@ -17,7 +17,7 @@ import Fmt (Builder, build, fmt) import Text.Interpolation.Nyan.Core.Internal.Base import Text.Megaparsec (Parsec, customFailure, eof, errorBundlePretty, label, lookAhead, parse, single, takeWhile1P, takeWhileP) -import Text.Megaparsec.Char.Lexer (skipLineComment) +import Text.Megaparsec.Char.Lexer (skipBlockComment, skipLineComment) import Text.Megaparsec.Error (ShowErrorComponent (..)) newtype OptionChanged = OptionChanged Bool @@ -279,7 +279,8 @@ intPieceP :: Ord e => SwitchesOptions -> Parsec e Text [ParsedIntPiece] intPieceP SwitchesOptions{..} = asum [ -- ignore comments if 'commenting' switch is on - guard commenting *> skipLineComment "--" $> [] + guard commenting *> + asum [ skipLineComment "--" , skipBlockComment "{-" "-}" ] $> [] -- consume normal text , one . PipString <$> takeWhile1P Nothing (notAnyOf [(== '\\'), (== '#'), isSpace]) diff --git a/core/tests/Test/Interpolator.hs b/core/tests/Test/Interpolator.hs index b3aa01c..4651777 100644 --- a/core/tests/Test/Interpolator.hs +++ b/core/tests/Test/Interpolator.hs @@ -309,6 +309,17 @@ test_DefaultInterpolator = testGroup "Default interpolator" My text -- comments in the middle -- comments at the end |] @?= " \nMy text \n\n" + + , testCase "Inline block comments" do + [int|tc| My text {- inline comment -} + |] @?= " My text \n" + + , testCase "Multiline block comments" do + [int|tc| My text {- multiline block + comments are fun, + aren't they? + -} + |] @?= " My text \n" ] ] From 6f9f4ccb6c8f136a9768642a710ef1274fab5fd5 Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Mon, 27 Jun 2022 21:34:26 +0600 Subject: [PATCH 6/8] fixup! [#6] Add a switch for allowing comments --- .../Interpolation/Nyan/Core/Internal/Parser.hs | 4 ++-- core/tests/Test/Interpolator.hs | 7 +++++++ full/src/Text/Interpolation/Nyan/Tutorial.hs | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs index cbab063..bb3e198 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs @@ -53,7 +53,7 @@ toSwitchesOptionsBuilder DefaultSwitchesOptions{..} = finalizeSwitchesOptions :: MonadFail m => SwitchesOptionsBuilder -> m SwitchesOptions finalizeSwitchesOptions SwitchesOptionsBuilder{..} = do spacesTrimming <- fromOptional "spaces trimming" spacesTrimmingB - commenting <- fromOptional "allow commenting" commentingB + commenting <- fromOptional "comments handling" commentingB indentationStripping <- fromOptional "indentation stripping" indentationStrippingB leadingNewlineStripping <- fromOptional "leading newline stripping" leadingNewlineStrippingB trailingSpacesStripping <- fromOptional "trailing spaces stripping" trailingSpacesStrippingB @@ -80,7 +80,7 @@ setIfNew desc new (OptionChanged ch, old) setCommenting :: SwitchesOptionsSetter m => Bool -> m () setCommenting enable = do opts <- get - res <- setIfNew "allow comments" enable (commentingB opts) + res <- setIfNew "comments handling" enable (commentingB opts) put opts{ commentingB = res } setSpacesTrimming :: SwitchesOptionsSetter m => Bool -> m () diff --git a/core/tests/Test/Interpolator.hs b/core/tests/Test/Interpolator.hs index 4651777..27c9f0a 100644 --- a/core/tests/Test/Interpolator.hs +++ b/core/tests/Test/Interpolator.hs @@ -320,6 +320,13 @@ test_DefaultInterpolator = testGroup "Default interpolator" aren't they? -} |] @?= " My text \n" + + , testCase "Other switches keep the comments" do + [int|t| + The beginning + -- some comments in the middle + The end + |] @?= "The beginning\n-- some comments in the middle\nThe end\n" ] ] diff --git a/full/src/Text/Interpolation/Nyan/Tutorial.hs b/full/src/Text/Interpolation/Nyan/Tutorial.hs index 98be387..47817ae 100644 --- a/full/src/Text/Interpolation/Nyan/Tutorial.hs +++ b/full/src/Text/Interpolation/Nyan/Tutorial.hs @@ -223,6 +223,10 @@ The quoter will return concrete t'Builder'. The quoter will return any type with t'FromBuilder' instance. +==== c ([c]omments handling) + +Ignore line comments starting with @--@ and/or block comments enclosed in @{- ... -}@. + ==== ! (preview) Quoter will show as an error (non-blocking for the module's build) how the @@ -272,6 +276,16 @@ affects indentation stripping to also ignore the line with @|]@: :} "\nValue 1 is 5, value 2 is 10\n" +* Comments __are not ignored__ by other switches unless comments handling is explicitly turned on: + +>>> :{ + [int|t| + The beginning + -- some comments in the middle + The end + |] +:} +"The beginning\n-- some comments in the middle\nThe end\n" === Customizing the interpolator From 26aedc699ec341db320545ff1f51f6dda0cb93ff Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Wed, 29 Jun 2022 18:15:18 +0600 Subject: [PATCH 7/8] fixup! [#6] Add a switch for allowing comments --- core/tests/Test/Interpolator.hs | 17 +++++++---- full/src/Text/Interpolation/Nyan/Tutorial.hs | 30 +++++++++++++------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/core/tests/Test/Interpolator.hs b/core/tests/Test/Interpolator.hs index 27c9f0a..fc23e22 100644 --- a/core/tests/Test/Interpolator.hs +++ b/core/tests/Test/Interpolator.hs @@ -321,13 +321,20 @@ test_DefaultInterpolator = testGroup "Default interpolator" -} |] @?= " My text \n" - , testCase "Other switches keep the comments" do - [int|t| + , testCase "Line comments do not affect the indentation" do + [int|tc| The beginning - -- some comments in the middle + -- some comments in the middle The end - |] @?= "The beginning\n-- some comments in the middle\nThe end\n" - ] + |] @?= " The beginning\n\n The end\n" + + , testCase "Block comments do not affect the indentation" do + [int|tc| + The beginning {- some clarifying + comments in the middle -} + The end + |] @?= "The beginning \nThe end\n" + ] ] diff --git a/full/src/Text/Interpolation/Nyan/Tutorial.hs b/full/src/Text/Interpolation/Nyan/Tutorial.hs index 47817ae..5540f63 100644 --- a/full/src/Text/Interpolation/Nyan/Tutorial.hs +++ b/full/src/Text/Interpolation/Nyan/Tutorial.hs @@ -207,6 +207,20 @@ In case monadic actions have side effects, they will be applied in the same orde in which placeholders appear in the quoter. /But you are not going to use this behaviour, don't you?/ +==== c ([c]omments handling) + +Handle line comments starting with @--@ and/or block comments enclosed in @{- ... -}@. + +>>> :{ + [int|c|My text -- this is a line comment|] +:} +"My text " + +>>> :{ + [int|c|My {- this is a block comment -} text|] +:} +"My text" + ==== t (return [t]ext) The quoter will return concrete t'Text'. @@ -223,10 +237,6 @@ The quoter will return concrete t'Builder'. The quoter will return any type with t'FromBuilder' instance. -==== c ([c]omments handling) - -Ignore line comments starting with @--@ and/or block comments enclosed in @{- ... -}@. - ==== ! (preview) Quoter will show as an error (non-blocking for the module's build) how the @@ -276,16 +286,16 @@ affects indentation stripping to also ignore the line with @|]@: :} "\nValue 1 is 5, value 2 is 10\n" -* Comments __are not ignored__ by other switches unless comments handling is explicitly turned on: +* Comments remain invisible for other switches and thus do not affect them. +Consider the following indentation stripping example: >>> :{ - [int|t| + [int|c| The beginning - -- some comments in the middle - The end - |] +-- some comments in the middle + The end|] :} -"The beginning\n-- some comments in the middle\nThe end\n" +"The beginning\n\nThe end" === Customizing the interpolator From fc4c8d2f6fca59214bb27549582b8f99c06c7f76 Mon Sep 17 00:00:00 2001 From: Nurlan Alkuatov Date: Thu, 30 Jun 2022 17:41:16 +0600 Subject: [PATCH 8/8] fixup! fixup! [#6] Add a switch for allowing comments --- .../Text/Interpolation/Nyan/Core/Internal/Parser.hs | 10 ++++++++-- core/tests/Test/Interpolator.hs | 10 +++++----- full/src/Text/Interpolation/Nyan/Tutorial.hs | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs index bb3e198..c60c4af 100644 --- a/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs +++ b/core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs @@ -16,7 +16,8 @@ import qualified Data.Text as T import Fmt (Builder, build, fmt) import Text.Interpolation.Nyan.Core.Internal.Base import Text.Megaparsec (Parsec, customFailure, eof, errorBundlePretty, label, lookAhead, parse, - single, takeWhile1P, takeWhileP) + single, takeWhile1P, takeWhileP, try) +import Text.Megaparsec.Char (spaceChar) import Text.Megaparsec.Char.Lexer (skipBlockComment, skipLineComment) import Text.Megaparsec.Error (ShowErrorComponent (..)) @@ -280,7 +281,7 @@ intPieceP SwitchesOptions{..} = asum [ -- ignore comments if 'commenting' switch is on guard commenting *> - asum [ skipLineComment "--" , skipBlockComment "{-" "-}" ] $> [] + asum [ skipLineComment "--", skipBlockComment' ] $> [] -- consume normal text , one . PipString <$> takeWhile1P Nothing (notAnyOf [(== '\\'), (== '#'), isSpace]) @@ -329,6 +330,11 @@ intPieceP SwitchesOptions{..} = asum [ ] where + skipBlockComment' = asum + [ skipBlockComment "{-" "-}" + , try $ spaceChar *> skipBlockComment "{-" "-}" + ] + newline = PipNewline . mconcat <$> sequence [ maybe "" T.singleton <$> optional (single '\r') , T.singleton <$> single '\n' diff --git a/core/tests/Test/Interpolator.hs b/core/tests/Test/Interpolator.hs index fc23e22..1f89ea8 100644 --- a/core/tests/Test/Interpolator.hs +++ b/core/tests/Test/Interpolator.hs @@ -311,15 +311,15 @@ test_DefaultInterpolator = testGroup "Default interpolator" |] @?= " \nMy text \n\n" , testCase "Inline block comments" do - [int|tc| My text {- inline comment -} - |] @?= " My text \n" + [int|tc| My {- inline comment -} text + |] @?= " My text\n" , testCase "Multiline block comments" do [int|tc| My text {- multiline block comments are fun, aren't they? -} - |] @?= " My text \n" + |] @?= " My text\n" , testCase "Line comments do not affect the indentation" do [int|tc| @@ -331,9 +331,9 @@ test_DefaultInterpolator = testGroup "Default interpolator" , testCase "Block comments do not affect the indentation" do [int|tc| The beginning {- some clarifying - comments in the middle -} + comments in the middle -} The end - |] @?= "The beginning \nThe end\n" + |] @?= "The beginning\nThe end\n" ] ] diff --git a/full/src/Text/Interpolation/Nyan/Tutorial.hs b/full/src/Text/Interpolation/Nyan/Tutorial.hs index 5540f63..c775ce6 100644 --- a/full/src/Text/Interpolation/Nyan/Tutorial.hs +++ b/full/src/Text/Interpolation/Nyan/Tutorial.hs @@ -219,7 +219,7 @@ Handle line comments starting with @--@ and/or block comments enclosed in @{- .. >>> :{ [int|c|My {- this is a block comment -} text|] :} -"My text" +"My text" ==== t (return [t]ext)