Skip to content

Commit 9a55e7a

Browse files
authored
(#936) Parser: annotateLocation: fix source locations
Add small test for source locations Relates to: #743 & #744
1 parent 7fda56f commit 9a55e7a

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Nix/Parser.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,10 @@ annotateLocation :: Parser a -> Parser (Ann SrcSpan a)
609609
annotateLocation p =
610610
do
611611
begin <- getSourcePos
612+
res <- p
612613
end <- get -- The state set before the last whitespace
613614

614-
Ann (SrcSpan begin end) <$> p
615+
pure $ Ann (SrcSpan begin end) res
615616

616617
annotateLocation1 :: Parser (NExprF NExprLoc) -> Parser NExprLoc
617618
annotateLocation1 = fmap annToAnnF . annotateLocation

tests/ParserTests.hs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,42 @@ in null|] [text|let
357357
in (matcher.case or null).foo (v.case);
358358
in null|]
359359

360+
case_simpleLoc =
361+
let
362+
mkSPos l c = SourcePos "<string>" (mkPos l) (mkPos c)
363+
mkSpan l1 c1 l2 c2 = SrcSpan (mkSPos l1 c1) (mkSPos l2 c2)
364+
in
365+
assertParseTextLoc [text|let
366+
foo = bar
367+
baz "qux";
368+
in foo
369+
|]
370+
(Fix
371+
(NLet_
372+
(mkSpan 1 1 4 7)
373+
[ NamedVar
374+
(StaticKey "foo" :| [])
375+
(Fix
376+
(NBinary_
377+
(mkSpan 2 7 3 15)
378+
NApp
379+
(Fix
380+
(NBinary_ (mkSpan 2 7 3 9)
381+
NApp
382+
(Fix (NSym_ (mkSpan 2 7 2 10) "bar"))
383+
(Fix (NSym_ (mkSpan 3 6 3 9) "baz"))
384+
)
385+
)
386+
(Fix (NStr_ (mkSpan 3 10 3 15) (DoubleQuoted [Plain "qux"])))
387+
)
388+
)
389+
(mkSPos 2 1)
390+
]
391+
(Fix (NSym_ (mkSpan 4 4 4 7) "foo"))
392+
)
393+
)
394+
395+
360396
tests :: TestTree
361397
tests = $testGroupGenerator
362398

@@ -375,6 +411,18 @@ assertParseText str expected =
375411
)
376412
(parseNixText str)
377413

414+
assertParseTextLoc :: Text -> NExprLoc -> Assertion
415+
assertParseTextLoc str expected =
416+
either
417+
(\ err ->
418+
assertFailure $ toString $ "Unexpected fail parsing `" <> str <> "':\n" <> show err
419+
)
420+
(assertEqual
421+
("When parsing " <> toString str)
422+
expected
423+
)
424+
(parseNixTextLoc str)
425+
378426
assertParseFile :: FilePath -> NExpr -> Assertion
379427
assertParseFile file expected =
380428
do

0 commit comments

Comments
 (0)