-
Notifications
You must be signed in to change notification settings - Fork 25
/
parser.go
182 lines (165 loc) · 3.88 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
package svg
import (
"fmt"
"strconv"
mt "github.com/rustyoz/Mtransform"
gl "github.com/rustyoz/genericlexer"
)
func parseNumber(i gl.Item) (float64, error) {
var n float64
var ok error
if i.Type == gl.ItemNumber {
n, ok = strconv.ParseFloat(i.Value, 64)
if ok != nil {
return n, fmt.Errorf("Error passing number %s", ok)
}
}
return n, nil
}
func parseTuple(l *gl.Lexer) (Tuple, error) {
t := Tuple{}
l.ConsumeWhiteSpace()
ni := l.NextItem()
if ni.Type == gl.ItemNumber {
n, ok := strconv.ParseFloat(ni.Value, 64)
if ok != nil {
return t, fmt.Errorf("Error parsing number %s", ok)
}
t[0] = n
} else {
return t, fmt.Errorf("Error parsing Tuple expected Number got: %s", ni.Value)
}
if l.PeekItem().Type == gl.ItemWSP || l.PeekItem().Type == gl.ItemComma {
l.NextItem()
}
ni = l.NextItem()
if ni.Type == gl.ItemNumber {
n, ok := strconv.ParseFloat(ni.Value, 64)
if ok != nil {
return t, fmt.Errorf("Error passing Number %s", ok)
}
t[1] = n
} else {
return t, fmt.Errorf("Error passing Tuple expected Number got: %v", ni)
}
return t, nil
}
func parseTransform(tstring string) (mt.Transform, error) {
var x *mt.Transform
lexer, _ := gl.Lex("tlexer", tstring)
for {
i := lexer.NextItem()
var t mt.Transform
var err error
switch i.Type {
case gl.ItemEOS:
if x == nil {
return mt.Identity(),
fmt.Errorf("transform parse failed")
}
return *x, nil
case gl.ItemWord:
switch i.Value {
case "matrix":
t, err = parseMatrix(lexer)
case "translate":
t, err = parseTranslate(lexer)
case "rotate":
t, err = parseRotate(lexer)
case "scale":
t, err = parseScale(lexer)
}
case gl.ItemWSP:
continue
}
if err != nil {
return t, err
} else if x == nil {
x = &t
} else {
x.MultiplyWith(t) // see https://www.w3.org/TR/SVGTiny12
}
}
}
func parseMatrix(l *gl.Lexer) (mt.Transform, error) {
nums, err := parseParenNumList(l)
if err != nil || len(nums) != 6 {
return mt.Identity(),
fmt.Errorf("Error Parsing Transform Matrix: %v", err)
}
var tm mt.Transform
tm[0][0] = nums[0]
tm[0][1] = nums[2]
tm[0][2] = nums[4]
tm[1][0] = nums[1]
tm[1][1] = nums[3]
tm[1][2] = nums[5]
tm[2][0] = 0
tm[2][1] = 0
tm[2][2] = 1
return tm, nil
}
func parseTranslate(l *gl.Lexer) (mt.Transform, error) {
nums, err := parseParenNumList(l)
if err != nil || len(nums) != 2 {
return mt.Identity(), fmt.Errorf("Error Parsing Translate: %v", err)
}
tm := mt.Identity()
tm[0][2] = nums[0]
tm[1][2] = nums[1]
return tm, nil
}
func parseRotate(l *gl.Lexer) (mt.Transform, error) {
nums, err := parseParenNumList(l)
if err != nil || (len(nums) != 1 && len(nums) != 3) {
return mt.Identity(), fmt.Errorf("Error Parsing Rotate: %v", err)
}
a, px, py := nums[0], 0.0, 0.0
if len(nums) == 3 {
px, py = nums[1], nums[2]
}
tm := mt.Identity()
(&tm).RotatePoint(a, px, py)
return tm, nil
}
func parseScale(l *gl.Lexer) (mt.Transform, error) {
nums, err := parseParenNumList(l)
if err != nil || (len(nums) != 1 && len(nums) != 2) {
return mt.Identity(), fmt.Errorf("Error Parsing Scale: %v", err)
}
x, y := nums[0], nums[0]
if len(nums) == 2 {
y = nums[1]
}
tm := mt.Identity()
(&tm).Scale(x, y)
return tm, nil
}
// Parse a parenthesized list of ncount numbers.
func parseParenNumList(l *gl.Lexer) ([]float64, error) {
i := l.NextItem()
if i.Type != gl.ItemParan {
return nil, fmt.Errorf("Expected Opening Parantheses")
}
var nums []float64
for {
if len(nums) > 0 {
for l.PeekItem().Type == gl.ItemComma || l.PeekItem().Type == gl.ItemWSP {
l.NextItem()
}
}
if l.PeekItem().Type == gl.ItemParan {
l.NextItem() // consume Parantheses
break
}
if l.PeekItem().Type != gl.ItemNumber {
return nil, fmt.Errorf("Expected Number got %v", l.PeekItem().String())
}
n, err := parseNumber(l.NextItem())
if err != nil {
return nil, err
}
nums = append(nums, n)
}
return nums, nil
}