-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
148 lines (125 loc) · 3.04 KB
/
parser.go
File metadata and controls
148 lines (125 loc) · 3.04 KB
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
package main
import (
"log"
"strconv"
)
type Parser struct {
lex *Lexer
currentToken Token
nextToken Token
}
func NewParser(lexer *Lexer) *Parser {
parser := Parser{lex: lexer}
parser.readToken()
parser.readToken()
return &parser
}
func (p *Parser) readToken() {
p.currentToken = p.nextToken
p.nextToken = p.lex.NextToken()
}
func (p *Parser) ParseSvg() *svg {
s := svg{}
s.drawables = []drawable{}
p.expect(TokenOpen)
if tagName := p.parseIdentifier(); tagName != "svg" {
log.Fatalf("expected tag 'svg', got '%s'\n", tagName)
}
attributes := p.parseAttributes()
s.width = lookupInt(attributes, "width")
s.height = lookupInt(attributes, "height")
p.expect(TokenClose)
// Stop parsing shapes when finding "</"
for p.currentToken.Type != TokenOpen || p.nextToken.Type != TokenSlash {
d := p.parseShape()
s.drawables = append(s.drawables, d)
}
// Consume and ignore remaining tokens
for p.currentToken.Type != EOF {
p.readToken()
}
return &s
}
func (p *Parser) parseAttributes() map[string]string {
attributes := make(map[string]string)
for p.currentToken.Type != TokenSlash && p.currentToken.Type != TokenClose {
k, v := p.parseAttribute()
attributes[k] = v
}
return attributes
}
func (p *Parser) parseAttribute() (key, value string) {
key = p.parseIdentifier()
p.expect(TokenEqual)
p.expect(TokenQuote)
value = p.parseIdentifier()
p.expect(TokenQuote)
return
}
func (p *Parser) expect(tokenType TokenType) {
if p.currentToken.Type != tokenType {
log.Fatalf("expect %q, got %q (%q)", tokenType, p.currentToken.Type, p.currentToken.Literal)
}
p.readToken()
}
func (p *Parser) parseIdentifier() string {
if p.currentToken.Type != TokenIdentifier {
log.Printf("expect identifier, got %q (%q)", p.currentToken.Type, p.currentToken.Literal)
}
id := p.currentToken.Literal
p.readToken()
return id
}
func (p *Parser) parseShape() drawable {
p.expect(TokenOpen)
shapeName := p.parseIdentifier()
attributes := p.parseAttributes()
var d drawable
switch shapeName {
case "circle":
d = parseCircle(attributes)
default:
log.Printf("unknown shape %s\n", shapeName)
}
p.expect(TokenSlash)
p.expect(TokenClose)
return d
}
func parseCircle(attributes map[string]string) *circle {
cx := lookupFloat(attributes, "cx")
cy := lookupFloat(attributes, "cy")
r := lookupFloat(attributes, "r")
strokeWidth := lookupFloat(attributes, "stroke-width")
stroke := attributes["stroke"]
fill := attributes["fill"]
return &circle{
shape: shape{
x: cx,
y: cy,
strokeWidth: strokeWidth,
fillColor: fill,
strokeColor: stroke,
},
radius: r,
}
}
func lookupFloat(m map[string]string, key string) float64 {
if m[key] == "" {
return 0.0
}
fl, err := strconv.ParseFloat(m[key], 64)
if err != nil {
panic("cannot parse float: " + key + ":" + m[key])
}
return fl
}
func lookupInt(m map[string]string, key string) int {
if m[key] == "" {
return 0
}
integer, err := strconv.Atoi(m[key])
if err != nil {
panic("cannot parse integer: " + key + ":" + m[key])
}
return integer
}