-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
359 lines (307 loc) · 10.9 KB
/
index.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// This script create 3 sections: keywords-<A>, handle-attributes-<Z> and abl-functions-<A> . ABL keywords can only be in
// one of these sections.
// kwlist.txt file can be generated with `prowin -zgenkwlist > abl-keywords.txt`
//
// methods.txt can be created by copying the 'Related Links' from https://docs.progress.com/bundle/abl-reference/page/Handle-Attributes-and-Methods-Reference.html//
// It will look something like:
// ACCELERATOR attribute
// ACCEPT-CHANGES( ) method
// ACCEPT-ROW-CHANGES( ) method
//
// abl-functions.txt can be created by copying the 'Related Links' from https://docs.progress.com/bundle/abl-reference/page/ABL-Syntax-Reference.html
//
const fs = require('fs');
let lineReaderMethods = require('readline').createInterface({
input: fs.createReadStream('abl-methods.txt')
});
let lineReaderFunctions = require('readline').createInterface({
input: fs.createReadStream('abl-functions.txt')
});
let lineReaderKeywords = require('readline').createInterface({
input: fs.createReadStream('abl-keywords.txt')
});
let output = 'grammar.json';
let result = {};
let attributeBlocks = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []];
let methodBlocks = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []];
let keywordBlocks = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []];
let functionBlocks = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []];
const re = /(?:\w|-|\()+(?=\s|$)/g
// The doc is not always accurate ...
// Add the full keyword to one of these arrays, and
// it and any abbreviated versions will be added to the
// appropriate scopes
let alsoStatements = [];
alsoStatements.push('ambiguous');
alsoStatements.push('auto-go');
alsoStatements.push('available');
alsoStatements.push('bgcolor');
alsoStatements.push('centered');
alsoStatements.push('context-help-id');
alsoStatements.push('dcolor');
alsoStatements.push('default');
alsoStatements.push('delete');
alsoStatements.push('delimiter');
alsoStatements.push('drop-target');
alsoStatements.push('fgcolor');
alsoStatements.push('first');
alsoStatements.push('flat-button');
alsoStatements.push('font');
alsoStatements.push('format');
alsoStatements.push('image-down');
alsoStatements.push('image-up');
alsoStatements.push('image-insensitive');
alsoStatements.push('initial');
alsoStatements.push('foreign-key-hidden');
alsoStatements.push('label');
alsoStatements.push('locked');
alsoStatements.push('mouse-pointer');
alsoStatements.push('namespace-prefix');
alsoStatements.push('namespace-uri');
alsoStatements.push('nested');
alsoStatements.push('no-focus');
alsoStatements.push('pfcolor');
alsoStatements.push('recursive');
alsoStatements.push('return-value');
alsoStatements.push('row');
alsoStatements.push('serialize-hidden');
alsoStatements.push('serialize-name');
alsoStatements.push('single-run');
alsoStatements.push('tooltip');
alsoStatements.push('transaction');
alsoStatements.push('view-as');
alsoStatements.push('warning');
alsoStatements.push('xml-data-type');
alsoStatements.push('xml-node-name');
alsoStatements.push('xml-node-type');
let functionsNotStatements = [];
functionsNotStatements.push('set-size');
let alsoFunctions = [];
alsoFunctions.push('lower');
alsoFunctions.push('relation-fields');
alsoFunctions.push('return');
alsoFunctions.push('skip');
alsoFunctions.push('substitute');
alsoFunctions.push('this-object');
alsoFunctions.push('value');
lineReaderMethods.on('line', line => {
let results;
line = line.toLowerCase();
if (!line.startsWith("#")) {
let kw = line.split(' ');
let charIdx = kw[0].charCodeAt(0) - 97;
let keyWord = kw[0].split('(')[0];
if (kw.includes('attribute')) {
if (!attributeBlocks[charIdx].includes(keyWord)) {
attributeBlocks[charIdx].push(keyWord);
}
} else if (kw.includes('method')) {
if (!methodBlocks[charIdx].includes(keyWord)) {
methodBlocks[charIdx].push(keyWord);
}
}
if (alsoStatements.includes(keyWord)) {
if (!keywordBlocks[charIdx].includes(keyWord)) {
keywordBlocks[charIdx].push(keyWord);
}
}
if (alsoFunctions.includes(keyWord)) {
if (!functionBlocks[charIdx].includes(keyWord)) {
functionBlocks[charIdx].push(keyWord);
}
}
}
});
lineReaderFunctions.on('line', line => {
let results;
line = line.toLowerCase();
if (!line.startsWith("#")) {
let kw = line.split(' ');
let kwName = '';
// This is to skip the FUNCTION statement
if (kw.includes('function') && !kw.includes('statement')) {
if (kw.includes('preprocessor')) {
kwName = kw[0];
} else {
kwName = kw.slice(0, kw.indexOf('function')).join(' ');
}
// CAPS letter alphabet
let charIdx = kwName.charCodeAt(0) - 97;
if (!functionBlocks[charIdx].includes(kwName) && !kwName.includes('...')) {
functionBlocks[charIdx].push(kwName);
}
if (alsoStatements.includes(kwName)) {
if (!keywordBlocks[charIdx].includes(kwName)) {
keywordBlocks[charIdx].push(kwName);
}
}
} else if (kw.includes('statement')) {
for (const keyWord of kw) {
let charIdx = keyWord.charCodeAt(0) - 97;
if (keyWord == "statement") { break; }
if (keyWord.indexOf('(') !== -1) { continue; }
if (keyWord.includes('...')) { break; }
kw2 = keyWord.replace(",", "");
if (charIdx > 0 && functionsNotStatements.includes(kw2)) {
functionBlocks[charIdx].push(kw2);
} else if (charIdx > 0 && !keywordBlocks[charIdx].includes(kw2)) {
keywordBlocks[charIdx].push(kw2);
}
}
}
}
});
lineReaderKeywords.on('line', line => {
let results;
line = line.toLowerCase();
if (!line.startsWith("#")) {
while ((results = re.exec(line)) !== null) {
let kw = results[0];
// CAPS letter alphabet
let charIdx = kw.charCodeAt(0) - 97;
if (kw.indexOf('(') !== -1) {
let kwParts = kw.split('(');
let fullKw = kwParts[0] + kwParts[1];
kw = kwParts[0];
addToBlock(charIdx, fullKw, kw);
let kwComplete = kwParts[1];
for (const element of kwComplete) {
kw += element;
addToBlock(charIdx, fullKw, kw);
}
} else {
addToBlock(charIdx, kw, kw);
}
}
}
});
function addToBlock(charIdx, fullKw, addKw) {
if (attributeBlocks[charIdx].includes(fullKw)) {
if (!attributeBlocks[charIdx].includes(addKw)) {
attributeBlocks[charIdx].push(addKw);
}
} else if (methodBlocks[charIdx].includes(fullKw)) {
if (!methodBlocks[charIdx].includes(addKw)) {
methodBlocks[charIdx].push(addKw);
}
} else if (functionBlocks[charIdx].includes(fullKw)) {
if (!functionBlocks[charIdx].includes(addKw)) {
functionBlocks[charIdx].push(addKw);
}
} else {
if (!keywordBlocks[charIdx].includes(addKw)) {
keywordBlocks[charIdx].push(addKw);
}
}
if (alsoFunctions.includes(fullKw)) {
if (!functionBlocks[charIdx].includes(addKw)) {
functionBlocks[charIdx].push(addKw);
}
}
}
lineReaderKeywords.on('close', () => {
result['keywords'] = { patterns: [] }
for (var zz = 0; zz < 26; zz++) {
if (keywordBlocks[zz].length > 0) {
result.keywords.patterns.push({ include: "#keywords-" + String.fromCharCode(97 + zz).toUpperCase() });
result['keywords-' + String.fromCharCode(97 + zz).toUpperCase()] =
{
comment: "The keyword must not have a trailing variable character (one of #$-_%&)",
match: "(?i)\\b(" + keywordBlocks[zz].sort(reverseSort).join('|') + ")\\b(?![\\#\\$\\-\\_\\%\\&])",
captures: {
1: {
name: "keyword.other.abl"
}
}
}
}
}
result['handle-attributes'] = { patterns: [] }
for (var zz = 0; zz < 26; zz++) {
if (attributeBlocks[zz].length > 0) {
result['handle-attributes'].patterns.push({ include: "#handle-attributes-" + String.fromCharCode(97 + zz).toUpperCase() });
result['handle-attributes-' + String.fromCharCode(97 + zz).toUpperCase()] =
{
match: "(?i)(:)(" + attributeBlocks[zz].sort(reverseSort).join('|') + ")\\b(?![\\#\\$\\-\\_\\%\\&])",
captures: {
1: {
name: "punctuation.separator.colon.abl"
},
2: {
name: "entity.name.function.abl"
}
}
}
}
}
result['handle-methods'] = { patterns: [] }
for (var zz = 0; zz < 26; zz++) {
if (methodBlocks[zz].length > 0) {
result['handle-methods'].patterns.push({ include: "#handle-methods-" + String.fromCharCode(97 + zz).toUpperCase() });
result['handle-methods-' + String.fromCharCode(97 + zz).toUpperCase()] =
{
begin: "(?i)(:)(" + methodBlocks[zz].sort(reverseSort).join('|') + ")\\s*(?=\\()",
beginCaptures: {
1: {
name: "punctuation.separator.colon.abl"
},
2: {
name: "support.function.abl"
}
},
end: "(\\))",
endCaptures: {
1: {
name: "meta.brace.round.js"
}
},
patterns: [
{
include: "#function-arguments"
}
]
}
}
}
result['abl-functions'] = { patterns: [] }
for (var zz = 0; zz < 26; zz++) {
if (functionBlocks[zz].length > 0) {
result['abl-functions'].patterns.push({ include: "#abl-functions-" + String.fromCharCode(97 + zz).toUpperCase() });
result['abl-functions-' + String.fromCharCode(97 + zz).toUpperCase()] =
{
name: "meta.function-call.abl",
begin: "(?i)\\s*(" + functionBlocks[zz].sort(reverseSort).join('|') + ")\\s*(?=\\()",
beginCaptures: {
1: {
name: "support.function.abl"
}
},
end: "(\\))",
endCaptures: {
1: {
name: "meta.brace.round.js"
}
},
patterns: [
{
include: "#function-arguments"
}
]
}
}
}
fs.writeFileSync(output, JSON.stringify(result, undefined, 4));
})
// Sorts in reverse order - so that the longer versions of the keywords appear earlier in the regex
function reverseSort(a, b) {
const nameA = a.toLowerCase(); // ignore upper and lowercase
const nameB = b.toLowerCase(); // ignore upper and lowercase
if (nameA < nameB) {
return 1;
}
if (nameA > nameB) {
return -1;
}
// names are equal
return 0;
}