-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
172 lines (156 loc) · 3.56 KB
/
parse.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
package robots
import (
"fmt"
"net/url"
"sort"
"strings"
"github.com/0x51-dev/robots/abnf"
"github.com/0x51-dev/upeg/parser"
)
func parseIdentifier(n *parser.Node) (string, error) {
return n.Value(), nil
}
func parsePathPattern(n *parser.Node) (*url.URL, error) {
return url.Parse(n.Value())
}
func parseSitemap(n *parser.Node) (*url.URL, error) {
return url.Parse(n.Children()[0].Value())
}
func parseRobotsTXT(n *parser.Node) (*File, error) {
groups := make(map[string]*Group)
var gs []*Group
var sms []*url.URL
var os []OtherField
for _, c := range n.Children() {
switch c.Name {
case abnf.Group.Name:
g, err := parseGroup(c)
if err != nil {
return nil, err
}
if len(g.ProductTokens) == 1 {
// Only support merging groups with a single product token, for now.
pt := g.ProductTokens[0]
name := strings.ToLower(string(*pt))
if group, ok := groups[name]; !ok {
groups[name] = g
} else {
group.Rules = append(group.Rules, g.Rules...)
continue // Skip adding the group to the list of groups.
}
}
gs = append(gs, g)
case abnf.Sitemap.Name:
u, err := parseSitemap(c)
if err != nil {
return nil, err
}
sms = append(sms, u)
case abnf.Others.Name:
o, err := parseOther(c)
if err != nil {
return nil, err
}
os = append(os, *o)
default:
return nil, fmt.Errorf("unexpected root node %q", c.Name)
}
}
return &File{
Groups: gs,
Sitemaps: sms,
Others: os,
}, nil
}
func parseGroup(n *parser.Node) (*Group, error) {
productTokens := make(map[string]struct{})
var pts ProductTokens
var rs []*Rule
for _, c := range n.Children() {
switch c.Name {
case abnf.Startgroupline.Name:
pt, err := parseStartGroupLine(c)
if err != nil {
return nil, err
}
name := strings.ToLower(string(*pt))
if _, ok := productTokens[name]; ok {
continue
}
productTokens[name] = struct{}{}
pts = append(pts, pt)
case abnf.Rule.Name:
r, err := parseRule(c)
if err != nil {
return nil, err
}
if r == nil {
continue
}
rs = append(rs, r)
default:
return nil, fmt.Errorf("unexpected group node %q", c.Name)
}
}
sort.Sort(pts) // Sort the product tokens.
if *pts[0] == Wildcard {
// The wildcard will always be the first product token if present.
// No need to keep other product tokens if wildcard is present.
pts = pts[:1]
}
return &Group{
ProductTokens: pts,
Rules: rs,
}, nil
}
func parseOther(n *parser.Node) (*OtherField, error) {
key, err := parseIdentifier(n.Children()[0])
if err != nil {
return nil, err
}
return &OtherField{
Key: key,
Value: n.Children()[1].Value(),
}, nil
}
func parseProductToken(n *parser.Node) (*ProductToken, error) {
if ProductToken(n.Value()) == Wildcard {
wc := Wildcard
return &wc, nil
}
id, err := parseIdentifier(n.Children()[0])
if err != nil {
return nil, err
}
pt := ProductToken(id)
return &pt, nil
}
func parseStartGroupLine(n *parser.Node) (*ProductToken, error) {
return parseProductToken(n.Children()[0])
}
func parseRule(n *parser.Node) (*Rule, error) {
r, err := parseType(n.Children()[0])
if err != nil {
return nil, err
}
var pp *url.URL
if len(n.Children()) == 2 {
if pp, err = parsePathPattern(n.Children()[1]); err != nil {
return nil, err
}
}
return &Rule{
Type: r,
Path: pp,
}, nil
}
func parseType(n *parser.Node) (RuleType, error) {
switch n.Children()[0].Name {
case abnf.Allow.Name:
return Allow, nil
case abnf.Disallow.Name:
return Disallow, nil
default:
return "", fmt.Errorf("unexpected type value %q", n.Value())
}
}