Skip to content

Commit

Permalink
Revert "Fully preserve indented strings and match Nix parser"
Browse files Browse the repository at this point in the history
This reverts commit ea74366.
  • Loading branch information
infinisil committed Jul 9, 2024
1 parent ba9cd57 commit dc18b5c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 61 deletions.
55 changes: 11 additions & 44 deletions src/Nixfmt/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -193,50 +193,17 @@ indentedStringPart :: Parser StringPart
indentedStringPart =
TextPart
<$> someText
( {-
This should match https://github.com/NixOS/nix/blob/052f1320ddf72d617e337479ff1bf22cb4ee682a/src/libexpr/lexer.l#L182-L205
To understand that file, [this](https://westes.github.io/flex/manual/Matching.html#Matching) is important:
> If it finds more than one match, it takes the one matching the most text.
> If it finds two or more matches of the same length, the rule listed first in the flex input file is chosen.
There are some inconsequential differences though:
- We only parse one line at a time here, so we make sure to not process any newlines
- We don't want to transform the input in any way, e.g. don't turn ''' into ''
This is to preserve the input string as-is
-}

-- <IND_STRING>([^\$\']|\$[^\{\']|\'[^\'\$])+
-- While this rule doesn't have a fixed length, it's non-conflicting with the rules below,
-- so we can do it first without worrying about the length matching
someText
( Text.singleton
<$> satisfy (\t -> t /= '$' && t /= '\'' && t /= '\n')
<|> try (Text.snoc <$> chunk "$" <*> satisfy (\t -> t /= '{' && t /= '\'' && t /= '\n'))
<|> try (Text.snoc <$> chunk "'" <*> satisfy (\t -> t /= '\'' && t /= '$' && t /= '\n'))
)
-- These rules are of length 3, they need to come before shorter ones
-- <IND_STRING>\'\'\$
<|> chunk "''$"
-- <IND_STRING>\'\'\'
<|> chunk "'''"
-- <IND_STRING>\'\'\\{ANY} -> Note that ANY can match newlines as well, but we need to ignore those
<|> do
prefix <- chunk "''\\"
-- If we do have a newline
rest <-
-- If there's no newline, take the next character
(notFollowedBy (char '\n') *> (Text.singleton <$> anySingle))
-- Otherwise there's an unconsumed newline, which we don't need to handle,
-- it's consumed elsewhere
<|> pure ""
pure $ prefix <> rest
-- These are rules with length 2 and 1
-- <IND_STRING>\$\{ -> don't match, this will be an interpolation
-- <IND_STRING>\$ -> do match, just a dollar
<|> try (chunk "$" <* notFollowedBy (char '{'))
-- <IND_STRING>\'\' -> don't match, indented string ends
-- <IND_STRING>\' -> do match, just a quote
<|> try (chunk "'" <* notFollowedBy (char '\''))
( chunk "''\\n"
<|> chunk "''\\r"
<|> chunk "''\\t"
<|> chunk "''\\"
*> (Text.singleton <$> anySingle)
<|> chunk "''$"
<|> chunk "'''"
<|> chunk "$$"
<|> try (chunk "$" <* notFollowedBy (char '{'))
<|> try (chunk "'" <* notFollowedBy (char '\''))
<|> someP (\t -> t /= '\'' && t /= '$' && t /= '\n')
)

indentedLine :: Parser [StringPart]
Expand Down
14 changes: 0 additions & 14 deletions test/correct/indented-string.nix

This file was deleted.

3 changes: 0 additions & 3 deletions test/correct/regression-197.nix

This file was deleted.

0 comments on commit dc18b5c

Please sign in to comment.