-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathregex.js
More file actions
executable file
·76 lines (70 loc) · 2.78 KB
/
Copy pathregex.js
File metadata and controls
executable file
·76 lines (70 loc) · 2.78 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
function regex(parseTreeRootNode) {
var matcherFunctions = [];
function walkParseTree(node) {
var buildMatcher = node.matched.buildMatcher;
if (buildMatcher) {
matcherFunctions.push(buildMatcher(node, matcherFunctions));
} else {
node.children.forEach(walkParseTree);
}
}
walkParseTree(parseTreeRootNode);
function makeMatchStateList(){
var states = [];
return {
addMatchState : function(remainingText, capturedTextArray) {
states.push({
remainingText : remainingText,
capturedTextArray : capturedTextArray
})
},
forEachMatchState : function(fn) {
states.forEach(fn);
},
size : function() {
return states.length;
},
contains : function(value) {
return states.some(function(matchState){
return matchState.remainingText === value;
});
}
};
}
return {
toString : function(){
return node.toString();
},
match: function (remainingText) {
var matchStates = makeMatchStateList();
matchStates.addMatchState(remainingText, []);
var allMatchersMatched = matcherFunctions.every(function (matcher) {
var newMatchStates = makeMatchStateList();
matchStates.forEachMatchState(function(matchState) {
var possibleMatches = matcher(matchState.remainingText, matchState.capturedTextArray);
(possibleMatches || []).forEach(function(match) {
var textToMatch = matchState.remainingText.substring(match.length);
var capturedTextArray = matchState.capturedTextArray;
if (matcher.isCapturingGroup) {
capturedTextArray = capturedTextArray.concat(match);
}
newMatchStates.addMatchState(textToMatch, capturedTextArray);
});
});
matchStates = newMatchStates;
return matchStates.size() > 0;
});
var allTextMatched = matchStates.contains('');
var matchSet = set();
matchStates.forEachMatchState(function(matchState){
matchSet.add(remainingText.substring(0, remainingText.length - matchState.remainingText.length))
});
return {
matchSet : matchSet,
allTextMatched : allTextMatched,
allMatchersMatched : allMatchersMatched,
matches : allTextMatched && allMatchersMatched
};
}
};
}