-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
274 lines (245 loc) · 7.92 KB
/
index.js
File metadata and controls
274 lines (245 loc) · 7.92 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
jQuery(document).keydown(function(event) {
// If Control or Command key is pressed and the S key is pressed
// run save function. 83 is the key code for S.
if((event.ctrlKey || event.metaKey) && event.which == 13) {
// Save Function
event.preventDefault();
compile();
return false;
}
}
);
const hintText = ["Press Ctrl / Cmd + Enter for compiling","Press Ctrl / Cmd + E for showing code auto complete"];
$("#compile").click(function(){
compile();
});
$('#clear').click(function(){
$("#regex").val('');
$("#flag").val('');
$("#match_string").val('');
$('#match_string').highlightWithinTextarea('update');
});
const codemirror = CodeMirror.fromTextArea(document.getElementById("verbal_regex"), {
lineNumbers: true,
styleActiveLine: true,
theme: 'dracula',
lineWrapping: true
});
$('#match_string').highlightWithinTextarea({
highlight: '',
className: 'pick-color'
});
codemirror.on('change', function() {
codemirror.save();
});
// keymap を指定
codemirror.setCursor({
line: 2,
ch: 1
});
function showSnippet(cm){
snippet();
if(cm.state.completionActive!=null){
let completion = cm.state.completionActive.data;
CodeMirror.on(completion, 'pick', function(completion, element) {
if(completion.text.indexOf("\n") === -1){
const cursor = codemirror.getCursor();
const end = cursor.ch;
const line = cursor.line;
codemirror.setCursor({line:line,ch:end-2});
}
});
}
}
codemirror.setOption('extraKeys', {
'Cmd-E': function (cm) {
showSnippet(cm);
},
'Ctrl-E': function (cm) {
showSnippet(cm);
}
});
// スニペットの配列
const snippets = [{
text: 'VerEx()\n',
displayText: 'VerEx() Head of verbal regex string (also used in recursion case)'
}, {
text: '.find(\'\')',
displayText: '.find(value) Find exactly the given value'
}, {
text: '.maybe(\'\')',
displayText: '.maybe(value) 0 or 1 times'
}, {
text: '.then(\'\')',
displayText: '.then(value) Shorthand for find'
}, {
text: '.anyOf(\'\')',
displayText: '.anyOf(value) Matches any char in value'
}, {
text: '.any(\'\')',
displayText: '.any(value) Shorthand for anyOf'
}, {
text: '.lineBreak()\n',
displayText: '.lineBreak() Matches any linebreak'
}, {
text: '.br()\n',
displayText: '.br() Shorthand for linebreak()'
}, {
text: '.tab()\n',
displayText: '.tab() Match tab char'
}, {
text: '.word()\n',
displayText: '.word() Matches at least one word'
}, {
text: '.digit()\n',
displayText: '.digit() Matches at least one digit'
}, {
text: '.whiteSpace()\n',
displayText: '.whitespace() Matches at least one whitespace'
}, {
text: '.repeatPrevious()\n',
displayText: '.repeatPrevious() Repeats the previous item exactly n times or between n and m times.'
}, {
text: '.oneOrMore()\n',
displayText: '.oneOrMore() Repeats the previous at least once'
}, {
text: '.beginCapture()\n',
displayText: '.beginCapture() Starts a capturing group'
}, {
text: '.endCapture()\n',
displayText: '.endCapture() Ends a capturing group'
}, {
text: '.endOfLine()\n',
displayText: '.endOfLine() Append "$" at end of expression'
}, {
text: '.startOfLine()\n',
displayText: '.startOfLine() Append "^" at start of expression'
}, {
text: '.something()\n',
displayText: '.something() Any character at least one time'
}, {
text: '.somethingBut(\'\')\n',
displayText: '.somethingBut(value) Any character at least one time except for these characters'
}, {
text: '.anything()\n',
displayText: '.anything() Matches everything'
}, {
text: '.anythingBut(\'\')',
displayText: '.anythingBut(value) Matches everything excepting letter in given value'
}, {
text: '.withAnyCase()\n',
displayText: '.withAnyCase() Ignore case insensitive (append modifier "i")'
}, {
text: '.stopAtFirst()\n',
displayText: '.stopAtFirst() Stop at first match (remove modifier "g")'
}, {
text: '.searchOneLine()\n',
displayText: '.searchOneLine() Only search in one line (remove modifier "m")'
}, {
text: '.range(\'\', \'\')',
displayText: '.range(from, to) Add expression to match a range (or multiply ranges)'
}, {
text: '.add(\'\')'
}, {
text: '.multiple(\'\')'
}, {
text: '.or()\n'
}];
function snippet() {
CodeMirror.showHint(codemirror, function() {
const cursor = codemirror.getCursor();
const token = codemirror.getTokenAt(cursor);
let declineStart = 0;
if(codemirror.getTokenAt({line:cursor.line,ch:cursor.ch - token.string.length}).string==='.'){
declineStart = 1;
}
const start = token.string===")" ? token.start+1 : token.start;
const end = cursor.ch;
const line = cursor.line;
const currentWord = token.string;
// 入力した文字列をスニペット配列から探す
const list = snippets.filter(function(item) {
return item.text.toLowerCase().indexOf(currentWord.toLowerCase()) >= 0
});
return {
list: list.length ? list : snippets,
from: CodeMirror.Pos(line, start - declineStart),
to: CodeMirror.Pos(line, end)
}
}, {
completeSingle: false
})
}
function validateVerbalString(verbalString) {
verbalString = verbalString.replace("\n","").replace("\t","");
if (verbalString === "VerEx()") {
throw "Invalid verbal syntax: missing method";
}
if (verbalString.indexOf("\'\'")!==-1) {
throw "Invalid verbal syntax: missing value";
}
if (verbalString.indexOf("VerEx()VerEx()")>=0) {
throw "Invalid verbal syntax: VerEx";
}
if (verbalString.indexOf("VerEx().anything")===0) {
throw "Invalid verbal syntax: anything";
}
if (verbalString.indexOf("VerEx().endOfLine")===0) {
throw "Invalid verbal syntax: endOfLine";
}
if (verbalString.indexOf("VerEx().searchOneLine")===0) {
throw "Invalid verbal syntax: searchOneLine";
}
if (verbalString.indexOf("VerEx().multiple")===0) {
throw "Invalid verbal syntax: multiple";
}
if (verbalString.indexOf("VerEx().or")===0) {
throw "Invalid verbal syntax: or";
}
if (verbalString.indexOf("VerEx().withAnyCase")===0) {
throw "Invalid verbal syntax: withAnyCase";
}
}
function compile() {
let verbalRegex = $("#verbal_regex").val();
let tester = VerEx();
try {
validateVerbalString(verbalRegex);
eval("tester = " + verbalRegex);
let regexString = tester.toString();
let flagPart = regexString.split("/").pop();
let regexPart = '';
if(flagPart !== undefined){
regexPart = regexString.substr(1,regexString.length - 2 - flagPart.length);
}
else{
regexPart = regexString.substr(1,regexString.length - 1);
}
$("#regex").val(regexPart);
$("#flag").val(flagPart);
const re = new RegExp(regexPart,flagPart);
$('#match_string').highlightWithinTextarea({
highlight: re,
className: 'pick-color'
});
}
catch(e){
// TODO show error in a more beautiful way
alert("Error on converting verbal string to Regex:\n" + e);
}
}
let count = 0;
setInterval(function(){
count = count + 1;
count = count % 2;
$("#usage-hint").text(hintText[count]);
}, 5000);
const textarea = document.getElementById('match_string');
tabOverride.autoIndent(false);
tabOverride.set(textarea);
$('#regex').click(function () {
this.select();
});
$('#flag').click(function () {
this.select();
});