parseFiles parses the Go source files within directory dir and returns the ASTs of the ones that could be at least partially parsed, along with a list of I/O and parse errors encountered. I/O is done via ctxt, which may specify a virtual file system. displayPath is used to transform the filenames a
(ctxt *build.Context, displayPath func(string) string, dir string, files []string)
| 27 | // displayPath is used to transform the filenames attached to the ASTs. |
| 28 | // |
| 29 | func parseFiles(ctxt *build.Context, displayPath func(string) string, dir string, files []string) ([]*syntax.File, []error) { |
| 30 | if displayPath == nil { |
| 31 | displayPath = func(path string) string { return path } |
| 32 | } |
| 33 | var wg sync.WaitGroup |
| 34 | n := len(files) |
| 35 | parsed := make([]*syntax.File, n) |
| 36 | errors := make([]error, n) |
| 37 | for i, file := range files { |
| 38 | if !buildutil.IsAbsPath(ctxt, file) { |
| 39 | file = buildutil.JoinPath(ctxt, dir, file) |
| 40 | } |
| 41 | wg.Add(1) |
| 42 | go func(i int, file string) { |
| 43 | ioLimit <- true // wait |
| 44 | defer func() { |
| 45 | wg.Done() |
| 46 | <-ioLimit // signal |
| 47 | }() |
| 48 | var rd io.ReadCloser |
| 49 | var err error |
| 50 | if ctxt.OpenFile != nil { |
| 51 | rd, err = ctxt.OpenFile(file) |
| 52 | } else { |
| 53 | rd, err = os.Open(file) |
| 54 | } |
| 55 | if err != nil { |
| 56 | errors[i] = err // open failed |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | // ParseFile may return both an AST and an error. |
| 61 | parsed[i], errors[i] = syntax.ParseReader(displayPath(file), rd) |
| 62 | rd.Close() |
| 63 | }(i, file) |
| 64 | } |
| 65 | wg.Wait() |
| 66 | |
| 67 | // Eliminate nils, preserving order. |
| 68 | var o int |
| 69 | for _, f := range parsed { |
| 70 | if f != nil { |
| 71 | parsed[o] = f |
| 72 | o++ |
| 73 | } |
| 74 | } |
| 75 | parsed = parsed[:o] |
| 76 | |
| 77 | o = 0 |
| 78 | for _, err := range errors { |
| 79 | if err != nil { |
| 80 | errors[o] = err |
| 81 | o++ |
| 82 | } |
| 83 | } |
| 84 | errors = errors[:o] |
| 85 | |
| 86 | return parsed, errors |
no test coverage detected