Commit a3d1045f authored by Robert Griesemer's avatar Robert Griesemer

1) Change default gofmt default settings for

   parsing and printing to new syntax.

   Use -oldparser to parse the old syntax,
   use -oldprinter to print the old syntax.

2) Change default gofmt formatting settings
   to use tabs for indentation only and to use
   spaces for alignment. This will make the code
   alignment insensitive to an editor's tabwidth.

   Use -spaces=false to use tabs for alignment.

3) Manually changed src/exp/parser/parser_test.go
   so that it doesn't try to parse the parser's
   source files using the old syntax (they have
   new syntax now).

4) gofmt -w src misc test/bench

3rd set of files.

R=rsc
CC=golang-dev
https://golang.org/cl/180048
parent 5a1d3323
This diff is collapsed.
...@@ -8,14 +8,14 @@ import "go/token" ...@@ -8,14 +8,14 @@ import "go/token"
func filterIdentList(list []*Ident) []*Ident { func filterIdentList(list []*Ident) []*Ident {
j := 0; j := 0
for _, x := range list { for _, x := range list {
if x.IsExported() { if x.IsExported() {
list[j] = x; list[j] = x
j++; j++
} }
} }
return list[0:j]; return list[0:j]
} }
...@@ -32,14 +32,14 @@ func isExportedType(typ Expr) bool { ...@@ -32,14 +32,14 @@ func isExportedType(typ Expr) bool {
case *StarExpr: case *StarExpr:
return isExportedType(t.X) return isExportedType(t.X)
} }
return false; return false
} }
func filterFieldList(list []*Field, incomplete *bool) []*Field { func filterFieldList(list []*Field, incomplete *bool) []*Field {
j := 0; j := 0
for _, f := range list { for _, f := range list {
exported := false; exported := false
if len(f.Names) == 0 { if len(f.Names) == 0 {
// anonymous field // anonymous field
// (Note that a non-exported anonymous field // (Note that a non-exported anonymous field
...@@ -49,23 +49,23 @@ func filterFieldList(list []*Field, incomplete *bool) []*Field { ...@@ -49,23 +49,23 @@ func filterFieldList(list []*Field, incomplete *bool) []*Field {
// type information.) // type information.)
exported = isExportedType(f.Type) exported = isExportedType(f.Type)
} else { } else {
n := len(f.Names); n := len(f.Names)
f.Names = filterIdentList(f.Names); f.Names = filterIdentList(f.Names)
if len(f.Names) < n { if len(f.Names) < n {
*incomplete = true *incomplete = true
} }
exported = len(f.Names) > 0; exported = len(f.Names) > 0
} }
if exported { if exported {
filterType(f.Type); filterType(f.Type)
list[j] = f; list[j] = f
j++; j++
} }
} }
if j < len(list) { if j < len(list) {
*incomplete = true *incomplete = true
} }
return list[0:j]; return list[0:j]
} }
...@@ -85,13 +85,13 @@ func filterType(typ Expr) { ...@@ -85,13 +85,13 @@ func filterType(typ Expr) {
case *StructType: case *StructType:
t.Fields = filterFieldList(t.Fields, &t.Incomplete) t.Fields = filterFieldList(t.Fields, &t.Incomplete)
case *FuncType: case *FuncType:
filterParamList(t.Params); filterParamList(t.Params)
filterParamList(t.Results); filterParamList(t.Results)
case *InterfaceType: case *InterfaceType:
t.Methods = filterFieldList(t.Methods, &t.Incomplete) t.Methods = filterFieldList(t.Methods, &t.Incomplete)
case *MapType: case *MapType:
filterType(t.Key); filterType(t.Key)
filterType(t.Value); filterType(t.Value)
case *ChanType: case *ChanType:
filterType(t.Value) filterType(t.Value)
} }
...@@ -101,48 +101,48 @@ func filterType(typ Expr) { ...@@ -101,48 +101,48 @@ func filterType(typ Expr) {
func filterSpec(spec Spec) bool { func filterSpec(spec Spec) bool {
switch s := spec.(type) { switch s := spec.(type) {
case *ValueSpec: case *ValueSpec:
s.Names = filterIdentList(s.Names); s.Names = filterIdentList(s.Names)
if len(s.Names) > 0 { if len(s.Names) > 0 {
filterType(s.Type); filterType(s.Type)
return true; return true
} }
case *TypeSpec: case *TypeSpec:
// TODO(gri) consider stripping forward declarations // TODO(gri) consider stripping forward declarations
// of structs, interfaces, functions, and methods // of structs, interfaces, functions, and methods
if s.Name.IsExported() { if s.Name.IsExported() {
filterType(s.Type); filterType(s.Type)
return true; return true
} }
} }
return false; return false
} }
func filterSpecList(list []Spec) []Spec { func filterSpecList(list []Spec) []Spec {
j := 0; j := 0
for _, s := range list { for _, s := range list {
if filterSpec(s) { if filterSpec(s) {
list[j] = s; list[j] = s
j++; j++
} }
} }
return list[0:j]; return list[0:j]
} }
func filterDecl(decl Decl) bool { func filterDecl(decl Decl) bool {
switch d := decl.(type) { switch d := decl.(type) {
case *GenDecl: case *GenDecl:
d.Specs = filterSpecList(d.Specs); d.Specs = filterSpecList(d.Specs)
return len(d.Specs) > 0; return len(d.Specs) > 0
case *FuncDecl: case *FuncDecl:
// TODO consider removing function declaration altogether if // TODO consider removing function declaration altogether if
// forward declaration (i.e., if d.Body == nil) because // forward declaration (i.e., if d.Body == nil) because
// in that case the actual declaration will come later. // in that case the actual declaration will come later.
d.Body = nil; // strip body d.Body = nil // strip body
return d.Name.IsExported(); return d.Name.IsExported()
} }
return false; return false
} }
...@@ -157,15 +157,15 @@ func filterDecl(decl Decl) bool { ...@@ -157,15 +157,15 @@ func filterDecl(decl Decl) bool {
// false otherwise. // false otherwise.
// //
func FileExports(src *File) bool { func FileExports(src *File) bool {
j := 0; j := 0
for _, d := range src.Decls { for _, d := range src.Decls {
if filterDecl(d) { if filterDecl(d) {
src.Decls[j] = d; src.Decls[j] = d
j++; j++
} }
} }
src.Decls = src.Decls[0:j]; src.Decls = src.Decls[0:j]
return j > 0; return j > 0
} }
...@@ -177,13 +177,13 @@ func FileExports(src *File) bool { ...@@ -177,13 +177,13 @@ func FileExports(src *File) bool {
// returns false otherwise. // returns false otherwise.
// //
func PackageExports(pkg *Package) bool { func PackageExports(pkg *Package) bool {
hasExports := false; hasExports := false
for _, f := range pkg.Files { for _, f := range pkg.Files {
if FileExports(f) { if FileExports(f) {
hasExports = true hasExports = true
} }
} }
return hasExports; return hasExports
} }
...@@ -199,13 +199,13 @@ var separator = &Comment{noPos, []byte{'/', '/'}} ...@@ -199,13 +199,13 @@ var separator = &Comment{noPos, []byte{'/', '/'}}
func MergePackageFiles(pkg *Package) *File { func MergePackageFiles(pkg *Package) *File {
// Count the number of package comments and declarations across // Count the number of package comments and declarations across
// all package files. // all package files.
ncomments := 0; ncomments := 0
ndecls := 0; ndecls := 0
for _, f := range pkg.Files { for _, f := range pkg.Files {
if f.Doc != nil { if f.Doc != nil {
ncomments += len(f.Doc.List) + 1 // +1 for separator ncomments += len(f.Doc.List) + 1 // +1 for separator
} }
ndecls += len(f.Decls); ndecls += len(f.Decls)
} }
// Collect package comments from all package files into a single // Collect package comments from all package files into a single
...@@ -213,35 +213,35 @@ func MergePackageFiles(pkg *Package) *File { ...@@ -213,35 +213,35 @@ func MergePackageFiles(pkg *Package) *File {
// is unspecified. In general there should be only one file with // is unspecified. In general there should be only one file with
// a package comment; but it's better to collect extra comments // a package comment; but it's better to collect extra comments
// than drop them on the floor. // than drop them on the floor.
var doc *CommentGroup; var doc *CommentGroup
if ncomments > 0 { if ncomments > 0 {
list := make([]*Comment, ncomments-1); // -1: no separator before first group list := make([]*Comment, ncomments-1) // -1: no separator before first group
i := 0; i := 0
for _, f := range pkg.Files { for _, f := range pkg.Files {
if f.Doc != nil { if f.Doc != nil {
if i > 0 { if i > 0 {
// not the first group - add separator // not the first group - add separator
list[i] = separator; list[i] = separator
i++; i++
} }
for _, c := range f.Doc.List { for _, c := range f.Doc.List {
list[i] = c; list[i] = c
i++; i++
} }
} }
} }
doc = &CommentGroup{list, nil}; doc = &CommentGroup{list, nil}
} }
// Collect declarations from all package files. // Collect declarations from all package files.
var decls []Decl; var decls []Decl
if ndecls > 0 { if ndecls > 0 {
decls = make([]Decl, ndecls); decls = make([]Decl, ndecls)
i := 0; i := 0
for _, f := range pkg.Files { for _, f := range pkg.Files {
for _, d := range f.Decls { for _, d := range f.Decls {
decls[i] = d; decls[i] = d
i++; i++
} }
} }
} }
...@@ -249,5 +249,5 @@ func MergePackageFiles(pkg *Package) *File { ...@@ -249,5 +249,5 @@ func MergePackageFiles(pkg *Package) *File {
// TODO(gri) Should collect comments as well. For that the comment // TODO(gri) Should collect comments as well. For that the comment
// list should be changed back into a []*CommentGroup, // list should be changed back into a []*CommentGroup,
// otherwise need to modify the existing linked list. // otherwise need to modify the existing linked list.
return &File{doc, noPos, &Ident{noPos, pkg.Name}, decls, nil}; return &File{doc, noPos, &Ident{noPos, pkg.Name}, decls, nil}
} }
...@@ -11,13 +11,13 @@ package ast ...@@ -11,13 +11,13 @@ package ast
// NOTE: WORK IN PROGRESS // NOTE: WORK IN PROGRESS
// //
type Scope struct { type Scope struct {
Outer *Scope; Outer *Scope
Names map[string]*Ident; Names map[string]*Ident
} }
// NewScope creates a new scope nested in the outer scope. // NewScope creates a new scope nested in the outer scope.
func NewScope(outer *Scope) *Scope { return &Scope{outer, make(map[string]*Ident)} } func NewScope(outer *Scope) *Scope { return &Scope{outer, make(map[string]*Ident)} }
// Declare inserts an identifier into the scope s. If the // Declare inserts an identifier into the scope s. If the
...@@ -28,8 +28,8 @@ func (s *Scope) Declare(ident *Ident) bool { ...@@ -28,8 +28,8 @@ func (s *Scope) Declare(ident *Ident) bool {
if _, found := s.Names[ident.Value]; found { if _, found := s.Names[ident.Value]; found {
return false return false
} }
s.Names[ident.Value] = ident; s.Names[ident.Value] = ident
return true; return true
} }
...@@ -43,7 +43,7 @@ func (s *Scope) Lookup(name string) *Ident { ...@@ -43,7 +43,7 @@ func (s *Scope) Lookup(name string) *Ident {
return ident return ident
} }
} }
return nil; return nil
} }
......
...@@ -10,7 +10,7 @@ import "fmt" ...@@ -10,7 +10,7 @@ import "fmt"
// If the result visitor w is not nil, Walk visits each of the children // If the result visitor w is not nil, Walk visits each of the children
// of node with the visitor w, followed by a call of w.Visit(nil). // of node with the visitor w, followed by a call of w.Visit(nil).
type Visitor interface { type Visitor interface {
Visit(node interface{}) (w Visitor); Visit(node interface{}) (w Visitor)
} }
...@@ -71,13 +71,13 @@ func Walk(v Visitor, node interface{}) { ...@@ -71,13 +71,13 @@ func Walk(v Visitor, node interface{}) {
// comments list. // comments list.
case *Field: case *Field:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc)
Walk(v, n.Names); Walk(v, n.Names)
Walk(v, n.Type); Walk(v, n.Type)
for _, x := range n.Tag { for _, x := range n.Tag {
Walk(v, x) Walk(v, x)
} }
walkCommentGroup(v, n.Comment); walkCommentGroup(v, n.Comment)
// Expressions // Expressions
case *BadExpr, *Ident, *Ellipsis, *BasicLit: case *BadExpr, *Ident, *Ellipsis, *BasicLit:
...@@ -92,35 +92,35 @@ func Walk(v Visitor, node interface{}) { ...@@ -92,35 +92,35 @@ func Walk(v Visitor, node interface{}) {
if n != nil { if n != nil {
Walk(v, n.Type) Walk(v, n.Type)
} }
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body)
case *CompositeLit: case *CompositeLit:
Walk(v, n.Type); Walk(v, n.Type)
Walk(v, n.Elts); Walk(v, n.Elts)
case *ParenExpr: case *ParenExpr:
Walk(v, n.X) Walk(v, n.X)
case *SelectorExpr: case *SelectorExpr:
Walk(v, n.X); Walk(v, n.X)
walkIdent(v, n.Sel); walkIdent(v, n.Sel)
case *IndexExpr: case *IndexExpr:
Walk(v, n.X); Walk(v, n.X)
Walk(v, n.Index); Walk(v, n.Index)
case *SliceExpr: case *SliceExpr:
Walk(v, n.X); Walk(v, n.X)
Walk(v, n.Index); Walk(v, n.Index)
Walk(v, n.End); Walk(v, n.End)
case *TypeAssertExpr: case *TypeAssertExpr:
Walk(v, n.X); Walk(v, n.X)
Walk(v, n.Type); Walk(v, n.Type)
case *CallExpr: case *CallExpr:
Walk(v, n.Fun); Walk(v, n.Fun)
Walk(v, n.Args); Walk(v, n.Args)
case *StarExpr: case *StarExpr:
Walk(v, n.X) Walk(v, n.X)
...@@ -129,31 +129,31 @@ func Walk(v Visitor, node interface{}) { ...@@ -129,31 +129,31 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.X) Walk(v, n.X)
case *BinaryExpr: case *BinaryExpr:
Walk(v, n.X); Walk(v, n.X)
Walk(v, n.Y); Walk(v, n.Y)
case *KeyValueExpr: case *KeyValueExpr:
Walk(v, n.Key); Walk(v, n.Key)
Walk(v, n.Value); Walk(v, n.Value)
// Types // Types
case *ArrayType: case *ArrayType:
Walk(v, n.Len); Walk(v, n.Len)
Walk(v, n.Elt); Walk(v, n.Elt)
case *StructType: case *StructType:
Walk(v, n.Fields) Walk(v, n.Fields)
case *FuncType: case *FuncType:
Walk(v, n.Params); Walk(v, n.Params)
Walk(v, n.Results); Walk(v, n.Results)
case *InterfaceType: case *InterfaceType:
Walk(v, n.Methods) Walk(v, n.Methods)
case *MapType: case *MapType:
Walk(v, n.Key); Walk(v, n.Key)
Walk(v, n.Value); Walk(v, n.Value)
case *ChanType: case *ChanType:
Walk(v, n.Value) Walk(v, n.Value)
...@@ -169,8 +169,8 @@ func Walk(v Visitor, node interface{}) { ...@@ -169,8 +169,8 @@ func Walk(v Visitor, node interface{}) {
// nothing to do // nothing to do
case *LabeledStmt: case *LabeledStmt:
walkIdent(v, n.Label); walkIdent(v, n.Label)
Walk(v, n.Stmt); Walk(v, n.Stmt)
case *ExprStmt: case *ExprStmt:
Walk(v, n.X) Walk(v, n.X)
...@@ -179,8 +179,8 @@ func Walk(v Visitor, node interface{}) { ...@@ -179,8 +179,8 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.X) Walk(v, n.X)
case *AssignStmt: case *AssignStmt:
Walk(v, n.Lhs); Walk(v, n.Lhs)
Walk(v, n.Rhs); Walk(v, n.Rhs)
case *GoStmt: case *GoStmt:
if n.Call != nil { if n.Call != nil {
...@@ -202,99 +202,99 @@ func Walk(v Visitor, node interface{}) { ...@@ -202,99 +202,99 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.List) Walk(v, n.List)
case *IfStmt: case *IfStmt:
Walk(v, n.Init); Walk(v, n.Init)
Walk(v, n.Cond); Walk(v, n.Cond)
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body)
Walk(v, n.Else); Walk(v, n.Else)
case *CaseClause: case *CaseClause:
Walk(v, n.Values); Walk(v, n.Values)
Walk(v, n.Body); Walk(v, n.Body)
case *SwitchStmt: case *SwitchStmt:
Walk(v, n.Init); Walk(v, n.Init)
Walk(v, n.Tag); Walk(v, n.Tag)
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body)
case *TypeCaseClause: case *TypeCaseClause:
Walk(v, n.Types); Walk(v, n.Types)
Walk(v, n.Body); Walk(v, n.Body)
case *TypeSwitchStmt: case *TypeSwitchStmt:
Walk(v, n.Init); Walk(v, n.Init)
Walk(v, n.Assign); Walk(v, n.Assign)
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body)
case *CommClause: case *CommClause:
Walk(v, n.Lhs); Walk(v, n.Lhs)
Walk(v, n.Rhs); Walk(v, n.Rhs)
Walk(v, n.Body); Walk(v, n.Body)
case *SelectStmt: case *SelectStmt:
walkBlockStmt(v, n.Body) walkBlockStmt(v, n.Body)
case *ForStmt: case *ForStmt:
Walk(v, n.Init); Walk(v, n.Init)
Walk(v, n.Cond); Walk(v, n.Cond)
Walk(v, n.Post); Walk(v, n.Post)
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body)
case *RangeStmt: case *RangeStmt:
Walk(v, n.Key); Walk(v, n.Key)
Walk(v, n.Value); Walk(v, n.Value)
Walk(v, n.X); Walk(v, n.X)
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body)
// Declarations // Declarations
case *ImportSpec: case *ImportSpec:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc)
walkIdent(v, n.Name); walkIdent(v, n.Name)
for _, x := range n.Path { for _, x := range n.Path {
Walk(v, x) Walk(v, x)
} }
walkCommentGroup(v, n.Comment); walkCommentGroup(v, n.Comment)
case *ValueSpec: case *ValueSpec:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc)
Walk(v, n.Names); Walk(v, n.Names)
Walk(v, n.Type); Walk(v, n.Type)
Walk(v, n.Values); Walk(v, n.Values)
walkCommentGroup(v, n.Comment); walkCommentGroup(v, n.Comment)
case *TypeSpec: case *TypeSpec:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc)
walkIdent(v, n.Name); walkIdent(v, n.Name)
Walk(v, n.Type); Walk(v, n.Type)
walkCommentGroup(v, n.Comment); walkCommentGroup(v, n.Comment)
case *BadDecl: case *BadDecl:
// nothing to do // nothing to do
case *GenDecl: case *GenDecl:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc)
for _, s := range n.Specs { for _, s := range n.Specs {
Walk(v, s) Walk(v, s)
} }
case *FuncDecl: case *FuncDecl:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc)
if n.Recv != nil { if n.Recv != nil {
Walk(v, n.Recv) Walk(v, n.Recv)
} }
walkIdent(v, n.Name); walkIdent(v, n.Name)
if n.Type != nil { if n.Type != nil {
Walk(v, n.Type) Walk(v, n.Type)
} }
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body)
// Files and packages // Files and packages
case *File: case *File:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc)
walkIdent(v, n.Name); walkIdent(v, n.Name)
for _, d := range n.Decls { for _, d := range n.Decls {
Walk(v, d) Walk(v, d)
} }
walkCommentGroup(v, n.Comments); walkCommentGroup(v, n.Comments)
case *Package: case *Package:
for _, f := range n.Files { for _, f := range n.Files {
...@@ -322,9 +322,9 @@ func Walk(v Visitor, node interface{}) { ...@@ -322,9 +322,9 @@ func Walk(v Visitor, node interface{}) {
} }
default: default:
fmt.Printf("ast.Walk: unexpected type %T", n); fmt.Printf("ast.Walk: unexpected type %T", n)
panic(); panic()
} }
v.Visit(nil); v.Visit(nil)
} }
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
package doc package doc
import ( import (
"go/ast"; "go/ast"
"io"; "io"
"strings"; "strings"
"template"; // for htmlEscape "template" // for htmlEscape
) )
// Comment extraction // Comment extraction
...@@ -21,12 +21,12 @@ func CommentText(comment *ast.CommentGroup) string { ...@@ -21,12 +21,12 @@ func CommentText(comment *ast.CommentGroup) string {
if comment == nil { if comment == nil {
return "" return ""
} }
comments := make([]string, len(comment.List)); comments := make([]string, len(comment.List))
for i, c := range comment.List { for i, c := range comment.List {
comments[i] = string(c.Text) comments[i] = string(c.Text)
} }
lines := make([]string, 0, 20); lines := make([]string, 0, 20)
for _, c := range comments { for _, c := range comments {
// Remove comment markers. // Remove comment markers.
// The parser has given us exactly the comment text. // The parser has given us exactly the comment text.
...@@ -34,7 +34,7 @@ func CommentText(comment *ast.CommentGroup) string { ...@@ -34,7 +34,7 @@ func CommentText(comment *ast.CommentGroup) string {
case n >= 4 && c[0:2] == "/*" && c[n-2:n] == "*/": case n >= 4 && c[0:2] == "/*" && c[n-2:n] == "*/":
c = c[2 : n-2] c = c[2 : n-2]
case n >= 2 && c[0:2] == "//": case n >= 2 && c[0:2] == "//":
c = c[2:n]; c = c[2:n]
// Remove leading space after //, if there is one. // Remove leading space after //, if there is one.
if len(c) > 0 && c[0] == ' ' { if len(c) > 0 && c[0] == ' ' {
c = c[1:] c = c[1:]
...@@ -42,61 +42,61 @@ func CommentText(comment *ast.CommentGroup) string { ...@@ -42,61 +42,61 @@ func CommentText(comment *ast.CommentGroup) string {
} }
// Split on newlines. // Split on newlines.
cl := strings.Split(c, "\n", 0); cl := strings.Split(c, "\n", 0)
// Walk lines, stripping trailing white space and adding to list. // Walk lines, stripping trailing white space and adding to list.
for _, l := range cl { for _, l := range cl {
// Strip trailing white space // Strip trailing white space
m := len(l); m := len(l)
for m > 0 && (l[m-1] == ' ' || l[m-1] == '\n' || l[m-1] == '\t' || l[m-1] == '\r') { for m > 0 && (l[m-1] == ' ' || l[m-1] == '\n' || l[m-1] == '\t' || l[m-1] == '\r') {
m-- m--
} }
l = l[0:m]; l = l[0:m]
// Add to list. // Add to list.
n := len(lines); n := len(lines)
if n+1 >= cap(lines) { if n+1 >= cap(lines) {
newlines := make([]string, n, 2*cap(lines)); newlines := make([]string, n, 2*cap(lines))
for k := range newlines { for k := range newlines {
newlines[k] = lines[k] newlines[k] = lines[k]
} }
lines = newlines; lines = newlines
} }
lines = lines[0 : n+1]; lines = lines[0 : n+1]
lines[n] = l; lines[n] = l
} }
} }
// Remove leading blank lines; convert runs of // Remove leading blank lines; convert runs of
// interior blank lines to a single blank line. // interior blank lines to a single blank line.
n := 0; n := 0
for _, line := range lines { for _, line := range lines {
if line != "" || n > 0 && lines[n-1] != "" { if line != "" || n > 0 && lines[n-1] != "" {
lines[n] = line; lines[n] = line
n++; n++
} }
} }
lines = lines[0:n]; lines = lines[0:n]
// Add final "" entry to get trailing newline from Join. // Add final "" entry to get trailing newline from Join.
// The original loop always leaves room for one more. // The original loop always leaves room for one more.
if n > 0 && lines[n-1] != "" { if n > 0 && lines[n-1] != "" {
lines = lines[0 : n+1]; lines = lines[0 : n+1]
lines[n] = ""; lines[n] = ""
} }
return strings.Join(lines, "\n"); return strings.Join(lines, "\n")
} }
// Split bytes into lines. // Split bytes into lines.
func split(text []byte) [][]byte { func split(text []byte) [][]byte {
// count lines // count lines
n := 0; n := 0
last := 0; last := 0
for i, c := range text { for i, c := range text {
if c == '\n' { if c == '\n' {
last = i + 1; last = i + 1
n++; n++
} }
} }
if last < len(text) { if last < len(text) {
...@@ -104,76 +104,76 @@ func split(text []byte) [][]byte { ...@@ -104,76 +104,76 @@ func split(text []byte) [][]byte {
} }
// split // split
out := make([][]byte, n); out := make([][]byte, n)
last = 0; last = 0
n = 0; n = 0
for i, c := range text { for i, c := range text {
if c == '\n' { if c == '\n' {
out[n] = text[last : i+1]; out[n] = text[last : i+1]
last = i + 1; last = i + 1
n++; n++
} }
} }
if last < len(text) { if last < len(text) {
out[n] = text[last:] out[n] = text[last:]
} }
return out; return out
} }
var ( var (
ldquo = strings.Bytes("&ldquo;"); ldquo = strings.Bytes("&ldquo;")
rdquo = strings.Bytes("&rdquo;"); rdquo = strings.Bytes("&rdquo;")
) )
// Escape comment text for HTML. // Escape comment text for HTML.
// Also, turn `` into &ldquo; and '' into &rdquo;. // Also, turn `` into &ldquo; and '' into &rdquo;.
func commentEscape(w io.Writer, s []byte) { func commentEscape(w io.Writer, s []byte) {
last := 0; last := 0
for i := 0; i < len(s)-1; i++ { for i := 0; i < len(s)-1; i++ {
if s[i] == s[i+1] && (s[i] == '`' || s[i] == '\'') { if s[i] == s[i+1] && (s[i] == '`' || s[i] == '\'') {
template.HTMLEscape(w, s[last:i]); template.HTMLEscape(w, s[last:i])
last = i + 2; last = i + 2
switch s[i] { switch s[i] {
case '`': case '`':
w.Write(ldquo) w.Write(ldquo)
case '\'': case '\'':
w.Write(rdquo) w.Write(rdquo)
} }
i++; // loop will add one more i++ // loop will add one more
} }
} }
template.HTMLEscape(w, s[last:]); template.HTMLEscape(w, s[last:])
} }
var ( var (
html_p = strings.Bytes("<p>\n"); html_p = strings.Bytes("<p>\n")
html_endp = strings.Bytes("</p>\n"); html_endp = strings.Bytes("</p>\n")
html_pre = strings.Bytes("<pre>"); html_pre = strings.Bytes("<pre>")
html_endpre = strings.Bytes("</pre>\n"); html_endpre = strings.Bytes("</pre>\n")
) )
func indentLen(s []byte) int { func indentLen(s []byte) int {
i := 0; i := 0
for i < len(s) && (s[i] == ' ' || s[i] == '\t') { for i < len(s) && (s[i] == ' ' || s[i] == '\t') {
i++ i++
} }
return i; return i
} }
func isBlank(s []byte) bool { return len(s) == 0 || (len(s) == 1 && s[0] == '\n') } func isBlank(s []byte) bool { return len(s) == 0 || (len(s) == 1 && s[0] == '\n') }
func commonPrefix(a, b []byte) []byte { func commonPrefix(a, b []byte) []byte {
i := 0; i := 0
for i < len(a) && i < len(b) && a[i] == b[i] { for i < len(a) && i < len(b) && a[i] == b[i] {
i++ i++
} }
return a[0:i]; return a[0:i]
} }
...@@ -183,13 +183,13 @@ func unindent(block [][]byte) { ...@@ -183,13 +183,13 @@ func unindent(block [][]byte) {
} }
// compute maximum common white prefix // compute maximum common white prefix
prefix := block[0][0:indentLen(block[0])]; prefix := block[0][0:indentLen(block[0])]
for _, line := range block { for _, line := range block {
if !isBlank(line) { if !isBlank(line) {
prefix = commonPrefix(prefix, line[0:indentLen(line)]) prefix = commonPrefix(prefix, line[0:indentLen(line)])
} }
} }
n := len(prefix); n := len(prefix)
// remove // remove
for i, line := range block { for i, line := range block {
...@@ -212,37 +212,37 @@ func unindent(block [][]byte) { ...@@ -212,37 +212,37 @@ func unindent(block [][]byte) {
// TODO(rsc): I'd like to pass in an array of variable names []string // TODO(rsc): I'd like to pass in an array of variable names []string
// and then italicize those strings when they appear as words. // and then italicize those strings when they appear as words.
func ToHTML(w io.Writer, s []byte) { func ToHTML(w io.Writer, s []byte) {
inpara := false; inpara := false
close := func() { close := func() {
if inpara { if inpara {
w.Write(html_endp); w.Write(html_endp)
inpara = false; inpara = false
} }
}; }
open := func() { open := func() {
if !inpara { if !inpara {
w.Write(html_p); w.Write(html_p)
inpara = true; inpara = true
} }
}; }
lines := split(s); lines := split(s)
unindent(lines); unindent(lines)
for i := 0; i < len(lines); { for i := 0; i < len(lines); {
line := lines[i]; line := lines[i]
if isBlank(line) { if isBlank(line) {
// close paragraph // close paragraph
close(); close()
i++; i++
continue; continue
} }
if indentLen(line) > 0 { if indentLen(line) > 0 {
// close paragraph // close paragraph
close(); close()
// count indented or blank lines // count indented or blank lines
j := i + 1; j := i + 1
for j < len(lines) && (isBlank(lines[j]) || indentLen(lines[j]) > 0) { for j < len(lines) && (isBlank(lines[j]) || indentLen(lines[j]) > 0) {
j++ j++
} }
...@@ -250,25 +250,25 @@ func ToHTML(w io.Writer, s []byte) { ...@@ -250,25 +250,25 @@ func ToHTML(w io.Writer, s []byte) {
for j > i && isBlank(lines[j-1]) { for j > i && isBlank(lines[j-1]) {
j-- j--
} }
block := lines[i:j]; block := lines[i:j]
i = j; i = j
unindent(block); unindent(block)
// put those lines in a pre block. // put those lines in a pre block.
// they don't get the nice text formatting, // they don't get the nice text formatting,
// just html escaping // just html escaping
w.Write(html_pre); w.Write(html_pre)
for _, line := range block { for _, line := range block {
template.HTMLEscape(w, line) template.HTMLEscape(w, line)
} }
w.Write(html_endpre); w.Write(html_endpre)
continue; continue
} }
// open paragraph // open paragraph
open(); open()
commentEscape(w, lines[i]); commentEscape(w, lines[i])
i++; i++
} }
close(); close()
} }
This diff is collapsed.
...@@ -7,15 +7,15 @@ ...@@ -7,15 +7,15 @@
package parser package parser
import ( import (
"bytes"; "bytes"
"fmt"; "fmt"
"go/ast"; "go/ast"
"go/scanner"; "go/scanner"
"io"; "io"
"io/ioutil"; "io/ioutil"
"os"; "os"
pathutil "path"; pathutil "path"
"strings"; "strings"
) )
...@@ -36,18 +36,18 @@ func readSource(filename string, src interface{}) ([]byte, os.Error) { ...@@ -36,18 +36,18 @@ func readSource(filename string, src interface{}) ([]byte, os.Error) {
return s.Bytes(), nil return s.Bytes(), nil
} }
case io.Reader: case io.Reader:
var buf bytes.Buffer; var buf bytes.Buffer
_, err := io.Copy(&buf, s); _, err := io.Copy(&buf, s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return buf.Bytes(), nil; return buf.Bytes(), nil
default: default:
return nil, os.ErrorString("invalid source") return nil, os.ErrorString("invalid source")
} }
} }
return ioutil.ReadFile(filename); return ioutil.ReadFile(filename)
} }
...@@ -57,14 +57,14 @@ func readSource(filename string, src interface{}) ([]byte, os.Error) { ...@@ -57,14 +57,14 @@ func readSource(filename string, src interface{}) ([]byte, os.Error) {
// may be nil or contain a partial AST. // may be nil or contain a partial AST.
// //
func ParseExpr(filename string, src interface{}) (ast.Expr, os.Error) { func ParseExpr(filename string, src interface{}) (ast.Expr, os.Error) {
data, err := readSource(filename, src); data, err := readSource(filename, src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var p parser; var p parser
p.init(filename, data, 0); p.init(filename, data, 0)
return p.parseExpr(), p.GetError(scanner.Sorted); return p.parseExpr(), p.GetError(scanner.Sorted)
} }
...@@ -74,14 +74,14 @@ func ParseExpr(filename string, src interface{}) (ast.Expr, os.Error) { ...@@ -74,14 +74,14 @@ func ParseExpr(filename string, src interface{}) (ast.Expr, os.Error) {
// list may be nil or contain partial ASTs. // list may be nil or contain partial ASTs.
// //
func ParseStmtList(filename string, src interface{}) ([]ast.Stmt, os.Error) { func ParseStmtList(filename string, src interface{}) ([]ast.Stmt, os.Error) {
data, err := readSource(filename, src); data, err := readSource(filename, src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var p parser; var p parser
p.init(filename, data, 0); p.init(filename, data, 0)
return p.parseStmtList(), p.GetError(scanner.Sorted); return p.parseStmtList(), p.GetError(scanner.Sorted)
} }
...@@ -91,14 +91,14 @@ func ParseStmtList(filename string, src interface{}) ([]ast.Stmt, os.Error) { ...@@ -91,14 +91,14 @@ func ParseStmtList(filename string, src interface{}) ([]ast.Stmt, os.Error) {
// list may be nil or contain partial ASTs. // list may be nil or contain partial ASTs.
// //
func ParseDeclList(filename string, src interface{}) ([]ast.Decl, os.Error) { func ParseDeclList(filename string, src interface{}) ([]ast.Decl, os.Error) {
data, err := readSource(filename, src); data, err := readSource(filename, src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var p parser; var p parser
p.init(filename, data, 0); p.init(filename, data, 0)
return p.parseDeclList(), p.GetError(scanner.Sorted); return p.parseDeclList(), p.GetError(scanner.Sorted)
} }
...@@ -121,14 +121,14 @@ func ParseDeclList(filename string, src interface{}) ([]ast.Decl, os.Error) { ...@@ -121,14 +121,14 @@ func ParseDeclList(filename string, src interface{}) ([]ast.Decl, os.Error) {
// 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(filename string, src interface{}, mode uint) (*ast.File, os.Error) { func ParseFile(filename string, src interface{}, mode uint) (*ast.File, os.Error) {
data, err := readSource(filename, src); data, err := readSource(filename, src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var p parser; var p parser
p.init(filename, data, mode); p.init(filename, data, mode)
return p.parseFile(), p.GetError(scanner.NoMultiples); return p.parseFile(), p.GetError(scanner.NoMultiples)
} }
...@@ -139,13 +139,13 @@ func ParseFile(filename string, src interface{}, mode uint) (*ast.File, os.Error ...@@ -139,13 +139,13 @@ func ParseFile(filename string, src interface{}, mode uint) (*ast.File, os.Error
// flags that control the amount of source text parsed are ignored. // flags that control the amount of source text parsed are ignored.
// //
func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) { func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) {
src, err := ioutil.ReadFile(filename); src, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if pkgname != "" { if pkgname != "" {
prog, err := ParseFile(filename, src, PackageClauseOnly); prog, err := ParseFile(filename, src, PackageClauseOnly)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -155,7 +155,7 @@ func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) { ...@@ -155,7 +155,7 @@ func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) {
} }
// ignore flags that control partial parsing // ignore flags that control partial parsing
return ParseFile(filename, src, mode&^(PackageClauseOnly|ImportsOnly)); return ParseFile(filename, src, mode&^(PackageClauseOnly|ImportsOnly))
} }
...@@ -167,27 +167,27 @@ func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) { ...@@ -167,27 +167,27 @@ func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) {
// Mode flags that control the amount of source text parsed are ignored. // Mode flags that control the amount of source text parsed are ignored.
// //
func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Package, os.Error) { func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Package, os.Error) {
fd, err := os.Open(path, os.O_RDONLY, 0); fd, err := os.Open(path, os.O_RDONLY, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer fd.Close(); defer fd.Close()
list, err := fd.Readdir(-1); list, err := fd.Readdir(-1)
if err != nil { if err != nil {
return nil, err return nil, err
} }
name := ""; name := ""
files := make(map[string]*ast.File); files := make(map[string]*ast.File)
for i := 0; i < len(list); i++ { for i := 0; i < len(list); i++ {
entry := &list[i]; entry := &list[i]
if filter == nil || filter(entry) { if filter == nil || filter(entry) {
src, err := ParsePkgFile(name, pathutil.Join(path, entry.Name), mode); src, err := ParsePkgFile(name, pathutil.Join(path, entry.Name), mode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
files[entry.Name] = src; files[entry.Name] = src
if name == "" { if name == "" {
name = src.Name.Value name = src.Name.Value
} }
...@@ -198,5 +198,5 @@ func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Packa ...@@ -198,5 +198,5 @@ func ParsePackage(path string, filter func(*os.Dir) bool, mode uint) (*ast.Packa
return nil, os.NewError(path + ": no package found") return nil, os.NewError(path + ": no package found")
} }
return &ast.Package{name, path, files}, nil; return &ast.Package{name, path, files}, nil
} }
This diff is collapsed.
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
package parser package parser
import ( import (
"os"; "os"
"testing"; "testing"
) )
...@@ -20,7 +20,7 @@ var illegalInputs = []interface{}{ ...@@ -20,7 +20,7 @@ var illegalInputs = []interface{}{
func TestParseIllegalInputs(t *testing.T) { func TestParseIllegalInputs(t *testing.T) {
for _, src := range illegalInputs { for _, src := range illegalInputs {
_, err := ParseFile("", src, 0); _, err := ParseFile("", src, 0)
if err == nil { if err == nil {
t.Errorf("ParseFile(%v) should have failed", src) t.Errorf("ParseFile(%v) should have failed", src)
} }
...@@ -37,7 +37,7 @@ var validPrograms = []interface{}{ ...@@ -37,7 +37,7 @@ var validPrograms = []interface{}{
func TestParseValidPrograms(t *testing.T) { func TestParseValidPrograms(t *testing.T) {
for _, src := range validPrograms { for _, src := range validPrograms {
_, err := ParseFile("", src, 0); _, err := ParseFile("", src, 0)
if err != nil { if err != nil {
t.Errorf("ParseFile(%q): %v", src, err) t.Errorf("ParseFile(%q): %v", src, err)
} }
...@@ -53,7 +53,7 @@ var validFiles = []string{ ...@@ -53,7 +53,7 @@ var validFiles = []string{
func TestParse3(t *testing.T) { func TestParse3(t *testing.T) {
for _, filename := range validFiles { for _, filename := range validFiles {
_, err := ParseFile(filename, nil, 0); _, err := ParseFile(filename, nil, 0)
if err != nil { if err != nil {
t.Errorf("ParseFile(%s): %v", filename, err) t.Errorf("ParseFile(%s): %v", filename, err)
} }
...@@ -69,16 +69,16 @@ func nameFilter(filename string) bool { ...@@ -69,16 +69,16 @@ func nameFilter(filename string) bool {
default: default:
return false return false
} }
return true; return true
} }
func dirFilter(d *os.Dir) bool { return nameFilter(d.Name) } func dirFilter(d *os.Dir) bool { return nameFilter(d.Name) }
func TestParse4(t *testing.T) { func TestParse4(t *testing.T) {
path := "."; path := "."
pkg, err := ParsePackage(path, dirFilter, 0); pkg, err := ParsePackage(path, dirFilter, 0)
if err != nil { if err != nil {
t.Fatalf("ParsePackage(%s): %v", path, err) t.Fatalf("ParsePackage(%s): %v", path, err)
} }
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -10,14 +10,14 @@ import "io" ...@@ -10,14 +10,14 @@ import "io"
// The Write method never returns an error. // The Write method never returns an error.
// Sum returns the bytes of integer hash codes in big-endian order. // Sum returns the bytes of integer hash codes in big-endian order.
type Hash interface { type Hash interface {
io.Writer; io.Writer
Sum() []byte; Sum() []byte
Reset(); Reset()
Size() int; // number of bytes Sum returns Size() int // number of bytes Sum returns
} }
// Hash32 is the common interface implemented by all 32-bit hash functions. // Hash32 is the common interface implemented by all 32-bit hash functions.
type Hash32 interface { type Hash32 interface {
Hash; Hash
Sum32() uint32; Sum32() uint32
} }
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -9,8 +9,8 @@ import "fmt" ...@@ -9,8 +9,8 @@ import "fmt"
// ParseError aggregates information about a JSON parse error. It is // ParseError aggregates information about a JSON parse error. It is
// compatible with the os.Error interface. // compatible with the os.Error interface.
type ParseError struct { type ParseError struct {
Index int; // A byte index in JSON string where the error occurred Index int // A byte index in JSON string where the error occurred
Token string; // An offending token Token string // An offending token
} }
// Produce a string representation of this ParseError. // Produce a string representation of this ParseError.
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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