OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // The printer package implements printing of AST nodes. | 5 // The printer package implements printing of AST nodes. |
6 package printer | 6 package printer |
7 | 7 |
8 import ( | 8 import ( |
9 "bytes" | 9 "bytes" |
10 "fmt" | 10 "fmt" |
(...skipping 22 matching lines...) Expand all Loading... |
33 newline = whiteSpace('\n') | 33 newline = whiteSpace('\n') |
34 formfeed = whiteSpace('\f') | 34 formfeed = whiteSpace('\f') |
35 indent = whiteSpace('>') | 35 indent = whiteSpace('>') |
36 unindent = whiteSpace('<') | 36 unindent = whiteSpace('<') |
37 ) | 37 ) |
38 | 38 |
39 | 39 |
40 var ( | 40 var ( |
41 esc = []byte{tabwriter.Escape} | 41 esc = []byte{tabwriter.Escape} |
42 htab = []byte{'\t'} | 42 htab = []byte{'\t'} |
43 » htabs = [...]byte{'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t'} | 43 » htabs = []byte("\t\t\t\t\t\t\t\t") |
44 » newlines = [...]byte{'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'} //
more than maxNewlines | 44 » newlines = []byte("\n\n\n\n\n\n\n\n") // more than maxNewlines |
45 » formfeeds = [...]byte{'\f', '\f', '\f', '\f', '\f', '\f', '\f', '\f'} //
more than maxNewlines | 45 » formfeeds = []byte("\f\f\f\f\f\f\f\f") // more than maxNewlines |
46 | 46 |
47 esc_quot = []byte(""") // shorter than """ | 47 esc_quot = []byte(""") // shorter than """ |
48 esc_apos = []byte("'") // shorter than "'" | 48 esc_apos = []byte("'") // shorter than "'" |
49 esc_amp = []byte("&") | 49 esc_amp = []byte("&") |
50 esc_lt = []byte("<") | 50 esc_lt = []byte("<") |
51 esc_gt = []byte(">") | 51 esc_gt = []byte(">") |
52 ) | 52 ) |
53 | 53 |
54 | 54 |
55 // Use noPos when a position is needed but not known. | 55 // Use noPos when a position is needed but not known. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 p.pos.Offset += i + 1 - i0 | 140 p.pos.Offset += i + 1 - i0 |
141 p.pos.Line++ | 141 p.pos.Line++ |
142 p.pos.Column = 1 | 142 p.pos.Column = 1 |
143 | 143 |
144 if !p.escape { | 144 if !p.escape { |
145 // write indentation | 145 // write indentation |
146 // use "hard" htabs - indentation columns | 146 // use "hard" htabs - indentation columns |
147 // must not be discarded by the tabwriter | 147 // must not be discarded by the tabwriter |
148 j := p.indent | 148 j := p.indent |
149 for ; j > len(htabs); j -= len(htabs) { | 149 for ; j > len(htabs); j -= len(htabs) { |
150 » » » » » p.write0(&htabs) | 150 » » » » » p.write0(htabs) |
151 } | 151 } |
152 p.write0(htabs[0:j]) | 152 p.write0(htabs[0:j]) |
153 | 153 |
154 // update p.pos | 154 // update p.pos |
155 p.pos.Offset += p.indent | 155 p.pos.Offset += p.indent |
156 p.pos.Column += p.indent | 156 p.pos.Column += p.indent |
157 } | 157 } |
158 | 158 |
159 // next segment start | 159 // next segment start |
160 i0 = i + 1 | 160 i0 = i + 1 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 if bytes.HasSuffix(prefix, suffix) { | 519 if bytes.HasSuffix(prefix, suffix) { |
520 prefix = prefix[0 : len(prefix)-len(suffix)] | 520 prefix = prefix[0 : len(prefix)-len(suffix)] |
521 } | 521 } |
522 } | 522 } |
523 } | 523 } |
524 | 524 |
525 // Handle last line: If it only contains a closing */, align it | 525 // Handle last line: If it only contains a closing */, align it |
526 // with the opening /*, otherwise align the text with the other | 526 // with the opening /*, otherwise align the text with the other |
527 // lines. | 527 // lines. |
528 last := lines[len(lines)-1] | 528 last := lines[len(lines)-1] |
529 » closing := []byte{'*', '/'} | 529 » closing := []byte("*/") |
530 i := bytes.Index(last, closing) | 530 i := bytes.Index(last, closing) |
531 if isBlank(last[0:i]) { | 531 if isBlank(last[0:i]) { |
532 // last line only contains closing */ | 532 // last line only contains closing */ |
533 var sep []byte | 533 var sep []byte |
534 if lineOfStars { | 534 if lineOfStars { |
535 // insert an aligning blank | 535 // insert an aligning blank |
536 sep = []byte{' '} | 536 sep = []byte{' '} |
537 } | 537 } |
538 lines[len(lines)-1] = bytes.Join([][]byte{prefix, closing}, sep) | 538 lines[len(lines)-1] = bytes.Join([][]byte{prefix, closing}, sep) |
539 } else { | 539 } else { |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1008 } | 1008 } |
1009 | 1009 |
1010 | 1010 |
1011 // Fprint "pretty-prints" an AST node to output. | 1011 // Fprint "pretty-prints" an AST node to output. |
1012 // It calls Config.Fprint with default settings. | 1012 // It calls Config.Fprint with default settings. |
1013 // | 1013 // |
1014 func Fprint(output io.Writer, node interface{}) os.Error { | 1014 func Fprint(output io.Writer, node interface{}) os.Error { |
1015 _, err := (&Config{Tabwidth: 8}).Fprint(output, node) // don't care abou
t number of bytes written | 1015 _, err := (&Config{Tabwidth: 8}).Fprint(output, node) // don't care abou
t number of bytes written |
1016 return err | 1016 return err |
1017 } | 1017 } |
OLD | NEW |