FormatNode recurses into a node for pretty-printing. Flag-driven special cases can hook into this.
(n NodeFormatter)
| 374 | // FormatNode recurses into a node for pretty-printing. |
| 375 | // Flag-driven special cases can hook into this. |
| 376 | func (ctx *FmtCtx) FormatNode(n NodeFormatter) { |
| 377 | f := ctx.flags |
| 378 | if f.HasFlags(FmtShowTypes) { |
| 379 | if te, ok := n.(TypedExpr); ok { |
| 380 | ctx.WriteByte('(') |
| 381 | ctx.formatNodeOrHideConstants(n) |
| 382 | ctx.WriteString(")[") |
| 383 | if rt := te.ResolvedType(); rt == nil { |
| 384 | // An attempt is made to pretty-print an expression that was |
| 385 | // not assigned a type yet. This should not happen, so we make |
| 386 | // it clear in the output this needs to be investigated |
| 387 | // further. |
| 388 | ctx.Printf("??? %v", te) |
| 389 | } else { |
| 390 | ctx.WriteString(rt.String()) |
| 391 | } |
| 392 | ctx.WriteByte(']') |
| 393 | return |
| 394 | } |
| 395 | } |
| 396 | if f.HasFlags(FmtAlwaysGroupExprs) { |
| 397 | if _, ok := n.(Expr); ok { |
| 398 | ctx.WriteByte('(') |
| 399 | } |
| 400 | } |
| 401 | ctx.formatNodeOrHideConstants(n) |
| 402 | if f.HasFlags(FmtAlwaysGroupExprs) { |
| 403 | if _, ok := n.(Expr); ok { |
| 404 | ctx.WriteByte(')') |
| 405 | } |
| 406 | } |
| 407 | if f.HasAnyFlags(fmtDisambiguateDatumTypes | FmtPGCatalog) { |
| 408 | var typ *types.T |
| 409 | if d, isDatum := n.(Datum); isDatum { |
| 410 | if p, isPlaceholder := d.(*Placeholder); isPlaceholder { |
| 411 | // p.typ will be nil if the placeholder has not been type-checked yet. |
| 412 | typ = p.typ |
| 413 | } else if d.AmbiguousFormat() { |
| 414 | typ = d.ResolvedType() |
| 415 | } |
| 416 | } |
| 417 | if typ != nil { |
| 418 | if f.HasFlags(fmtDisambiguateDatumTypes) { |
| 419 | ctx.WriteString(":::") |
| 420 | ctx.FormatTypeReference(typ) |
| 421 | } else if f.HasFlags(FmtPGCatalog) && !typ.IsNumeric() { |
| 422 | ctx.WriteString("::") |
| 423 | ctx.FormatTypeReference(typ) |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // AsStringWithFlags pretty prints a node to a string given specific flags; only |
| 430 | // flags that don't require Annotations can be used. |