-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
264 lines (223 loc) · 6.31 KB
/
Copy pathparser.js
File metadata and controls
264 lines (223 loc) · 6.31 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
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
/**
* @file input stream
* @author zdying
*/
var Tokenizer = require('./tokenizer');
function Parser (source, filePath) {
this.tokenizer = new Tokenizer(source, filePath);
this.input = this.tokenizer.input;
}
Parser.prototype = {
constructor: Parser,
parseToplevel: function () {
var ast = {
type: 'GlobalBlock',
body: []
};
ast.body = this.parseBlock(ast.type);
return ast;
},
parseBlock: function (parentBlock) {
var tokenizer = this.tokenizer;
var input = this.input;
var tokens = null;
var lastToken = null;
var firstToken = null;
var domainValue;
var type = '';
var body = [];
var block = null;
var call = null;
while (!tokenizer.eof()) {
tokens = this.readStatementTokens();
/* istanbul ignore if */
if (tokens.length === 0) {
continue;
}
lastToken = tokens[tokens.length - 1];
firstToken = tokens[0];
type = lastToken.type;
if (
type === 'block_start' ||
type === 'block_end' ||
type === 'line_terminal' ||
type === 'express_end'
) {
tokens.pop();
}
if (type === 'block_end') {
break;
}
if (type === 'block_start') {
if (tokens.length < 2) {
return input.error(
'Unexpected ' + (parentBlock === 'GlobalBlock' ? 'DomainBlock' : 'LocationBlock') + ' declaration',
(firstToken || lastToken).loc.start.line,
(firstToken || lastToken).loc.start.column
);
}
if (firstToken.type === 'keyword' && firstToken.value === 'location') {
// location /
this.checkBlock('LocationBlock', parentBlock, firstToken.loc);
block = this.parseBlock('LocationBlock');
body.push({
type: 'LocationBlock',
location: this.getLocationValue(tokens),
body: block
});
} else if (tokens[tokens.length - 1].type === 'arrow') {
// hiproxy.org => {
this.checkBlock('DomainBlock', parentBlock, firstToken.loc);
block = this.parseBlock('DomainBlock');
domainValue = this._getDomainValue(tokens.slice(0, tokens.length - 1));
body.push({
type: 'DomainBlock',
domain: domainValue,
body: block
});
} else if (tokens[1]) {
// domain hiproxy.org {
this.checkBlock('DomainBlock', parentBlock, firstToken.loc);
block = this.parseBlock('DomainBlock');
domainValue = this._getDomainValue(tokens.slice(1));
body.push({
type: 'DomainBlock',
domain: domainValue,
body: block
});
}
} else if (firstToken && firstToken.type !== 'comment') {
call = this.parseCall(tokens);
call && body.push(call);
}
}
return body;
},
_getDomainValue: function (tokens) {
var domainValue = [];
tokens.forEach(function (token) {
var val = token.value.replace(/^[,\s]+|[,\s]+$/g, '');
if (val) {
domainValue = domainValue.concat(val.split(/,+/));
}
});
return domainValue;
},
readStatementTokens: function () {
var tokenizer = this.tokenizer;
var token = null;
var type = '';
var tokens = [];
// set $A 1234;
// set $A 1234
// domain hiproxy.org {
// hiproxy.org => {
// }
while (!tokenizer.eof()) {
token = tokenizer.next();
type = token.type;
tokens.push(token);
if (
type === 'express_end' ||
type === 'line_terminal' ||
type === 'block_start' ||
type === 'block_end'
) {
break;
}
}
return tokens;
},
getLocationValue: function (tokens) {
var value = tokens[1].value;
var isRegExp = value.indexOf('~') !== -1;
if (!isRegExp) {
return value;
} else {
/* istanbul ignore else */
if (tokens[2]) {
value = tokens[2].value;
// if start with '//' and end with '/'
// remove the FIRST and the LAST '/'
if (/\/\\?\//.test(value) && value.charAt(value.length - 1) === '/') {
value = value.replace(/^\/|\/$/g, '');
}
return new RegExp(value);
} else {
this.input.error(
'Invalid RegExp location config, Regexp location format: ' + '`location ~ /(user|info)/(.*)`'.bold.green,
tokens[1].loc.start.line,
tokens[1].loc.start.column + 1
);
}
}
},
parseCall: function (tokens) {
/* istanbul ignore if */
if (tokens.length === 0) {
return null;
}
var values = tokens.map(function (token) {
return token.value;
});
var name = tokens.shift();
var params = tokens;
// base rules, for example:
// http://api.hiproxy.org/ => http://hiproxy.org/api/
if (values.indexOf('=>') > -1) {
if (values.length === 3) {
return {
type: 'SimpleRule',
left: name,
right: params.pop()
};
} else {
this.input.error(
'Simple Rule syntax error',
tokens[0].loc.start.line,
tokens[0].loc.start.column
);
}
}
if (this.isSet(name)) {
return {
type: 'VariableDeclaration',
declaration: {
id: params.shift(),
value: params
}
};
}
return {
type: 'CallExpression',
directive: name.value,
arguments: params
};
},
isSet: function (token) {
return token && token.value === 'set';
},
// isProxyPass: function (token) {
// return token && token.value === 'proxy_pass';
// },
checkBlock: function (curBlock, parentBlock, loc) {
var input = this.input;
var order = ['GlobalBlock', 'DomainBlock', 'LocationBlock'];
var curIndex = order.indexOf(curBlock);
var parentIndex = order.indexOf(parentBlock);
if (curIndex - parentIndex !== 1) {
input.error(
curBlock + ' should be wrapped in ' + order[curIndex - 1],
loc.start.line,
loc.start.column
);
}
}
};
module.exports = Parser;
// test
// var file = require('path').join(__dirname, 'test.txt');
// var source = require('fs').readFileSync(file, 'utf-8');
// var parser = new Parser(source, file);
// var ast = parser.parseToplevel();
// console.log(JSON.stringify(ast, null, 2));