This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringStream.js
252 lines (230 loc) · 6.57 KB
/
stringStream.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
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
/**
* A trimmed version of CodeMirror's StringStream module for string parsing
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
/**
* @type StringStream
* @constructor
* @param {String} string Assuming that bound string should be
* immutable
*/
function StringStream(string) {
this.pos = this.start = 0;
this.string = string;
this._length = string.length;
}
StringStream.prototype = {
/**
* Returns true only if the stream is at the end of the line.
* @returns {Boolean}
*/
eol: function() {
return this.pos >= this._length;
},
/**
* Returns true only if the stream is at the start of the line
* @returns {Boolean}
*/
sol: function() {
return this.pos === 0;
},
/**
* Returns the next character in the stream without advancing it.
* Will return <code>undefined</code> at the end of the line.
* @returns {String}
*/
peek: function() {
return this.string.charAt(this.pos);
},
/**
* Returns the next character in the stream and advances it.
* Also returns <code>undefined</code> when no more characters are available.
* @returns {String}
*/
next: function() {
if (this.pos < this._length)
return this.string.charAt(this.pos++);
},
/**
* match can be a character, a regular expression, or a function that
* takes a character and returns a boolean. If the next character in the
* stream 'matches' the given argument, it is consumed and returned.
* Otherwise, undefined is returned.
* @param {Object} match
* @returns {String}
*/
eat: function(match) {
var ch = this.string.charAt(this.pos), ok;
if (typeof match == "string")
ok = ch == match;
else
ok = ch && (match.test ? match.test(ch) : match(ch));
if (ok) {
++this.pos;
return ch;
}
},
/**
* Repeatedly calls <code>eat</code> with the given argument, until it
* fails. Returns <code>true</code> if any characters were eaten.
* @param {Object} match
* @returns {Boolean}
*/
eatWhile: function(match) {
var start = this.pos;
while (this.eat(match)) {}
return this.pos > start;
},
/**
* Shortcut for <code>eatWhile</code> when matching white-space.
* @returns {Boolean}
*/
eatSpace: function() {
var start = this.pos;
while (/[\s\u00a0]/.test(this.string.charAt(this.pos)))
++this.pos;
return this.pos > start;
},
/**
* Moves the position to the end of the line.
*/
skipToEnd: function() {
this.pos = this._length;
},
/**
* Skips to the next occurrence of the given character, if found on the
* current line (doesn't advance the stream if the character does not
* occur on the line). Returns true if the character was found.
* @param {String} ch
* @returns {Boolean}
*/
skipTo: function(ch) {
var found = this.string.indexOf(ch, this.pos);
if (found > -1) {
this.pos = found;
return true;
}
},
/**
* Skips to <code>close</code> character which is pair to <code>open</code>
* character, considering possible pair nesting. This function is used
* to consume pair of characters, like opening and closing braces
* @param {String} open
* @param {String} close
* @returns {Boolean} Returns <code>true</code> if pair was successfully
* consumed
*/
skipToPair: function(open, close, skipString) {
var braceCount = 0, ch;
var pos = this.pos, len = this._length;
while (pos < len) {
ch = this.string.charAt(pos++);
if (ch == open) {
braceCount++;
} else if (ch == close) {
braceCount--;
if (braceCount < 1) {
this.pos = pos;
return true;
}
} else if (skipString && (ch == '"' || ch == "'")) {
this.skipString(ch);
}
}
return false;
},
/**
* A helper function which, in case of either single or
* double quote was found in current position, skips entire
* string (quoted value)
* @return {Boolean} Wether quoted string was skipped
*/
skipQuoted: function(noBackup) {
var ch = this.string.charAt(noBackup ? this.pos : this.pos - 1);
if (ch === '"' || ch === "'") {
if (noBackup) {
this.pos++;
}
return this.skipString(ch);
}
},
/**
* A custom function to skip string literal, e.g. a "double-quoted"
* or 'single-quoted' value
* @param {String} quote An opening quote
* @return {Boolean}
*/
skipString: function(quote) {
var pos = this.pos, len = this._length, ch;
while (pos < len) {
ch = this.string.charAt(pos++);
if (ch == '\\') {
continue;
} else if (ch == quote) {
this.pos = pos;
return true;
}
}
return false;
},
/**
* Backs up the stream n characters. Backing it up further than the
* start of the current token will cause things to break, so be careful.
* @param {Number} n
*/
backUp : function(n) {
this.pos -= n;
},
/**
* Act like a multi-character <code>eat</code>—if <code>consume</code> is true or
* not given—or a look-ahead that doesn't update the stream position—if
* it is false. <code>pattern</code> can be either a string or a
* regular expression starting with ^. When it is a string,
* <code>caseInsensitive</code> can be set to true to make the match
* case-insensitive. When successfully matching a regular expression,
* the returned value will be the array returned by <code>match</code>,
* in case you need to extract matched groups.
*
* @param {RegExp} pattern
* @param {Boolean} consume
* @param {Boolean} caseInsensitive
* @returns
*/
match: function(pattern, consume, caseInsensitive) {
if (typeof pattern == "string") {
var cased = caseInsensitive
? function(str) {return str.toLowerCase();}
: function(str) {return str;};
if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
if (consume !== false)
this.pos += pattern.length;
return true;
}
} else {
var match = this.string.slice(this.pos).match(pattern);
if (match && consume !== false)
this.pos += match[0].length;
return match;
}
},
/**
* Get the string between the start of the current token and the
* current stream position.
* @returns {String}
*/
current: function(backUp) {
return this.string.slice(this.start, this.pos - (backUp ? 1 : 0));
}
};
module.exports = function(string) {
return new StringStream(string);
};
/** @deprecated */
module.exports.create = module.exports;
return module.exports;
});