Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct location annotation for parenthesized expressions #739

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Nix/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import Data.Char ( isAlpha
, isSpace
)
import Data.Data ( Data(..) )
import Data.Fix ( Fix(..) )
import Data.Functor
import Data.Functor.Identity
import Data.HashSet ( HashSet )
Expand Down Expand Up @@ -195,8 +196,11 @@ nixBool =
nixNull :: Parser NExprLoc
nixNull = annotateLocation1 (mkNullF <$ reserved "null" <?> "null")

-- | 'nixTopLevelForm' returns an expression annotated with a source position,
-- however this position doesn't include the parsed parentheses, so remove the
-- "inner" location annotateion and annotate again, including the parentheses.
nixParens :: Parser NExprLoc
nixParens = parens nixToplevelForm <?> "parens"
nixParens = annotateLocation1 (parens (stripAnn . unFix <$> nixToplevelForm) <?> "parens")

nixList :: Parser NExprLoc
nixList = annotateLocation1 (brackets (NList <$> many nixTerm) <?> "list")
Expand Down Expand Up @@ -407,7 +411,7 @@ nixBinders = (inherit <+> namedVar) `endBy` semi where
<*> (equals *> nixToplevelForm)
<*> pure p
<?> "variable binding"
scope = parens nixToplevelForm <?> "inherit scope"
scope = nixParens <?> "inherit scope"

keyName :: Parser (NKeyName NExprLoc)
keyName = dynamicKey <+> staticKey where
Expand Down Expand Up @@ -492,6 +496,13 @@ identifier = lexeme $ try $ do
where
identLetter x = isAlpha x || isDigit x || x == '_' || x == '\'' || x == '-'

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