| Conditions | 83 |
| Total Lines | 262 |
| Code Lines | 186 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like text.*Decoder.parseNext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // Copyright 2018 The Go Authors. All rights reserved. |
||
| 98 | func (d *Decoder) parseNext(lastKind Kind) (Token, error) { |
||
| 99 | // Trim leading spaces. |
||
| 100 | d.consume(0) |
||
| 101 | isEOF := false |
||
| 102 | if len(d.in) == 0 { |
||
| 103 | isEOF = true |
||
| 104 | } |
||
| 105 | |||
| 106 | switch lastKind { |
||
| 107 | case EOF: |
||
| 108 | return d.consumeToken(EOF, 0, 0), nil |
||
| 109 | |||
| 110 | case bof: |
||
| 111 | // Start of top level message. Next token can be EOF or Name. |
||
| 112 | if isEOF { |
||
| 113 | return d.consumeToken(EOF, 0, 0), nil |
||
| 114 | } |
||
| 115 | return d.parseFieldName() |
||
| 116 | |||
| 117 | case Name: |
||
| 118 | // Next token can be MessageOpen, ListOpen or Scalar. |
||
| 119 | if isEOF { |
||
| 120 | return Token{}, ErrUnexpectedEOF |
||
| 121 | } |
||
| 122 | switch ch := d.in[0]; ch { |
||
| 123 | case '{', '<': |
||
| 124 | d.pushOpenStack(ch) |
||
| 125 | return d.consumeToken(MessageOpen, 1, 0), nil |
||
| 126 | case '[': |
||
| 127 | d.pushOpenStack(ch) |
||
| 128 | return d.consumeToken(ListOpen, 1, 0), nil |
||
| 129 | default: |
||
| 130 | return d.parseScalar() |
||
| 131 | } |
||
| 132 | |||
| 133 | case Scalar: |
||
| 134 | openKind, closeCh := d.currentOpenKind() |
||
| 135 | switch openKind { |
||
| 136 | case bof: |
||
| 137 | // Top level message. |
||
| 138 | // Next token can be EOF, comma, semicolon or Name. |
||
| 139 | if isEOF { |
||
| 140 | return d.consumeToken(EOF, 0, 0), nil |
||
| 141 | } |
||
| 142 | switch d.in[0] { |
||
| 143 | case ',': |
||
| 144 | return d.consumeToken(comma, 1, 0), nil |
||
| 145 | case ';': |
||
| 146 | return d.consumeToken(semicolon, 1, 0), nil |
||
| 147 | default: |
||
| 148 | return d.parseFieldName() |
||
| 149 | } |
||
| 150 | |||
| 151 | case MessageOpen: |
||
| 152 | // Next token can be MessageClose, comma, semicolon or Name. |
||
| 153 | if isEOF { |
||
| 154 | return Token{}, ErrUnexpectedEOF |
||
| 155 | } |
||
| 156 | switch ch := d.in[0]; ch { |
||
| 157 | case closeCh: |
||
| 158 | d.popOpenStack() |
||
| 159 | return d.consumeToken(MessageClose, 1, 0), nil |
||
| 160 | case otherCloseChar[closeCh]: |
||
| 161 | return Token{}, d.newSyntaxError(mismatchedFmt, ch) |
||
| 162 | case ',': |
||
| 163 | return d.consumeToken(comma, 1, 0), nil |
||
| 164 | case ';': |
||
| 165 | return d.consumeToken(semicolon, 1, 0), nil |
||
| 166 | default: |
||
| 167 | return d.parseFieldName() |
||
| 168 | } |
||
| 169 | |||
| 170 | case ListOpen: |
||
| 171 | // Next token can be ListClose or comma. |
||
| 172 | if isEOF { |
||
| 173 | return Token{}, ErrUnexpectedEOF |
||
| 174 | } |
||
| 175 | switch ch := d.in[0]; ch { |
||
| 176 | case ']': |
||
| 177 | d.popOpenStack() |
||
| 178 | return d.consumeToken(ListClose, 1, 0), nil |
||
| 179 | case ',': |
||
| 180 | return d.consumeToken(comma, 1, 0), nil |
||
| 181 | default: |
||
| 182 | return Token{}, d.newSyntaxError(unexpectedFmt, ch) |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | case MessageOpen: |
||
| 187 | // Next token can be MessageClose or Name. |
||
| 188 | if isEOF { |
||
| 189 | return Token{}, ErrUnexpectedEOF |
||
| 190 | } |
||
| 191 | _, closeCh := d.currentOpenKind() |
||
| 192 | switch ch := d.in[0]; ch { |
||
| 193 | case closeCh: |
||
| 194 | d.popOpenStack() |
||
| 195 | return d.consumeToken(MessageClose, 1, 0), nil |
||
| 196 | case otherCloseChar[closeCh]: |
||
| 197 | return Token{}, d.newSyntaxError(mismatchedFmt, ch) |
||
| 198 | default: |
||
| 199 | return d.parseFieldName() |
||
| 200 | } |
||
| 201 | |||
| 202 | case MessageClose: |
||
| 203 | openKind, closeCh := d.currentOpenKind() |
||
| 204 | switch openKind { |
||
| 205 | case bof: |
||
| 206 | // Top level message. |
||
| 207 | // Next token can be EOF, comma, semicolon or Name. |
||
| 208 | if isEOF { |
||
| 209 | return d.consumeToken(EOF, 0, 0), nil |
||
| 210 | } |
||
| 211 | switch ch := d.in[0]; ch { |
||
| 212 | case ',': |
||
| 213 | return d.consumeToken(comma, 1, 0), nil |
||
| 214 | case ';': |
||
| 215 | return d.consumeToken(semicolon, 1, 0), nil |
||
| 216 | default: |
||
| 217 | return d.parseFieldName() |
||
| 218 | } |
||
| 219 | |||
| 220 | case MessageOpen: |
||
| 221 | // Next token can be MessageClose, comma, semicolon or Name. |
||
| 222 | if isEOF { |
||
| 223 | return Token{}, ErrUnexpectedEOF |
||
| 224 | } |
||
| 225 | switch ch := d.in[0]; ch { |
||
| 226 | case closeCh: |
||
| 227 | d.popOpenStack() |
||
| 228 | return d.consumeToken(MessageClose, 1, 0), nil |
||
| 229 | case otherCloseChar[closeCh]: |
||
| 230 | return Token{}, d.newSyntaxError(mismatchedFmt, ch) |
||
| 231 | case ',': |
||
| 232 | return d.consumeToken(comma, 1, 0), nil |
||
| 233 | case ';': |
||
| 234 | return d.consumeToken(semicolon, 1, 0), nil |
||
| 235 | default: |
||
| 236 | return d.parseFieldName() |
||
| 237 | } |
||
| 238 | |||
| 239 | case ListOpen: |
||
| 240 | // Next token can be ListClose or comma |
||
| 241 | if isEOF { |
||
| 242 | return Token{}, ErrUnexpectedEOF |
||
| 243 | } |
||
| 244 | switch ch := d.in[0]; ch { |
||
| 245 | case closeCh: |
||
| 246 | d.popOpenStack() |
||
| 247 | return d.consumeToken(ListClose, 1, 0), nil |
||
| 248 | case ',': |
||
| 249 | return d.consumeToken(comma, 1, 0), nil |
||
| 250 | default: |
||
| 251 | return Token{}, d.newSyntaxError(unexpectedFmt, ch) |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | case ListOpen: |
||
| 256 | // Next token can be ListClose, MessageStart or Scalar. |
||
| 257 | if isEOF { |
||
| 258 | return Token{}, ErrUnexpectedEOF |
||
| 259 | } |
||
| 260 | switch ch := d.in[0]; ch { |
||
| 261 | case ']': |
||
| 262 | d.popOpenStack() |
||
| 263 | return d.consumeToken(ListClose, 1, 0), nil |
||
| 264 | case '{', '<': |
||
| 265 | d.pushOpenStack(ch) |
||
| 266 | return d.consumeToken(MessageOpen, 1, 0), nil |
||
| 267 | default: |
||
| 268 | return d.parseScalar() |
||
| 269 | } |
||
| 270 | |||
| 271 | case ListClose: |
||
| 272 | openKind, closeCh := d.currentOpenKind() |
||
| 273 | switch openKind { |
||
| 274 | case bof: |
||
| 275 | // Top level message. |
||
| 276 | // Next token can be EOF, comma, semicolon or Name. |
||
| 277 | if isEOF { |
||
| 278 | return d.consumeToken(EOF, 0, 0), nil |
||
| 279 | } |
||
| 280 | switch ch := d.in[0]; ch { |
||
| 281 | case ',': |
||
| 282 | return d.consumeToken(comma, 1, 0), nil |
||
| 283 | case ';': |
||
| 284 | return d.consumeToken(semicolon, 1, 0), nil |
||
| 285 | default: |
||
| 286 | return d.parseFieldName() |
||
| 287 | } |
||
| 288 | |||
| 289 | case MessageOpen: |
||
| 290 | // Next token can be MessageClose, comma, semicolon or Name. |
||
| 291 | if isEOF { |
||
| 292 | return Token{}, ErrUnexpectedEOF |
||
| 293 | } |
||
| 294 | switch ch := d.in[0]; ch { |
||
| 295 | case closeCh: |
||
| 296 | d.popOpenStack() |
||
| 297 | return d.consumeToken(MessageClose, 1, 0), nil |
||
| 298 | case otherCloseChar[closeCh]: |
||
| 299 | return Token{}, d.newSyntaxError(mismatchedFmt, ch) |
||
| 300 | case ',': |
||
| 301 | return d.consumeToken(comma, 1, 0), nil |
||
| 302 | case ';': |
||
| 303 | return d.consumeToken(semicolon, 1, 0), nil |
||
| 304 | default: |
||
| 305 | return d.parseFieldName() |
||
| 306 | } |
||
| 307 | |||
| 308 | default: |
||
| 309 | // It is not possible to have this case. Let it panic below. |
||
| 310 | } |
||
| 311 | |||
| 312 | case comma, semicolon: |
||
| 313 | openKind, closeCh := d.currentOpenKind() |
||
| 314 | switch openKind { |
||
| 315 | case bof: |
||
| 316 | // Top level message. Next token can be EOF or Name. |
||
| 317 | if isEOF { |
||
| 318 | return d.consumeToken(EOF, 0, 0), nil |
||
| 319 | } |
||
| 320 | return d.parseFieldName() |
||
| 321 | |||
| 322 | case MessageOpen: |
||
| 323 | // Next token can be MessageClose or Name. |
||
| 324 | if isEOF { |
||
| 325 | return Token{}, ErrUnexpectedEOF |
||
| 326 | } |
||
| 327 | switch ch := d.in[0]; ch { |
||
| 328 | case closeCh: |
||
| 329 | d.popOpenStack() |
||
| 330 | return d.consumeToken(MessageClose, 1, 0), nil |
||
| 331 | case otherCloseChar[closeCh]: |
||
| 332 | return Token{}, d.newSyntaxError(mismatchedFmt, ch) |
||
| 333 | default: |
||
| 334 | return d.parseFieldName() |
||
| 335 | } |
||
| 336 | |||
| 337 | case ListOpen: |
||
| 338 | if lastKind == semicolon { |
||
| 339 | // It is not be possible to have this case as logic here |
||
| 340 | // should not have produced a semicolon Token when inside a |
||
| 341 | // list. Let it panic below. |
||
| 342 | break |
||
| 343 | } |
||
| 344 | // Next token can be MessageOpen or Scalar. |
||
| 345 | if isEOF { |
||
| 346 | return Token{}, ErrUnexpectedEOF |
||
| 347 | } |
||
| 348 | switch ch := d.in[0]; ch { |
||
| 349 | case '{', '<': |
||
| 350 | d.pushOpenStack(ch) |
||
| 351 | return d.consumeToken(MessageOpen, 1, 0), nil |
||
| 352 | default: |
||
| 353 | return d.parseScalar() |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | line, column := d.Position(len(d.orig) - len(d.in)) |
||
| 359 | panic(fmt.Sprintf("Decoder.parseNext: bug at handling line %d:%d with lastKind=%v", line, column, lastKind)) |
||
| 360 | } |
||
| 666 |