forked from parinfer/parinfer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestParsingLib.js
529 lines (465 loc) · 14.2 KB
/
testParsingLib.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
//
// Parinfer Test 3.12.0
//
// Copyright 2015-2017 © Shaun Lebron
// MIT License
//
var parinfer = require('./parinfer.js')
var LINE_ENDING_REGEX = /\r?\n/
function isOpenParen (c) {
return c === '{' || c === '(' || c === '['
}
function isCloseParen (c) {
return c === '}' || c === ')' || c === ']'
}
// ------------------------------------------------------------------------------
// String Operations
// ------------------------------------------------------------------------------
function replaceWithinString (orig, start, end, replace) {
return (
orig.substring(0, start) +
replace +
orig.substring(end)
)
}
function repeatString (text, n) {
var i
var result = ''
for (i = 0; i < n; i++) {
result += text
}
return result
}
// ------------------------------------------------------------------------------
// Input Parser
// ------------------------------------------------------------------------------
function error (lineNo, msg) {
return 'test parse error at line ' + lineNo + ': ' + msg
}
function parsePrevCursorLine (options, inputLineNo, outputLineNo, input) {
var match = input.match(/^\s*\^\s*prevCursor\s*$/)
if (!match) {
return false
}
var x = input.indexOf('^')
if (options.cursorX < x && options.cursorLine === outputLineNo) {
x++
}
options.prevCursorX = x
options.prevCursorLine = outputLineNo
return true
}
function parseCursorFromLine (options, inputLineNo, outputLineNo, input) {
var cursorX = input.indexOf('|')
if (cursorX !== -1) {
if (options.cursorX) {
throw error(inputLineNo, 'only one cursor allowed. cursor already found at line', options.cursorLine)
}
var clean = input.split('|').join('')
if (clean.length < input.length - 1) {
throw error(inputLineNo, 'only one cursor allowed')
}
input = clean
options.cursorX = cursorX
options.cursorLine = outputLineNo
}
return input
}
function initialDiffState () {
var diff = {
code: null, // the code that the diff line is annotating
codeLineNo: null,
prevCode: null, // the code that the previous diff line annotated
prevCodeLineNo: null,
prevNewlineChar: '', // the previous diff line's newline annotation ('', '-', or '+')
merge: null, // the code should be merged with the next code line?
mergeOffset: 0 // code growth after merge
}
return diff
}
function parseDiffLine (options, inputLineNo, input, diff) {
// lines with only -/+ chars are considered diff lines.
var looseMatch = input.match(/^\s*[-+]+[-+\s]*$/)
if (!looseMatch) {
return
}
var match = input.match(/^((\s*)(-*)(\+*))\s*$/)
if (!match) {
throw error(inputLineNo, " diff chars must be adjacent with '-'s before '+'s.")
}
var x = match[2].length
if (!diff.code) {
throw error(inputLineNo, ' diff lines must be directly below a code line')
}
// "open" means current and previous diffs can be connected
var prevDiffOpen = (
diff.prevCode &&
diff.prevNewlineChar !== '' &&
(diff.prevCodeLineNo === diff.codeLineNo ||
diff.prevCodeLineNo + 1 === diff.codeLineNo)
)
var currDiffOpen = x === 0
var change
if (prevDiffOpen && currDiffOpen) {
if (diff.prevNewlineChar === '+' && input[0] === '-') {
throw error(inputLineNo, "diff line starts with '-', which cannot come after '+' which previous diff line ends with")
}
} else {
// create a new change since diffs are not connected
options.changes = options.changes || []
options.changes.push({
lineNo: diff.codeLineNo,
x: x + diff.mergeOffset,
oldText: '',
newText: ''
})
}
// get the current active change
change = options.changes[options.changes.length - 1]
if (match[1].length > diff.code.length + 1) {
throw error(inputLineNo, "diff line can only extend one character past the previous line length (to annotate the 'newline' char)")
}
x += diff.mergeOffset
var oldLen = match[3].length
var newLen = match[4].length
var oldX = x
var newX = x + oldLen
var len = oldLen + newLen
var xEnd = x + len - 1
if (options.cursorLine === diff.codeLineNo) {
if (x <= options.cursorX && options.cursorX < x + len) {
throw error(inputLineNo, 'cursor cannot be over a diff annotation')
} else if (options.cursorX < x) {
x--; oldX--; newX--
} else {
options.cursorX -= oldLen
}
}
var newlineChar = (diff.code.length === xEnd ? input.charAt(xEnd) : '')
var oldText = diff.code.substring(oldX, oldX + oldLen) + (newlineChar === '-' ? '\n' : '')
var newText = diff.code.substring(newX, newX + newLen) + (newlineChar === '+' ? '\n' : '')
change.oldText += oldText
change.newText += newText
// update diff state
diff.prevCode = diff.code
diff.prevCodeLineNo = diff.codeLineNo
diff.merge = newlineChar === '-'
diff.prevNewlineChar = newlineChar
return replaceWithinString(diff.code, oldX, oldX + oldLen, '')
}
function handlePostDiffLine (options, inputLineNo, outputLineNo, outputLines, output, diff) {
var j = outputLineNo
if (diff.merge) {
diff.mergeOffset = outputLines[j - 1].length
if (options.cursorLine === j) {
options.cursorLine = j - 1
options.cursorX += diff.mergeOffset
}
outputLines[j - 1] += output
} else {
diff.mergeOffset = 0
outputLines.push(output)
}
diff.merge = false
diff.codeLineNo = outputLines.length - 1
diff.code = outputLines[diff.codeLineNo]
}
function _parseInput (text, extras) {
extras = extras || {}
var options = {}
if (extras.commentChars) { options.commentChars = extras.commentChars }
if (extras.forceBalance) { options.forceBalance = true }
if (extras.partialResult) { options.partialResult = true }
if (extras.printParensOnly) { options.returnParens = true }
var inputLines = text.split(LINE_ENDING_REGEX)
var outputLines = []
var i, j // input and output line numbers
var input, output // input and output line text
var diff = initialDiffState()
for (i = 0; i < inputLines.length; i++) {
input = inputLines[i]
j = outputLines.length
if (parsePrevCursorLine(options, i, j - 1, input)) {
continue
}
output = parseDiffLine(options, i, input, diff)
if (output != null) {
outputLines[j - 1] = output
delete diff.code
continue
}
output = parseCursorFromLine(options, i, j, input)
handlePostDiffLine(options, i, j, outputLines, output, diff)
}
return {
text: outputLines.join('\n'),
options: options
}
}
function parseInput (texts, extras) {
if (!Array.isArray(texts)) {
return _parseInput(texts, extras)
}
var changes = []
var resultText
var resultOptions
var i, j
for (i = 0; i < texts.length; i++) {
var result = _parseInput(texts[i], extras)
resultText = result.text
resultOptions = result.options
var newChanges = result.options.changes
if (newChanges) {
for (j = 0; j < newChanges.length; j++) {
changes.push(newChanges[j])
}
}
}
if (changes.length > 0) {
resultOptions.changes = changes
}
return {
text: resultText,
options: resultOptions
}
}
// ------------------------------------------------------------------------------
// Output Parser
// ------------------------------------------------------------------------------
function parseErrorLine (result, inputLineNo, outputLineNo, input) {
var match = input.match(/^\s*\^\s*error: ([a-z-]+)\s*$/)
if (!match) {
return false
}
if (result.error) {
throw error(inputLineNo, 'only one error can be specified')
}
var x = input.indexOf('^')
if (result.cursorLine === outputLineNo && result.cursorX < x) {
x--
}
result.error = {
name: match[1],
lineNo: outputLineNo,
x: x
}
return true
}
function parseTabStopsLine (result, inputLineNo, outputLineNo, input) {
var match = input.match(/^([\^>\s]+)tabStops?\s*$/)
if (!match) {
return false
}
if (result.tabStops) {
throw error(inputLineNo, 'only one tabStop line can be specified')
}
result.tabStops = []
var sym, ch, x, i, tabStop
for (x = 0; x < input.length; x++) {
sym = input[x]
if (sym === '^') {
tabStop = { x: x }
for (i = outputLineNo; i >= 0; i--) {
ch = result.lines[i][x]
if (isOpenParen(ch)) {
tabStop.ch = ch
tabStop.lineNo = i
break
}
}
if (!tabStop.ch) {
throw error(inputLineNo, 'tabStop at ' + x + ' does not point to open paren')
}
result.tabStops.push(tabStop)
} else if (sym === '>') {
tabStop = result.tabStops[result.tabStops.length - 1]
if (!tabStop) {
throw error(inputLineNo, 'tabStop at ' + x + ' ">" is a dependent on a preceding "^"')
}
if (tabStop.argX != null) {
throw error(inputLineNo, 'tabStop at ' + x + ' ">" cannot come after another ">"')
}
tabStop.argX = x
}
}
return true
}
function parseParenTrailLine (result, inputLineNo, outputLineNo, input) {
var match = input.match(/^\s*(\^*)\s*parenTrail\s*$/)
if (!match) {
return false
}
if (result.cursorLine != null) {
throw error(inputLineNo, 'parenTrail cannot currently be printed when a cursor is present')
}
var trail = match[1]
var startX = input.indexOf('^')
var endX = startX + trail.length
var x
for (x = startX; x < endX; x++) {
var ch = result.lines[outputLineNo][x]
if (!isCloseParen(ch)) {
throw error(inputLineNo, '^ parenTrail must point to close-parens only')
}
}
if (!result.parenTrails) {
result.parenTrails = []
}
result.parenTrails.push({
lineNo: outputLineNo,
startX: startX,
endX: endX
})
return true
}
function parseOutput (text) {
var lines = text.split(LINE_ENDING_REGEX)
var result = {
lines: []
}
var i, j // input and output line numbers
var input, output // input and output line text
for (i = 0; i < lines.length; i++) {
input = lines[i]
j = result.lines.length
if (parseErrorLine(result, i, j - 1, input) ||
parseTabStopsLine(result, i, j - 1, input) ||
parseParenTrailLine(result, i, j - 1, input)) {
continue
}
output = parseCursorFromLine(result, i, j, input)
result.lines.push(output)
}
result.text = result.lines.join('\n')
result.success = result.error == null
delete result.lines
return result
}
// ------------------------------------------------------------------------------
// Output Printer
// ------------------------------------------------------------------------------
function printErrorLine (result) {
// shift x position back if previous line has cursor before our error caret
var x = result.error.x
if (result.cursorLine === result.error.lineNo &&
result.cursorX <= x) {
x++
}
return repeatString(' ', x) + '^ error: ' + result.error.name
}
function printTabStopLine (tabStops) {
var i, x
var lastX = -1
var line = ''
var count = 0
for (i = 0; i < tabStops.length; i++) {
x = tabStops[i].x
line += repeatString(' ', x - lastX - 1) + '^'
lastX = x
count++
x = tabStops[i].argX
if (x != null) {
line += repeatString(' ', x - lastX - 1) + '>'
lastX = x
count++
}
}
line += ' tabStop' + (count > 1 ? 's' : '')
return line
}
function printParenTrailLine (parenTrail) {
return (
repeatString(' ', parenTrail.startX) +
repeatString('^', parenTrail.endX - parenTrail.startX) +
' parenTrail'
)
}
function printPadding (lineNo, x, nextLineNo, nextX) {
var newlines = nextLineNo - lineNo
var spaces = (nextLineNo > lineNo) ? nextX : nextX - x - 1
return repeatString('\n', newlines) + repeatString(' ', spaces)
}
function printParen (lineNo, x, opener) {
var s = printPadding(lineNo, x, opener.lineNo, opener.x)
s += opener.ch
lineNo = opener.lineNo
x = opener.x
s += printParens(lineNo, x, opener.children)
var lastChild = opener.children[opener.children.length - 1]
if (lastChild) {
lineNo = lastChild.closer.lineNo
x = lastChild.closer.x
}
s += printPadding(lineNo, x, opener.closer.lineNo, opener.closer.x)
s += opener.closer.ch
if (opener.closer.trail) {
s += '*'
}
return s
}
function printParens (lineNo, x, parens) {
var s = ''
var i
var opener
for (i = 0; i < parens.length; i++) {
opener = parens[i]
s += printParen(lineNo, x, opener)
lineNo = opener.closer.lineNo
x = opener.closer.x
}
return s
}
function printOutput (result, extras) {
extras = extras || {}
var lines = result.text.split(LINE_ENDING_REGEX)
var hasCursor = (
result.cursorX != null &&
result.cursorLine != null &&
// could be false if `partialResult` is true and parinfer failed before reaching cursor line
result.cursorLine < lines.length
)
if (hasCursor) {
var line = lines[result.cursorLine]
lines[result.cursorLine] = replaceWithinString(line, result.cursorX, result.cursorX, '|')
}
if (result.error) {
lines.splice(result.error.lineNo + 1, 0, printErrorLine(result))
} else if (extras.printParensOnly) {
return printParens(0, 0, result.parens)
} else if (extras.printParenTrails && result.parenTrails) {
var i, parenTrail
for (i = result.parenTrails.length - 1; i >= 0; i--) {
parenTrail = result.parenTrails[i]
lines.splice(parenTrail.lineNo + 1, 0, printParenTrailLine(parenTrail))
}
} else if (hasCursor && extras.printTabStops && result.tabStops.length > 0) {
lines.splice(result.cursorLine, 0, printTabStopLine(result.tabStops))
}
return lines.join('\n')
}
// ------------------------------------------------------------------------------
// Public API
// ------------------------------------------------------------------------------
function indentMode (text, extras) {
var parsed = parseInput(text, extras)
var result = parinfer.indentMode(parsed.text, parsed.options)
return printOutput(result, extras)
}
function parenMode (text, extras) {
var parsed = parseInput(text, extras)
var result = parinfer.parenMode(parsed.text, parsed.options)
return printOutput(result, extras)
}
function smartMode (text, extras) {
var parsed = parseInput(text, extras)
var result = parinfer.smartMode(parsed.text, parsed.options)
return printOutput(result, extras)
}
module.exports = {
indentMode: indentMode,
parenMode: parenMode,
smartMode: smartMode,
parseInput: parseInput,
parseOutput: parseOutput,
printOutput: printOutput
}