-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
234 lines (227 loc) · 5.51 KB
/
Copy pathparse.js
File metadata and controls
234 lines (227 loc) · 5.51 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
steal("steal/parse/tokens.js")
.then('steal/build').then(function(steal){
var isArray = function( array ) {
return Object.prototype.toString.call( array ) === "[object Array]";
},
same = function(a, b){
a = steal.extend({},a);
for(var name in b){
if(b[name] != a[name]){
return false;
}else{
delete a[name];
}
}
//there should be nothing left in a
for(var name in a){
return false;
}
return true;
},
// a is like b, but doesn't have to have all of b's properties
like = function(a, b){
for(var name in a){
if(b[name] != a[name]){
return false;
}
}
return true;
};
/**
* @class steal.parse
* @parent stealjs
* Returns an pull parser useful for walking through
* token streams.
*
* var p = steal.parse(" steal.dev.log('fo(') ");
*
* //parses until it finds thing(
* p.until( [ "thing", "(" ] );
*
* //parse until it finds the matching ) to (
* p.partner("(");
*
* ## API
* @constructor
* @param {String} str
* @return {steal.parse} an object that can be used to pull tokens.
*/
steal.parse = function(str){
//print("Breaking up strs")
var tokens = str.tokens('=<>!+-*&|/%^', '=<>&|'),
tokenNum = 0,
lines;
var moveNext = function(ignoreComments){
var next = tokens[tokenNum++];
if(next){
//print("Next TOken = "+next.value);
}
if(next && ignoreComments && next.type === 'comment'){
return moveNext(ignoreComments);
}else{
return next;
}
},
getLineNum = function(pos){
if(!lines){
lines = str.split("\n");
}
var cur = 0,
line = 0;
while(pos < cur+lines[line].length){
line++;
}
return line
}
return {
/**
* @attribute ignoreComments
* A boolean you can set to ignore comments.
* Comments are ignored by default.
*
* p.ignoreComments = true
*/
ignoreComments : true,
/**
* Moves to the next token and returns it.
*
* var p = steal.parse("CONTENT"),
* cur
* while( cur = p.moveNext() ){
*
* }
*
* @return {token} A token like:
*
* {from: 22, to: 24, value: "hi", type: "string"}
*/
moveNext : function(){
return moveNext(this.ignoreComments)
},
/**
* Returns the next token.
*
* @return {token} A token like:
*
* {from: 22, to: 24, value: "hi", type: "string"}
*/
next : function(){
return tokens[tokenNum];
},
/**
* Returns the current token.
*
* @return {token} A token like:
*
* {from: 22, to: 24, value: "hi", type: "string"}
*/
cur : function(){
return tokens[tokenNum-1];
},
lineNum : function(token){
token = token || tokens[tokenNum];
return getLineNum(token.from);
},
line : function(tokenOrLineNum){
token = token || tokens[tokenNum];
},
/**
* Parses it until it finds the right partner of the
* left parameter.
*
* p.partner("(", function(token){
*
* })
*
* @param {String} left a string like (,[,{,<
* @param {Function} cb a function that gets called
* with all tokens between the left and right token.
* @return {token} the ending token
*/
partner : function(left, cb){
var right = {
"(" : ")",
"[" : "]",
"{" : "}",
"<" : ">"
}[left],
count = 1,
token,
last,
prev;
if(this.cur().value != left){
this.until(left);
}
while(token = this.moveNext()){
if(token.type == 'operator'){
if(token.value === left){
count++;
}else if(token.value === right){
count--;
//print(" -"+count+" "+prev+" "+last)
if(count === 0){
cb && cb(token);
return token;
}
}else if(token.value === "/"){
print("YOU SHOULD NOT BE HERE")
this.comment();
}
}
cb && cb(token)
prev = last;
last = (token.value)
}
},
/**
* Parses until it finds something you are looking for.
*
* until("function",")") -> looks for function or )
* until(["foo",".","bar"]) -> looks for foo.bar
*
* @return {Array} an array of tokens of the matches. The last item in the array
* is the last part matched.
*/
until: function(){
var token,
//where in each Pattern we've got a match
patternMatchPosition = [],
// an array of pattern arrays ...
patterns = [],
//makes an option into a token that can be compared against
makeTokens = function(tokens){
var res = [];
for(var i =0 ; i < tokens.length; i++){
res.push(typeof tokens[i] === 'string' ? {value : tokens[i]} : tokens[i])
}
return res;
},
callback = function(){};
for(var i =0; i < arguments.length;i++){
patternMatchPosition[i] =[];
if(isArray(arguments[i])){
patterns.push(makeTokens(arguments[i]))
}else if(typeof arguments[i] == 'function'){
callback = arguments[i];
}
else{
patterns.push(makeTokens([arguments[i]]))
}
}
while (token = this.moveNext() ) {
for(i =0; i< patterns.length; i++){
var pattern = patterns[i];
if( token.type !== "string" && like( pattern[patternMatchPosition[i].length], token) ){
patternMatchPosition[i].push(token);
if(patternMatchPosition[i].length === pattern.length){
return patternMatchPosition[i];
}
}else{
patternMatchPosition[i] = [];
}
}
}
}
}
};
})