~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/go/parser/interface.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
type Mode uint
53
53
 
54
54
const (
55
 
        PackageClauseOnly Mode = 1 << iota // parsing stops after package clause
56
 
        ImportsOnly                        // parsing stops after import declarations
57
 
        ParseComments                      // parse comments and add them to AST
58
 
        Trace                              // print a trace of parsed productions
59
 
        DeclarationErrors                  // report declaration errors
60
 
        SpuriousErrors                     // report all (not just the first) errors per line
 
55
        PackageClauseOnly Mode             = 1 << iota // stop parsing after package clause
 
56
        ImportsOnly                                    // stop parsing after import declarations
 
57
        ParseComments                                  // parse comments and add them to AST
 
58
        Trace                                          // print a trace of parsed productions
 
59
        DeclarationErrors                              // report declaration errors
 
60
        SpuriousErrors                                 // same as AllErrors, for backward-compatibility
 
61
        AllErrors         = SpuriousErrors             // report all errors (not just the first 10 on different lines)
61
62
)
62
63
 
63
64
// ParseFile parses the source code of a single Go source file and returns
79
80
// representing the fragments of erroneous source code). Multiple errors
80
81
// are returned via a scanner.ErrorList which is sorted by file position.
81
82
//
82
 
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (*ast.File, error) {
 
83
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error) {
83
84
        // get source
84
85
        text, err := readSource(filename, src)
85
86
        if err != nil {
86
87
                return nil, err
87
88
        }
88
89
 
 
90
        var p parser
 
91
        defer func() {
 
92
                if e := recover(); e != nil {
 
93
                        _ = e.(bailout) // re-panics if it's not a bailout
 
94
                }
 
95
 
 
96
                // set result values
 
97
                if f == nil {
 
98
                        // source is not a valid Go source file - satisfy
 
99
                        // ParseFile API and return a valid (but) empty
 
100
                        // *ast.File
 
101
                        f = &ast.File{
 
102
                                Name:  new(ast.Ident),
 
103
                                Scope: ast.NewScope(nil),
 
104
                        }
 
105
                }
 
106
 
 
107
                p.errors.Sort()
 
108
                err = p.errors.Err()
 
109
        }()
 
110
 
89
111
        // parse source
90
 
        var p parser
91
112
        p.init(fset, filename, text, mode)
92
 
        f := p.parseFile()
93
 
 
94
 
        // sort errors
95
 
        if p.mode&SpuriousErrors == 0 {
96
 
                p.errors.RemoveMultiples()
97
 
        } else {
98
 
                p.errors.Sort()
99
 
        }
100
 
 
101
 
        return f, p.errors.Err()
 
113
        f = p.parseFile()
 
114
 
 
115
        return
102
116
}
103
117
 
104
118
// ParseDir calls ParseFile for the files in the directory specified by path and
148
162
}
149
163
 
150
164
// ParseExpr is a convenience function for obtaining the AST of an expression x.
151
 
// The position information recorded in the AST is undefined.
152
 
// 
 
165
// The position information recorded in the AST is undefined. The filename used
 
166
// in error messages is the empty string.
 
167
//
153
168
func ParseExpr(x string) (ast.Expr, error) {
154
 
        // parse x within the context of a complete package for correct scopes;
155
 
        // use //line directive for correct positions in error messages and put
156
 
        // x alone on a separate line (handles line comments), followed by a ';'
157
 
        // to force an error if the expression is incomplete
158
 
        file, err := ParseFile(token.NewFileSet(), "", "package p;func _(){_=\n//line :1\n"+x+"\n;}", 0)
159
 
        if err != nil {
160
 
                return nil, err
 
169
        var p parser
 
170
        p.init(token.NewFileSet(), "", []byte(x), 0)
 
171
 
 
172
        // Set up pkg-level scopes to avoid nil-pointer errors.
 
173
        // This is not needed for a correct expression x as the
 
174
        // parser will be ok with a nil topScope, but be cautious
 
175
        // in case of an erroneous x.
 
176
        p.openScope()
 
177
        p.pkgScope = p.topScope
 
178
        e := p.parseRhsOrType()
 
179
        p.closeScope()
 
180
        assert(p.topScope == nil, "unbalanced scopes")
 
181
 
 
182
        if p.errors.Len() > 0 {
 
183
                p.errors.Sort()
 
184
                return nil, p.errors.Err()
161
185
        }
162
 
        return file.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt).Rhs[0], nil
 
186
 
 
187
        return e, nil
163
188
}