-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathgo_parser.htm
144 lines (123 loc) · 7.17 KB
/
go_parser.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="../assets/site.css" rel="stylesheet">
<title>go/parser</title>
</head>
<body>
<div class="container">
<h2 id="pkg-overview">package parser</h2>
<p><code>import "go/parser"</code>
<p>Package parser implements a parser for Go source files. Input may be provided in a variety of forms (see the various Parse* functions); the output is an abstract syntax tree (AST) representing the Go source. The parser is invoked through one of the Parse* functions.</p>
<h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">¶</a></h3>
<a href="../main.html"><h3>返回首页</h3></a>
</br>
<li><a href="#ParseDir">func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error)</a></li>
<li><a href="#ParseExpr">func ParseExpr(x string) (ast.Expr, error)</a></li>
<li><a href="#ParseFile">func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error)</a></li>
<li><a href="#Mode">type Mode</a></li>
</ul>
<h3 id="ParseDir">func ParseDir</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>func ParseDir(fset *<a href="./go_token.htm">token</a>.<a href="./go_token.htm#FileSet">FileSet</a>, path <a href="./builtin.htm#string">string</a>, filter func(<a href="./os.htm">os</a>.<a href="./os.htm#FileInfo">FileInfo</a>) <a href="./builtin.htm#bool">bool</a>, mode <a href="#Mode">Mode</a>) (pkgs map[<a href="./builtin.htm#string">string</a>]*<a href="./go_ast.htm">ast</a>.<a href="./go_ast.htm#Package">Package</a>, first <a href="./builtin.htm#error">error</a>)</pre>
<p>
ParseDir calls ParseFile for all files with names ending in ".go" in the
directory specified by path and returns a map of package name -> package
AST with all the packages found.
</p>
<p>
If filter != nil, only the files with os.FileInfo entries passing through
the filter (and ending in ".go") are considered. The mode bits are passed
to ParseFile unchanged. Position information is recorded in fset.
</p>
<p>
If the directory couldn't be read, a nil map and the respective error are
returned. If a parse error occurred, a non-nil but incomplete map and the
first error encountered are returned.
</p>
<h3 id="ParseExpr">func ParseExpr <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>func ParseExpr(x <a href="./builtin.htm#string">string</a>) (<a href="./go_ast.htm">ast</a>.<a href="./go_ast.htm#Expr">Expr</a>, <a href="./builtin.htm#error">error</a>)</pre>
<p>
ParseExpr is a convenience function for obtaining the AST of an expression x.
The position information recorded in the AST is undefined. The filename used
in error messages is the empty string.
</p>
<h3 id="ParseFile">func ParseFile <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>func ParseFile(fset *<a href="./go_token.htm">token</a>.<a href="./go_token.htm#FileSet">FileSet</a>, filename <a href="./builtin.htm#string">string</a>, src interface{}, mode <a href="#Mode">Mode</a>) (f *<a href="./go_ast.htm">ast</a>.<a href="./go_ast.htm#File">File</a>, err <a href="./builtin.htm#error">error</a>)</pre>
<p>
ParseFile parses the source code of a single Go source file and returns
the corresponding ast.File node. The source code may be provided via
the filename of the source file, or via the src parameter.
</p>
<p>
If src != nil, ParseFile parses the source from src and the filename is
only used when recording position information. The type of the argument
for the src parameter must be string, []byte, or io.Reader.
If src == nil, ParseFile parses the file specified by filename.
</p>
<p>
The mode parameter controls the amount of source text parsed and other
optional parser functionality. Position information is recorded in the
file set fset.
</p>
<p>
If the source couldn't be read, the returned AST is nil and the error
indicates the specific failure. If the source was read but syntax
errors were found, the result is a partial AST (with ast.Bad* nodes
representing the fragments of erroneous source code). Multiple errors
are returned via a scanner.ErrorList which is sorted by file position.
</p>
<div id="example_ParseFile" class="toggle">
<div class="expanded">
<p>Code:</p>
<pre class="code">fset := token.NewFileSet() <span class="comment">// positions are relative to fset</span>
<span class="comment">// Parse the file containing this very example</span>
<span class="comment">// but stop after processing the imports.</span>
f, err := parser.ParseFile(fset, "example_test.go", nil, parser.ImportsOnly)
if err != nil {
fmt.Println(err)
return
}
<span class="comment">// Print the imports from the file's AST.</span>
for _, s := range f.Imports {
fmt.Println(s.Path.Value)
}
<span class="comment"></pre>
<p>Output:</p>
<pre class="output">
"fmt"
"go/parser"
"go/token"
</pre>
</div>
</div>
<h3 id="Mode">type Mode <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type Mode <a href="./builtin.htm#uint">uint</a></pre>
<p>
A Mode value is a set of flags (or 0).
They control the amount of source code parsed and other optional
parser functionality.
</p>
<pre>const (
<span id="PackageClauseOnly">PackageClauseOnly</span> <a href="#Mode">Mode</a> = 1 << <a href="./builtin.htm#iota">iota</a> <span class="comment">// stop parsing after package clause</span>
<span id="ImportsOnly">ImportsOnly</span> <span class="comment">// stop parsing after import declarations</span>
<span id="ParseComments">ParseComments</span> <span class="comment">// parse comments and add them to AST</span>
<span id="Trace">Trace</span> <span class="comment">// print a trace of parsed productions</span>
<span id="DeclarationErrors">DeclarationErrors</span> <span class="comment">// report declaration errors</span>
<span id="SpuriousErrors">SpuriousErrors</span> <span class="comment">// same as AllErrors, for backward-compatibility</span>
<span id="AllErrors">AllErrors</span> = <a href="#SpuriousErrors">SpuriousErrors</a> <span class="comment">// report all errors (not just the first 10 on different lines)</span>
)</pre>
</div>
<div id="x-footer" class="clearfix">
<div class="container">
<a href="http://studygolang.com/" target="_blank">Go语言中文网</a>
<span class="text-muted">|</span> <a href="http://golang.org/" target="_blank">Go Language</a>
<span class="pull-right"><a href="#">Back to top</a></span>
</div>
</div>
<script src="../assets/jquery-2.0.3.min.js"></script>
<script src="../assets/bootstrap.min.js"></script>
<script src="../assets/site.js"></script>
</body>
</html>