Skip to content

Commit a05a7c4

Browse files
committed
Parser: nixString': refactor
1 parent f9b93a1 commit a05a7c4

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

src/Nix/Parser.hs

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ exprAfterReservedWord word = exprAfterP $ reserved word
150150
-- Overall, parser should simply @\r\n -> \n@.
151151
skipLineComment' :: Tokens Text -> Parser ()
152152
skipLineComment' prefix =
153-
chunk prefix *> void (takeWhileP (pure "character") (\x -> x /= '\n' && x /= '\r'))
153+
chunk prefix *> void (takeWhileP (pure "character") $ \x -> x /= '\n' && x /= '\r')
154154

155155
whiteSpace :: Parser ()
156156
whiteSpace =
@@ -273,57 +273,68 @@ nixAntiquoted p =
273273
<|> Plain <$> p
274274

275275
nixString' :: Parser (NString NExprLoc)
276-
nixString' = lexeme $ label "string" $ doubleQuoted <|> indented
276+
nixString' = label "string" $ lexeme $ doubleQuoted <|> indented
277277
where
278278
doubleQuoted :: Parser (NString NExprLoc)
279279
doubleQuoted =
280280
label "double quoted string" $
281281
DoubleQuoted . removeEmptyPlains . mergePlain <$>
282-
( doubleQ *>
283-
many (stringChar doubleQ (void $ char '\\') doubleEscape)
284-
<* doubleQ
285-
)
282+
inQuotationMarks (many $ stringChar quotationMark (void $ char '\\') doubleEscape)
283+
where
284+
inQuotationMarks :: Parser a -> Parser a
285+
inQuotationMarks expr = quotationMark *> expr <* quotationMark
286+
287+
quotationMark :: Parser ()
288+
quotationMark = void $ char '"'
286289

287-
doubleQ = void $ char '"'
288-
doubleEscape = Plain . one <$> (char '\\' *> escapeCode)
290+
doubleEscape :: Parser (Antiquoted Text r)
291+
doubleEscape = Plain . one <$> (char '\\' *> escapeCode)
289292

290293
indented :: Parser (NString NExprLoc)
291294
indented =
292295
label "indented string" $
293296
stripIndent <$>
294-
( indentedQ *>
295-
many (stringChar indentedQ indentedQ indentedEscape)
296-
<* indentedQ
297-
)
298-
299-
indentedQ :: Parser ()
300-
indentedQ = void . label "\"''\"" $ chunk "''"
301-
302-
indentedEscape =
303-
try $
304-
do
305-
indentedQ
306-
(Plain <$> ("''" <$ char '\'' <|> "$" <$ char '$'))
307-
<|>
308-
do
309-
_ <- char '\\'
310-
c <- escapeCode
311-
312-
pure $
313-
bool
314-
EscapedNewline
315-
(Plain $ one c)
316-
(c /= '\n')
317-
297+
inIndentedQuotation (many $ stringChar indentedQuotationMark indentedQuotationMark indentedEscape)
298+
where
299+
indentedEscape :: Parser (Antiquoted Text r)
300+
indentedEscape =
301+
try $
302+
do
303+
indentedQuotationMark
304+
(Plain <$> ("''" <$ char '\'' <|> "$" <$ char '$'))
305+
<|>
306+
do
307+
_ <- char '\\'
308+
c <- escapeCode
309+
310+
pure $
311+
bool
312+
EscapedNewline
313+
(Plain $ one c)
314+
(c /= '\n')
315+
316+
inIndentedQuotation :: Parser a -> Parser a
317+
inIndentedQuotation expr = indentedQuotationMark *> expr <* indentedQuotationMark
318+
319+
indentedQuotationMark :: Parser ()
320+
indentedQuotationMark = label "\"''\"" . void $ chunk "''"
321+
322+
stringChar
323+
:: Parser ()
324+
-> Parser ()
325+
-> Parser (Antiquoted Text NExprLoc)
326+
-> Parser (Antiquoted Text NExprLoc)
318327
stringChar end escStart esc =
319328
antiquoted
320329
<|> Plain . one <$> char '$'
321330
<|> esc
322331
<|> Plain . toText <$> some plainChar
323332
where
333+
plainChar :: Parser Char
324334
plainChar =
325335
notFollowedBy (end <|> void (char '$') <|> escStart) *> anySingle
326336

337+
escapeCode :: Parser Char
327338
escapeCode =
328339
msum
329340
[ c <$ char e | (c, e) <- escapeCodes ]
@@ -737,8 +748,8 @@ nixIf =
737748
annotateNamedLocation "if" $
738749
liftA3 NIf
739750
(reserved "if" *> nixExprAlgebra)
740-
(exprAfterReservedWord "then" )
741-
(exprAfterReservedWord "else" )
751+
(exprAfterReservedWord "then")
752+
(exprAfterReservedWord "else")
742753

743754
-- ** with
744755

0 commit comments

Comments
 (0)