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

Fix source locations #936

Merged
merged 1 commit into from
May 22, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/Nix/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,10 @@ annotateLocation :: Parser a -> Parser (Ann SrcSpan a)
annotateLocation p =
do
begin <- getSourcePos
res <- p
end <- get -- The state set before the last whitespace

Ann (SrcSpan begin end) <$> p
pure $ Ann (SrcSpan begin end) res

annotateLocation1 :: Parser (NExprF NExprLoc) -> Parser NExprLoc
annotateLocation1 = fmap annToAnnF . annotateLocation
Expand Down
48 changes: 48 additions & 0 deletions tests/ParserTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,42 @@ in null|] [text|let
in (matcher.case or null).foo (v.case);
in null|]

case_simpleLoc =
let
mkSPos l c = SourcePos "<string>" (mkPos l) (mkPos c)
mkSpan l1 c1 l2 c2 = SrcSpan (mkSPos l1 c1) (mkSPos l2 c2)
in
assertParseTextLoc [text|let
foo = bar
baz "qux";
in foo
|]
(Fix
(NLet_
(mkSpan 1 1 4 7)
[ NamedVar
(StaticKey "foo" :| [])
(Fix
(NBinary_
(mkSpan 2 7 3 15)
NApp
(Fix
(NBinary_ (mkSpan 2 7 3 9)
NApp
(Fix (NSym_ (mkSpan 2 7 2 10) "bar"))
(Fix (NSym_ (mkSpan 3 6 3 9) "baz"))
)
)
(Fix (NStr_ (mkSpan 3 10 3 15) (DoubleQuoted [Plain "qux"])))
)
)
(mkSPos 2 1)
]
(Fix (NSym_ (mkSpan 4 4 4 7) "foo"))
)
)


tests :: TestTree
tests = $testGroupGenerator

Expand All @@ -375,6 +411,18 @@ assertParseText str expected =
)
(parseNixText str)

assertParseTextLoc :: Text -> NExprLoc -> Assertion
assertParseTextLoc str expected =
either
(\ err ->
assertFailure $ toString $ "Unexpected fail parsing `" <> str <> "':\n" <> show err
)
(assertEqual
("When parsing " <> toString str)
expected
)
(parseNixTextLoc str)

assertParseFile :: FilePath -> NExpr -> Assertion
assertParseFile file expected =
do
Expand Down