-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-opcode-completions.js
562 lines (529 loc) · 20.8 KB
/
update-opcode-completions.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
const csound = require('csound-api');
const fs = require('fs');
const libxml = require('libxmljs');
const path = require('path');
const stripBom = require('strip-bom');
// Use OpcodeInfo objects to organize opcode information for creating
// completions.
class OpcodeInfo {
constructor(opcodeEntry) {
this.name = opcodeEntry.opname;
this.inputTypeStrings = [];
this.inputTypeStringsByLengthByOutputTypeString = {};
this.addInfoForOpcodeEntry(opcodeEntry);
}
addInfoForOpcodeEntry(opcodeEntry) {
// Ignore array inputs.
const inputTypeString = opcodeEntry.intypes.replace(/\[\]/g, '');
if (this.inputTypeStrings.indexOf(inputTypeString) < 0)
this.inputTypeStrings.push(inputTypeString);
// According to
// https://github.com/csound/csound/blob/develop/Engine/entry1.c, the output
// type 's' is deprecated but means either an a- or k-rate output.
const outputTypes = (opcodeEntry.outypes === 's') ? ['a', 'k'] : [opcodeEntry.outypes];
for (const outputType of outputTypes) {
let inputTypeStringsByLength = this.inputTypeStringsByLengthByOutputTypeString[outputType];
if (inputTypeStringsByLength) {
const inputTypeStrings = inputTypeStringsByLength[inputTypeString.length];
if (inputTypeStrings) {
if (inputTypeStrings.indexOf(inputTypeString) < 0)
inputTypeStrings.push(inputTypeString);
} else {
inputTypeStringsByLength[inputTypeString.length] = [inputTypeString];
}
} else {
inputTypeStringsByLength = {};
inputTypeStringsByLength[inputTypeString.length] = [inputTypeString];
this.inputTypeStringsByLengthByOutputTypeString[outputType] = inputTypeStringsByLength;
}
}
}
}
// Create a map from opcode names to opcode properties.
const ignoredOpcodes = [
// Function opcodes
'a',
'abs',
'ampdb',
'ampdbfs',
'birnd',
'ceil',
'cent',
'cos',
'cosh',
'cosinv',
'cosinv',
'cpsmidinn',
'cpsoct',
'cpspch',
'db',
'dbamp',
'dbfsamp',
'exp',
'floor',
'frac',
'ftchnls',
'ftcps',
'ftlen',
'ftlptim',
'ftsr',
'i',
'int',
'k',
'log',
'log10',
'log2',
'logbtwo',
'nsamp',
'octave',
'octcps',
'octmidinn',
'octpch',
'p',
'pchmidinn',
'pchoct',
'powoftwo',
'qinf',
'qnan',
'rnd',
'round',
'semitone',
'signum',
'sin',
'sinh',
'sininv',
'sqrt',
'tan',
'tanh',
'taninv',
'urd',
// Keyword-like opcodes
'cggoto',
'cigoto',
'cingoto',
'ckgoto',
'cngoto',
'endin',
'endop',
'goto',
'igoto',
'instr',
'kgoto',
'opcode',
'return',
'rigoto',
'tigoto',
// Opcodes that use comma-separated lists of arguments of any type
'changed2',
'framebuffer',
'hdf5read',
'hdf5write',
'xin',
'xout',
// Opcodes with input arguments that are difficult to parse
'alwayson'
];
const opcodeInfoByName = {};
const opcodeList = [];
csound.NewOpcodeList(csound.Create(), opcodeList);
for (const opcodeEntry of opcodeList) {
if (ignoredOpcodes.indexOf(opcodeEntry.opname) < 0) {
const opcodeInfo = opcodeInfoByName[opcodeEntry.opname];
if (opcodeInfo)
opcodeInfo.addInfoForOpcodeEntry(opcodeEntry);
else
opcodeInfoByName[opcodeEntry.opname] = new OpcodeInfo(opcodeEntry);
}
}
// Use InputArgumentInfo objects to organize input argument types and names for
// completions.
const openingBracket = '(';
const separator = '|';
const closingBracket = ')';
class InputArgumentInfo {
constructor(inputTypeStrings, nameArray) {
this.nameArray = nameArray;
this.typeStringsArray = [];
this.listArray = [];
this.optionalArray = [];
// Resolve input type characters based on
// <https://github.com/csound/csound/blob/develop/Engine/entry1.c>. The
// opcodes framebuffer, hdf5read, hdf5write, xin, and xout use the * type
// character, which is documented in the release notes of Csound 6
// <https://csound.github.io/docs/manual/PrefaceWhatsNew.html> as indicating
// “a var-arg list of any-type”.
// i i-time scalar
// o optional i-time scalar defaulting to 0
// j optional i-time scalar defaulting to -1
// v optional i-time scalar defaulting to 0.5
// p optional i-time scalar defaulting to 1
// q optional i-time scalar defaulting to 10
// h optional i-time scalar defaulting to 127
// m comma-separated list of any number of i-time scalars
// k k-rate scalar
// O optional k-rate scalar defaulting to 0
// J optional k-rate scalar defaulting to -1
// V optional k-rate scalar defaulting to 0.5
// P optional k-rate scalar defaulting to 1
// z comma-separated list of k-rate scalars
// a a-rate vector
// y comma-separated list of a-rate vectors
// S string
// W comma-separated list of strings
// T i-time scalar or string
// U i-time scalar, k-rate scalar, or string
// x k-rate scalar or a-rate vector
// M comma-separated list of i-time scalars, k-rate scalars, and a-rate
// vectors
// N comma-separated list of i-time scalars, k-rate scalars, a-rate vectors,
// and strings
// n comma-separated list of an odd number of i-time scalars
// Z comma-separated list of alternating k-rate scalars and a-rate vectors,
// used by mac and outch
// f frequency-domain variable, used by phase vocoder opcodes
// w frequency-domain variable, used by specaddm, specdiff, specdisp,
// specfilt, spechist, specptrk, specscal, specsum, and spectrum
// B Boolean, used by cggoto, cigoto, cingoto, and ckgoto
// l label, used by goto, igoto, kgoto, loop_ge, loop_gt, loop_le, loop_lt,
// rigoto, and tigoto
// . required argument of any type, used by init (for arrays), lenarray,
// print_type, and slicearray
// ? optional argument of any type, possibly unused
// * comma-separated list of arguments of any type, used by changed2,
// framebuffer, hdf5read, hdf5write, xin, and xout
const typeInfoByType = {
'a': {strings: ['a'], optional: false, list: false},
'B': {strings: ['B'], optional: false, list: false},
'f': {strings: ['f'], optional: false, list: false},
'h': {strings: ['i'], optional: true, list: false},
'i': {strings: ['i'], optional: false, list: false},
'J': {strings: ['k'], optional: false, list: false},
'j': {strings: ['i'], optional: true, list: false},
'k': {strings: ['k'], optional: false, list: false},
'l': {strings: ['l'], optional: false, list: false},
'M': {strings: ['a', 'i', 'k'], optional: false, list: true},
'm': {strings: ['i'], optional: false, list: true},
'N': {strings: ['a', 'i', 'k', 'S'], optional: false, list: true},
'n': {strings: ['i'], optional: false, list: true},
'O': {strings: ['k'], optional: true, list: false},
'o': {strings: ['i'], optional: true, list: false},
'P': {strings: ['k'], optional: true, list: false},
'p': {strings: ['i'], optional: true, list: false},
'q': {strings: ['i'], optional: true, list: false},
'S': {strings: ['S'], optional: false, list: false},
'T': {strings: ['i', 'S'], optional: false, list: false},
'U': {strings: ['i', 'k', 'S'], optional: false, list: false},
'V': {strings: ['k'], optional: true, list: false},
'v': {strings: ['i'], optional: true, list: false},
'W': {strings: ['S'], optional: false, list: true},
'w': {strings: ['w'], optional: false, list: false},
'x': {strings: ['a', 'k'], optional: false, list: false},
'y': {strings: ['a'], optional: false, list: true},
'Z': {strings: ['a', 'k'], optional: false, list: true},
'z': {strings: ['k'], optional: false, list: true},
'.': {strings: ['a', 'i', 'k', 'S'], optional: false, list: false}
};
for (const inputTypeString of inputTypeStrings) {
if (inputTypeString.length > 0) {
const inputTypes = inputTypeString.match(/(?:[aBfhijklOoPpqSTUVvwx\.]|[MmNnWyZz]+)/g);
for (let i = 0, length = inputTypes.length; i < length; i++) {
const typeInfo = typeInfoByType[inputTypes[i]];
const typeStrings = this.typeStringsArray[i];
if (typeStrings) {
for (const string of typeInfo.strings) {
if (typeStrings.indexOf(string) < 0)
typeStrings.push(string);
}
} else {
this.typeStringsArray[i] = typeInfo.strings.slice();
}
this.listArray[i] = typeInfo.list;
this.optionalArray[i] = typeInfo.optional;
}
}
}
}
get displayText() {
const length = this.typeStringsArray.length;
if (length === 0)
return '';
const components = [];
for (let i = 0; i < length; i++) {
let string = this.typeStringsArray[i].join(separator);
if (string.length > 1)
string = openingBracket + string + closingBracket;
if (this.nameArray[i])
string += this.nameArray[i];
components.push(string);
}
return ' ' + components.join(', ');
}
get snippet() {
const length = this.typeStringsArray.length;
if (length === 0)
return '';
let snippet = '';
let optionalIndex = null;
for (let i = 0; i < length; i++) {
let string = this.typeStringsArray[i].join(separator);
if (string.length > 1)
string = openingBracket + string + closingBracket;
if (this.nameArray[i])
string += this.nameArray[i];
if (this.listArray[i])
string += '…';
if (i === 0) {
snippet += '${1:' + string;
} else if (optionalIndex === null) {
if (this.optionalArray[i]) {
optionalIndex = i - 1;
snippet += '/*, ' + string;
} else {
snippet += '}, ${' + (i + 1) + ':' + string;
}
} else {
snippet += ', ' + string;
}
}
if (optionalIndex !== null)
snippet += '*/';
return ' ' + snippet + '}';
}
}
function formatOutputTypeString(string) {
string = string.replace(/m+/g, 'a…');
string = string.replace(/z+/g, 'k…');
string = string.replace(/I+/g, 'i…');
string = string.replace(/X+/g, openingBracket + ['a', 'k', 'i'].join(separator) + closingBracket + '…');
string = string.replace(/N+/g, openingBracket + ['a', 'k', 'i', 'S'].join(separator) + closingBracket + '…');
string = string.replace(/F+/g, 'f…');
return string;
}
// Create a list of Csound Manual XML file names.
const opcodesPath = path.join('csound', 'manual', 'opcodes');
const opcodeXMLFileNames = fs.readdirSync(opcodesPath).map(opcodePath => path.parse(opcodePath).name);
// Csound manual XML files describing opcodes need a header to be parsed without
// errors.
const XMLHeader = `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE refentry [
<!-- From http://dev.w3.org/html5/html-author/charref -->
<!ENTITY auml "ä">
<!ENTITY beta "β">
<!ENTITY circ "^">
<!ENTITY lambda "λ">
<!ENTITY le "≤">
<!ENTITY nbsp " ">
<!ENTITY num "#">
<!ENTITY percnt "%">
<!ENTITY pi "π">
<!ENTITY plusmn "±">
<!ENTITY shy "­">
<!ENTITY tilde "˜">
<!-- From https://github.com/csound/manual/blob/master/manual.xml -->
<!ENTITY nameandres "Andrés Cabrera">
<!ENTITY nameanthony "Anthony Kozar">
<!ENTITY namebarry "Barry L. Vercoe">
<!ENTITY namegabriel "Gabriel Maldonado">
<!ENTITY namehans "Hans Mikelson">
<!ENTITY nameistvan "Istvan Varga">
<!ENTITY namejohn "John ffitch">
<!ENTITY namekanata "Kanata Motohashi">
<!ENTITY namekevin "Kevin Conder">
<!ENTITY nameluis "Luis Jure">
<!ENTITY namematt "Matt Ingalls">
<!ENTITY namemichael "Michael Gogins">
<!ENTITY namemike "Mike Berry">
<!ENTITY nameoeyvind "Øyvind Brandtsegg">
<!ENTITY nameparis "Paris Smaragdis">
<!ENTITY namepaul "Paul Batchelor">
<!ENTITY nameperry "Perry Cook">
<!ENTITY namepeter "Peter Brinkmann">
<!ENTITY namepinot "François Pinot">
<!ENTITY namerasmus "Rasmus Ekman">
<!ENTITY namerichard "Richard Dobson">
<!ENTITY namesean "Sean Costello">
<!ENTITY namesteven "Steven Yi">
<!ENTITY nametito "Tito Latini">
<!ENTITY namevictor "Victor Lazzarini">
]>
`;
// Create a map of more descriptive input argument names.
const descriptiveNamesByName = {
'amp': 'Amplitude',
'atdec': 'AttenuationFactor',
'cps': 'Frequency',
'dec': 'DecayTime',
'del': 'DelayTime',
'dur': 'Duration',
'filname': 'Filename',
'fn': 'FunctionTable',
'freq': 'Frequency',
'frq': 'Frequency',
'ndex': 'Index',
'phs': 'InitialPhase',
'pitch': 'Pitch',
'rise': 'RiseTime',
'sig': 'Signal'
};
let allCompletions = [];
for (const opcodeName in opcodeInfoByName) {
let inputArgumentNames = [];
if (opcodeInfoByName.hasOwnProperty(opcodeName) && opcodeXMLFileNames.indexOf(opcodeName) >= 0) {
// Get opcode descriptions and input argument names from the Csound Manual
// XML file.
let XMLString = fs.readFileSync(path.join(opcodesPath, `${opcodeName}.xml`), 'utf8');
// Csound manual XML files may include a Unicode byte order mark (BOM).
XMLString = stripBom(XMLString);
const XMLDocument = libxml.parseXmlString(XMLHeader + XMLString);
const description = XMLDocument.get('//refpurpose').text().trim().replace(/\s+/g, ' ');
// Assume the XML document contains at least one synopsis element with one
// child command element.
const commandElement = XMLDocument.get('//synopsis/command');
if (commandElement) {
// Assume the next sibling of the command element is a text node
// describing input arguments.
const node = commandElement.nextSibling();
if (node && node.type() === 'text') {
inputArgumentNames = node.toString().replace(/[\\\[\]]/g, '').replace(/\s+/g, ' ').split(',');
for (let i = 0, length = inputArgumentNames.length; i < length; i++) {
let name = inputArgumentNames[i].trim();
if (name.charAt(0) === '"' && name.charAt(name.length - 1) === '"') {
// If the name is enclosed in double quotes, remove the quotes and
// create a camel-case name.
inputArgumentNames[i] = name.replace(/"/g, '').split(' ').map(string => string.charAt(0).toUpperCase() + string.slice(1)).join('');
} else {
// Otherwise, remove the first two characters if the first character
// is a g (indicating a global variable), and just the first
// character otherwise.
name = name.slice((name.charAt(0) === 'g') ? 2 : 1);
const descriptiveName = descriptiveNamesByName[name];
inputArgumentNames[i] = descriptiveName ? descriptiveName : name;
}
}
}
}
const completions = [];
const opcodeInfo = opcodeInfoByName[opcodeName];
const inputTypeStringsByLengthByOutputTypeString = opcodeInfo.inputTypeStringsByLengthByOutputTypeString;
// If all input type strings are equal, then the inputTypeStrings property
// of opcodeInfo will be a length-1 array.
if (opcodeInfo.inputTypeStrings.length === 1) {
// Create a single completion for all output type strings.
const completion = {};
const leftLabel = formatOutputTypeString(Object.getOwnPropertyNames(inputTypeStringsByLengthByOutputTypeString).join(''));
if (leftLabel.length > 0)
completion.leftLabel = leftLabel;
const inputArgumentInfo = new InputArgumentInfo(opcodeInfo.inputTypeStrings, inputArgumentNames);
completion.snippet = opcodeName + inputArgumentInfo.snippet;
completion.displayText = opcodeName + inputArgumentInfo.displayText;
completions.push(completion);
} else {
// Create completions for each output type string and input type string
// length.
for (const outputTypeString in inputTypeStringsByLengthByOutputTypeString) {
if (inputTypeStringsByLengthByOutputTypeString.hasOwnProperty(outputTypeString)) {
const inputTypeStringsByLength = inputTypeStringsByLengthByOutputTypeString[outputTypeString];
for (const length in inputTypeStringsByLength) {
const completion = {leftLabel: formatOutputTypeString(outputTypeString)};
if (inputTypeStringsByLength.hasOwnProperty(length)) {
const inputArgumentInfo = new InputArgumentInfo(inputTypeStringsByLength[length], inputArgumentNames);
completion.snippet = opcodeName + inputArgumentInfo.snippet;
completion.displayText = opcodeName + inputArgumentInfo.displayText;
completions.push(completion);
}
}
}
}
}
// Add additional completion properties from the Csound Manual XML file.
for (const completion of completions) {
completion.opcode = opcodeName;
completion.description = description;
completion.descriptionMoreURL = `https://csound.github.io/docs/manual/${opcodeName}.html`;
completion.type = 'function';
}
allCompletions = allCompletions.concat(completions);
} else {
console.log(`Skipping opcode ${opcodeName} because ${opcodeName}.xml is not in the Csound manual repository.`);
}
}
// Add completions of opcodes that use comma-separated lists of arguments of any
// type, or whose entries are difficult to parse.
allCompletions = allCompletions.concat([
{
snippet: 'alwayson ${1:(i|S)Instrument/*, p4, p5, …*/}',
displayText: 'alwayson (i|S)Instrument, p4, p5, …',
opcode: 'alwayson',
description: 'Activates the indicated instrument in the orchestra header.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/alwayson.html',
type: 'function'
},
{
leftLabel: 'k[]',
snippet: 'changed2 ${1:kVariableName1/*, kVariableName2, …*/}',
displayText: 'changed2 kVariableName1, kVariableName2, …',
opcode: 'changed2',
description: 'k-rate signal change detector.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/changed2.html',
type: 'function'
},
{
leftLabel: 'k[]',
snippet: 'framebuffer ${1:aInput}, ${2:iSize}',
displayText: 'framebuffer aInput, iSize',
opcode: 'framebuffer',
description: 'Read audio signals into 1 dimensional k-rate arrays and vice-versa with a specified buffer size.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/framebuffer.html',
type: 'function'
},
{
leftLabel: 'a',
snippet: 'framebuffer ${1:kInput}, ${2:iSize}',
displayText: 'framebuffer kInput, iSize',
opcode: 'framebuffer',
description: 'Read audio signals into 1 dimensional k-rate arrays and vice-versa with a specified buffer size.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/framebuffer.html',
type: 'function'
},
{
leftLabel: formatOutputTypeString('N'),
snippet: 'hdf5read ${1:Sfilename}, ${2:SVariableName1/*, SVariableName2, …*/}',
displayText: 'hdf5read Sfilename, SVariableName1, SVariableName2, …',
opcode: 'hdf5read',
description: 'Read signals and arrays from an hdf5 file.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/hdf5read.html',
type: 'function'
},
{
snippet: 'hdf5write ${1:Sfilename}, ${2:(a|i|k|S)Output1/*, (a|i|k|S)Output2, …*/}',
displayText: 'hdf5write Sfilename, (a|i|k|S)Output1, (a|i|k|S)Output2, …',
opcode: 'hdf5write',
description: 'Write signals and arrays to an hdf5 file.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/hdf5write.html',
type: 'function'
},
{
leftLabel: formatOutputTypeString('N'),
snippet: 'xin',
displayText: 'xin',
opcode: 'xin',
description: 'Passes variables to a user-defined opcode block.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/xin.html',
type: 'function'
},
{
snippet: 'xout ${1:(a|i|k|S)Output1/*, (a|i|k|S)Output2, …*/}',
displayText: 'xout (a|i|k|S)Output1, (a|i|k|S)Output2, …',
opcode: 'xout',
description: 'Retrieves variables from a user-defined opcode block.',
descriptionMoreURL: 'https://csound.github.io/docs/manual/xout.html',
type: 'function'
}
]);
// Write the completions dictionary to a JSON file.
const fileDescriptor = fs.openSync('opcode-completions.json', 'w');
fs.writeSync(fileDescriptor, JSON.stringify({
about: 'The contents of this file are derived from the source files of The Canonical Csound Reference Manual <https://github.com/csound/manual>. The Canonical Csound Reference Manual is licensed under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. Copyrights of The Canonical Csound Reference Manual are held by the Massachusetts Institute of Technology (1986, 1992), Kevin Conder (2003), and others noted in individual source files. This file is licensed under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.',
completions: allCompletions
}, null, 2));
fs.closeSync(fileDescriptor);