forked from marko-js/htmljs-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.js
65 lines (50 loc) · 1.49 KB
/
operators.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
var operators = exports.operators = [
//Multiplicative Operators
'*', '/', '%',
//Additive Operators
'+', '-',
//Bitwise Shift Operators
'<<', '>>', '>>>',
//Relational Operators
'<', '<=', '>', '>=',
// Readable Operators
// NOTE: These become reserved words and cannot be used as attribute names
'instanceof',
'in',
// 'from', -- as in <import x from './file'/>
// 'typeof', -- would need to look behind, not ahead
// Equality Operators
'==', '!=', '===', '!==',
// Binary Bitwise Operators
'&', '^', '|',
// Binary Logical Operators
'&&', '||',
// Ternary Operator
'?', ':',
// Member
'['
];
// Look for longest operators first
operators.sort(function(a, b) {
return b.length - a.length;
});
var requiresWhitespace = exports.requiresWhitespace = {
'instanceof':true,
'in':true,
'typeof':true
};
var escapedOperators = operators.map(o => {
if(requiresWhitespace[o]) {
return '\\s'+escapeNonAlphaNumeric(o)+'\\s';
}
if(o === '/') {
return '\\/(?:\\b|\\s)'; //make sure this isn't a comment
}
return escapeNonAlphaNumeric(o);
});
exports.longest = operators.sort((a, b) => b.length-a.length)[0].length+1;
exports.patternNext = new RegExp('^\\s*('+escapedOperators.join('|')+')\\s*(?!-)');
exports.patternPrev = new RegExp('[^-+](?:'+escapedOperators.join('|')+')(\\s*)$');
function escapeNonAlphaNumeric(str) {
return str.replace(/([^\w\d])/g, '\\$1');
}