-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternMatcher.js
27 lines (24 loc) · 1.05 KB
/
PatternMatcher.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
class PatternMatcher {
constructor() {
/* Create a map object using the regexes as the keys, and a friendly name for the values. */
this.regexList = new Map();
this.regexList.set(/\/\*[\s\S]*?/, 'comment_ML_StartComment');
this.regexList.set(/[\s\S]*?\*\//, 'comment_ML_EndComment');
this.regexList.set(/^(\/\/.*)|^(\s.*\/\/.*)/, 'comment_SL_StartOfLine');
this.regexList.set(/[^\s][^\/]\/\/.*/, 'comment_SL_EndOfLine');
this.regexList.set(/(^(\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^\s*$/gm, 'blankLine');
}
}
/* Generator function to yield the friendly names of any matched objects */
PatternMatcher.prototype.checkMatchGenerator = function* (line) {
for (regex of this.regexList) {
if (regex[0].test(line)) {
yield regex[1];
}
}
}
/* Run the generator function and return the full array once complete */
PatternMatcher.prototype.checkMatch = function (line) {
return [...this.checkMatchGenerator(line)];
}
exports.PatternMatcher = PatternMatcher;