Skip to content

Commit 6b9d4d1

Browse files
expipiplus1Anton-Latukha
authored andcommitted
Restrict type of parens and brackets to prevent negative use of NExprLoc
it is not generally appropriate to have higher-order parsers operate on annotated locations as they are liable to perform changes to the parser which are not captured in the annotation. See #739 and #744 for examples.
1 parent dba7650 commit 6b9d4d1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/Nix/Parser.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ nixNull = annotateLocation1 (mkNullF <$ reserved "null" <?> "null")
200200
-- however this position doesn't include the parsed parentheses, so remove the
201201
-- "inner" location annotateion and annotate again, including the parentheses.
202202
nixParens :: Parser NExprLoc
203-
nixParens = annotateLocation1 (stripAnn . unFix <$> (parens nixToplevelForm <?> "parens"))
203+
nixParens = annotateLocation1 (parens (stripAnn . unFix <$> nixToplevelForm) <?> "parens")
204204

205205
nixList :: Parser NExprLoc
206206
nixList = annotateLocation1 (brackets (NList <$> many nixTerm) <?> "list")
@@ -411,7 +411,7 @@ nixBinders = (inherit <+> namedVar) `endBy` semi where
411411
<*> (equals *> nixToplevelForm)
412412
<*> pure p
413413
<?> "variable binding"
414-
scope = parens nixToplevelForm <?> "inherit scope"
414+
scope = nixParens <?> "inherit scope"
415415

416416
keyName :: Parser (NKeyName NExprLoc)
417417
keyName = dynamicKey <+> staticKey where
@@ -496,6 +496,13 @@ identifier = lexeme $ try $ do
496496
where
497497
identLetter x = isAlpha x || isDigit x || x == '_' || x == '\'' || x == '-'
498498

499+
-- We restrict the type of 'parens' and 'brackets' here because if they were to
500+
-- take a @Parser NExprLoc@ argument they would parse additional text which
501+
-- wouldn't be captured in the source location annotation.
502+
--
503+
-- Braces and angles in hnix don't enclose a single expression so this type
504+
-- restriction would not be useful.
505+
parens, brackets :: Parser (NExprF f) -> Parser (NExprF f)
499506
parens = between (symbol "(") (symbol ")")
500507
braces = between (symbol "{") (symbol "}")
501508
-- angles = between (symbol "<") (symbol ">")

0 commit comments

Comments
 (0)