ghc-exactprint icon indicating copy to clipboard operation
ghc-exactprint copied to clipboard

Weird annotations for comments

Open mrkkrp opened this issue 5 years ago • 1 comments

If we parse this input:

import qualified MegaModule as M
  ( (>>>) -- (1)
  , (<<<) -- (2)
  , Either -- (3)
  )

-- (1) appears in annotations for (>>>), -- (2) appears in annotations for (<<<), but -- (3) is attached to some weird thingy:

[(AnnKey Foo.hs:1:1 CN "HsModule",
  (Ann (DP (0,0)) [] [] [((G AnnEofPos),DP (1,0))] Nothing Nothing)),
 (AnnKey Foo.hs:(1,1)-(5,3) CN "ImportDecl",
  (Ann (DP (0,0)) [] [] [((G AnnImport),DP (0,0)),((G AnnQualified),DP (0,1)),((G AnnAs),DP (0,1))] Nothing Nothing)),
 (AnnKey Foo.hs:1:18-27 CN "{abstract:ModuleName}",
  (Ann (DP (0,1)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing)),
 (AnnKey Foo.hs:1:32 CN "{abstract:ModuleName}",
  (Ann (DP (0,1)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing)),
 (AnnKey Foo.hs:(2,3)-(5,3) CN "(:)",
  (Ann (DP (1,2)) [] [] [((G AnnOpenP),DP (0,0)),((AnnComment (Comment "-- (3)" Foo.hs:4:12-17 Nothing)),DP (0,1)),((G AnnCloseP),DP (1,2))] Nothing Nothing)),
 (AnnKey Foo.hs:2:5-9 CN "IEName",
  (Ann (DP (0,0)) [] [] [] Nothing Nothing)),
 (AnnKey Foo.hs:2:5-9 CN "IEVar",
  (Ann (DP (0,1)) [] [] [((AnnComment (Comment "-- (1)" Foo.hs:2:11-16 Nothing)),DP (0,1)),((G AnnComma),DP (1,2))] Nothing Nothing)),
 (AnnKey Foo.hs:2:5-9 CN "Unqual",
  (Ann (DP (0,0)) [] [] [((G AnnOpenP),DP (0,0)),((G AnnVal),DP (0,0)),((G AnnCloseP),DP (0,0))] Nothing Nothing)),
 (AnnKey Foo.hs:3:5-9 CN "IEName",
  (Ann (DP (0,0)) [] [] [] Nothing Nothing)),
 (AnnKey Foo.hs:3:5-9 CN "IEVar",
  (Ann (DP (0,1)) [] [] [((AnnComment (Comment "-- (2)" Foo.hs:3:11-16 Nothing)),DP (0,1)),((G AnnComma),DP (1,2))] Nothing Nothing)),
 (AnnKey Foo.hs:3:5-9 CN "Unqual",
  (Ann (DP (0,0)) [] [] [((G AnnOpenP),DP (0,0)),((G AnnVal),DP (0,0)),((G AnnCloseP),DP (0,0))] Nothing Nothing)),
 (AnnKey Foo.hs:4:5-10 CN "IEName",
  (Ann (DP (0,0)) [] [] [] Nothing Nothing)),
 (AnnKey Foo.hs:4:5-10 CN "IEThingAbs",
  (Ann (DP (0,1)) [] [] [] Nothing Nothing)),
 (AnnKey Foo.hs:4:5-10 CN "Unqual",
  (Ann (DP (0,0)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing))]

What on the earth is this "(:)" and why the last comment is treated specially?

mrkkrp avatar Mar 04 '19 20:03 mrkkrp

Using brittany --dump-ast-full explains the "(:)", as it yields

---- ast ----
A Just (Ann (DP (0,0)) [] [] [((G AnnEofPos),DP (1,0))] Nothing Nothing)
  HsModule
    Nothing
    Nothing
    [ A Just (Ann (DP (0,0)) [] [] [((G AnnImport),DP (0,0)),((G AnnQualified),DP (0,1)),((G AnnAs),DP (0,1))] Nothing Nothing)
        ImportDecl
          NoExt
          NoSourceText
          A Just (Ann (DP (0,1)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing)
            {abstract:ModuleName}
          Nothing
          False
          False
          True
          False
          Just
            A Just (Ann (DP (0,1)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing)
              {abstract:ModuleName}
          Just
            (,)
              False
              A Just (Ann (DP (1,2)) [] [] [((G AnnOpenP),DP (0,0)),((AnnComment (Comment "-- (3)" stdin:4:12-17 Nothing)),DP (0,1)),((G AnnCloseP),DP (1,2))] Nothing Nothing)
                [ A Just (Ann (DP (0,1)) [] [] [((AnnComment (Comment "-- (1)" stdin:2:11-16 Nothing)),DP (0,1)),((G AnnComma),DP (1,2))] Nothing Nothing)
                    IEVar
                      NoExt
                      A Just (Ann (DP (0,0)) [] [] [] Nothing Nothing)
                        IEName
                          A Just (Ann (DP (0,0)) [] [] [((G AnnOpenP),DP (0,0)),((G AnnVal),DP (0,0)),((G AnnCloseP),DP (0,0))] Nothing Nothing)
                            Unqual {OccName: >>>}
                , A Just (Ann (DP (0,1)) [] [] [((AnnComment (Comment "-- (2)" stdin:3:11-16 Nothing)),DP (0,1)),((G AnnComma),DP (1,2))] Nothing Nothing)
                    IEVar
                      NoExt
                      A Just (Ann (DP (0,0)) [] [] [] Nothing Nothing)
                        IEName
                          A Just (Ann (DP (0,0)) [] [] [((G AnnOpenP),DP (0,0)),((G AnnVal),DP (0,0)),((G AnnCloseP),DP (0,0))] Nothing Nothing)
                            Unqual {OccName: <<<}
                , A Just (Ann (DP (0,1)) [] [] [] Nothing Nothing)
                    IEThingAbs
                      NoExt
                      A Just (Ann (DP (0,0)) [] [] [] Nothing Nothing)
                        IEName
                          A Just (Ann (DP (0,0)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing)
                            Unqual {OccName: Either}
                ]
    ]
    []
    Nothing
    Nothing
----

I.e. it is connected to the list of import items. Quite similar to #77 where the last comment is connected to the surrounding construct instead of the last contained item.

lspitzner avatar Jan 03 '20 10:01 lspitzner