|
15 | 15 | package parser |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "bytes" |
19 | | - "encoding/json" |
20 | | - "flag" |
21 | 18 | "fmt" |
22 | | - "os" |
23 | | - "strings" |
24 | 19 |
|
25 | 20 | . "github.com/cloudwego/abcoder/lang/uniast" |
26 | 21 | ) |
27 | 22 |
|
28 | | -var ( |
29 | | - referCodeDepth int |
30 | | - collectComment bool |
31 | | - excludes string |
32 | | -) |
33 | | - |
34 | | -func init() { |
35 | | - // init args with flags |
36 | | - flag.Usage = func() { |
37 | | - fmt.Fprintf(os.Stderr, "Usage: %s [options] <RepoDir> [id]\n", os.Args[0]) |
38 | | - flag.PrintDefaults() |
39 | | - } |
40 | | - flag.BoolVar(&collectComment, "collect_comment", false, "collect comments for each node") |
41 | | - flag.IntVar(&referCodeDepth, "refer_code_depth", 0, "the depth to referenced codes, 0 means only return its identity") |
42 | | - flag.StringVar(&excludes, "excludes", "", "exclude paths, seperated by comma") |
43 | | -} |
44 | | - |
45 | | -func Main() { |
46 | | - var err error |
47 | | - defer func() { |
48 | | - if err != nil { |
49 | | - fmt.Fprintln(os.Stderr, err.Error()) |
50 | | - } |
51 | | - }() |
52 | | - flag.Parse() |
53 | | - as := flag.Args() |
54 | | - if len(as) < 1 { |
55 | | - flag.Usage() |
56 | | - os.Exit(1) |
57 | | - } |
58 | | - |
59 | | - homeDir := as[0] |
60 | | - id := "" |
61 | | - if len(as) >= 2 { |
62 | | - id = as[1] |
63 | | - } |
64 | | - |
65 | | - var exs []string |
66 | | - if excludes != "" { |
67 | | - exs = strings.Split(excludes, ",") |
68 | | - } |
69 | | - // p := NewParser(homeDir, homeDir, WithReferCodeDepth(referCodeDepth), WithExcludes(exs), WithCollectComment(collectComment)) |
70 | | - p := NewParser(homeDir, homeDir, Options{ |
71 | | - ReferCodeDepth: referCodeDepth, |
72 | | - Excludes: exs, |
73 | | - CollectComment: collectComment, |
74 | | - }) |
75 | | - |
76 | | - var out interface{} |
77 | | - |
78 | | - if id == "" { |
79 | | - // parse whole repo |
80 | | - if out, err = p.ParseRepo(); err != nil { |
81 | | - return |
82 | | - } |
83 | | - } else { |
84 | | - // SPEC: seperate the packagepath and entity name by # |
85 | | - ids := strings.Split(id, "#") |
86 | | - |
87 | | - if len(ids) == 1 { |
88 | | - // parse pacakge |
89 | | - pkgPath := ids[0] |
90 | | - if out, err = p.ParsePackage(pkgPath); err != nil { |
91 | | - return |
92 | | - } |
93 | | - } else if len(ids) == 2 { |
94 | | - if out, err = p.ParseNode(ids[0], ids[1]); err != nil { |
95 | | - return |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - buf := bytes.NewBuffer(nil) |
101 | | - encoder := json.NewEncoder(buf) |
102 | | - encoder.SetEscapeHTML(false) |
103 | | - err = encoder.Encode(out) |
104 | | - if err != nil { |
105 | | - fmt.Fprintln(os.Stderr, "Error marshalling functions to JSON:", err) |
106 | | - os.Exit(1) |
107 | | - } |
108 | | - |
109 | | - fmt.Println(buf.String()) |
110 | | -} |
111 | | - |
112 | 23 | func loadNode(p *GoParser, pkgPath string, name string, out *Repository) error { |
113 | 24 | mod, _ := p.getModuleFromPkg(pkgPath) |
114 | 25 | np, err := p.getNode(NewIdentity(mod, PkgPath(pkgPath), name)) |
|
0 commit comments