-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
271 lines (248 loc) · 6.97 KB
/
parser.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// gobar
//
// Copyright (C) 2014,2022 Karol 'Kenji Takahashi' Woźniak
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"bufio"
"io"
"log"
"regexp"
"strconv"
"github.com/jezek/xgbutil/xgraphics"
)
// Align defines text piece alignment on the screen.
type Align uint8
const (
LEFT Align = iota
RIGHT
)
// EndScan is an artifical Error.
// Raised when parser should stop scanning.
type EndScan struct{}
func (e EndScan) Error() string { return "EndScan" }
// NewBGRA returns a new color definition in X compatible format.
// Input should be a hexagonal representation with alpha, i.e 0xAARRGGBB.
func NewBGRA(color uint64) *xgraphics.BGRA {
a := uint8(color >> 24)
r := uint8((color & 0x00ff0000) >> 16)
g := uint8((color & 0x0000ff00) >> 8)
b := uint8(color & 0x000000ff)
return &xgraphics.BGRA{B: b, G: g, R: r, A: a}
}
// TextPiece stores formatting information for a text
// within single pair of brackets.
type TextPiece struct {
Text string
Font uint
Align Align
Foreground *xgraphics.BGRA
Background *xgraphics.BGRA
Screens []uint
NotScreens []uint
Origin *TextPiece
}
// TextParser is used to create a set of TextPieces from a textual definition.
type TextParser struct {
rgbPattern *regexp.Regexp
}
// NewTextParser creates TextParser instance with
// correct necessary regexp definitions.
func NewTextParser() *TextParser {
return &TextParser{regexp.MustCompile(`^0[xX][0-9a-fA-F]{8}$`)}
}
// Tokenize turns textual definition into a series of valid tokens.
// If no valid token is found at given place, char at 0 position is returned.
func (tp *TextParser) Tokenize(
data []byte, EOF bool,
) (advance int, token []byte, err error) {
if EOF {
return
}
switch {
case data[0] == '\n':
err = EndScan{}
case len(data) < 2:
advance, token, err = 1, data[:1], nil
case string(data[:2]) == "{F":
advance, token, err = 2, data[:2], nil
case string(data[:2]) == "{S":
advance, token, err = 2, data[:2], nil
case len(data) < 3:
advance, token, err = 1, data[:1], nil
case string(data[:3]) == "{CF":
advance, token, err = 3, data[:3], nil
case string(data[:3]) == "{CB":
advance, token, err = 3, data[:3], nil
case string(data[:3]) == "{AR":
advance, token, err = 3, data[:3], nil
case len(data) >= 10 && tp.rgbPattern.Match(data[:10]):
advance, token, err = 10, data[:10], nil
case ('0' <= data[0] && data[0] <= '9') || data[0] == '-':
i := 0
if data[0] == '-' {
i = 1
}
for _, n := range data[i:] {
if !('0' <= n && n <= '9') {
break
}
i++
}
advance, token, err = i, data[:i], nil
default: // Also contains '}' and ','
// TODO: Parsing whole text piece here, instead of returning
// char-by-char, should perform better
advance, token, err = 1, data[:1], nil
}
return
}
// Scan scans textual definition and returns array of TextPieces.
// Possible empty pieces are omitted in the returned array.
func (tp *TextParser) Scan(r io.Reader) []*TextPiece {
var text []*TextPiece
scanner := bufio.NewScanner(r)
scanner.Split(tp.Tokenize)
currentText := &TextPiece{}
text = append(text, currentText)
currentIndex := func() int {
for i, t := range text {
if t == currentText {
return i
}
}
return 0
}
moveCurrent := func(end bool) *TextPiece {
newCurrent := &TextPiece{}
if end {
*newCurrent = *currentText.Origin
} else {
*newCurrent = *currentText
newCurrent.Origin = currentText
}
newCurrent.Text = ""
if currentText.Align == RIGHT {
i := currentIndex()
text = append(text, &TextPiece{})
copy(text[i+1:], text[i:])
text[i] = newCurrent
} else {
text = append(text, newCurrent)
}
currentText = newCurrent
return newCurrent
}
logPieceError := func(err error, pieces ...string) {
log.Printf("Problem parsing `%q`: %s", pieces, err)
for _, piece := range pieces {
currentText.Text += piece
}
}
screening := false
escaping := false
bracketing := 0
for scanner.Scan() {
stext := scanner.Text()
switch {
case stext == "\\":
escaping = true
continue
case !escaping && stext == "{F":
scanner.Scan()
text := scanner.Text()
font, err := strconv.Atoi(text)
if err != nil {
logPieceError(err, stext, text)
}
newCurrent := moveCurrent(false)
newCurrent.Font = uint(font)
case !escaping && stext == "{S":
scanner.Scan()
text := scanner.Text()
screen, err := strconv.Atoi(text)
if err != nil {
logPieceError(err, stext, text)
}
newCurrent := moveCurrent(false)
if text[0] == '-' {
newCurrent.NotScreens = append(newCurrent.NotScreens, uint(-screen))
} else {
newCurrent.Screens = append(newCurrent.Screens, uint(screen))
}
screening = true
case !escaping && stext == "{CF":
scanner.Scan()
text := scanner.Text()
fg, err := strconv.ParseUint(text, 0, 32)
if err != nil {
logPieceError(err, stext, text)
}
newCurrent := moveCurrent(false)
newCurrent.Foreground = NewBGRA(fg)
case !escaping && stext == "{CB":
scanner.Scan()
text := scanner.Text()
bg, err := strconv.ParseUint(text, 0, 32)
if err != nil {
logPieceError(err, stext, text)
}
newCurrent := moveCurrent(false)
newCurrent.Background = NewBGRA(bg)
case !escaping && stext == "{AR":
newCurrent := moveCurrent(false)
newCurrent.Align = RIGHT
case !escaping && stext == "{":
bracketing++
case !escaping && stext == "}":
if bracketing > 0 {
bracketing--
continue
}
screening = false
if currentText.Origin != nil {
moveCurrent(true)
continue
}
fallthrough
default:
if screening && stext == "," {
scanner.Scan()
text := scanner.Text()
screen, err := strconv.Atoi(text)
if err != nil {
logPieceError(err, stext, text)
}
currentText.Screens = append(currentText.Screens, uint(screen))
} else {
currentText.Text += stext
}
escaping = false
}
}
//Remove possible empty pieces.
var text2 []*TextPiece
for _, piece := range text {
if piece.Text != "" {
text2 = append(text2, piece)
}
}
return text2
}