-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressionism.js
More file actions
124 lines (99 loc) · 2.99 KB
/
expressionism.js
File metadata and controls
124 lines (99 loc) · 2.99 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
module.exports.expr = expr;
function expr() {
return argsToArray(arguments).reduce(combine);
}
module.exports.combine = combine;
function combine(regex1, regex2) {
var source1;
var source2;
if(typeof regex2 == 'string') {
source2 = escapeSpecialCharacters(regex2);
} else {
source2 = regex2.source;
}
if(typeof regex1 == 'string') {
source1 = escapeSpecialCharacters(regex1);
} else {
source1 = regex1.source;
}
return new RegExp(source1 + source2);
}
module.exports.zeroOrOne = zeroOrOne;
function zeroOrOne() {
var pattern = argsToArray(arguments).map(regexToString).join('');
return new RegExp(quantify(escapeSpecialCharacters(pattern), '?'));
};
module.exports.zeroOrMore = zeroOrMore;
function zeroOrMore(pattern) {
var pattern = argsToArray(arguments).map(regexToString).join('');
return new RegExp(quantify(escapeSpecialCharacters(pattern), '*'));
};
module.exports.oneOrMore = oneOrMore;
function oneOrMore(pattern) {
var pattern = argsToArray(arguments).map(regexToString).join('');
return new RegExp(quantify(escapeSpecialCharacters(pattern), '+'));
};
module.exports.anythingBut = anythingBut;
function anythingBut(pattern) {
var pattern = regexToString(pattern);
// TODO: Improve type checking
if(typeof pattern == 'string') {
return negateClasses("[" + pattern + "]");
} else {
return negateClasses(pattern.source);
}
};
module.exports.many = many;
function many(pattern, quantity1, quantity2) {
if(arguments.length === 2) {
return new RegExp(pattern + "{" + quantity1 + "}");
}
if(arguments.length === 3) {
switch([quantity1, quantity2]) {
}
return new RegExp(pattern + "{" + quantity1 + "," + quantity2 + "}");
}
}
module.exports.or = /|/;
module.exports.word = /\w/;
module.exports.digit = /\d/;
module.exports.space = /\s/;
function quantify(pattern, quantifier) {
if(pattern.length > 1) {
return "[" + pattern + "]" + quantifier;
}
return pattern + quantifier;
}
function negateClasses(pattern) {
// TODO: Ignore escaped brackets
// TODO: Profile performance
return new RegExp(pattern.replace('[', '[^'))
}
function escapeSpecialCharacters(pattern) {
var str = "";
for(var index = 0, limit = pattern.length; index < limit; index++) {
if(['?', '*', '+', '-', '[', ']', '{', '}', '|', '.'].indexOf(pattern.charAt(index)) > -1 && pattern.charAt(index - 1) !== '\\') {
str += '\\' + pattern.charAt(index);
} else {
str += pattern.charAt(index);
}
}
return str;
}
function argsToArray(args) {
var arr = [];
for(var index = 0, length = args.length; index < length; index++) {
arr.push(args[index]);
}
return arr;
}
function argsToString(args) {
return argsToArray(args).join('');
}
function regexToString(obj) {
// TODO: Improve type checking
if(obj.source) {
return obj.source;
}
return obj;
}