/
modes.js
148 lines (144 loc) · 3.64 KB
/
modes.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
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
import { inherit } from './utils';
import * as regex from './regex';
// Common regexps
export const IDENT_RE = '[a-zA-Z]\\w*';
export const UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*';
export const NUMBER_RE = '\\b\\d+(\\.\\d+)?';
export const C_NUMBER_RE = '(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)'; // 0x..., 0..., decimal, float
export const BINARY_NUMBER_RE = '\\b(0b[01]+)'; // 0b...
export const RE_STARTERS_RE = '!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~';
export const SHEBANG = (opts = {}) => {
if (opts.binary) {
opts.begin = regex.concat(
/^#!\//,
'.*',
opts.binary,
'.*');
}
return inherit({
className: 'meta',
begin: /^#!\//,
end: /$/,
illegal: '\\n',
relevance: 0,
"on:begin": (m, resp) => {
if (m.index != 0 ) resp.ignoreMatch();
}
}, opts);
};
// Common modes
export const BACKSLASH_ESCAPE = {
begin: '\\\\[\\s\\S]', relevance: 0
};
export const APOS_STRING_MODE = {
className: 'string',
begin: '\'',
end: '\'',
illegal: '\\n',
contains: [BACKSLASH_ESCAPE]
};
export const QUOTE_STRING_MODE = {
className: 'string',
begin: '"',
end: '"',
illegal: '\\n',
contains: [BACKSLASH_ESCAPE]
};
export const PHRASAL_WORDS_MODE = {
begin: /\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/
};
export const COMMENT = function(begin, end, inherits) {
var mode = inherit(
{
className: 'comment',
begin: begin,
end: end,
contains: []
},
inherits || {}
);
mode.contains.push(PHRASAL_WORDS_MODE);
mode.contains.push({
className: 'doctag',
begin: '(?:TODO|FIXME|NOTE|BUG|XXX):',
relevance: 0
});
return mode;
};
export const C_LINE_COMMENT_MODE = COMMENT('//', '$');
export const C_BLOCK_COMMENT_MODE = COMMENT('/\\*', '\\*/');
export const HASH_COMMENT_MODE = COMMENT('#', '$');
export const NUMBER_MODE = {
className: 'number',
begin: NUMBER_RE,
relevance: 0
};
export const C_NUMBER_MODE = {
className: 'number',
begin: C_NUMBER_RE,
relevance: 0
};
export const BINARY_NUMBER_MODE = {
className: 'number',
begin: BINARY_NUMBER_RE,
relevance: 0
};
export const CSS_NUMBER_MODE = {
className: 'number',
begin: NUMBER_RE + '(' +
'%|em|ex|ch|rem' +
'|vw|vh|vmin|vmax' +
'|cm|mm|in|pt|pc|px' +
'|deg|grad|rad|turn' +
'|s|ms' +
'|Hz|kHz' +
'|dpi|dpcm|dppx' +
')?',
relevance: 0
};
export const REGEXP_MODE = {
// this outer rule makes sure we actually have a WHOLE regex and not simply
// an expression such as:
//
// 3 / something
//
// (which will then blow up when regex's `illegal` sees the newline)
begin: /(?=\/[^/\n]*\/)/,
contains: [{
className: 'regexp',
begin: /\//,
end: /\/[gimuy]*/,
illegal: /\n/,
contains: [
BACKSLASH_ESCAPE,
{
begin: /\[/,
end: /\]/,
relevance: 0,
contains: [BACKSLASH_ESCAPE]
}
]
}]
};
export const TITLE_MODE = {
className: 'title',
begin: IDENT_RE,
relevance: 0
};
export const UNDERSCORE_TITLE_MODE = {
className: 'title',
begin: UNDERSCORE_IDENT_RE,
relevance: 0
};
export const METHOD_GUARD = {
// excludes method names from keyword processing
begin: '\\.\\s*' + UNDERSCORE_IDENT_RE,
relevance: 0
};
export const END_SAME_AS_BEGIN = function(mode) {
return Object.assign(mode,
{
'on:begin': (m, resp) => { resp.data._beginMatch = m[1]; },
'on:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch() }
});
};