Commit e7d513ea authored by Robert Griesemer's avatar Robert Griesemer

go/parser: Use explicit parser.Mode type.

R=r, bradfitz
CC=golang-dev
https://golang.org/cl/5574058
parent 324513bc
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
"strings" "strings"
) )
func parse(name string, flags uint) *ast.File { func parse(name string, flags parser.Mode) *ast.File {
ast1, err := parser.ParseFile(fset, name, nil, flags) ast1, err := parser.ParseFile(fset, name, nil, flags)
if err != nil { if err != nil {
if list, ok := err.(scanner.ErrorList); ok { if list, ok := err.(scanner.ErrorList); ok {
......
...@@ -17,7 +17,7 @@ import ( ...@@ -17,7 +17,7 @@ import (
"path/filepath" "path/filepath"
) )
func parseFile(fset *token.FileSet, filename string, mode uint) (*ast.File, error) { func parseFile(fset *token.FileSet, filename string, mode parser.Mode) (*ast.File, error) {
src, err := ReadFile(fs, filename) src, err := ReadFile(fs, filename)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -44,7 +44,7 @@ var ( ...@@ -44,7 +44,7 @@ var (
fset = token.NewFileSet() fset = token.NewFileSet()
exitCode = 0 exitCode = 0
rewrite func(*ast.File) *ast.File rewrite func(*ast.File) *ast.File
parserMode uint parserMode parser.Mode
printerMode uint printerMode uint
) )
...@@ -60,7 +60,7 @@ func usage() { ...@@ -60,7 +60,7 @@ func usage() {
} }
func initParserMode() { func initParserMode() {
parserMode = uint(0) parserMode = parser.Mode(0)
if *comments { if *comments {
parserMode |= parser.ParseComments parserMode |= parser.ParseComments
} }
......
...@@ -45,12 +45,14 @@ func readSource(filename string, src interface{}) ([]byte, error) { ...@@ -45,12 +45,14 @@ func readSource(filename string, src interface{}) ([]byte, error) {
return ioutil.ReadFile(filename) return ioutil.ReadFile(filename)
} }
// The mode parameter to the Parse* functions is a set of flags (or 0). // A Mode value is a set of flags (or 0).
// They control the amount of source code parsed and other optional // They control the amount of source code parsed and other optional
// parser functionality. // parser functionality.
// //
type Mode uint
const ( const (
PackageClauseOnly uint = 1 << iota // parsing stops after package clause PackageClauseOnly Mode = 1 << iota // parsing stops after package clause
ImportsOnly // parsing stops after import declarations ImportsOnly // parsing stops after import declarations
ParseComments // parse comments and add them to AST ParseComments // parse comments and add them to AST
Trace // print a trace of parsed productions Trace // print a trace of parsed productions
...@@ -77,7 +79,7 @@ const ( ...@@ -77,7 +79,7 @@ const (
// representing the fragments of erroneous source code). Multiple errors // representing the fragments of erroneous source code). Multiple errors
// are returned via a scanner.ErrorList which is sorted by file position. // are returned via a scanner.ErrorList which is sorted by file position.
// //
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint) (*ast.File, error) { func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (*ast.File, error) {
text, err := readSource(filename, src) text, err := readSource(filename, src)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -97,7 +99,7 @@ func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint) ...@@ -97,7 +99,7 @@ func ParseFile(fset *token.FileSet, filename string, src interface{}, mode uint)
// returned. If a parse error occurred, a non-nil but incomplete map and the // returned. If a parse error occurred, a non-nil but incomplete map and the
// first error encountered are returned. // first error encountered are returned.
// //
func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode uint) (pkgs map[string]*ast.Package, first error) { func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error) {
fd, err := os.Open(path) fd, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -117,7 +119,10 @@ func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, m ...@@ -117,7 +119,10 @@ func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, m
name := src.Name.Name name := src.Name.Name
pkg, found := pkgs[name] pkg, found := pkgs[name]
if !found { if !found {
pkg = &ast.Package{name, nil, nil, make(map[string]*ast.File)} pkg = &ast.Package{
Name: name,
Files: make(map[string]*ast.File),
}
pkgs[name] = pkg pkgs[name] = pkg
} }
pkg.Files[filename] = src pkg.Files[filename] = src
......
...@@ -23,7 +23,7 @@ type parser struct { ...@@ -23,7 +23,7 @@ type parser struct {
scanner scanner.Scanner scanner scanner.Scanner
// Tracing/debugging // Tracing/debugging
mode uint // parsing mode mode Mode // parsing mode
trace bool // == (mode & Trace != 0) trace bool // == (mode & Trace != 0)
indent uint // indentation used for tracing output indent uint // indentation used for tracing output
...@@ -52,7 +52,7 @@ type parser struct { ...@@ -52,7 +52,7 @@ type parser struct {
targetStack [][]*ast.Ident // stack of unresolved labels targetStack [][]*ast.Ident // stack of unresolved labels
} }
func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode uint) { func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode Mode) {
p.file = fset.AddFile(filename, fset.Base(), len(src)) p.file = fset.AddFile(filename, fset.Base(), len(src))
var m uint var m uint
if mode&ParseComments != 0 { if mode&ParseComments != 0 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment