-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
344 lines (279 loc) · 8.5 KB
/
main.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// ANTLR-based parser for iconscript.
// Author: Sergey Vartanov <[email protected]>
package main
import (
"flag"
"fmt"
"log"
"strconv"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/enzet/iconscript/grammar/parser"
)
// Figure is a simple 2D figure on the surface.
type Figure interface {
setWidth(width float32)
}
// Line is a polyline through a set of positions.
type Line struct {
positions []Position
isFilled bool
width float32
}
func (line *Line) setWidth(width float32) {
line.width = width
}
// Get string representation of a line.
func (line *Line) String() string {
result := "LINE"
if line.isFilled {
result += "_FILLED"
}
for _, position := range line.positions {
result += " " + position.String()
}
return result
}
type Rectangle struct {
start Position
end Position
width float32
}
func (rectangle *Rectangle) setWidth(width float32) {
rectangle.width = width
}
// Get string representation of a rectangle.
func (rectangle *Rectangle) String() string {
return fmt.Sprintf("RECTANGLE %s %s", rectangle.start.String(),
rectangle.end.String())
}
type Circle struct {
center Position
radius float32
width float32
}
func (circle *Circle) setWidth(width float32) {
circle.width = width
}
// Get string representation of a circle.
func (circle *Circle) String() string {
return fmt.Sprintf("CIRCLE %s %f", circle.center.String(), circle.radius)
}
// Arc is a part of a circle.
type Arc struct {
center Position
radius float32
startAngle float32 // Start angle in radians.
endAngle float32 // End angle in radians.
width float32
}
func (arc *Arc) setWidth(width float32) {
arc.width = width
}
// Get string representation of an arc.
func (arc *Arc) String() string {
return fmt.Sprintf("ARC %s %f %f %f", arc.center.String(), arc.radius,
arc.startAngle, arc.endAngle)
}
// Icon is a 2-dimensional shape, described by the number of figures, that
// should be united or subtracted.
type Icon struct {
name string
figures []Figure
}
// Get string representation of an icon.
func (icon Icon) String() string {
result := icon.name + "\n"
for _, figure := range icon.figures {
result += fmt.Sprintf(" %s\n", figure)
}
return result
}
// Context describes current state of the drawing process.
type Context struct {
currentPosition *Position
currentWidth float32
unnamedIconId int
isUnionMode bool
currentIcon *Icon
icons []*Icon
}
// Parser listener, that stores current parser state as well.
type iconScriptListener struct {
*parser.BaseIconScriptListener
context *Context
}
// Position is a 2-dimensional point on the plane.
type Position struct {
x float32
y float32
}
// Get string representation of a position.
func (position *Position) String() string {
return fmt.Sprintf("%f,%f", position.x, position.y)
}
// Add other position to the given one and return the combination.
func (position *Position) Add(other *Position) Position {
position.x += other.x
position.y += other.y
return Position{position.x, position.y}
}
// Errors are ignored since it should be checked by ANTLR.
func parseFloat(node string) float32 {
float, _ := strconv.ParseFloat(node, 32)
return float32(float)
}
// Read position from its string representation.
func readPosition(context parser.IPositionContext) Position {
x := parseFloat(context.GetX().GetText())
y := float32(0.0)
if context.GetY() != nil {
y = parseFloat(context.GetY().GetText())
} else {
println("Error: no Y component.")
}
return Position{x, y}
}
// Parse position from string representation and update current position.
func parsePosition(positionContext parser.IPositionContext,
context *Context) Position {
position := readPosition(positionContext)
if positionContext.GetRelative() != nil {
return context.currentPosition.Add(&position)
} else {
*context.currentPosition = position
return position
}
}
// ExitLine constructs line and adds it to the current icon.
func (listener *iconScriptListener) ExitLine(context *parser.LineContext) {
line := new(Line)
line.setWidth(listener.context.currentWidth)
positions := context.AllPosition()
line.positions = make([]Position, len(positions))
command := context.GetChild(0).GetPayload().(*antlr.CommonToken).GetText()
if command == "lf" {
line.isFilled = true
}
for i, position := range positions {
line.positions[i] = parsePosition(position, listener.context)
}
if listener.context.currentIcon != nil {
listener.context.currentIcon.figures =
append(listener.context.currentIcon.figures, line)
} else {
println("Error: no current icon.")
}
}
// ExitRectangle constructs a rectangle and adds it to the current icon.
func (listener *iconScriptListener) ExitRectangle(
context *parser.RectangleContext) {
rectangle := new(Rectangle)
rectangle.setWidth(listener.context.currentWidth)
positions := context.AllPosition()
rectangle.start = parsePosition(positions[0], listener.context)
rectangle.end = parsePosition(positions[1], listener.context)
if listener.context.currentIcon != nil {
listener.context.currentIcon.figures =
append(listener.context.currentIcon.figures, rectangle)
} else {
println("Error: no current icon.")
}
}
// ExitCircle constructs acircle and adds it to the current icon.
func (listener *iconScriptListener) ExitCircle(context *parser.CircleContext) {
circle := new(Circle)
circle.setWidth(listener.context.currentWidth)
circle.center = parsePosition(context.Position(), listener.context)
circle.radius = parseFloat(context.FLOAT().GetText())
if listener.context.currentIcon != nil {
listener.context.currentIcon.figures =
append(listener.context.currentIcon.figures, circle)
} else {
println("Error: no current icon.")
}
}
// ExitArc constructs an arc and adds it to the current icon.
func (listener *iconScriptListener) ExitArc(context *parser.ArcContext) {
arc := new(Arc)
arc.setWidth(listener.context.currentWidth)
floats := context.AllFLOAT()
arc.center = parsePosition(context.Position(), listener.context)
arc.radius = parseFloat(floats[0].GetText())
arc.startAngle = parseFloat(floats[1].GetText())
arc.endAngle = parseFloat(floats[2].GetText())
if listener.context.currentIcon != nil {
listener.context.currentIcon.figures =
append(listener.context.currentIcon.figures, arc)
} else {
println("Error: no current icon.")
}
}
// ExitSetPosition updates current position.
func (listener *iconScriptListener) ExitSetPosition(
context *parser.SetPositionContext) {
parsePosition(context.Position(), listener.context)
}
// ExitsetWidth sets current width.
func (listener *iconScriptListener) ExitsetWidth(
context *parser.SetWidthContext) {
listener.context.currentWidth = parseFloat(context.FLOAT().GetText())
}
// ExitName stores icon name.
func (listener *iconScriptListener) ExitName(context *parser.NameContext) {
listener.context.currentIcon.name = context.IDENTIFIER().GetText()
}
// EnterIcon creates a new icon.
func (listener *iconScriptListener) EnterIcon(_ *parser.IconContext) {
listener.context.currentIcon = new(Icon)
}
// ExitIcon adds constructed icon to the final set.
func (listener *iconScriptListener) ExitIcon(_ *parser.IconContext) {
if listener.context.currentIcon.name == "" {
listener.context.currentIcon.name =
fmt.Sprintf("icon%d", listener.context.unnamedIconId)
listener.context.unnamedIconId++
}
listener.context.icons =
append(listener.context.icons, listener.context.currentIcon)
}
// Parse iconscript using ANTLR.
func parse(stream antlr.CharStream) (*Context, error) {
lexer := parser.NewIconScriptLexer(stream)
tokenStream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
p := parser.NewIconScriptParser(tokenStream)
listener := new(iconScriptListener)
listener.context = &Context{new(Position), 1.0, 0, false, nil, nil}
antlr.ParseTreeWalkerDefault.Walk(listener, p.Script())
return listener.context, nil
}
// Script entry point: use `-i` to specify file name or `-c` to specify string
// commands.
func main() {
fileName := flag.String("i", "icons.txt", "input file name")
commands := flag.String("c", "", "input string")
flag.Parse()
if *commands == "" {
stream, err := antlr.NewFileStream(*fileName)
if err != nil {
log.Fatal(err)
}
parsed, err := parse(stream)
if err == nil {
for _, icon := range parsed.icons {
fmt.Print(icon)
}
} else {
log.Fatal("Failed to parse file.")
}
} else {
stream := antlr.NewInputStream(*commands)
parsed, err := parse(stream)
if err == nil {
for _, icon := range parsed.icons {
fmt.Print(icon)
}
} else {
log.Fatal("Failed to parse command.")
}
}
}