-
Notifications
You must be signed in to change notification settings - Fork 4
/
regex-mode.js
211 lines (178 loc) · 7.06 KB
/
regex-mode.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (c) 2013 Peter Flynn.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, regexp: true */
/*global define, brackets, CodeMirror */
/**
* A CodeMirror mode for syntax-highlighting regular expressions, mapped to .regex file extension.
* Each line is assumed to be a separate regex.
*
* The fact that escaped "()"s are a different token class from unescaped ones means CodeMirror's
* standard brace matching will work correctly with this mode as well.
*/
define(function (require, exports, module) {
"use strict";
// Brackets modules
var LanguageManager = brackets.getModule("language/LanguageManager"),
CodeMirror = brackets.getModule("thirdparty/CodeMirror2/lib/codemirror");
function clearState(state) {
state.nesting = [];
state.quantifiable = false;
state.justOpened = null;
return state;
}
function startState() {
return clearState({});
}
var charClasses_arr = ["d", "D", "w", "W", "s", "S", "t", "r", "n", "v", "f", "b", "B", "0"];
var charClasses = {};
charClasses_arr.forEach(function (ch) { charClasses[ch] = true; });
function token(stream, state) {
var newScope = state.justOpened;
if (newScope) {
state.justOpened = null;
if (newScope === "(") {
if (stream.match(/\?[:=!]/)) {
// ?: is non-capturing qualifier
// ?= and ?! are only-if-[not]-followed-by directives, technically making the whole group a quantifier
state.quantifiable = false;
return "keyword";
}
} else if (newScope === "[") {
if (stream.eat("^")) { // char set inversion
state.quantifiable = false;
return "keyword";
}
}
}
var ch = stream.next();
// Reset for new regexp at start of next line
if (!ch) {
clearState(state);
return;
}
function pushNest() {
state.nesting.push(ch);
state.justOpened = ch;
}
function popNest() {
state.nesting.pop();
}
if (ch === "\\") {
state.quantifiable = true;
var nextCh = stream.next();
if (charClasses[nextCh]) {
return "atom";
} else if (nextCh === "u") {
stream.next();
stream.next();
stream.next();
stream.next();
return "atom";
} else if (nextCh === "x") {
stream.next();
stream.next();
return "atom";
} else if (!nextCh) {
return "error"; // regexp cannot end in \
} else {
return null; // just a random escaped character
// TODO: render the "\" slightly grayed out?
}
// TODO: \n backreferences (n>0)
}
if (ch === ".") {
state.quantifiable = true;
return "atom";
}
var scope = state.nesting[state.nesting.length - 1]; // may yield undefined
if (scope === "[") {
if (ch === "]") {
popNest();
state.quantifiable = true; // overall char set can be quantified
// (else: no need to set quantifiable=false otherwise, since quantifiers not accepted inside char clas anyway)
} else if (ch === "-" && !newScope) {
return "keyword"; // char range (unless 1st char after [)
}
return "number";
}
if (ch === "(") {
pushNest();
state.quantifiable = false;
return "bracket";
} else if (ch === "[") {
pushNest();
state.quantifiable = false;
return "number";
} else if (ch === ")") {
state.quantifiable = true; // overall group can be quantified
if (scope === "(") {
popNest();
return "bracket";
} else {
return "error";
}
}
// Note: closing "]" floating outside a "[" context is fine - just plain text
// Start-line/end-line marker
// Theyre actually interpreted this way even if not at start/end of regexp... which does make sense sometimes, e.g. with | operator
if (ch === "^") {
state.quantifiable = false;
return "keyword";
}
if (ch === "$") {
state.quantifiable = false;
return "keyword";
}
// Quantifiers
function handleQuantifier() {
if (state.quantifiable) {
state.quantifiable = false;
return "rangeinfo";
} else {
return "error";
}
}
if (ch === "+" || ch === "*" || ch === "?") {
stream.eat("?"); // +? or *? or ?? (non-greedy quantifier)
return handleQuantifier();
} else if (ch === "{") {
if (stream.match(/\d+(,\d*)?\}/)) {
// TODO: turn red if n>m (ok if n==m though)
return handleQuantifier();
}
// Anything after "{" other than {n} {n,} or {n,m} makes it just plain text to match
return null;
}
if (ch === "|") {
state.quantifiable = false;
return "keyword";
}
// Any other plain text chars to match (e.g. letters or digits)
state.quantifiable = true;
return null;
}
var modeFactory = function () {
return { token: token, startState: startState };
};
CodeMirror.defineMode("regex", modeFactory);
exports.mode = modeFactory();
});