Skip to content

Commit

Permalink
fixup! fixup! [#6] Add a switch for allowing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nalkuatov committed Jun 30, 2022
1 parent 26aedc6 commit fc4c8d2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
10 changes: 8 additions & 2 deletions core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..))

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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'
Expand Down
10 changes: 5 additions & 5 deletions core/tests/Test/Interpolator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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"
]

]
Expand Down
2 changes: 1 addition & 1 deletion full/src/Text/Interpolation/Nyan/Tutorial.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fc4c8d2

Please sign in to comment.