Skip to content

Commit

Permalink
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 15, 2022
1 parent 9958b29 commit 3e2d1e1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
2 changes: 0 additions & 2 deletions core/src/Text/Interpolation/Nyan/Core/Internal/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down
15 changes: 6 additions & 9 deletions core/src/Text/Interpolation/Nyan/Core/Internal/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 0 additions & 8 deletions core/src/Text/Interpolation/Nyan/Core/Internal/Processor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
[] -> []

Expand All @@ -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
Expand Down

0 comments on commit 3e2d1e1

Please sign in to comment.