[go-nuts] Re: golang.org/x/tools/go/loader: file names of parsed *ast.File

2016-08-15 Thread adonovan via golang-nuts
On Saturday, 13 August 2016 22:27:42 UTC+1, Paul Jolly wrote:
>
> On Saturday, 13 August 2016 21:58:37 UTC+1, Paul Jolly wrote:
>>
>> Am I missing something obvious here?
>>
>
> Please excuse the reply to self, but I have discovered what I was missing.
>
> For the sake of completeness, here is how the equivalent mapping is 
> achieved in golang.org/x/tools/go/loader world:
>
> import (
> "go/ast"
> "go/parser"
> "golang.org/x/tools/go/loader"
> )
>
>
> conf := loader.Config{
> ParserMode: parser.AllErrors | parser.ParseComments,
> Cwd:path,
> }
>
> ...
>
> conf.CreateFromFilenames(path, toParse...)
>
> prog, err := conf.Load()
> if err != nil {
> panic(err)
> }
>
> At this point conf.CreatePkgs (type []loader.PkgSpec) and prog.Created 
> (type []*loader.PackageInfo) correspond to each other.
>
> For each (loader.PkgSpec, *loader.PackageInfo) pairing, say pkgSpec and 
> pkgInfo, then pkgSpec.Filenames (type []string) and pkgInfo (type 
> []*ast.File) again correspond, which gives us the required mapping.
>

You can obtain the name of the file from which the *ast.File f was parsed 
using the following expression: prog.Fset.Position(f.Pos()).Filename

cheers
alan

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: golang.org/x/tools/go/loader: file names of parsed *ast.File

2016-08-13 Thread Paul Jolly
On Saturday, 13 August 2016 21:58:37 UTC+1, Paul Jolly wrote:
>
> Am I missing something obvious here?
>

Please excuse the reply to self, but I have discovered what I was missing.

For the sake of completeness, here is how the equivalent mapping is 
achieved in golang.org/x/tools/go/loader world:

import (
"go/ast"
"go/parser"
"golang.org/x/tools/go/loader"
)


conf := loader.Config{
ParserMode: parser.AllErrors | parser.ParseComments,
Cwd:path,
}

...

conf.CreateFromFilenames(path, toParse...)

prog, err := conf.Load()
if err != nil {
panic(err)
}

At this point conf.CreatePkgs (type []loader.PkgSpec) and prog.Created 
(type []*loader.PackageInfo) correspond to each other.

For each (loader.PkgSpec, *loader.PackageInfo) pairing, say pkgSpec and 
pkgInfo, then pkgSpec.Filenames (type []string) and pkgInfo (type 
[]*ast.File) again correspond, which gives us the required mapping.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.