1. npe
  2. printer

Commits

npe  committed 5ef4578

gofix

  • Participants
  • Parent commits 9ac882f
  • Branches default

Comments (0)

Files changed (2)

File print.go

View file
  • Ignore whitespace
 // A FieldFilter may be provided to Fprint to control the output.
 type FieldFilter func(name string, value reflect.Value) bool
 
-
 // NotNilFilter returns true for field values that are not nil;
 // it returns false otherwise.
 func NotNilFilter(_ string, value reflect.Value) bool {
 	v, ok := value.(interface {
 		IsNil() bool
 	})
-	return !ok || !v.IsNil() || value.Type() == reflect.Typeof(token.Position{})
+	return !ok || !v.IsNil() || value.Type() == reflect.TypeOf(token.Position{})
 }
 
-
 // Fprint prints the (sub-)tree starting at AST node x to w.
 //
 // A non-nil FieldFilter f may be provided to control the output:
 // struct fields for which f(fieldname, fieldvalue) is true are
 // are printed; all others are filtered from the output.
 //
-func Fprint(w io.Writer, x interface{}, f FieldFilter) (n int, err os.Error) {
+func Fprint(w io.Writer, x interface{}, f FieldFilter) (n int, err error) {
 	// setup printer
 	p := printer{
 		output: w,
 		p.printf("nil\n")
 		return
 	}
-	p.print(reflect.NewValue(x))
+	p.print(reflect.ValueOf(x))
 	p.printf("\n")
 
 	return
 }
 
-
 // Print prints x to standard output, skipping nil fields.
 // Print(x) is the same as Fprint(os.Stdout, x, NotNilFilter).
-func Print(x interface{}) (int, os.Error) {
+func Print(x interface{}) (int, error) {
 	return Fprint(os.Stdout, x, NotNilFilter)
 }
 
-
 type printer struct {
 	output  io.Writer
 	filter  FieldFilter
 	line    int                 // current line number
 }
 
-
 var indent = []byte("  ")
 
-func (p *printer) Write(data []byte) (n int, err os.Error) {
+func (p *printer) Write(data []byte) (n int, err error) {
 	var m int
 	for i, b := range data {
 		// invariant: data[0:n] has been written
 	return
 }
 
-
 // localError wraps locally caught os.Errors so we can distinguish
 // them from genuine panics which we don't want to return as errors.
 type localError struct {
-	err os.Error
+	err error
 }
 
-
 // printf is a convenience wrapper that takes care of print errors.
 func (p *printer) printf(format string, args ...interface{}) {
 	n, err := fmt.Fprintf(p, format, args...)
 	}
 }
 
-
 // Implementation note: Print is written for AST nodes but could be
 // used to print arbitrary data structures; such a version should
 // probably be in a different package.
 		return
 	}
 
-	switch v := x.(type) {
-	case *reflect.InterfaceValue:
+	switch v := x; v.Kind() {
+	case reflect.Interface:
 		p.print(v.Elem())
 
-	case *reflect.MapValue:
+	case reflect.Map:
 		p.printf("%v {\n", x.Type())
 		p.indent++
-		for _, key := range v.Keys() {
+		for _, key := range v.MapKeys() {
 			p.print(key)
 			p.printf(": ")
-			p.print(v.Elem(key))
+			p.print(v.MapIndex(key))
 		}
 		p.indent--
 		p.printf("}")
 
-	case *reflect.PtrValue:
+	case reflect.Ptr:
 		p.printf("&")
 		// type-checked ASTs may contain cycles - use ptrmap
 		// to keep track of objects that have been printed
 			p.print(v.Elem())
 		}
 
-	case *reflect.SliceValue:
+	case reflect.Slice:
 		if s, ok := v.Interface().([]byte); ok {
 			p.printf("%#v", s)
 			return
 		p.indent++
 		p.printf("%s{\n", v.Type())
 		for i, n := 0, v.Len(); i < n; i++ {
-			p.print(v.Elem(i))
+			p.print(v.Index(i))
 			p.printf(",\n")
 		}
 		p.indent--
 		p.printf("}")
 
-	case *reflect.StructValue:
-		if x.Type() == reflect.Typeof(token.Position{}) {
+	case reflect.Struct:
+		if x.Type() == reflect.TypeOf(token.Position{}) {
 			p.printf("token.Position{}")
 			return
 		}
 
 		p.printf("%v {\n", x.Type())
 		p.indent++
-		t := v.Type().(*reflect.StructType)
+		t := v.Type()
 		for i, n := 0, t.NumField(); i < n; i++ {
 			name := t.Field(i).Name
 			value := v.Field(i)

File printer.go

View file
  • Ignore whitespace
 	"os"
 	"path"
 	"runtime"
-	"tabwriter"
+	"text/tabwriter"
 )
 
-
 const debug = false // enable for debugging
 
 
 	unindent = whiteSpace('<')
 )
 
-
 var (
 	esc       = []byte{tabwriter.Escape}
 	htab      = []byte{'\t'}
 	esc_gt   = []byte("&gt;")
 )
 
-
 // Special positions
 var noPos token.Position // use noPos when a position is needed but not known
 var infinity = 1 << 30
 
-
 // Use ignoreMultiLine if the multiLine information is not important.
 var ignoreMultiLine = new(bool)
 
-
 type printer struct {
 	// Configuration (does not change after initialization)
 	output io.Writer
 	Config
 	fset   *token.FileSet
-	errors chan os.Error
+	errors chan error
 
 	// Current state
 	nesting int         // nesting level (0: top-level (package scope), >0: functions/decls.)
 	useNodeComments bool                // if not set, ignore lead and line comments of nodes
 }
 
-
 func (p *printer) init(output io.Writer, cfg *Config, fset *token.FileSet) {
 	p.output = output
 	p.Config = *cfg
 	p.fset = fset
-	p.errors = make(chan os.Error)
+	p.errors = make(chan error)
 	p.buffer = make([]whiteSpace, 0, 16) // whitespace sequences are short
 }
 
 	}
 }
 
-
 // nlines returns the adjusted number of linebreaks given the desired number
 // of breaks n such that min <= result <= max where max depends on the current
 // nesting level.
 	return n
 }
 
-
 // write0 writes raw (uninterpreted) data to p.output and handles errors.
 // write0 does not indent after newlines, and does not HTML-escape or update p.pos.
 //
 	}
 }
 
-
 // write interprets data and writes it to p.output. It inserts indentation
 // after a line break unless in a tabwriter escape sequence, and it HTML-
 // escapes characters if GenHTML is set. It updates p.pos as a side-effect.
 	p.pos.Column += d
 }
 
-
 func (p *printer) writeNewlines(n int, useFF bool) {
 	if n > 0 {
 		n = p.nlines(n, 0)
 	}
 }
 
-
 func (p *printer) writeTaggedItem(data []byte, tag HTMLTag) {
 	// write start tag, if any
 	// (no html-escaping and no p.pos update for tags - use write0)
 	}
 }
 
-
 // writeItem writes data at position pos. data is the text corresponding to
 // a single lexical token, but may also be comment text. pos is the actual
 // (or at least very accurately estimated) position of the data in the original
 	p.last = p.pos
 }
 
-
 // writeCommentPrefix writes the whitespace before a comment.
 // If there is any pending whitespace, it consumes as much of
 // it as is likely to help position the comment nicely.
 	}
 }
 
-
 func (p *printer) writeCommentLine(comment *ast.Comment, pos token.Position, line []byte) {
 	// line must pass through unchanged, bracket it with tabwriter.Escape
 	line = bytes.Join([][]byte{esc, line, esc}, nil)
 	p.writeItem(pos, line, tag)
 }
 
-
 // TODO(gri): Similar (but not quite identical) functionality for
 //            comment processing can be found in go/doc/comment.go.
 //            Perhaps this can be factored eventually.
 	return lines
 }
 
-
 func isBlank(s []byte) bool {
 	for _, b := range s {
 		if b > ' ' {
 	return true
 }
 
-
 func commonPrefix(a, b []byte) []byte {
 	i := 0
 	for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') {
 	return a[0:i]
 }
 
-
 func stripCommonPrefix(lines [][]byte) {
 	if len(lines) < 2 {
 		return // at most one line - nothing to do
 	}
 }
 
-
 func (p *printer) writeComment(comment *ast.Comment) {
 	text := comment.Text
 
 	}
 }
 
-
 // writeCommentSuffix writes a line break after a comment if indicated
 // and processes any leftover indentation information. If a line break
 // is needed, the kind of break (newline vs formfeed) depends on the
 	return
 }
 
-
 // intersperseComments consumes all comments that appear before the next token
 // tok and prints it together with the buffered whitespace (i.e., the whitespace
 // that needs to be written before the next token). A heuristic is used to mix
 	return false
 }
 
-
 // whiteWhitespace writes the first n whitespace entries.
 func (p *printer) writeWhitespace(n int) {
 	// write entries
 	p.buffer = p.buffer[0:i]
 }
 
-
 // ----------------------------------------------------------------------------
 // Printing interface
 
 	return
 }
 
-
 // print prints a list of "items" (roughly corresponding to syntactic
 // tokens, but also including whitespace and formatting information).
 // It is the only print function that should be called directly from
 	}
 }
 
-
 // commentBefore returns true iff the current comment occurs
 // before the next position in the source code.
 //
 	return p.cindex < len(p.comments) && p.fset.Position(p.comments[p.cindex].List[0].Pos()).Offset < next.Offset
 }
 
-
 // Flush prints any pending comments and whitespace occuring
 // textually before the position of the next token tok. Flush
 // returns true if a pending formfeed character was dropped
 	return
 }
 
-
 // ----------------------------------------------------------------------------
 // Trimmer
 
 	state  int
 }
 
-
 // trimmer is implemented as a state machine.
 // It can be in one of the following states:
 const (
 	inText
 )
 
-
 // Design note: It is tempting to eliminate extra blanks occuring in
 //              whitespace in this function as it could simplify some
 //              of the blanks logic in the node printing functions.
 //              However, this would mess up any formatting done by
 //              the tabwriter.
 
-func (p *trimmer) Write(data []byte) (n int, err os.Error) {
+func (p *trimmer) Write(data []byte) (n int, err error) {
 	m := 0 // if p.state != inSpace, data[m:n] is unwritten
 	var b byte
 	for n, b = range data {
 	return
 }
 
-
 // ----------------------------------------------------------------------------
 // Public interface
 
 	UseSpaces                  // use spaces instead of tabs for alignment
 )
 
-
 // An HTMLTag specifies a start and end tag.
 type HTMLTag struct {
 	Start, End string // empty if tags are absent
 }
 
-
 // A Styler specifies formatting of line tags and elementary Go words.
 // A format consists of text and a (possibly empty) surrounding HTML tag.
 //
 	Token(tok token.Token) ([]byte, HTMLTag)
 }
 
-
 // A Config node controls the output of Fprint.
 type Config struct {
 	Mode     uint   // default: 0
 	Styler   Styler // default: nil
 }
 
-
 // Fprint "pretty-prints" an AST node to output and returns the number
 // of bytes written and an error (if any) for a given configuration cfg.
 // The node type must be *ast.File, or assignment-compatible to ast.Expr,
 // ast.Decl, ast.Spec, or ast.Stmt.
 //
-func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) (int, os.Error) {
+func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) (int, error) {
 	// redirect output through a trimmer to eliminate trailing whitespace
 	// (Input to a tabwriter must be untrimmed since trailing tabs provide
 	// formatting information. The tabwriter could provide trimming
 	return p.written, err
 }
 
-
 // Fprint "pretty-prints" an AST node to output.
 // It calls Config.Fprint with default settings.
 //
-func Fprint(output io.Writer, fset *token.FileSet, node interface{}) os.Error {
+func Fprint(output io.Writer, fset *token.FileSet, node interface{}) error {
 	_, err := (&Config{Tabwidth: 8}).Fprint(output, fset, node) // don't care about number of bytes written
 	return err
 }