-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcompiler.js
126 lines (105 loc) · 10.3 KB
/
compiler.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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (response, asArray, isLogging) {
var respParts = [];
var resp = (response.tag || '') + (response.command ? ' ' + response.command : '');
var needsSpace = true;
var walk = function walk(node) {
if (resp.length > 0 && needsSpace) {
resp += ' ';
}
if (Array.isArray(node)) {
needsSpace = false;
resp += '(';
node.forEach(walk);
resp += ')';
return;
} else {
needsSpace = true;
}
if (!node && typeof node !== 'string' && typeof node !== 'number') {
resp += 'NIL';
return;
}
if (typeof node === 'string') {
if (isLogging && node.length > 20) {
resp += '"(* ' + node.length + 'B string *)"';
} else {
resp += JSON.stringify(node);
}
return;
}
if (typeof node === 'number') {
resp += Math.round(node) || 0; // Only integers allowed
return;
}
if (isLogging && node.sensitive) {
resp += '"(* value hidden *)"';
return;
}
switch (node.type.toUpperCase()) {
case 'LITERAL':
if (isLogging) {
resp += '"(* ' + node.value.length + 'B literal *)"';
} else {
if (!node.value) {
resp += '{0}\r\n';
} else {
resp += '{' + node.value.length + '}\r\n';
}
respParts.push(resp);
resp = node.value || '';
}
break;
case 'STRING':
if (isLogging && node.value.length > 20) {
resp += '"(* ' + node.value.length + 'B string *)"';
} else {
resp += JSON.stringify(node.value || '');
}
break;
case 'TEXT':
case 'SEQUENCE':
resp += node.value || '';
break;
case 'NUMBER':
resp += node.value || 0;
break;
case 'ATOM':
case 'SECTION':
var val = node.value || '';
for (var i = val.charCodeAt(0) === _formalSyntax.ASCII_BACKSLASH ? 1 : 0; i < val.length; i++) {
if (!(0, _formalSyntax.IS_ATOM_CHAR)(val.charCodeAt(i))) {
val = JSON.stringify(val);
break;
}
}
resp += val;
if (node.section) {
resp += '[';
if (node.section.length) {
needsSpace = false;
node.section.forEach(walk);
}
resp += ']';
}
if (node.partial) {
resp += '<' + node.partial.join('.') + '>';
}
break;
}
};
[].concat(response.attributes || []).forEach(walk);
if (resp.length) {
respParts.push(resp);
}
return asArray ? respParts : respParts.join('');
};
var _formalSyntax = require('./formal-syntax');
;
/**
* Compiles an input object into
*/
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9jb21waWxlci5qcyJdLCJuYW1lcyI6WyJyZXNwb25zZSIsImFzQXJyYXkiLCJpc0xvZ2dpbmciLCJyZXNwUGFydHMiLCJyZXNwIiwidGFnIiwiY29tbWFuZCIsIm5lZWRzU3BhY2UiLCJ3YWxrIiwibm9kZSIsImxlbmd0aCIsIkFycmF5IiwiaXNBcnJheSIsImZvckVhY2giLCJKU09OIiwic3RyaW5naWZ5IiwiTWF0aCIsInJvdW5kIiwic2Vuc2l0aXZlIiwidHlwZSIsInRvVXBwZXJDYXNlIiwidmFsdWUiLCJwdXNoIiwidmFsIiwiaSIsImNoYXJDb2RlQXQiLCJBU0NJSV9CQUNLU0xBU0giLCJzZWN0aW9uIiwicGFydGlhbCIsImpvaW4iLCJjb25jYXQiLCJhdHRyaWJ1dGVzIl0sIm1hcHBpbmdzIjoiOzs7Ozs7a0JBUWUsVUFBVUEsUUFBVixFQUFvQkMsT0FBcEIsRUFBNkJDLFNBQTdCLEVBQXdDO0FBQ3JELE1BQUlDLFlBQVksRUFBaEI7QUFDQSxNQUFJQyxPQUFPLENBQUNKLFNBQVNLLEdBQVQsSUFBZ0IsRUFBakIsS0FBd0JMLFNBQVNNLE9BQVQsR0FBbUIsTUFBTU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ICAgICAgICAgICAgbm9kZS5zZWN0aW9uLmZvckVhY2god2FsaylcbiAgICAgICAgICB9XG4gICAgICAgICAgcmVzcCArPSAnXSdcbiAgICAgICAgfVxuICAgICAgICBpZiAobm9kZS5wYXJ0aWFsKSB7XG4gICAgICAgICAgcmVzcCArPSAnPCcgKyBub2RlLnBhcnRpYWwuam9pbignLicpICsgJz4nXG4gICAgICAgIH1cbiAgICAgICAgYnJlYWtcbiAgICB9XG4gIH07XG5cbiAgW10uY29uY2F0KHJlc3BvbnNlLmF0dHJpYnV0ZXMgfHwgW10pLmZvckVhY2god2FsaylcblxuICBpZiAocmVzcC5sZW5ndGgpIHtcbiAgICByZXNwUGFydHMucHVzaChyZXNwKVxuICB9XG5cbiAgcmV0dXJuIGFzQXJyYXkgPyByZXNwUGFydHMgOiByZXNwUGFydHMuam9pbignJylcbn07XG4iXX0=