-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyntax.js
executable file
·247 lines (207 loc) · 5.49 KB
/
syntax.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
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
import Regexp from './buffer/regexp.js'
var R = Regexp.create;
var NewLine = R(['newline'], 'g')
//NOTE: order matters
var syntax = Regexp.join([
'newline',
'comment',
['number', R(['special', 'number'], '')],
'operator',
'symbol',
// 'params',
'attribute',
// 'keyword',
['variable', R(['variable','call'], '')],
'keyword',
// ['keyword', R(['operator'])],
// 'string',
'definition',
], 'gm')
var Indent = {
regexp: R(['indent'], 'gm'),
replacer: (s) => s.replace(/ {1,2}|\t/g, '<x>$&</x>')
};
var AnyChar = /\S/g;
var Blocks = Regexp.join([
'comment',
// 'string',
// ['definition', R(['arguments']), '^'],
['property', R(['declare'])],
['keyword', R(['keyword'])],
['string', R(['string'])],
// 'regexp',
], 'gm');
var LongLines = /(^.{1000,})/gm;
var Tag = {
'//': 'comment',
'/*': 'comment',
'`': 'string',
'"': 'string',
"'": 'string',
// '/': 'regexp',
};
export default function Syntax(o) {
o = o || {};
this.tab = o.tab || '\t';
this.blocks = [];
this.blockIndex = 0
}
Syntax.prototype.entities = entities;
Syntax.prototype.highlight = function(code, offset) {
code += '\n`*/\n'
// code = this.createIndents(code);
code = this.createBlocks(code);
// console.log(code)
// code = entities(code);
const pieces = []
let match, piece, lastPos = 0, text, add = 0
while (match = syntax.exec(code)) {
if (match.index > lastPos) {
text = code.slice(lastPos, match.index)
let blocks = this.restoreBlocks(text)
blocks.forEach(block => {
pieces.push([block[0], block[1], block[2] + lastPos])
})
}
piece = Object.entries(match.groups).filter(([key, value]) => value !== undefined)[0]
piece.push(match.index)
pieces.push(piece)
lastPos = match.index + piece[1].length
}
// code = this.restoreBlocks(code);
// code = code.replace(Indent.regexp, Indent.replacer);
pieces.pop()
while (pieces.pop()[0] !== 'newline') {} // whoa
return pieces
};
Syntax.prototype.createIndents = function(code) {
var lines = code.split(/\n/g);
var indent = 0;
var match;
var line;
var i;
i = lines.length;
while (i--) {
line = lines[i];
AnyChar.lastIndex = 0;
match = AnyChar.exec(line);
if (match) indent = match.index;
else if (indent && !line.length) {
lines[i] = new Array(indent + 1).join(this.tab);
}
}
code = lines.join('\n');
return code;
};
Syntax.prototype.restoreBlocks = function(code) {
var block
var blocks = this.blocks
var regexp = /\uffee/g
let match, lastPos = 0, text, add = 0, out = []
let newLineMatch, lastNewLinePos = 0
while (match = regexp.exec(code)) {
if (match.index > lastPos) {
text = code.slice(lastPos, match.index)
if (text.length) {
out.push(['text', text, lastPos])
}
add += text.length
}
block = blocks[this.blockIndex++]
var tag = block[0]
lastNewLinePos = 0
while (newLineMatch = NewLine.exec(block[1])) {
text = block[1].slice(lastNewLinePos, newLineMatch.index)
out.push([tag, text, lastNewLinePos + add])
out.push(['newline', '\n', newLineMatch.index + add])
lastNewLinePos = newLineMatch.index + 1
}
if (!lastNewLinePos) {
out.push([tag, block[1], match.index])
} else {
out.push([tag, block[1].slice(lastNewLinePos), lastNewLinePos + add])
}
add += block[1].length
lastPos = match.index + block[1].length
}
text = code.slice(lastPos)
if (text.length) out.push(['text', text, lastPos])
return out
};
Syntax.prototype.createBlocks = function(code) {
this.blocks = [];
this.blockIndex = 0;
let parts = []
let match, piece, lastPos = 0, text, add = 0
while (match = Blocks.exec(code)) {
if (match.index > lastPos) {
text = code.slice(lastPos, match.index)
parts.push(text)
}
piece = Object.entries(match.groups).filter(([key, value]) => value !== undefined)[0]
piece.push(match.index)
this.blocks.push(piece)
parts.push('\uffee' + ' '.repeat(piece[1].length - 1))
lastPos = match.index + piece[1].length
}
if (lastPos < code.length) parts.push(code.slice(lastPos))
return parts.join('')
// code = code
// // .replace(LongLines, (block) => {
// // this.blocks.push(block);
// // return '\uffec';
// // })
// .replace(Blocks, (block, a, b, c, d, e, f, g) => {
// console.log(block, a, b, c, d, e, f, g)
// this.blocks.push(block)
// return '\uffee' + ','.repeat(block.length - 1);
// });
// return code;
};
function createId() {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var length = alphabet.length - 1;
var i = 6;
var s = '';
while (i--) {
s += alphabet[Math.random() * length | 0];
}
return s;
}
function entities(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
;
}
function compile(regexp, tag) {
var openTag = '<' + tag + '>';
var closeTag = '</' + tag + '>';
return {
name: tag,
regexp: regexp,
replacer: openTag + '$&' + closeTag
};
}
function map(obj, fn) {
var result = {};
for (var key in obj) {
result[key] = fn(obj[key], key);
}
return result;
}
function replace(pass, code) {
for (var i = 0; i < pass.length; i++) {
code = code.replace(pass[i][0], pass[i][1]);
}
return code;
}
function insert(offset, string, part) {
return string.slice(0, offset) + part + string.slice(offset);
}
function identify(block) {
var one = block[0];
var two = one + block[1];
return Tag[two] || Tag[one];
}