This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
renderer.go
162 lines (133 loc) · 2.76 KB
/
renderer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package sqlfmt
import (
"io"
"strings"
)
const (
NullToken = iota
KeywordToken = iota
IdentifierToken = iota
SymbolToken = iota
ConstantToken = iota
SpaceToken = iota
RefuseSpaceToken = iota
NewLineToken = iota
IndentToken = iota
UnindentToken = iota
)
type RenderToken struct {
Type int
Value string
}
type Renderer interface {
Text(val string, typ int)
Control(typ int)
}
type TextRenderer struct {
w io.Writer
err error
indentLvl int
indent string
lineIndented bool
newLine bool
lastRenderToken RenderToken
UpperCase bool
}
func (left RenderToken) SpaceBetween(right RenderToken) bool {
if left.Type == RefuseSpaceToken {
return false
}
switch left.Type {
case KeywordToken, IdentifierToken, ConstantToken:
switch right.Type {
case KeywordToken, IdentifierToken, ConstantToken:
return true
case SymbolToken:
switch right.Value {
case "[", "(", "]", ")", ".", ",", "::", ":":
return false
}
return true
}
case SymbolToken:
switch left.Value {
case ".", "(", "[", "::", ":":
return false
}
if right.Type == NewLineToken {
return false
}
if left.Value == "," {
return true
}
if right.Type == SymbolToken {
switch right.Value {
case ".", "(", "[", "::", ")", "]", ",", ":":
return false
}
}
return true
}
return false
}
func NewTextRenderer(w io.Writer) *TextRenderer {
return &TextRenderer{w: w, indent: " "}
}
func (tr *TextRenderer) Text(val string, tokenType int) {
if !tr.lineIndented {
for i := 0; i < tr.indentLvl; i++ {
_, tr.err = io.WriteString(tr.w, tr.indent)
if tr.err != nil {
return
}
}
tr.lineIndented = true
}
token := RenderToken{Type: tokenType, Value: val}
if tr.newLine {
tr.newLine = false
} else if tr.lastRenderToken.SpaceBetween(token) {
_, tr.err = io.WriteString(tr.w, " ")
if tr.err != nil {
return
}
}
tr.lastRenderToken = token
if tr.UpperCase {
if tokenType == KeywordToken || tokenType == SymbolToken {
val = strings.ToUpper(val)
}
}
_, tr.err = io.WriteString(tr.w, val)
}
func (tr *TextRenderer) Control(typ int) {
if tr.err != nil {
return
}
switch typ {
case SpaceToken:
_, tr.err = io.WriteString(tr.w, " ")
case NewLineToken:
tr.renderNewLine()
case IndentToken:
tr.indentLvl = tr.indentLvl + 1
case UnindentToken:
tr.indentLvl = tr.indentLvl - 1
}
tr.lastRenderToken = RenderToken{Type: typ}
}
func (tr *TextRenderer) renderNewLine() {
if tr.newLine {
return
}
tr.newLine = true
tr.lastRenderToken = RenderToken{Type: NewLineToken}
_, tr.err = io.WriteString(tr.w, "\n")
if tr.err != nil {
return
}
tr.lineIndented = false
}
func (tr *TextRenderer) Error() error {
return tr.err
}