FAQ
eg. collect structs and functions from a file and put them in maps where
the key is their name

after reading go/parser, this should be straight forward. here is what i
have now:
https://gist.github.com/91207c4e461de312391a

I detailed where and why I'm stuck. Really appreciate any help with this.
Thanks.



ps. this is a great community. im part of of scala and clojure etc and no
group comes close to this. pretty sad even though it's 2012...sigh.

--

Search Discussions

  • Aaron Lifton at Nov 2, 2012 at 4:33 pm
    so parseFile returns the following:

    return &ast.File{ 2340 Doc: doc, 2341 Package: pos, 2342 Name: ident, 2343 Decls: decls, 2344 Scope: p.pkgScope, 2345 Imports: p.imports, 2346 Unresolved: p.unresolved[0:i], 2347 Comments: p.comments, 2348 }

    I need to use ast.File.Doc, i'm assuming, to gather types, only structs and
    functions, into simple maps. I have no idea how to do that, I've been
    trying since last week. I'm trying to implement this in the most minimal
    way possible and all the examples i've seen, such as gocode, do not help
    me, as they try to handle all possible types

    On Friday, November 2, 2012 12:23:44 PM UTC-4, Aaron Lifton wrote:

    eg. collect structs and functions from a file and put them in maps where
    the key is their name

    after reading go/parser, this should be straight forward. here is what i
    have now:
    https://gist.github.com/91207c4e461de312391a

    I detailed where and why I'm stuck. Really appreciate any help with this.
    Thanks.



    ps. this is a great community. im part of of scala and clojure etc and no
    group comes close to this. pretty sad even though it's 2012...sigh.
    --
  • Jimmy frasche at Nov 2, 2012 at 5:13 pm
    (I know you posted code that isn't meant to compile but it would help
    if the only reason it didn't compile were the part you specifically
    flagged as wrong)

    For one thing you're telling the parser to only parse the imports of
    the file. Use mode 0 if you don't need to do anything else special
    (you'll want to use ast.ParseComments if you need the docs for
    anything)

    Then everything you want will be in f.Decls. You can loop over it with
    a typeswitch in the loop testing for ast.FuncDecl or an ast.GenDecl
    whose .Tok field == token.TYPE and then drill down to see if that's a
    struct.

    --
  • Archos at Nov 2, 2012 at 5:25 pm
    Use go/printer to show the output of AST nodes, so you can know how to get
    a determined AST type

    http://golang.org/pkg/go/printer/

    El viernes, 2 de noviembre de 2012 17:13:39 UTC, soapboxcicero escribió:
    (I know you posted code that isn't meant to compile but it would help
    if the only reason it didn't compile were the part you specifically
    flagged as wrong)

    For one thing you're telling the parser to only parse the imports of
    the file. Use mode 0 if you don't need to do anything else special
    (you'll want to use ast.ParseComments if you need the docs for
    anything)

    Then everything you want will be in f.Decls. You can loop over it with
    a typeswitch in the loop testing for ast.FuncDecl or an ast.GenDecl
    whose .Tok field == token.TYPE and then drill down to see if that's a
    struct.
    --
  • Aaron Lifton at Nov 2, 2012 at 5:39 pm
    thanks guys, appreciate it. will post later after i've made some progress.

    Also how do you contribute to the wiki? I noticed this example is broken:
    http://golang.org/pkg/go/printer/#example_Fprint
    On Friday, November 2, 2012 1:25:02 PM UTC-4, Archos wrote:

    Use go/printer to show the output of AST nodes, so you can know how to get
    a determined AST type

    http://golang.org/pkg/go/printer/

    El viernes, 2 de noviembre de 2012 17:13:39 UTC, soapboxcicero escribió:
    (I know you posted code that isn't meant to compile but it would help
    if the only reason it didn't compile were the part you specifically
    flagged as wrong)

    For one thing you're telling the parser to only parse the imports of
    the file. Use mode 0 if you don't need to do anything else special
    (you'll want to use ast.ParseComments if you need the docs for
    anything)

    Then everything you want will be in f.Decls. You can loop over it with
    a typeswitch in the loop testing for ast.FuncDecl or an ast.GenDecl
    whose .Tok field == token.TYPE and then drill down to see if that's a
    struct.
    --
  • Aaron Lifton at Nov 2, 2012 at 5:50 pm
    ok, i was able to print the body of a function in a different file :)
    thanks again. shouldn't be too hard from here. Here's the code in case
    anyone else needs help: https://gist.github.com/91207c4e461de312391a

    So now, i just go through each token somehow to find and categorize data
    types. Oddly structs typically pass as interfaces in functions (because
    they are an interface type?) from my experience, but I'll see.

    On Friday, November 2, 2012 1:39:14 PM UTC-4, Aaron Lifton wrote:

    thanks guys, appreciate it. will post later after i've made some progress.

    Also how do you contribute to the wiki? I noticed this example is broken:
    http://golang.org/pkg/go/printer/#example_Fprint
    On Friday, November 2, 2012 1:25:02 PM UTC-4, Archos wrote:

    Use go/printer to show the output of AST nodes, so you can know how to
    get a determined AST type

    http://golang.org/pkg/go/printer/

    El viernes, 2 de noviembre de 2012 17:13:39 UTC, soapboxcicero escribió:
    (I know you posted code that isn't meant to compile but it would help
    if the only reason it didn't compile were the part you specifically
    flagged as wrong)

    For one thing you're telling the parser to only parse the imports of
    the file. Use mode 0 if you don't need to do anything else special
    (you'll want to use ast.ParseComments if you need the docs for
    anything)

    Then everything you want will be in f.Decls. You can loop over it with
    a typeswitch in the loop testing for ast.FuncDecl or an ast.GenDecl
    whose .Tok field == token.TYPE and then drill down to see if that's a
    struct.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 2, '12 at 4:23p
activeNov 2, '12 at 5:50p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase