Commit 0ea9dd81 authored by Robert Griesemer's avatar Robert Griesemer

gofmt the last outstanding files in src/pkg

- added a list of issues to printer/nodes.go

R=rsc
http://go/go-review/1024002
parent 39fd52d3
...@@ -623,7 +623,7 @@ const ( ...@@ -623,7 +623,7 @@ const (
string */ string */
DF_SYMBOLIC DynFlag = 0x0002; /* Indicates "symbolic" linking. */ DF_SYMBOLIC DynFlag = 0x0002; /* Indicates "symbolic" linking. */
DF_TEXTREL DynFlag = 0x0004; /* Indicates there may be relocations in DF_TEXTREL DynFlag = 0x0004; /* Indicates there may be relocations in
non-writable segments. */ non-writable segments. */
DF_BIND_NOW DynFlag = 0x0008; /* Indicates that the dynamic linker should DF_BIND_NOW DynFlag = 0x0008; /* Indicates that the dynamic linker should
process all relocations for the object process all relocations for the object
containing this entry before transferring containing this entry before transferring
...@@ -1394,7 +1394,7 @@ type Header32 struct { ...@@ -1394,7 +1394,7 @@ type Header32 struct {
*/ */
type Section32 struct { type Section32 struct {
Name uint32; /* Section name (index into the Name uint32; /* Section name (index into the
section header string table). */ section header string table). */
Type uint32; /* Section type. */ Type uint32; /* Section type. */
Flags uint32; /* Section flags. */ Flags uint32; /* Section flags. */
Addr uint32; /* Address in memory image. */ Addr uint32; /* Address in memory image. */
...@@ -1508,7 +1508,7 @@ type Header64 struct { ...@@ -1508,7 +1508,7 @@ type Header64 struct {
type Section64 struct { type Section64 struct {
Name uint32; /* Section name (index into the Name uint32; /* Section name (index into the
section header string table). */ section header string table). */
Type uint32; /* Section type. */ Type uint32; /* Section type. */
Flags uint64; /* Section flags. */ Flags uint64; /* Section flags. */
Addr uint64; /* Address in memory image. */ Addr uint64; /* Address in memory image. */
......
...@@ -232,40 +232,40 @@ type Formatter func(state *State, value interface{}, ruleName string) bool ...@@ -232,40 +232,40 @@ type Formatter func(state *State, value interface{}, ruleName string) bool
// A FormatterMap is a set of custom formatters. // A FormatterMap is a set of custom formatters.
// It maps a rule name to a formatter function. // It maps a rule name to a formatter function.
// //
type FormatterMap map [string] Formatter; type FormatterMap map[string]Formatter
// A parsed format expression is built from the following nodes. // A parsed format expression is built from the following nodes.
// //
type ( type (
expr interface {}; expr interface{};
alternatives []expr; // x | y | z alternatives []expr; // x | y | z
sequence []expr; // x y z sequence []expr; // x y z
literal [][]byte; // a list of string segments, possibly starting with '%' literal [][]byte; // a list of string segments, possibly starting with '%'
field struct { field struct {
fieldName string; // including "@", "*" fieldName string; // including "@", "*"
ruleName string; // "" if no rule name specified ruleName string; // "" if no rule name specified
}; };
group struct { group struct {
indent, body expr; // (indent >> body) indent, body expr; // (indent >> body)
}; };
option struct { option struct {
body expr; // [body] body expr; // [body]
}; };
repetition struct { repetition struct {
body, separator expr; // {body / separator} body, separator expr; // {body / separator}
}; };
custom struct { custom struct {
ruleName string; ruleName string;
fun Formatter fun Formatter;
}; };
) )
...@@ -273,7 +273,7 @@ type ( ...@@ -273,7 +273,7 @@ type (
// A Format is the result of parsing a format specification. // A Format is the result of parsing a format specification.
// The format may be applied repeatedly to format values. // The format may be applied repeatedly to format values.
// //
type Format map [string] expr; type Format map[string]expr
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -290,7 +290,7 @@ type Format map [string] expr; ...@@ -290,7 +290,7 @@ type Format map [string] expr;
// the receiver, and thus can be very light-weight. // the receiver, and thus can be very light-weight.
// //
type Environment interface { type Environment interface {
Copy() Environment Copy() Environment;
} }
...@@ -298,15 +298,15 @@ type Environment interface { ...@@ -298,15 +298,15 @@ type Environment interface {
// It is provided as argument to custom formatters. // It is provided as argument to custom formatters.
// //
type State struct { type State struct {
fmt Format; // format in use fmt Format; // format in use
env Environment; // user-supplied environment env Environment; // user-supplied environment
errors chan os.Error; // not chan *Error (errors <- nil would be wrong!) errors chan os.Error; // not chan *Error (errors <- nil would be wrong!)
hasOutput bool; // true after the first literal has been written hasOutput bool; // true after the first literal has been written
indent bytes.Buffer; // current indentation indent bytes.Buffer; // current indentation
output bytes.Buffer; // format output output bytes.Buffer; // format output
linePos token.Position; // position of line beginning (Column == 0) linePos token.Position; // position of line beginning (Column == 0)
default_ expr; // possibly nil default_ expr; // possibly nil
separator expr; // possibly nil separator expr; // possibly nil
} }
...@@ -365,22 +365,22 @@ func (s *State) Write(data []byte) (int, os.Error) { ...@@ -365,22 +365,22 @@ func (s *State) Write(data []byte) (int, os.Error) {
// write text segment and indentation // write text segment and indentation
n1, _ := s.output.Write(data[i0 : i+1]); n1, _ := s.output.Write(data[i0 : i+1]);
n2, _ := s.output.Write(s.indent.Bytes()); n2, _ := s.output.Write(s.indent.Bytes());
n += n1 + n2; n += n1+n2;
i0 = i + 1; i0 = i+1;
s.linePos.Offset = s.output.Len(); s.linePos.Offset = s.output.Len();
s.linePos.Line++; s.linePos.Line++;
} }
} }
n3, _ := s.output.Write(data[i0 : len(data)]); n3, _ := s.output.Write(data[i0:len(data)]);
return n + n3, nil; return n+n3, nil;
} }
type checkpoint struct { type checkpoint struct {
env Environment; env Environment;
hasOutput bool; hasOutput bool;
outputLen int; outputLen int;
linePos token.Position; linePos token.Position;
} }
...@@ -489,13 +489,13 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool { ...@@ -489,13 +489,13 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
if s.hasOutput { if s.hasOutput {
// not the first literal // not the first literal
if s.separator != nil { if s.separator != nil {
sep := s.separator; // save current separator sep := s.separator; // save current separator
s.separator = nil; // and disable it (avoid recursion) s.separator = nil; // and disable it (avoid recursion)
mark := s.save(); mark := s.save();
if !s.eval(sep, value, index) { if !s.eval(sep, value, index) {
s.restore(mark); s.restore(mark);
} }
s.separator = sep; // enable it again s.separator = sep; // enable it again
} }
} }
s.hasOutput = true; s.hasOutput = true;
...@@ -505,7 +505,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool { ...@@ -505,7 +505,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
// segment contains a %-format at the beginning // segment contains a %-format at the beginning
if lit[1] == '%' { if lit[1] == '%' {
// "%%" is printed as a single "%" // "%%" is printed as a single "%"
s.Write(lit[1 : len(lit)]); s.Write(lit[1:len(lit)]);
} else { } else {
// use s instead of s.output to get indentation right // use s instead of s.output to get indentation right
fmt.Fprintf(s, string(lit), value.Interface()); fmt.Fprintf(s, string(lit), value.Interface());
...@@ -515,7 +515,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool { ...@@ -515,7 +515,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
s.Write(lit); s.Write(lit);
} }
} }
return true; // a literal never evaluates to nil return true; // a literal never evaluates to nil
case *field: case *field:
// determine field value // determine field value
...@@ -580,7 +580,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool { ...@@ -580,7 +580,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
ruleName := t.ruleName; ruleName := t.ruleName;
if ruleName == "" { if ruleName == "" {
// no alternate rule name, value type determines rule // no alternate rule name, value type determines rule
ruleName = typename(value.Type()) ruleName = typename(value.Type());
} }
fexpr = s.getFormat(ruleName); fexpr = s.getFormat(ruleName);
...@@ -620,10 +620,10 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool { ...@@ -620,10 +620,10 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
// evaluate the body and append the result to the state's output // evaluate the body and append the result to the state's output
// buffer unless the result is nil // buffer unless the result is nil
mark := s.save(); mark := s.save();
if !s.eval(t.body, value, 0) { // TODO is 0 index correct? if !s.eval(t.body, value, 0) { // TODO is 0 index correct?
s.restore(mark); s.restore(mark);
} }
return true; // an option never evaluates to nil return true; // an option never evaluates to nil
case *repetition: case *repetition:
// evaluate the body and append the result to the state's output // evaluate the body and append the result to the state's output
...@@ -643,7 +643,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool { ...@@ -643,7 +643,7 @@ func (s *State) eval(fexpr expr, value reflect.Value, index int) bool {
break; break;
} }
} }
return true; // a repetition never evaluates to nil return true; // a repetition never evaluates to nil
case *custom: case *custom:
// invoke the custom formatter to obtain the result // invoke the custom formatter to obtain the result
...@@ -680,14 +680,14 @@ func (f Format) Eval(env Environment, args ...) ([]byte, os.Error) { ...@@ -680,14 +680,14 @@ func (f Format) Eval(env Environment, args ...) ([]byte, os.Error) {
for i := 0; i < value.NumField(); i++ { for i := 0; i < value.NumField(); i++ {
fld := value.Field(i); fld := value.Field(i);
mark := s.save(); mark := s.save();
if !s.eval(s.getFormat(typename(fld.Type())), fld, 0) { // TODO is 0 index correct? if !s.eval(s.getFormat(typename(fld.Type())), fld, 0) { // TODO is 0 index correct?
s.restore(mark); s.restore(mark);
} }
} }
errors <- nil; // no errors errors <- nil; // no errors
}(); }();
err := <- errors; err := <-errors;
return s.output.Bytes(), err; return s.output.Bytes(), err;
} }
......
...@@ -23,14 +23,13 @@ func parse(t *testing.T, form string, fmap FormatterMap) Format { ...@@ -23,14 +23,13 @@ func parse(t *testing.T, form string, fmap FormatterMap) Format {
func verify(t *testing.T, f Format, expected string, args ...) { func verify(t *testing.T, f Format, expected string, args ...) {
if f == nil { if f == nil {
return; // allow other tests to run return; // allow other tests to run
} }
result := f.Sprint(args); result := f.Sprint(args);
if result != expected { if result != expected {
t.Errorf( t.Errorf(
"result : `%s`\nexpected: `%s`\n\n", "result : `%s`\nexpected: `%s`\n\n",
result, expected result, expected);
)
} }
} }
...@@ -62,9 +61,9 @@ func formatter(s *State, value interface{}, rule_name string) bool { ...@@ -62,9 +61,9 @@ func formatter(s *State, value interface{}, rule_name string) bool {
func TestCustomFormatters(t *testing.T) { func TestCustomFormatters(t *testing.T) {
fmap0 := FormatterMap{ "/": formatter }; fmap0 := FormatterMap{"/": formatter};
fmap1 := FormatterMap{ "int": formatter, "blank": formatter, "nil": formatter }; fmap1 := FormatterMap{"int": formatter, "blank": formatter, "nil": formatter};
fmap2 := FormatterMap{ "testing.T": formatter }; fmap2 := FormatterMap{"testing.T": formatter};
f := parse(t, `int=`, fmap0); f := parse(t, `int=`, fmap0);
verify(t, f, ``, 1, 2, 3); verify(t, f, ``, 1, 2, 3);
...@@ -97,14 +96,13 @@ func TestCustomFormatters(t *testing.T) { ...@@ -97,14 +96,13 @@ func TestCustomFormatters(t *testing.T) {
func check(t *testing.T, form, expected string, args ...) { func check(t *testing.T, form, expected string, args ...) {
f := parse(t, form, nil); f := parse(t, form, nil);
if f == nil { if f == nil {
return; // allow other tests to run return; // allow other tests to run
} }
result := f.Sprint(args); result := f.Sprint(args);
if result != expected { if result != expected {
t.Errorf( t.Errorf(
"format : %s\nresult : `%s`\nexpected: `%s`\n\n", "format : %s\nresult : `%s`\nexpected: `%s`\n\n",
form, result, expected form, result, expected);
)
} }
} }
...@@ -164,7 +162,7 @@ func TestChanTypes(t *testing.T) { ...@@ -164,7 +162,7 @@ func TestChanTypes(t *testing.T) {
check(t, `chan="chan"`, `chan`, c0); check(t, `chan="chan"`, `chan`, c0);
c1 := make(chan int); c1 := make(chan int);
go func(){ c1 <- 42 }(); go func() { c1 <- 42 }();
check(t, `chan="chan"`, `chan`, c1); check(t, `chan="chan"`, `chan`, c1);
// check(t, `chan=*`, `42`, c1); // reflection support for chans incomplete // check(t, `chan=*`, `42`, c1); // reflection support for chans incomplete
} }
...@@ -174,14 +172,14 @@ func TestFuncTypes(t *testing.T) { ...@@ -174,14 +172,14 @@ func TestFuncTypes(t *testing.T) {
var f0 func() int; var f0 func() int;
check(t, `func="func"`, `func`, f0); check(t, `func="func"`, `func`, f0);
f1 := func() int { return 42; }; f1 := func() int { return 42 };
check(t, `func="func"`, `func`, f1); check(t, `func="func"`, `func`, f1);
// check(t, `func=*`, `42`, f1); // reflection support for funcs incomplete // check(t, `func=*`, `42`, f1); // reflection support for funcs incomplete
} }
func TestInterfaceTypes(t *testing.T) { func TestInterfaceTypes(t *testing.T) {
var i0 interface{}; var i0 interface{}
check(t, `interface="interface"`, `interface`, i0); check(t, `interface="interface"`, `interface`, i0);
i0 = "foo"; i0 = "foo";
...@@ -234,8 +232,7 @@ type T1 struct { ...@@ -234,8 +232,7 @@ type T1 struct {
a int; a int;
} }
const F1 = const F1 = `datafmt "datafmt";`
`datafmt "datafmt";`
`int = "%d";` `int = "%d";`
`datafmt.T1 = "<" a ">";` `datafmt.T1 = "<" a ">";`
...@@ -248,21 +245,19 @@ func TestStruct1(t *testing.T) { ...@@ -248,21 +245,19 @@ func TestStruct1(t *testing.T) {
// Formatting of a struct with an optional field (ptr) // Formatting of a struct with an optional field (ptr)
type T2 struct { type T2 struct {
s string; s string;
p *T1; p *T1;
} }
const F2a = const F2a = F1 +
F1 +
`string = "%s";` `string = "%s";`
`ptr = *;` `ptr = *;`
`datafmt.T2 = s ["-" p "-"];` `datafmt.T2 = s ["-" p "-"];`
const F2b = const F2b = F1 +
F1 +
`string = "%s";` `string = "%s";`
`ptr = *;` `ptr = *;`
`datafmt.T2 = s ("-" p "-" | "empty");`; `datafmt.T2 = s ("-" p "-" | "empty");`
func TestStruct2(t *testing.T) { func TestStruct2(t *testing.T) {
check(t, F2a, "foo", T2{"foo", nil}); check(t, F2a, "foo", T2{"foo", nil});
...@@ -275,18 +270,16 @@ func TestStruct2(t *testing.T) { ...@@ -275,18 +270,16 @@ func TestStruct2(t *testing.T) {
// Formatting of a struct with a repetitive field (slice) // Formatting of a struct with a repetitive field (slice)
type T3 struct { type T3 struct {
s string; s string;
a []int; a []int;
} }
const F3a = const F3a = `datafmt "datafmt";`
`datafmt "datafmt";`
`default = "%v";` `default = "%v";`
`array = *;` `array = *;`
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
const F3b = const F3b = `datafmt "datafmt";`
`datafmt "datafmt";`
`int = "%d";` `int = "%d";`
`string = "%s";` `string = "%s";`
`array = *;` `array = *;`
...@@ -306,12 +299,11 @@ func TestStruct3(t *testing.T) { ...@@ -306,12 +299,11 @@ func TestStruct3(t *testing.T) {
// Formatting of a struct with alternative field // Formatting of a struct with alternative field
type T4 struct { type T4 struct {
x *int; x *int;
a []int; a []int;
} }
const F4a = const F4a = `datafmt "datafmt";`
`datafmt "datafmt";`
`int = "%d";` `int = "%d";`
`ptr = *;` `ptr = *;`
`array = *;` `array = *;`
...@@ -319,8 +311,7 @@ const F4a = ...@@ -319,8 +311,7 @@ const F4a =
`empty = *:nil;` `empty = *:nil;`
`datafmt.T4 = "<" (x:empty x | "-") ">" ` `datafmt.T4 = "<" (x:empty x | "-") ">" `
const F4b = const F4b = `datafmt "datafmt";`
`datafmt "datafmt";`
`int = "%d";` `int = "%d";`
`ptr = *;` `ptr = *;`
`array = *;` `array = *;`
...@@ -341,12 +332,11 @@ func TestStruct4(t *testing.T) { ...@@ -341,12 +332,11 @@ func TestStruct4(t *testing.T) {
// Formatting a struct (documentation example) // Formatting a struct (documentation example)
type Point struct { type Point struct {
name string; name string;
x, y int; x, y int;
} }
const FPoint = const FPoint = `datafmt "datafmt";`
`datafmt "datafmt";`
`int = "%d";` `int = "%d";`
`hexInt = "0x%x";` `hexInt = "0x%x";`
`string = "---%s---";` `string = "---%s---";`
...@@ -361,8 +351,7 @@ func TestStructPoint(t *testing.T) { ...@@ -361,8 +351,7 @@ func TestStructPoint(t *testing.T) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Formatting a slice (documentation example) // Formatting a slice (documentation example)
const FSlice = const FSlice = `int = "%b";`
`int = "%b";`
`array = { * / ", " }` `array = { * / ", " }`
func TestSlice(t *testing.T) { func TestSlice(t *testing.T) {
......
This diff is collapsed.
...@@ -24,6 +24,15 @@ const ( ...@@ -24,6 +24,15 @@ const (
) )
// Other outstanding formatting issues:
// - replacement of expression spacing algorithm with rsc's algorithm
// - support for one-line composite types (e.g. structs) as composite literals types
// - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
// when the comment spans multiple lines
// - formatting of expression lists; especially for string lists (stringListMode)
// - blank after { and before } in one-line composite literals probably looks better
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Common AST nodes. // Common AST nodes.
......
This diff is collapsed.
...@@ -16,7 +16,8 @@ const testInput = ` ...@@ -16,7 +16,8 @@ const testInput = `
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<body xmlns:foo="ns1" xmlns="ns2" xmlns:tag="ns3" ` "\r\n\t" ` > <body xmlns:foo="ns1" xmlns="ns2" xmlns:tag="ns3" `
"\r\n\t" ` >
<hello lang="en">World &lt;&gt;&apos;&quot; &#x767d;&#40300;翔</hello> <hello lang="en">World &lt;&gt;&apos;&quot; &#x767d;&#40300;翔</hello>
<goodbye /> <goodbye />
<outer foo:attr="value" xmlns:tag="ns4"> <outer foo:attr="value" xmlns:tag="ns4">
...@@ -90,8 +91,8 @@ var cookedTokens = []Token{ ...@@ -90,8 +91,8 @@ var cookedTokens = []Token{
} }
type stringReader struct { type stringReader struct {
s string; s string;
off int; off int;
} }
func (r *stringReader) Read(b []byte) (n int, err os.Error) { func (r *stringReader) Read(b []byte) (n int, err os.Error) {
...@@ -116,7 +117,7 @@ func (r *stringReader) ReadByte() (b byte, err os.Error) { ...@@ -116,7 +117,7 @@ func (r *stringReader) ReadByte() (b byte, err os.Error) {
} }
func StringReader(s string) io.Reader { func StringReader(s string) io.Reader {
return &stringReader{s, 0} return &stringReader{s, 0};
} }
func TestRawToken(t *testing.T) { func TestRawToken(t *testing.T) {
...@@ -146,4 +147,3 @@ func TestToken(t *testing.T) { ...@@ -146,4 +147,3 @@ func TestToken(t *testing.T) {
} }
} }
} }
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