-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathscmindent.js
executable file
·223 lines (206 loc) · 5.01 KB
/
scmindent.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
#!/usr/bin/env node
// Dorai Sitaram
// last modified 2020-11-14
// this script takes lines of Lisp or Scheme code from its
// stdin and produces an indented version thereof on its
// stdout
var fs = require('fs');
var events = require('events');
var tbd = new events.EventEmitter();
var lispKeywords = {};
function numLeadingSpaces(s) {
if (s.match(/^\s*$/)) {
return 0;
} else {
s = s.replace(/^(\s*).*/, '$1');
s = s.replace(/\t/g, ' ');
return s.length;
}
}
function stringTrimBlanks(s) {
s = s.replace(/^\s*/, '');
s = s.replace(/\s*$/, '');
return s;
}
function literalTokenP(s) {
return !!s.match(/^[0-9#"]/);
}
function getLispIndentNumber(s) {
var n;
s = s.toLowerCase();
if (n = lispKeywords[s]) {
return n;
} else if (s.match(/^def/)) {
return 0;
} else {
return -1;
}
}
function pastNextAtom(s, i) {
var n = s.length;
while (true) {
if (i >= n) {
return n;
}
var c = s[i];
if (c === '\\') {
i++; i++;
} else if (c.match(/[ \t\(\)\[\]"'`,;]/)) {
return i;
} else {
i++;
}
}
}
function calcSubindent(s, i) {
var n = s.length;
var j = pastNextAtom(s, i);
var deltaIndent = 0;
var lispIndentNum = -1;
if (j === i) {
;
} else {
var w = s.substring(i, j);
if (i >= 2 && s[i-2].match(/['`]/)) {
;
} else {
var lispIndentNum = getLispIndentNumber(w);
if (lispIndentNum == -2) {
;
} else if (lispIndentNum == -1) {
if (j < n) {
deltaIndent = j - i + 1
} else {
deltaIndent = 1
}
} else {
deltaIndent = 1
}
}
}
return {
deltaIndent: deltaIndent,
lispIndentNum: lispIndentNum,
nextTokenIndex: j
}
}
var defaultLeftI = -1;
var leftI = 0;
var parenStack = [];
var insideStringP = false;
function indentLine(currLine) {
var leadingSpaces = numLeadingSpaces(currLine);
var currLeftI;
if (insideStringP) {
currLeftI = leadingSpaces;
} else if (parenStack.length === 0) {
if (defaultLeftI === -1) { defaultLeftI = leadingSpaces; }
leftI = defaultLeftI;
currLeftI = leftI;
} else {
currLeftI = parenStack[0].spacesBefore;
if (parenStack[0].numFinishedSubforms < parenStack[0].lispIndentNum) {
currLeftI += 2;
}
}
currLine = stringTrimBlanks(currLine);
if (currLine !== "") {
for (var i = 0; i < currLeftI; i++) {
process.stdout.write(' ');
}
process.stdout.write(currLine);
}
process.stdout.write('\n');
var escapeP = false;
var tokenIntersticeP = false;
var i = 0;
function incrFinishedSubforms() {
if (!tokenIntersticeP) {
if (parenStack.length > 0) {
parenStack[0].numFinishedSubforms++;
}
}
tokenIntersticeP = true;
}
while (i < currLine.length) {
var c = currLine[i];
if (escapeP) {
escapeP = false; i++;
} else if (c === '\\') {
tokenIntersticeP = false; escapeP = true; i++;
} else if (insideStringP) {
if (c === '"') { insideStringP = false; incrFinishedSubforms(); } i++;
} else if (c === ';') {
incrFinishedSubforms(); break;
} else if (c === '"') {
incrFinishedSubforms(); insideStringP = true; i++;
} else if (c.match(/[ \t]/)) {
incrFinishedSubforms(); i++;
} else if (c.match(/[\(\[]/)) {
incrFinishedSubforms();
var si = calcSubindent(currLine, i+1);
parenStack.unshift({ spacesBefore: 1 + i + currLeftI + si.deltaIndent,
lispIndentNum: si.lispIndentNum,
numFinishedSubforms: -1
});
tokenIntersticeP = true;
var iNext = i+1;
if (si.nextTokenIndex > iNext) {
iNext = si.nextTokenIndex;
tokenIntersticeP = false;
}
i = iNext;
} else if (c.match(/[\)\]]/)) {
tokenIntersticeP = false;
if (parenStack.length > 0) {
parenStack.shift();
} else {
leftI = 0;
}
i++;
} else {
tokenIntersticeP = false; i++;
}
}
incrFinishedSubforms();
}
function indentLines() {
process.stdin.setEncoding('utf8');
var prevLine;
process.stdin.on('data', function(data) {
var lines = data.split('\n');
if (lines.length) {
if (prevLine) {
lines[0] = prevLine + lines[0];
}
for (var i = 0; i < lines.length-1; i++) {
tbd.emit('indentLine', lines[i]);
}
prevLine = lines[lines.length-1];
}
});
process.stdin.on('end', function() {
if (prevLine) {
tbd.emit('indentLine', prevLine);
prevLine = '';
}
});
}
function customize() {
fs.readFile(process.env.LISPWORDS || (process.env.HOME + '/lispwords.json'), 'utf8',
function(err, data) {
if (!err) {
var lw = JSON.parse(data);
for (var kw in lw) {
lispKeywords[kw] = lw[kw];
}
} else {
// process.stderr.write('~/lispwords.json missing or ill-formed\n');
}
tbd.emit('customizationDone');
});
}
tbd.on('start', customize);
tbd.on('customizationDone', indentLines);
tbd.on('indentLine', indentLine);
tbd.emit('start');