Skip to content

Commit 9a1614f

Browse files
committed
fixup! [#6] Add a switch for allowing comments
1 parent 9958b29 commit 9a1614f

File tree

3 files changed

+6
-19
lines changed

3 files changed

+6
-19
lines changed

core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ data IntData = IntData
3232
data ParsedIntPiece
3333
= PipString Text
3434
-- ^ Mere text.
35-
| PipComments Text
36-
-- ^ Comments.
3735
| PipNewline Text
3836
-- ^ Some line feed.
3937
-- This must be preferred over 'PipString'.

core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs

+6-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Fmt (Builder, build, fmt)
1717
import Text.Interpolation.Nyan.Core.Internal.Base
1818
import Text.Megaparsec (Parsec, customFailure, eof, errorBundlePretty, label, lookAhead, parse,
1919
single, takeWhile1P, takeWhileP)
20-
import Text.Megaparsec.Char (string)
20+
import Text.Megaparsec.Char.Lexer (skipLineComment)
2121
import Text.Megaparsec.Error (ShowErrorComponent (..))
2222

2323
newtype OptionChanged = OptionChanged Bool
@@ -275,14 +275,11 @@ switchesHelpMessage sopts =
275275
, val /= defVal
276276
]
277277

278-
intPieceP :: Ord e => Parsec e Text [ParsedIntPiece]
279-
intPieceP = asum
280-
[
278+
intPieceP :: Ord e => SwitchesOptions -> Parsec e Text [ParsedIntPiece]
279+
intPieceP SwitchesOptions{..} = asum [
281280

282-
-- consume comments
283-
string "--" >>= \prefix -> do
284-
content <- takeWhile1P Nothing (/= '\n')
285-
pure $ one $ PipComments (prefix <> content)
281+
-- ignore comments if 'commenting' switch is on
282+
guard commenting *> skipLineComment "--" $> []
286283

287284
-- consume normal text
288285
, one . PipString <$> takeWhile1P Nothing (notAnyOf [(== '\\'), (== '#'), isSpace])
@@ -362,7 +359,7 @@ intStringP
362359
intStringP sopts = do
363360
switches <- switchesSectionP sopts
364361
_ <- single '|'
365-
pieces <- glueParsedStrings . concat <$> many intPieceP
362+
pieces <- glueParsedStrings . concat <$> many (intPieceP switches)
366363
eof
367364
return (switches, pieces)
368365

core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs

-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Text.Interpolation.Nyan.Core.Internal.Base
1515
processIntString :: SwitchesOptions -> ParsedInterpolatedString -> InterpolatedString
1616
processIntString sopts istr = istr
1717
& V.fromList
18-
& do if commenting sopts then skipComments else id
1918
& do if leadingNewlineStripping sopts then stripLeadingNewline else id
2019
& do if trailingSpacesStripping sopts then stripTrailingLeadingWs else id
2120
& do if indentationStripping sopts then stripCommonIndentation else id
@@ -31,8 +30,6 @@ processIntString sopts istr = istr
3130
where
3231
(&) = flip ($)
3332

34-
skipComments = V.filter \case PipComments{} -> False; _ -> True
35-
3633
stripLeadingNewline ps = case V.uncons ps of
3734
Just (PipNewline _, ps') -> ps'
3835
_ -> ps
@@ -48,9 +45,6 @@ processIntString sopts istr = istr
4845
PipString s ->
4946
let s' = trimText s
5047
in if T.null s' then Nothing else Just (PipString s')
51-
PipComments s ->
52-
let s' = trimText s
53-
in if T.null s' then Nothing else Just (PipString s')
5448
p@PipInt{} -> Just p
5549

5650
trimLeftSpaces ps = case V.uncons ps of
@@ -110,7 +104,6 @@ processIntString sopts istr = istr
110104
-- invisible spaces to break the newlines sequence.
111105
p@PipLeadingWs{} : l -> p : skipNext l
112106
p@PipString{} : l -> p : reduceNext l
113-
p@PipComments{} : l -> p : reduceNext l
114107
p@PipInt{} : l -> p : reduceNext l
115108
[] -> []
116109

@@ -120,7 +113,6 @@ processIntString sopts istr = istr
120113
PipNewline nl -> IpString nl
121114
PipLeadingWs n -> IpString . mconcat $ replicate (fromIntegral n) " "
122115
PipEmptyLine -> IpString mempty
123-
PipComments s -> IpString s
124116
PipInt i -> IpInt i
125117

126118
glueStrings :: InterpolatedString -> InterpolatedString

0 commit comments

Comments
 (0)