forked from cockroachdb/cockroach
/
format.go
81 lines (71 loc) · 2.43 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Raphael 'kena' Poss (knz@cockroachlabs.com)
package parser
import (
"bytes"
"fmt"
)
type fmtFlags struct {
showTypes bool
}
// FmtFlags enables conditional formatting in the pretty-printer.
type FmtFlags *fmtFlags
// FmtSimple instructs the pretty-printer to produce
// a straightforward representation, ideally using SQL
// syntax that makes prettyprint+parse idempotent.
var FmtSimple FmtFlags = &fmtFlags{showTypes: false}
// FmtShowTypes instructs the pretty-printer to
// annotate expressions with their resolved types.
var FmtShowTypes FmtFlags = &fmtFlags{showTypes: true}
// NodeFormatter is implemented by nodes that can be pretty-printed.
type NodeFormatter interface {
// Format performs pretty-printing towards a bytes buffer. The
// flags argument influences the results.
Format(buf *bytes.Buffer, flags FmtFlags)
}
// FormatNode recurses into a node for pretty-printing.
// Flag-driven special cases can hook into this.
func FormatNode(buf *bytes.Buffer, f FmtFlags, n NodeFormatter) {
if f.showTypes {
if te, ok := n.(TypedExpr); ok {
buf.WriteByte('(')
n.Format(buf, f)
buf.WriteString(")[")
if rt := te.ReturnType(); rt == nil {
// An attempt is made to pretty-print an expression that was
// not assigned a type yet. This should not happen, so we make
// it clear in the output this needs to be investigated
// further.
buf.WriteString(fmt.Sprintf("??? %v", te))
} else {
buf.WriteString(rt.Type())
}
buf.WriteByte(']')
return
}
}
n.Format(buf, f)
}
// AsStringWithFlags pretty prints a node to a string given specific flags.
func AsStringWithFlags(n NodeFormatter, f FmtFlags) string {
var buf bytes.Buffer
FormatNode(&buf, f, n)
return buf.String()
}
// AsString pretty prints a node to a string.
func AsString(n NodeFormatter) string {
return AsStringWithFlags(n, FmtSimple)
}