inputscanner.js
5.5 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
/*jshint node:true */
/*
The MIT License (MIT)
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict';
var regexp_has_sticky = RegExp.prototype.hasOwnProperty('sticky');
function InputScanner(input_string) {
this.__input = input_string || '';
this.__input_length = this.__input.length;
this.__position = 0;
}
InputScanner.prototype.restart = function() {
this.__position = 0;
};
InputScanner.prototype.back = function() {
if (this.__position > 0) {
this.__position -= 1;
}
};
InputScanner.prototype.hasNext = function() {
return this.__position < this.__input_length;
};
InputScanner.prototype.next = function() {
var val = null;
if (this.hasNext()) {
val = this.__input.charAt(this.__position);
this.__position += 1;
}
return val;
};
InputScanner.prototype.peek = function(index) {
var val = null;
index = index || 0;
index += this.__position;
if (index >= 0 && index < this.__input_length) {
val = this.__input.charAt(index);
}
return val;
};
// This is a JavaScript only helper function (not in python)
// Javascript doesn't have a match method
// and not all implementation support "sticky" flag.
// If they do not support sticky then both this.match() and this.test() method
// must get the match and check the index of the match.
// If sticky is supported and set, this method will use it.
// Otherwise it will check that global is set, and fall back to the slower method.
InputScanner.prototype.__match = function(pattern, index) {
pattern.lastIndex = index;
var pattern_match = pattern.exec(this.__input);
if (pattern_match && !(regexp_has_sticky && pattern.sticky)) {
if (pattern_match.index !== index) {
pattern_match = null;
}
}
return pattern_match;
};
InputScanner.prototype.test = function(pattern, index) {
index = index || 0;
index += this.__position;
if (index >= 0 && index < this.__input_length) {
return !!this.__match(pattern, index);
} else {
return false;
}
};
InputScanner.prototype.testChar = function(pattern, index) {
// test one character regex match
var val = this.peek(index);
pattern.lastIndex = 0;
return val !== null && pattern.test(val);
};
InputScanner.prototype.match = function(pattern) {
var pattern_match = this.__match(pattern, this.__position);
if (pattern_match) {
this.__position += pattern_match[0].length;
} else {
pattern_match = null;
}
return pattern_match;
};
InputScanner.prototype.read = function(starting_pattern, until_pattern, until_after) {
var val = '';
var match;
if (starting_pattern) {
match = this.match(starting_pattern);
if (match) {
val += match[0];
}
}
if (until_pattern && (match || !starting_pattern)) {
val += this.readUntil(until_pattern, until_after);
}
return val;
};
InputScanner.prototype.readUntil = function(pattern, until_after) {
var val = '';
var match_index = this.__position;
pattern.lastIndex = this.__position;
var pattern_match = pattern.exec(this.__input);
if (pattern_match) {
match_index = pattern_match.index;
if (until_after) {
match_index += pattern_match[0].length;
}
} else {
match_index = this.__input_length;
}
val = this.__input.substring(this.__position, match_index);
this.__position = match_index;
return val;
};
InputScanner.prototype.readUntilAfter = function(pattern) {
return this.readUntil(pattern, true);
};
InputScanner.prototype.get_regexp = function(pattern, match_from) {
var result = null;
var flags = 'g';
if (match_from && regexp_has_sticky) {
flags = 'y';
}
// strings are converted to regexp
if (typeof pattern === "string" && pattern !== '') {
// result = new RegExp(pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), flags);
result = new RegExp(pattern, flags);
} else if (pattern) {
result = new RegExp(pattern.source, flags);
}
return result;
};
InputScanner.prototype.get_literal_regexp = function(literal_string) {
return RegExp(literal_string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
};
/* css beautifier legacy helpers */
InputScanner.prototype.peekUntilAfter = function(pattern) {
var start = this.__position;
var val = this.readUntilAfter(pattern);
this.__position = start;
return val;
};
InputScanner.prototype.lookBack = function(testVal) {
var start = this.__position - 1;
return start >= testVal.length && this.__input.substring(start - testVal.length, start)
.toLowerCase() === testVal;
};
module.exports.InputScanner = InputScanner;