-
Notifications
You must be signed in to change notification settings - Fork 6
/
Parser.rl
325 lines (232 loc) · 8.16 KB
/
Parser.rl
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
package clojurewerkz.crawlista.robots;
import java.text.ParseException;
import clojure.lang.*;;
%%{
machine robots;
#
# Agents
#
action agent_start {
// System.out.println("action: agent_start. p = " + p);
ansp = p;
}
action agent_end {
// System.out.println("action: agent_end. p = " + p);
String lastSeenUserAgentName = new String(data, ansp, (p - ansp));
// System.out.println("Daw agent: " + lastSeenUserAgentName);
IPersistentMap m = PersistentHashMap.create();
result.conj(m.assoc("user-agent", lastSeenUserAgentName.trim()));
}
#
# Disallow rules
#
action disallow_rule_start {
// System.out.println("action: disallow_rule_start. p = " + p);
drsp = p;
}
action disallow_rule_end {
// System.out.println("action: disallow_rule_end. p = " + p);
String rule = new String(data, drsp, (p - drsp));
// we matched "disallow none" case (Disallow:)
if(rule.equals("Disallow:")) {
rule = "";
}
// System.out.println("Saw disallow_rule: " + rule);
IPersistentMap m = PersistentHashMap.create();
result.conj(m.assoc("disallow", rule.trim()));
}
#
# Allow rules
#
action allow_rule_start {
// System.out.println("action: allow_rule_start. p = " + p);
arsp = p;
}
action allow_rule_end {
// System.out.println("action: allow_rule_end. p = " + p);
String rule = new String(data, arsp, (p - arsp));
// we matched "allow none" case (Allow:)
if(rule.equals("Allow:")) {
rule = "";
}
// System.out.println("Saw allow_rule: " + rule);
IPersistentMap m = PersistentHashMap.create();
result.conj(m.assoc("allow", rule.trim()));
}
#
# Sitemap entries
#
action sitemap_rule_start {
// System.out.println("action: sitemap_rule_start. p = " + p);
slsp = p;
}
action sitemap_rule_end {
// System.out.println("action: sitemap_rule_end. p = " + p);
String rule = new String(data, slsp, (p - slsp));
// we matched empty sitemap line (Sitemap:)
if(!rule.equals("Sitemap:")) {
// System.out.println("Saw sitemap_rule: " + rule);
IPersistentMap m = PersistentHashMap.create();
result.conj(m.assoc("sitemap", rule.trim()));
}
}
#
# Host entries
#
action host_rule_start {
// System.out.println("action: host_rule_start. p = " + p);
hnsp = p;
}
action host_rule_end {
// System.out.println("action: host_rule_end. p = " + p);
String rule = new String(data, hnsp, (p - hnsp));
// we matched empty host line (Host:)
if(!rule.equals("Host:")) {
// System.out.println("Saw host_rule: " + rule);
IPersistentMap m = PersistentHashMap.create();
result.conj(m.assoc("host", rule.trim()));
}
}
#
# Noindex entries
#
action noindex_rule_start {
// System.out.println("action: noindex_rule_start. p = " + p);
nisp = p;
}
action noindex_rule_end {
// System.out.println("action: noindex_rule_end. p = " + p);
String rule = new String(data, nisp, (p - nisp));
// we matched empty noindex line (Noindex:)
if(!rule.equals("Noindex:")) {
// System.out.println("Saw noindex_rule: " + rule);
IPersistentMap m = PersistentHashMap.create();
result.conj(m.assoc("noindex", rule.trim()));
}
}
#
# Crawl-delay entries
#
action crawldelay_rule_start {
// System.out.println("action: crawldelay_rule_start. p = " + p);
cdsp = p;
}
action crawldelay_rule_end {
// System.out.println("action: crawldelay_rule_end. p = " + p);
String rule = new String(data, cdsp, (p - cdsp));
if(!rule.equals("Crawldelay:")) {
// System.out.println("Saw crawldelay_rule: " + rule);
IPersistentMap m = PersistentHashMap.create();
result.conj(m.assoc("crawl-delay", Long.valueOf(rule.trim())));
}
}
SP = " ";
HT = "\t";
HASH = "#";
LF = "\n";
CR = "\r";
CRLF = CR? LF;
# space built-in seems to be matching newlines and producing overly eager
# matches as a result
SPHT = SP | HT;
COLON = ":";
CTL = (cntrl | 127);
TEXT = any -- CTL;
ID = any -- HASH -- CRLF;
AGENT_INITIAL = 'a'..'z' | 'A'..'Z' | '0'-'9';
WILDCARD = '*';
SLASH = '/';
blank = SPHT*;
blankline = blank* CRLF;
comment = HASH TEXT*;
commentline = blank* comment CRLF;
blankcomment = CRLF | blank? commentline;
commentblank = commentline* blank blankcomment*;
U = 'U' | 'u';
S = 'S' | 's';
E = 'E' | 'e';
R = 'R' | 'r';
A = 'A' | 'a';
G = 'G' | 'g';
N = 'N' | 'n';
T = 'T' | 't';
D = 'D' | 'd';
I = 'I' | 'i';
L = 'L' | 'l';
O = 'O' | 'o';
W = 'W' | 'w';
M = 'M' | 'm';
P = 'P' | 'p';
C = 'C' | 'c';
Y = 'Y' | 'y';
X = 'X' | 'x';
H = 'H' | 'h';
agent_prefix = U S E R "-" A G E N T COLON;
wildcard_agent = WILDCARD SPHT*;
named_agent = AGENT_INITIAL ID*;
agent = (wildcard_agent | named_agent) >agent_start %/agent_end %agent_end;
uchar = (TEXT -- HASH -- CR -- LF);
pchar = uchar | ":" | "@" | "&" | "=" | SLASH;
segment = pchar*;
fsegment = pchar*;
path = fsegment (SLASH segment)*;
disallow_rule = path >disallow_rule_start %/disallow_rule_end %disallow_rule_end;
disallow_prefix = D I S A L L O W COLON;
allow_rule = path >allow_rule_start %/allow_rule_end %allow_rule_end;
allow_prefix = A L L O W COLON;
sitemap_rule = path >sitemap_rule_start %/sitemap_rule_end %sitemap_rule_end;
sitemap_prefix = S I T E M A P COLON;
host_rule = path >host_rule_start %/host_rule_end %host_rule_end;
host_prefix = H O S T COLON;
crawldelay_rule = digit+ >crawldelay_rule_start %/crawldelay_rule_end %crawldelay_rule_end;
crawldelay_prefix = C R A W L "-" D E L A Y COLON;
noindex_rule = path >noindex_rule_start %/noindex_rule_end %noindex_rule_end;
noindex_prefix = N O I N D E X COLON;
agentline = SPHT* agent_prefix SPHT* agent comment? CRLF;
disallowline = SPHT* disallow_prefix SPHT* CRLF
| SPHT* disallow_prefix SPHT* disallow_rule comment? CRLF;
allowline = SPHT* allow_prefix CRLF |
SPHT* allow_prefix SPHT* allow_rule comment? CRLF;
sitemapline = SPHT* sitemap_prefix CRLF |
SPHT* sitemap_prefix SPHT* sitemap_rule comment? CRLF;
hostline = SPHT* host_prefix CRLF |
SPHT* host_prefix SPHT* host_rule comment? CRLF;
crawldelayline = SPHT* crawldelay_prefix SPHT* crawldelay_rule comment? CRLF;
noindexline = SPHT* noindex_prefix SPHT* noindex_rule comment? CRLF;
ruleline = disallowline | allowline | sitemapline | noindexline | crawldelayline | hostline;
record = commentline* agentline (commentline agentline)*
| ruleline (commentline ruleline)*;
main := blankcomment*
| blankcomment* record (commentblank record)* blankcomment*;
}%%
public class Parser {
%% write data;
public IPersistentVector parse(String input) throws ParseException {
char[] data = input.toCharArray();
int cs;
int eof = data.length;
int p = 0;
int pe = eof;
// agent name start position
int ansp = 0;
// disallow rule start position
int drsp = 0;
// allow rule start position
int arsp = 0;
// sitemap line start position
int slsp = 0;
// hostname line start position
int hnsp = 0;
// noindex line start position
int nisp = 0;
// crawl-delay line start position
int cdsp = 0;
ITransientVector result = PersistentVector.create().asTransient();
%% write init;
%% write exec;
if(cs == robots_error) {
throw new ParseException("Unparseable input: " + input + ", p = " + p, p);
}
return (IPersistentVector)result.persistent();
}
}