Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
50442290
Commit
50442290
authored
Jan 04, 2010
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplified parser interface.
R=rsc, r CC=golang-dev, rog
https://golang.org/cl/183116
parent
a0ee18bd
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
54 deletions
+39
-54
src/cmd/cgo/ast.go
src/cmd/cgo/ast.go
+1
-1
src/cmd/godoc/godoc.go
src/cmd/godoc/godoc.go
+11
-3
src/pkg/go/parser/interface.go
src/pkg/go/parser/interface.go
+18
-46
src/pkg/go/parser/parser_test.go
src/pkg/go/parser/parser_test.go
+9
-4
No files found.
src/cmd/cgo/ast.go
View file @
50442290
...
...
@@ -59,7 +59,7 @@ type FuncType struct {
func
openProg
(
name
string
,
p
*
Prog
)
{
var
err
os
.
Error
p
.
AST
,
err
=
parser
.
Parse
PkgFile
(
""
,
name
,
parser
.
ParseComments
)
p
.
AST
,
err
=
parser
.
Parse
File
(
name
,
nil
,
parser
.
ParseComments
)
if
err
!=
nil
{
if
list
,
ok
:=
err
.
(
scanner
.
ErrorList
);
ok
{
// If err is a scanner.ErrorList, its String will print just
...
...
src/cmd/godoc/godoc.go
View file @
50442290
...
...
@@ -1021,10 +1021,18 @@ func (h *httpHandler) getPageInfo(path string) PageInfo {
}
// get package AST
pkg
,
err
:=
parser
.
ParsePackage
(
dirname
,
filter
,
parser
.
ParseComments
)
pkg
s
,
err
:=
parser
.
ParseDir
(
dirname
,
filter
,
parser
.
ParseComments
)
if
err
!=
nil
{
// TODO: parse errors should be shown instead of an empty directory
log
.
Stderrf
(
"parser.parsePackage: %s"
,
err
)
// TODO: errors should be shown instead of an empty directory
log
.
Stderrf
(
"parser.parseDir: %s"
,
err
)
}
if
len
(
pkgs
)
!=
1
{
// TODO: should handle multiple packages
log
.
Stderrf
(
"parser.parseDir: found %d packages"
,
len
(
pkgs
))
}
var
pkg
*
ast
.
Package
for
_
,
pkg
=
range
pkgs
{
break
// take the first package found
}
// compute package documentation
...
...
src/pkg/go/parser/interface.go
View file @
50442290
...
...
@@ -8,7 +8,6 @@ package parser
import
(
"bytes"
"fmt"
"go/ast"
"go/scanner"
"io"
...
...
@@ -132,41 +131,16 @@ func ParseFile(filename string, src interface{}, mode uint) (*ast.File, os.Error
}
// Parse
PkgFile parses the file specified by filename and returns the
//
corresponding AST. If the file cannot be read, has syntax errors, or
//
does not belong to the package (i.e., pkgname != "" and the package
//
name in the file doesn't match pkkname), an error is return
ed.
// Parse
Dir calls ParseFile for the files in the directory specified by path and
//
returns a map of package name -> package AST with all the packages found. If
//
filter != nil, only the files with os.Dir entries passing through the filter
//
are considered. The mode bits are passed to ParseFile unchang
ed.
//
func
ParsePkgFile
(
pkgname
,
filename
string
,
mode
uint
)
(
*
ast
.
File
,
os
.
Error
)
{
src
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
return
nil
,
err
}
if
pkgname
!=
""
{
prog
,
err
:=
ParseFile
(
filename
,
src
,
PackageClauseOnly
)
if
err
!=
nil
{
return
nil
,
err
}
if
prog
.
Name
.
Value
!=
pkgname
{
return
nil
,
os
.
NewError
(
fmt
.
Sprintf
(
"multiple packages found: %s, %s"
,
prog
.
Name
.
Value
,
pkgname
))
}
if
mode
==
PackageClauseOnly
{
return
prog
,
nil
}
}
return
ParseFile
(
filename
,
src
,
mode
)
}
// ParsePackage parses all files in the directory specified by path and
// returns an AST representing the package found. The set of files may be
// restricted by providing a non-nil filter function; only the files with
// os.Dir entries passing through the filter are considered.
// If ParsePackage does not find exactly one package, it returns an error.
// If the directory couldn't be read, a nil map and the respective error are
// returned. If a parse error occured, a non-nil but incomplete map and the
// error are returned.
//
func
Parse
Package
(
path
string
,
filter
func
(
*
os
.
Dir
)
bool
,
mode
uint
)
(
*
ast
.
Package
,
os
.
Error
)
{
func
Parse
Dir
(
path
string
,
filter
func
(
*
os
.
Dir
)
bool
,
mode
uint
)
(
map
[
string
]
*
ast
.
Package
,
os
.
Error
)
{
fd
,
err
:=
os
.
Open
(
path
,
os
.
O_RDONLY
,
0
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -178,25 +152,23 @@ func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Packa
return
nil
,
err
}
name
:=
""
files
:=
make
(
map
[
string
]
*
ast
.
File
)
pkgs
:=
make
(
map
[
string
]
*
ast
.
Package
)
for
i
:=
0
;
i
<
len
(
list
);
i
++
{
entry
:=
&
list
[
i
]
if
filter
==
nil
||
filter
(
entry
)
{
src
,
err
:=
Parse
PkgFile
(
name
,
pathutil
.
Join
(
path
,
entry
.
Name
)
,
mode
)
src
,
err
:=
Parse
File
(
pathutil
.
Join
(
path
,
entry
.
Name
),
nil
,
mode
)
if
err
!=
nil
{
return
nil
,
err
return
pkgs
,
err
}
files
[
entry
.
Name
]
=
src
if
name
==
""
{
name
=
src
.
Name
.
Value
name
:=
src
.
Name
.
Value
pkg
,
found
:=
pkgs
[
name
]
if
!
found
{
pkg
=
&
ast
.
Package
{
name
,
path
,
make
(
map
[
string
]
*
ast
.
File
)}
pkgs
[
name
]
=
pkg
}
pkg
.
Files
[
entry
.
Name
]
=
src
}
}
if
len
(
files
)
==
0
{
return
nil
,
os
.
NewError
(
path
+
": no package found"
)
}
return
&
ast
.
Package
{
name
,
path
,
files
},
nil
return
pkgs
,
nil
}
src/pkg/go/parser/parser_test.go
View file @
50442290
...
...
@@ -78,12 +78,17 @@ func dirFilter(d *os.Dir) bool { return nameFilter(d.Name) }
func
TestParse4
(
t
*
testing
.
T
)
{
path
:=
"."
pkg
,
err
:=
ParsePackage
(
path
,
dirFilter
,
0
)
pkg
s
,
err
:=
ParseDir
(
path
,
dirFilter
,
0
)
if
err
!=
nil
{
t
.
Fatalf
(
"Parse
Package
(%s): %v"
,
path
,
err
)
t
.
Fatalf
(
"Parse
Dir
(%s): %v"
,
path
,
err
)
}
if
pkg
.
Name
!=
"parser"
{
t
.
Errorf
(
"incorrect package name: %s"
,
pkg
.
Name
)
if
len
(
pkgs
)
!=
1
{
t
.
Errorf
(
"incorrect number of packages: %d"
,
len
(
pkgs
))
}
pkg
,
found
:=
pkgs
[
"parser"
]
if
pkg
==
nil
||
!
found
{
t
.
Errorf
(
`package "parser" not found`
)
return
}
for
filename
,
_
:=
range
pkg
.
Files
{
if
!
nameFilter
(
filename
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment