-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_parse.js
115 lines (87 loc) · 2.4 KB
/
_parse.js
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
'use strict';
var R_DEGRADE = /^\s*[^\s.[]/;
var R_SEARCH = /^\s*([^\s])([\s\S]*)$/;
var R_IDENT = /^\s*([_a-z$][\w$]*)([\s\S]*)$/i;
var R_OPEN_ACCESS = /^\s*(['"])([\s\S]*)$/;
var R_STRING1 = /^((?:\\[\s\S]|[^"])*)"([\s\S]*)$/;
var R_ESCAPED = /\\([\s\S])/g;
var R_STRING2 = /^((?:\\[\s\S]|[^'])*)'([\s\S]*)$/;
var R_CLOSE_ACCESS = /^\s*]([\s\S]*)$/;
function unescape(s) {
return s.replace(R_ESCAPED, '$1');
}
function _parse(str) {
/* eslint complexity: 0 */
var s = str;
var m;
var state = '?';
var parts = [];
if (R_DEGRADE.test(s)) {
state = '.';
}
while (state !== 'EOF') {
switch (state) {
case '?':
m = R_SEARCH.exec(s);
if (m) {
s = m[2];
state = m[1];
break;
}
state = 'EOF';
break;
case '.':
m = R_IDENT.exec(s);
if (m) {
parts.push(m[1]);
s = m[2];
state = '?';
break;
}
state = 'INVALID';
break;
case '[':
m = R_OPEN_ACCESS.exec(s);
if (m) {
s = m[2];
state = m[1];
break;
}
state = 'INVALID';
break;
case '"':
m = R_STRING1.exec(s);
if (m) {
parts.push(unescape(m[1]));
s = m[2];
state = ']';
break;
}
state = 'INVALID';
break;
case '\'':
m = R_STRING2.exec(s);
if (m) {
parts.push(unescape(m[1]));
s = m[2];
state = ']';
break;
}
state = 'INVALID';
break;
case ']':
m = R_CLOSE_ACCESS.exec(s);
if (m) {
s = m[1];
state = '?';
break;
}
state = 'INVALID';
break;
default:
throw new SyntaxError(str);
}
}
return parts;
}
module.exports = _parse;