Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 15 | package parser |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strconv" |
| 20 | "strings" |
| 21 | "text/scanner" |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 22 | "unicode" |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 25 | var noPos scanner.Position |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 26 | |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 27 | type printer struct { |
| 28 | defs []Definition |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 29 | comments []*CommentGroup |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 30 | |
| 31 | curComment int |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 32 | |
| 33 | pos scanner.Position |
| 34 | |
| 35 | pendingSpace bool |
| 36 | pendingNewline int |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 37 | |
| 38 | output []byte |
| 39 | |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 40 | indentList []int |
| 41 | wsBuf []byte |
| 42 | |
Colin Cross | 6b6735d | 2016-07-12 16:03:08 -0700 | [diff] [blame] | 43 | skippedComments []*CommentGroup |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func newPrinter(file *File) *printer { |
| 47 | return &printer{ |
| 48 | defs: file.Defs, |
| 49 | comments: file.Comments, |
| 50 | indentList: []int{0}, |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 51 | |
| 52 | // pendingNewLine is initialized to -1 to eat initial spaces if the first token is a comment |
| 53 | pendingNewline: -1, |
| 54 | |
| 55 | pos: scanner.Position{ |
| 56 | Line: 1, |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 57 | }, |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | func Print(file *File) ([]byte, error) { |
| 62 | p := newPrinter(file) |
| 63 | |
| 64 | for _, def := range p.defs { |
| 65 | p.printDef(def) |
| 66 | } |
| 67 | p.flush() |
| 68 | return p.output, nil |
| 69 | } |
| 70 | |
Jeff Gaston | a1dae80 | 2017-06-28 14:35:14 -0700 | [diff] [blame] | 71 | func PrintExpression(expression Expression) ([]byte, error) { |
| 72 | dummyFile := &File{} |
| 73 | p := newPrinter(dummyFile) |
| 74 | p.printExpression(expression) |
| 75 | p.flush() |
| 76 | return p.output, nil |
| 77 | } |
| 78 | |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 79 | func (p *printer) Print() ([]byte, error) { |
| 80 | for _, def := range p.defs { |
| 81 | p.printDef(def) |
| 82 | } |
| 83 | p.flush() |
| 84 | return p.output, nil |
| 85 | } |
| 86 | |
| 87 | func (p *printer) printDef(def Definition) { |
| 88 | if assignment, ok := def.(*Assignment); ok { |
| 89 | p.printAssignment(assignment) |
| 90 | } else if module, ok := def.(*Module); ok { |
| 91 | p.printModule(module) |
| 92 | } else { |
| 93 | panic("Unknown definition") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func (p *printer) printAssignment(assignment *Assignment) { |
Colin Cross | c32c479 | 2016-06-09 15:52:30 -0700 | [diff] [blame] | 98 | p.printToken(assignment.Name, assignment.NamePos) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 99 | p.requestSpace() |
Colin Cross | b3d0b8d | 2016-06-09 17:03:57 -0700 | [diff] [blame] | 100 | p.printToken(assignment.Assigner, assignment.EqualsPos) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 101 | p.requestSpace() |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 102 | p.printExpression(assignment.OrigValue) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 103 | p.requestNewline() |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | func (p *printer) printModule(module *Module) { |
Colin Cross | c32c479 | 2016-06-09 15:52:30 -0700 | [diff] [blame] | 107 | p.printToken(module.Type, module.TypePos) |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 108 | p.printMap(&module.Map) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 109 | p.requestDoubleNewline() |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 112 | func (p *printer) printExpression(value Expression) { |
| 113 | switch v := value.(type) { |
| 114 | case *Variable: |
| 115 | p.printToken(v.Name, v.NamePos) |
| 116 | case *Operator: |
| 117 | p.printOperator(v) |
| 118 | case *Bool: |
| 119 | var s string |
| 120 | if v.Value { |
| 121 | s = "true" |
| 122 | } else { |
| 123 | s = "false" |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 124 | } |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 125 | p.printToken(s, v.LiteralPos) |
Nan Zhang | f586544 | 2017-11-01 14:03:28 -0700 | [diff] [blame] | 126 | case *Int64: |
| 127 | p.printToken(strconv.FormatInt(v.Value, 10), v.LiteralPos) |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 128 | case *String: |
| 129 | p.printToken(strconv.Quote(v.Value), v.LiteralPos) |
| 130 | case *List: |
| 131 | p.printList(v.Values, v.LBracePos, v.RBracePos) |
| 132 | case *Map: |
| 133 | p.printMap(v) |
| 134 | default: |
Colin Cross | ba5bf88 | 2016-07-15 16:40:37 -0700 | [diff] [blame] | 135 | panic(fmt.Errorf("bad property type: %s", value.Type())) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 139 | func (p *printer) printList(list []Expression, pos, endPos scanner.Position) { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 140 | p.requestSpace() |
| 141 | p.printToken("[", pos) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 142 | if len(list) > 1 || pos.Line != endPos.Line { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 143 | p.requestNewline() |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 144 | p.indent(p.curIndent() + 4) |
| 145 | for _, value := range list { |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 146 | p.printExpression(value) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 147 | p.printToken(",", noPos) |
| 148 | p.requestNewline() |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 149 | } |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 150 | p.unindent(endPos) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 151 | } else { |
| 152 | for _, value := range list { |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 153 | p.printExpression(value) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 156 | p.printToken("]", endPos) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 159 | func (p *printer) printMap(m *Map) { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 160 | p.requestSpace() |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 161 | p.printToken("{", m.LBracePos) |
| 162 | if len(m.Properties) > 0 || m.LBracePos.Line != m.RBracePos.Line { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 163 | p.requestNewline() |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 164 | p.indent(p.curIndent() + 4) |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 165 | for _, prop := range m.Properties { |
Colin Cross | cb7b9ad | 2015-03-02 15:32:36 -0800 | [diff] [blame] | 166 | p.printProperty(prop) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 167 | p.printToken(",", noPos) |
| 168 | p.requestNewline() |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 169 | } |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 170 | p.unindent(m.RBracePos) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 171 | } |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 172 | p.printToken("}", m.RBracePos) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 175 | func (p *printer) printOperator(operator *Operator) { |
Dan Willemsen | d2c8162 | 2018-05-07 16:11:42 -0700 | [diff] [blame] | 176 | p.printOperatorInternal(operator, true) |
| 177 | } |
| 178 | |
| 179 | func (p *printer) printOperatorInternal(operator *Operator, allowIndent bool) { |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 180 | p.printExpression(operator.Args[0]) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 181 | p.requestSpace() |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 182 | p.printToken(string(operator.Operator), operator.OperatorPos) |
Dan Willemsen | d2c8162 | 2018-05-07 16:11:42 -0700 | [diff] [blame] | 183 | |
| 184 | indented := false |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 185 | if operator.Args[0].End().Line == operator.Args[1].Pos().Line { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 186 | p.requestSpace() |
| 187 | } else { |
Dan Willemsen | d2c8162 | 2018-05-07 16:11:42 -0700 | [diff] [blame] | 188 | if allowIndent { |
| 189 | indented = true |
| 190 | p.indent(p.curIndent() + 4) |
| 191 | } |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 192 | p.requestNewline() |
| 193 | } |
Dan Willemsen | d2c8162 | 2018-05-07 16:11:42 -0700 | [diff] [blame] | 194 | |
| 195 | if op, isOp := operator.Args[1].(*Operator); isOp { |
| 196 | p.printOperatorInternal(op, false) |
| 197 | } else { |
| 198 | p.printExpression(operator.Args[1]) |
| 199 | } |
| 200 | |
| 201 | if indented { |
| 202 | p.unindent(p.pos) |
| 203 | } |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Colin Cross | cb7b9ad | 2015-03-02 15:32:36 -0800 | [diff] [blame] | 206 | func (p *printer) printProperty(property *Property) { |
Colin Cross | c32c479 | 2016-06-09 15:52:30 -0700 | [diff] [blame] | 207 | p.printToken(property.Name, property.NamePos) |
Colin Cross | b3d0b8d | 2016-06-09 17:03:57 -0700 | [diff] [blame] | 208 | p.printToken(":", property.ColonPos) |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 209 | p.requestSpace() |
Colin Cross | e32cc80 | 2016-06-07 12:28:16 -0700 | [diff] [blame] | 210 | p.printExpression(property.Value) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Print a single token, including any necessary comments or whitespace between |
| 214 | // this token and the previously printed token |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 215 | func (p *printer) printToken(s string, pos scanner.Position) { |
| 216 | newline := p.pendingNewline != 0 |
| 217 | |
| 218 | if pos == noPos { |
| 219 | pos = p.pos |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 220 | } |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 221 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 222 | if newline { |
| 223 | p.printEndOfLineCommentsBefore(pos) |
| 224 | p.requestNewlinesForPos(pos) |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 227 | p.printInLineCommentsBefore(pos) |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 228 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 229 | p.flushSpace() |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 230 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 231 | p.output = append(p.output, s...) |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 232 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 233 | p.pos = pos |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 236 | // Print any in-line (single line /* */) comments that appear _before_ pos |
| 237 | func (p *printer) printInLineCommentsBefore(pos scanner.Position) { |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 238 | for p.curComment < len(p.comments) && p.comments[p.curComment].Pos().Offset < pos.Offset { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 239 | c := p.comments[p.curComment] |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 240 | if c.Comments[0].Comment[0][0:2] == "//" || len(c.Comments[0].Comment) > 1 { |
Colin Cross | 6b6735d | 2016-07-12 16:03:08 -0700 | [diff] [blame] | 241 | p.skippedComments = append(p.skippedComments, c) |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 242 | } else { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 243 | p.printComment(c) |
| 244 | p.requestSpace() |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 245 | } |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 246 | p.curComment++ |
| 247 | } |
| 248 | } |
| 249 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 250 | // Print any comments, including end of line comments, that appear _before_ the line specified |
| 251 | // by pos |
| 252 | func (p *printer) printEndOfLineCommentsBefore(pos scanner.Position) { |
Colin Cross | 6b6735d | 2016-07-12 16:03:08 -0700 | [diff] [blame] | 253 | if len(p.skippedComments) > 0 { |
| 254 | for _, c := range p.skippedComments { |
| 255 | p.printComment(c) |
| 256 | } |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 257 | p._requestNewline() |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 258 | p.skippedComments = nil |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 259 | } |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 260 | for p.curComment < len(p.comments) && p.comments[p.curComment].Pos().Line < pos.Line { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 261 | c := p.comments[p.curComment] |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 262 | p.printComment(c) |
| 263 | p._requestNewline() |
| 264 | p.curComment++ |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 265 | } |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 268 | // Compare the line numbers of the previous and current positions to determine whether extra |
| 269 | // newlines should be inserted. A second newline is allowed anywhere requestNewline() is called. |
| 270 | func (p *printer) requestNewlinesForPos(pos scanner.Position) bool { |
| 271 | if pos.Line > p.pos.Line { |
| 272 | p._requestNewline() |
| 273 | if pos.Line > p.pos.Line+1 { |
| 274 | p.pendingNewline = 2 |
| 275 | } |
| 276 | return true |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 279 | return false |
| 280 | } |
| 281 | |
| 282 | func (p *printer) requestSpace() { |
| 283 | p.pendingSpace = true |
| 284 | } |
| 285 | |
| 286 | // Ask for a newline to be inserted before the next token, but do not insert any comments. Used |
| 287 | // by the comment printers. |
| 288 | func (p *printer) _requestNewline() { |
| 289 | if p.pendingNewline == 0 { |
| 290 | p.pendingNewline = 1 |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Ask for a newline to be inserted before the next token. Also inserts any end-of line comments |
| 295 | // for the current line |
| 296 | func (p *printer) requestNewline() { |
| 297 | pos := p.pos |
| 298 | pos.Line++ |
| 299 | p.printEndOfLineCommentsBefore(pos) |
| 300 | p._requestNewline() |
| 301 | } |
| 302 | |
| 303 | // Ask for two newlines to be inserted before the next token. Also inserts any end-of line comments |
| 304 | // for the current line |
| 305 | func (p *printer) requestDoubleNewline() { |
| 306 | p.requestNewline() |
| 307 | p.pendingNewline = 2 |
| 308 | } |
| 309 | |
| 310 | // Flush any pending whitespace, ignoring pending spaces if there is a pending newline |
| 311 | func (p *printer) flushSpace() { |
| 312 | if p.pendingNewline == 1 { |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 313 | p.output = append(p.output, '\n') |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 314 | p.pad(p.curIndent()) |
| 315 | } else if p.pendingNewline == 2 { |
| 316 | p.output = append(p.output, "\n\n"...) |
| 317 | p.pad(p.curIndent()) |
| 318 | } else if p.pendingSpace == true && p.pendingNewline != -1 { |
| 319 | p.output = append(p.output, ' ') |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 322 | p.pendingSpace = false |
| 323 | p.pendingNewline = 0 |
| 324 | } |
| 325 | |
| 326 | // Print a single comment, which may be a multi-line comment |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 327 | func (p *printer) printComment(cg *CommentGroup) { |
| 328 | for _, comment := range cg.Comments { |
| 329 | if !p.requestNewlinesForPos(comment.Pos()) { |
| 330 | p.requestSpace() |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 331 | } |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 332 | for i, line := range comment.Comment { |
| 333 | line = strings.TrimRightFunc(line, unicode.IsSpace) |
| 334 | p.flushSpace() |
| 335 | if i != 0 { |
| 336 | lineIndent := strings.IndexFunc(line, func(r rune) bool { return !unicode.IsSpace(r) }) |
| 337 | lineIndent = max(lineIndent, p.curIndent()) |
| 338 | p.pad(lineIndent - p.curIndent()) |
| 339 | } |
| 340 | p.output = append(p.output, strings.TrimSpace(line)...) |
| 341 | if i < len(comment.Comment)-1 { |
| 342 | p._requestNewline() |
| 343 | } |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 344 | } |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 345 | p.pos = comment.End() |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 346 | } |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // Print any comments that occur after the last token, and a trailing newline |
| 350 | func (p *printer) flush() { |
Colin Cross | 6b6735d | 2016-07-12 16:03:08 -0700 | [diff] [blame] | 351 | for _, c := range p.skippedComments { |
| 352 | if !p.requestNewlinesForPos(c.Pos()) { |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 353 | p.requestSpace() |
| 354 | } |
Colin Cross | 6b6735d | 2016-07-12 16:03:08 -0700 | [diff] [blame] | 355 | p.printComment(c) |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 356 | } |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 357 | for p.curComment < len(p.comments) { |
Colin Cross | 1e73794 | 2016-06-10 17:27:12 -0700 | [diff] [blame] | 358 | p.printComment(p.comments[p.curComment]) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 359 | p.curComment++ |
| 360 | } |
| 361 | p.output = append(p.output, '\n') |
| 362 | } |
| 363 | |
| 364 | // Print whitespace to pad from column l to column max |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 365 | func (p *printer) pad(l int) { |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 366 | if l > len(p.wsBuf) { |
| 367 | p.wsBuf = make([]byte, l) |
| 368 | for i := range p.wsBuf { |
| 369 | p.wsBuf[i] = ' ' |
| 370 | } |
| 371 | } |
| 372 | p.output = append(p.output, p.wsBuf[0:l]...) |
| 373 | } |
| 374 | |
| 375 | func (p *printer) indent(i int) { |
| 376 | p.indentList = append(p.indentList, i) |
| 377 | } |
| 378 | |
Colin Cross | 85398a9 | 2015-03-19 14:49:38 -0700 | [diff] [blame] | 379 | func (p *printer) unindent(pos scanner.Position) { |
| 380 | p.printEndOfLineCommentsBefore(pos) |
Colin Cross | 5ad47f4 | 2015-01-08 19:35:10 -0800 | [diff] [blame] | 381 | p.indentList = p.indentList[0 : len(p.indentList)-1] |
| 382 | } |
| 383 | |
| 384 | func (p *printer) curIndent() int { |
| 385 | return p.indentList[len(p.indentList)-1] |
| 386 | } |
Colin Cross | 9a103c0 | 2015-01-30 16:38:08 -0800 | [diff] [blame] | 387 | |
| 388 | func max(a, b int) int { |
| 389 | if a > b { |
| 390 | return a |
| 391 | } else { |
| 392 | return b |
| 393 | } |
| 394 | } |