MCPcopy
hub / github.com/tinylib/msgp / File

Function File

parse/getast.go:56–119  ·  view source on GitHub ↗

File parses a file at the relative path provided and produces a new *FileSet. If you pass in a path to a directory, the entire directory will be parsed. If unexport is false, only exported identifiers are included in the FileSet. If the resulting FileSet would be empty, an error is returned.

(name string, unexported bool, directives []string)

Source from the content-addressed store, hash-verified

54// If unexport is false, only exported identifiers are included in the FileSet.
55// If the resulting FileSet would be empty, an error is returned.
56func File(name string, unexported bool, directives []string) (*FileSet, error) {
57 pushstate(name)
58 defer popstate()
59 fs := &FileSet{
60 Specs: make(map[string]ast.Expr),
61 TypeInfos: make(map[string]*TypeInfo),
62 Identities: make(map[string]gen.Elem),
63 Directives: append([]string{}, directives...),
64 ArrayLimit: math.MaxUint32,
65 MapLimit: math.MaxUint32,
66 }
67
68 fset := token.NewFileSet()
69 finfo, err := os.Stat(name)
70 if err != nil {
71 return nil, err
72 }
73 if finfo.IsDir() {
74 pkgs, err := parser.ParseDir(fset, name, nil, parser.ParseComments)
75 if err != nil {
76 return nil, err
77 }
78 if len(pkgs) != 1 {
79 return nil, fmt.Errorf("multiple packages in directory: %s", name)
80 }
81 var one *ast.Package
82 for _, nm := range pkgs {
83 one = nm
84 break
85 }
86 fs.Package = one.Name
87 for _, fl := range one.Files {
88 pushstate(fl.Name.Name)
89 fs.Directives = append(fs.Directives, yieldComments(fl.Comments)...)
90 if !unexported {
91 ast.FileExports(fl)
92 }
93 fs.getTypeSpecs(fl)
94 popstate()
95 }
96 } else {
97 f, err := parser.ParseFile(fset, name, nil, parser.ParseComments)
98 if err != nil {
99 return nil, err
100 }
101 fs.Package = f.Name.Name
102 fs.Directives = append(fs.Directives, yieldComments(f.Comments)...)
103 if !unexported {
104 ast.FileExports(f)
105 }
106 fs.getTypeSpecs(f)
107 }
108
109 if len(fs.Specs) == 0 {
110 return nil, fmt.Errorf("no definitions in %s", name)
111 }
112
113 fs.applyEarlyDirectives()

Callers 1

RunFunction · 0.92

Calls 8

getTypeSpecsMethod · 0.95
applyEarlyDirectivesMethod · 0.95
processMethod · 0.95
applyDirectivesMethod · 0.95
propInlineMethod · 0.95
pushstateFunction · 0.85
popstateFunction · 0.85
yieldCommentsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…