LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Parse input AST and prepare Prog structure. | 5 // Parse input AST and prepare Prog structure. |
6 | 6 |
7 package main | 7 package main |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
11 "go/ast" | 11 "go/ast" |
12 "go/parser" | 12 "go/parser" |
13 "go/scanner" | 13 "go/scanner" |
14 "go/token" | 14 "go/token" |
15 "os" | 15 "os" |
| 16 "path/filepath" |
16 "strings" | 17 "strings" |
17 ) | 18 ) |
18 | 19 |
19 func parse(name string, flags parser.Mode) *ast.File { | 20 func parse(name string, flags parser.Mode) *ast.File { |
20 ast1, err := parser.ParseFile(fset, name, nil, flags) | 21 ast1, err := parser.ParseFile(fset, name, nil, flags) |
21 if err != nil { | 22 if err != nil { |
22 if list, ok := err.(scanner.ErrorList); ok { | 23 if list, ok := err.(scanner.ErrorList); ok { |
23 // If err is a scanner.ErrorList, its String will print
just | 24 // If err is a scanner.ErrorList, its String will print
just |
24 // the first error and then (+n more errors). | 25 // the first error and then (+n more errors). |
25 // Instead, turn it into a new Error that will return | 26 // Instead, turn it into a new Error that will return |
(...skipping 11 matching lines...) Expand all Loading... |
37 func sourceLine(n ast.Node) int { | 38 func sourceLine(n ast.Node) int { |
38 return fset.Position(n.Pos()).Line | 39 return fset.Position(n.Pos()).Line |
39 } | 40 } |
40 | 41 |
41 // ReadGo populates f with information learned from reading the | 42 // ReadGo populates f with information learned from reading the |
42 // Go source file with the given file name. It gathers the C preamble | 43 // Go source file with the given file name. It gathers the C preamble |
43 // attached to the import "C" comment, a list of references to C.xxx, | 44 // attached to the import "C" comment, a list of references to C.xxx, |
44 // a list of exported functions, and the actual AST, to be rewritten and | 45 // a list of exported functions, and the actual AST, to be rewritten and |
45 // printed. | 46 // printed. |
46 func (f *File) ReadGo(name string) { | 47 func (f *File) ReadGo(name string) { |
| 48 // Create absolute path for file, so that it will be used in error |
| 49 // messages and recorded in debug line number information. |
| 50 // This matches the rest of the toolchain. See golang.org/issue/5122. |
| 51 if aname, err := filepath.Abs(name); err == nil { |
| 52 name = aname |
| 53 } |
| 54 |
47 // Two different parses: once with comments, once without. | 55 // Two different parses: once with comments, once without. |
48 // The printer is not good enough at printing comments in the | 56 // The printer is not good enough at printing comments in the |
49 // right place when we start editing the AST behind its back, | 57 // right place when we start editing the AST behind its back, |
50 // so we use ast1 to look for the doc comments on import "C" | 58 // so we use ast1 to look for the doc comments on import "C" |
51 // and on exported functions, and we use ast2 for translating | 59 // and on exported functions, and we use ast2 for translating |
52 // and reprinting. | 60 // and reprinting. |
53 ast1 := parse(name, parser.ParseComments) | 61 ast1 := parse(name, parser.ParseComments) |
54 ast2 := parse(name, 0) | 62 ast2 := parse(name, 0) |
55 | 63 |
56 f.Package = ast1.Name.Name | 64 f.Package = ast1.Name.Name |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 case []ast.Stmt: | 444 case []ast.Stmt: |
437 for _, s := range n { | 445 for _, s := range n { |
438 f.walk(s, context, visit) | 446 f.walk(s, context, visit) |
439 } | 447 } |
440 case []ast.Spec: | 448 case []ast.Spec: |
441 for _, s := range n { | 449 for _, s := range n { |
442 f.walk(s, context, visit) | 450 f.walk(s, context, visit) |
443 } | 451 } |
444 } | 452 } |
445 } | 453 } |
LEFT | RIGHT |