STUDIO HAZE · image
Topaz source
Open by house policy. Topaz is our own language.
Topaz 5.21 · 113 files
topaz/src/printers/javascript/layout.tpz
Topaz · 30347 B · Original file
1
// Row, column, and indentation decisions of the JavaScript and TypeScript printer,2
// taken from the geometry the image carries.4
import nodes { 노드종류 }5
import print { 인쇄조각평평하게, 인쇄조각행수 }6
import printer_cursor { 토파즈언어유티에프십육폭 }7
import printer_state { 자식순번, 자식순번들, 준비된인쇄상태 }8
import printers.javascript.comments { 댓글수사이, 줄댓글수사이, 줄댓글접두, 행끝줄댓글있음, 행끝줄댓글접두 }9
import structure { 구조노드, 구조입력, 스칼라논리, 스칼라정수 }10
import types { 인쇄조각판 }12
export function 명세닫기공백(준비: 준비된인쇄상태, 순번들: Array<int>) -> string {13
if !준비.오프셋배치 || 순번들.length == 0 { return " " }14
let 마지막 = 준비.구조.노드들[순번들[순번들.length - 1]]15
if 스칼라논리(마지막, "spelling.trailingComma") { "" } else { " " }16
}18
export function 모듈출처접두(준비: 준비된인쇄상태, 부모순번: int) -> string {19
let 출처순번 = 자식순번(준비, 부모순번, "source")20
if 출처순번 < 0 { return " from " }21
" from" + 가장자리접두(준비, 출처순번, " ")22
}24
// The row the printer is on when a child begins: the end row of the last25
// direct child that ends before the child starts, and the parent's own start26
// row when there is none. This is the predecessor the kernel oracle derives in27
// `delimiterRelativeGaps`, and the sibling chain visits exactly the parent's28
// own children in the same order the projection records them.29
export function 직접앞끝행(준비: 준비된인쇄상태, 부모순번: int, 순번: int) -> int {30
let 부모 = 준비.구조.노드들[부모순번]31
let 현재 = 준비.구조.노드들[순번]32
let mut 끝행 = 스칼라정수(부모, "startRow")33
let mut 끝 = 부모.시작34
let mut 후보순번 = 준비.첫자식들[부모순번]35
while 후보순번 >= 0 {36
let 후보 = 준비.구조.노드들[후보순번]37
if 후보.끝 <= 현재.시작 && 후보.끝 > 끝 {38
끝 = 후보.끝39
끝행 = 스칼라정수(후보, "endRow")40
}41
후보순번 = 준비.다음형제들[후보순번]42
}43
끝행44
}46
// The row the printer is on when a parameter list begins: after the name and47
// the type parameters the head already emitted, not at the head's own start.48
export function 매개기준끝행(준비: 준비된인쇄상태, 부모순번: int) -> int {49
let 부모 = 준비.구조.노드들[부모순번]50
let mut 끝행 = 스칼라정수(부모, "startRow")51
let 이름순번 = 자식순번(준비, 부모순번, "id")52
if 이름순번 >= 0 {53
끝행 = 스칼라정수(준비.구조.노드들[이름순번], "endRow")54
}55
let 키순번 = 자식순번(준비, 부모순번, "key")56
if 키순번 >= 0 {57
끝행 = 스칼라정수(준비.구조.노드들[키순번], "endRow")58
}59
// A head that prints its parameter list WITHOUT parentheses emits nothing60
// before the parameter, so the parameter is already where the arrow's own61
// edge put the printer and this list must not carry the row a second time.62
let 괄호생략매개들 = 자식순번들(준비, 부모순번, "params")63
if 화살표괄호생략인가(준비, 부모순번) && 괄호생략매개들.length == 1 {64
끝행 = 스칼라정수(준비.구조.노드들[괄호생략매개들[0]], "startRow")65
}66
let 형식매개 = 자식순번(준비, 부모순번, "typeParameters")67
if 형식매개 >= 0 {68
let 형식들 = 자식순번들(준비, 형식매개, "params")69
끝행 = if 형식들.length == 0 {70
스칼라정수(준비.구조.노드들[형식매개], "endRow")71
} else {72
스칼라정수(준비.구조.노드들[형식들[형식들.length - 1]], "endRow")73
}74
}75
끝행76
}78
function 매개목록다중행(준비: 준비된인쇄상태, 부모순번: int, 순번들: Array<int>) -> bool {79
if 순번들.length == 0 { return false }80
let 첫 = 준비.구조.노드들[순번들[0]]81
if 스칼라정수(첫, "startRow") > 매개기준끝행(준비, 부모순번) { return true }82
let mut 색인 = 183
while 색인 < 순번들.length {84
let 이전 = 준비.구조.노드들[순번들[색인 - 1]]85
let 현재 = 준비.구조.노드들[순번들[색인]]86
if 스칼라정수(현재, "startRow") > 스칼라정수(이전, "endRow") { return true }87
색인 = 색인 + 188
}89
false90
}92
export function 매개뒤형식접두(준비: 준비된인쇄상태, 부모순번: int, 순번들: Array<int>) -> string {93
if !준비.오프셋배치 { return "" }94
let 반환순번 = 자식순번(준비, 부모순번, "returnType")95
if 반환순번 < 0 { return "" }96
let 기준행 = if 순번들.length > 0 {97
스칼라정수(준비.구조.노드들[순번들[순번들.length - 1]], "endRow")98
} else { 매개기준끝행(준비, 부모순번) }99
let 줄수 = 스칼라정수(준비.구조.노드들[반환순번], "startRow") - 기준행100
if 줄수 > 0 { 줄접두(줄수, 노드들여쓰기(준비, 반환순번)) } else { "" }101
}103
export function 줄접두(줄수: int, 공백수: int) -> string {104
if 줄수 <= 0 { return "" }105
let mut 글 = ""106
let mut 남은줄 = 줄수107
while 남은줄 > 0 { 글 = 글 + "\n"; 남은줄 = 남은줄 - 1 }108
let mut 남은공백 = 공백수109
while 남은공백 > 0 { 글 = 글 + " "; 남은공백 = 남은공백 - 1 }110
글111
}113
// The reference printer is @babel/generator run with `retainLines`114
// (packages/kernel/src/print-projection-to-source.ts). A node whose carried row115
// is below the current row is reached by `Printer.catchUp`, which emits bare116
// line feeds; the column that follows is added lazily by `Printer._maybeIndent`117
// and is exactly `Printer._indent`. That counter is moved only by118
// `Printer.indent` / `Printer.dedent`, so the column after a carried row break119
// is the sum of the indent decisions taken on the path from the program root120
// down to the node - never the carried startColumn, and never a depth estimate.121
//122
// Which edges take a decision is DERIVED from the generator's own call sites123
// into `spec/g6-f/babel-generator-indent-inventory.v1.json` by124
// `scripts/generate-babel-indent-inventory.mjs`, and125
// `topaz/courts/printer-identity.test.mjs` asserts this table against that126
// inventory, so a decision this table does not carry - or one the generator127
// stops taking - is a red court rather than a silent byte divergence.128
//129
// A decision belongs to a PARENT KIND and a CHILD FIELD together, never to a130
// parent kind alone: `SwitchCase` indents its `consequent` and prints its131
// `test` at the enclosing level, and reading the field is what keeps a `case`132
// head out of the case body's column.133
//134
// BEGIN BABEL INDENT INVENTORY TABLE135
function 가장자리들여쓰기(준비: 준비된인쇄상태, 자식순번: int) -> int {136
if 자식순번 < 0 { return 0 }137
let 자식 = 준비.구조.노드들[자식순번]138
let 부모순번 = 자식.부모순번139
if 부모순번 < 0 { return 0 }140
let 부모종류 = 노드종류(준비.언어번호, 준비.구조.노드들[부모순번].코드워드)141
let 필드 = 자식.자식필드142
// mode `always`143
if (부모종류 == 410 && (필드 == "body" || 필드 == "directives")) ||144
(부모종류 == 603 && 필드 == "body") ||145
(부모종류 == 609 && 필드 == "body") ||146
(부모종류 == 720 && 필드 == "body") ||147
(부모종류 == 731 && 필드 == "body") ||148
(부모종류 == 717 && 필드 == "members") ||149
(부모종류 == 734 && 필드 == "members") ||150
(부모종류 == 510 && 필드 == "cases") ||151
(부모종류 == 511 && 필드 == "consequent") ||152
((부모종류 == 203 || 부모종류 == 204) && 필드 == "properties") {153
return 2154
}155
// mode `conditional:node.declarations.length > 1`156
if 부모종류 == 409 && 필드 == "declarations" {157
if 자식순번들(준비, 부모순번, "declarations").length > 1 { return 2 }158
return 0159
}160
// mode `row-dependent`. Every generator that prints a `decorators` list161
// prints it through `printJoin` with no indent argument, on every kind that162
// can hold one, so the field alone decides it.163
if 필드 == "decorators" ||164
((부모종류 == 210 || 부모종류 == 211 || 부모종류 == 212) &&165
필드 == "arguments") ||166
(부모종류 == 310 && 필드 == "expressions") ||167
((부모종류 == 736 || 부모종류 == 738) &&168
(필드 == "specifiers" || 필드 == "attributes" || 필드 == "assertions")) ||169
((부모종류 == 601 || 부모종류 == 602) && 필드 == "implements") ||170
(부모종류 == 721 && 필드 == "extends") ||171
((부모종류 == 712 || 부모종류 == 713) && 필드 == "params") ||172
(부모종류 == 715 && 필드 == "types") {173
return 목록행의존들여쓰기(준비, 부모순번, 필드)174
}175
0176
}177
// END BABEL INDENT INVENTORY TABLE179
// `printJoin`'s own run-time rule under `retainLines`: a list whose `indent`180
// argument is absent indents exactly when its first element carries a row181
// other than the row the printer is on when the list begins. An element the182
// image carries explicit parentheses for is rebuilt as a183
// `ParenthesizedExpression`, which carries no position at all, and a list whose184
// first element has no position never indents.185
function 목록행의존들여쓰기(186
준비: 준비된인쇄상태,187
부모순번: int,188
필드: string189
) -> int {190
let 요소들 = 자식순번들(준비, 부모순번, 필드)191
if 요소들.length == 0 { return 0 }192
let 첫순번 = 요소들[0]193
let 첫 = 준비.구조.노드들[첫순번]194
if 스칼라정수(첫, "spelling.parentheses") > 0 { return 0 }195
let 부모종류 = 노드종류(준비.언어번호, 준비.구조.노드들[부모순번].코드워드)196
let 앞행 = if (부모종류 == 210 || 부모종류 == 211 || 부모종류 == 212) &&197
필드 == "arguments" {198
호출대상끝행(준비, 부모순번)199
} else { 직접앞끝행(준비, 부모순번, 첫순번) }200
// `printJoin` reads the element's OWN carried position for this decision,201
// not the row its head first emits on, so a bare arrow whose parameter sits202
// a row below still leaves the list unindented.203
if 스칼라정수(첫, "startRow") != 앞행 { return 2 }204
0205
}207
// `Printer._indent` while the node's own tokens are printed.208
export function 노드들여쓰기(준비: 준비된인쇄상태, 순번: int) -> int {209
if 순번 < 0 { return 0 }210
let mut 합 = 0211
let mut 현재 = 순번212
while 현재 >= 0 {213
합 = 합 + 가장자리들여쓰기(준비, 현재)214
현재 = 준비.구조.노드들[현재].부모순번215
}216
합217
}219
// The ONE geometry-carrying edge primitive. Its domain is every220
// parent-child edge the guest printer emits, not a list of node kinds that221
// somebody remembered: the row it carries is the difference between the child's222
// carried startRow and the row the printer is on when the child begins, which223
// the carried geometry names as `직접앞끝행`, and the column it carries is224
// `노드들여쓰기`. Wherever a guest edge used to print a constant separator it225
// now prints that constant through this primitive, so the constant survives226
// only as the SAME-ROW spelling of one edge and never as its geometry.227
//228
// A child the image carries explicit parentheses for takes its row break INSIDE229
// those parentheses: the reference printer emits `(` and only then catches the230
// row up, because the parenthesis it rebuilds carries no position of its own.231
// `순번인쇄조각계산` owns that break, so this primitive leaves such an edge on232
// one row and the constant separator stands.233
// The row on which a node BEGINS PRINTING is the row of the first leaf it234
// emits, which is its own carried startRow for every node whose head emits a235
// token - and its parameter's row for a bare arrow head, which emits nothing236
// before the parameter.237
export function 유효시작행(준비: 준비된인쇄상태, 순번: int) -> int {238
if 화살표머리첫잎이매개인가(준비, 순번) {239
let 매개들 = 자식순번들(준비, 순번, "params")240
if 매개들.length == 1 {241
return 스칼라정수(준비.구조.노드들[매개들[0]], "startRow")242
}243
}244
스칼라정수(준비.구조.노드들[순번], "startRow")245
}247
function 가장자리행수(준비: 준비된인쇄상태, 자식순번: int) -> int {248
if !준비.오프셋배치 || 자식순번 < 0 { return 0 }249
let 자식 = 준비.구조.노드들[자식순번]250
if 자식.부모순번 < 0 { return 0 }251
if 스칼라정수(자식, "spelling.parentheses") > 0 { return 0 }252
let 줄수 = 유효시작행(준비, 자식순번) -253
직접앞끝행(준비, 자식.부모순번, 자식순번)254
if 줄수 < 0 { 0 } else { 줄수 }255
}257
export function 가장자리접두(258
준비: 준비된인쇄상태,259
자식순번: int,260
같은행: string261
) -> string {262
let 줄수 = 가장자리행수(준비, 자식순번)263
if 줄수 > 0 { 줄접두(줄수, 노드들여쓰기(준비, 자식순번)) } else { 같은행 }264
}266
// The row the printer is on after emitting a piece. The piece counts its own267
// row events, so this is the printer's real position rather than the position268
// the image carries for a node the printer may have emitted differently, which269
// is the position `catchUp` compares against.270
export function 조각끝행(준비: 준비된인쇄상태, 시작행: int, 조각: int) -> int {271
시작행 + 인쇄조각행수(준비.조각판, 조각)272
}274
// The column the printer is on after emitting a piece that contains at least275
// one row event, measured the way the reference printer's buffer measures it:276
// UTF-16 code units since the last row event. Walking the rope from the right277
// stops at the first piece that carries a row event, so only the final row is278
// ever flattened.279
function 조각끝열(준비: 준비된인쇄상태, 조각: int) -> Result<int, string> {280
let mut 열 = 0281
let mut 쌓임: Array<int> = []282
let mut 꼭대기 = 0283
let mut 현재 = 조각284
let mut 걸음 = 0285
let 최대걸음 = 준비.조각판.종류들.length * 2 + 32286
while 걸음 <= 최대걸음 {287
걸음 = 걸음 + 1288
if 현재 > 0 && 준비.조각판.종류들[현재] == 2 {289
let 왼 = 준비.조각판.왼들[현재]290
if 꼭대기 < 쌓임.length { 쌓임[꼭대기] = 왼 } else { 쌓임.push(왼) }291
꼭대기 = 꼭대기 + 1292
현재 = 준비.조각판.오른들[현재]293
continue294
}295
if 현재 > 0 {296
let 글 = 인쇄조각평평하게(준비.조각판, 현재)?297
let 글자들 = 글.scalars()298
let mut 색인 = 글자들.length299
let mut 꼬리 = 0300
let mut 행끝 = false301
while 색인 > 0 && !행끝 {302
let 글자 = 글자들[색인 - 1]303
if 글자 == "\n" || 글자 == "\r" || 글자 == "
" || 글자 == "
" {304
행끝 = true305
} else {306
꼬리 = 꼬리 + 토파즈언어유티에프십육폭(글자)307
색인 = 색인 - 1308
}309
}310
열 = 열 + 꼬리311
if 행끝 { return Ok(열) }312
}313
if 꼭대기 == 0 { return Ok(열) }314
꼭대기 = 꼭대기 - 1315
현재 = 쌓임[꼭대기]316
}317
Ok(열)318
}320
function 무해리터럴식문장인가(준비: 준비된인쇄상태, 순번: int) -> bool {321
if 순번 < 0 ||322
노드종류(준비.언어번호, 준비.구조.노드들[순번].코드워드) != 401 {323
return false324
}325
let 식순번 = 자식순번(준비, 순번, "expression")326
if 식순번 < 0 { return false }327
let 종류 = 노드종류(준비.언어번호, 준비.구조.노드들[식순번].코드워드)328
종류 == 108 || 종류 == 109 || 종류 == 110 || 종류 == 111 || 종류 == 112329
}331
export function 문장접두(332
준비: 준비된인쇄상태,333
순번들: Array<int>,334
색인: int,335
추가행: int336
) -> string {337
let 현재 = 준비.구조.노드들[순번들[색인]]338
let 부모순번 = 현재.부모순번339
let 부모 = 준비.구조.노드들[부모순번]340
let 부모종류 = 노드종류(준비.언어번호, 부모.코드워드)341
let 들여쓰기 = 노드들여쓰기(준비, 순번들[색인])342
let mut 시작행 = 스칼라정수(부모, "startRow")343
let mut 시작 = 부모.시작344
if 색인 > 0 {345
let 이전 = 준비.구조.노드들[순번들[색인 - 1]]346
시작 = 이전.끝347
시작행 = 스칼라정수(이전, "endRow")348
}349
else {350
// The first statement of a list begins on the row the printer is on, which351
// is the end row of the last direct child the parent emitted before it -352
// a directive, an interpreter line, or a `case` head - and the parent's own353
// start row when there is none. Naming the fields that can precede it is354
// what put a `case` body one row below its own head, so the walk asks the355
// projection instead. A detached comment is not emitted here; its rows are356
// carried by the comment machinery below.357
let mut 후보순번 = 준비.첫자식들[부모순번]358
while 후보순번 >= 0 {359
let 후보 = 준비.구조.노드들[후보순번]360
let 후보종류 = 노드종류(준비.언어번호, 후보.코드워드)361
if 후보종류 != 119 && 후보종류 != 120 &&362
후보.끝 <= 현재.시작 && 후보.끝 > 시작 {363
시작 = 후보.끝364
시작행 = 스칼라정수(후보, "endRow")365
}366
후보순번 = 준비.다음형제들[후보순번]367
}368
}369
let mut 줄수 = 스칼라정수(현재, "startRow") - 시작행 + 추가행370
if 색인 > 0 && 준비.언어번호 == 2 {371
줄수 = 줄수 - 준비.영상댓글누적추가행들[순번들[색인 - 1]]372
}373
if 부모종류 == 102 && 색인 > 0 && 색인 + 1 == 순번들.length && 줄수 == 0 &&374
무해리터럴식문장인가(준비, 순번들[색인 - 1]) &&375
무해리터럴식문장인가(준비, 순번들[색인]) { 줄수 = 1 }376
if 줄수 < 0 { 줄수 = 0 }377
let 출력들여쓰기 = 들여쓰기378
if 준비.언어번호 == 2 {379
let 계획댓글수 = 스칼라정수(현재, "emission.imageOnlyLeadingLineCommentRows")380
let 실제댓글수 = 줄댓글수사이(준비, 시작, 현재.시작)381
let 댓글수 = if 실제댓글수 > 계획댓글수 { 실제댓글수 } else { 계획댓글수 }382
let 출력줄수 = if 줄수 > 계획댓글수 { 줄수 } else { 계획댓글수 }383
if 색인 > 0 && 계획댓글수 > 0 && 준비.본문값들.length == 0 {384
let 이전 = 준비.구조.노드들[순번들[색인 - 1]]385
let 이전안댓글수 = 줄댓글수사이(준비, 이전.시작, 이전.끝)386
let 행끝댓글수 = if 이전안댓글수 < 계획댓글수 {387
이전안댓글수388
} else { 계획댓글수 }389
if 행끝댓글수 > 0 {390
return 행끝줄댓글접두(391
출력줄수,392
출력들여쓰기,393
행끝댓글수,394
댓글수 - 행끝댓글수395
)396
}397
}398
let 접두 = 줄댓글접두(출력줄수, 출력들여쓰기, 댓글수)399
if 색인 > 0 && 접두.startsWith("//") &&400
(계획댓글수 > 0 || 행끝줄댓글있음(준비, 시작, 현재.시작, 시작행)) {401
return " " + 접두402
}403
return 접두404
}405
줄접두(줄수, 출력들여쓰기)406
}408
export function 문장부족행(준비: 준비된인쇄상태, 순번: int, 조각: int) -> int {409
let 노드 = 준비.구조.노드들[순번]410
let 기대행 = 스칼라정수(노드, "endRow") - 스칼라정수(노드, "startRow")411
let 실제행 = 인쇄조각행수(준비.조각판, 조각)412
if 기대행 > 실제행 { 기대행 - 실제행 } else { 0 }413
}415
function 문장목록다중행(준비: 준비된인쇄상태, 순번들: Array<int>) -> bool {416
if 순번들.length == 0 { return false }417
let 첫 = 준비.구조.노드들[순번들[0]]418
let 부모순번 = 첫.부모순번419
let 부모 = 준비.구조.노드들[부모순번]420
let 부모종류 = 노드종류(준비.언어번호, 부모.코드워드)421
if 부모종류 == 511 { return false }422
let mut 시작행 = 스칼라정수(부모, "startRow")423
let mut 시작 = 부모.시작424
for 후보 in 준비.구조.노드들 {425
if 후보.부모순번 == 부모순번 &&426
(후보.자식필드 == "directives" || 후보.자식필드 == "interpreter") &&427
후보.끝 <= 첫.시작 && 후보.끝 > 시작 {428
시작 = 후보.끝429
시작행 = 스칼라정수(후보, "endRow")430
}431
}432
let 첫줄수 = 스칼라정수(첫, "startRow") - 시작행433
if 첫줄수 > 0 { return true }434
let mut 색인 = 1435
while 색인 < 순번들.length {436
let 이전 = 준비.구조.노드들[순번들[색인 - 1]]437
let 현재 = 준비.구조.노드들[순번들[색인]]438
let 줄수 = 스칼라정수(현재, "startRow") - 스칼라정수(이전, "endRow")439
if 줄수 > 0 { return true }440
색인 = 색인 + 1441
}442
false443
}445
// Whether the arrow's parameter list prints WITHOUT parentheses. This answers446
// one question only, and it is the same question the kind 308 emitter answers447
// when it builds `괄호없음`.448
export function 화살표괄호생략인가(준비: 준비된인쇄상태, 화살표순번: int) -> bool {449
if 화살표순번 < 0 { return false }450
let 화살표 = 준비.구조.노드들[화살표순번]451
if 노드종류(준비.언어번호, 화살표.코드워드) != 308 ||452
자식순번(준비, 화살표순번, "typeParameters") >= 0 ||453
자식순번(준비, 화살표순번, "returnType") >= 0 ||454
자식순번(준비, 화살표순번, "predicate") >= 0 {455
return false456
}457
let 매개들 = 자식순번들(준비, 화살표순번, "params")458
if 매개들.length != 1 { return false }459
let 매개 = 준비.구조.노드들[매개들[0]]460
노드종류(준비.언어번호, 매개.코드워드) == 106 &&461
!스칼라논리(매개, "optional") &&462
자식순번들(준비, 매개들[0], "decorators").length == 0 &&463
자식순번(준비, 매개들[0], "typeAnnotation") < 0464
}466
// Whether the FIRST LEAF the arrow head emits is the parameter itself,467
// which is the only case in which the parameter's carried row is the arrow's468
// carried row. This is not a list of modifiers to exclude: it quantifies over469
// the head's own emit sequence in the kind 308 emitter, which is exactly470
// [ "async " when the async scalar is set ]471
// [ the typeParameters child, when present ]472
// [ "(" when the parameter list keeps its parentheses ]473
// [ the first parameter ]474
// so the head begins at the parameter precisely when the first three are all475
// empty. Any modifier added to that head - a further keyword, another type476
// position, a decorator list - lands in the sequence above and is answered477
// here without touching this predicate, because the question asked is "does478
// anything print before the parameter", never "is it one of these".479
export function 화살표머리첫잎이매개인가(준비: 준비된인쇄상태, 화살표순번: int) -> bool {480
if !화살표괄호생략인가(준비, 화살표순번) { return false }481
let 화살표 = 준비.구조.노드들[화살표순번]482
!스칼라논리(화살표, "async") &&483
자식순번(준비, 화살표순번, "typeParameters") < 0 &&484
자식순번들(준비, 화살표순번, "params").length == 1485
}487
export function 블록닫기접두(488
준비: 준비된인쇄상태,489
순번: int,490
내용들: Array<int>,491
추가행: int492
) -> string {493
if !문장목록다중행(준비, 내용들) && 추가행 <= 0 { return "" }494
let 블록 = 준비.구조.노드들[순번]495
if 블록.자식필드 == "body" &&496
화살표괄호생략인가(준비, 블록.부모순번) { return "" }497
let 닫기들여쓰기 = 노드들여쓰기(준비, 순번)498
let 부모 = 준비.구조.노드들[순번]499
let 마지막 = 준비.구조.노드들[내용들[내용들.length - 1]]500
let mut 줄수 = 스칼라정수(부모, "endRow") - 스칼라정수(마지막, "endRow") + 추가행501
if 줄수 < 0 { 줄수 = 0 }502
줄접두(줄수, 닫기들여쓰기)503
}505
// 겹문의 절은 자식이 하나뿐인 필드라서 어떤 목록 묶음도 덮지 않았고, 원본이506
// 절 사이에 쓴 행을 여태 되살리지 못했다. Error.prototype.stack이 물리 행을507
// 드러내므로 삼켜진 절 행 하나가 그 뒤 모든 행을 다시 번호 매긴다.508
// 끊긴 구분자는 줄바꿈 하나에 다음 행 들여쓰기를 더해 쓰고, 끊기지 않은509
// 구분자는 빈칸 하나를 쓴다. 같은 바이트를 여러 (끊김수, 행수) 짝이 쓸 수510
// 있으니 간격과 맞는 가장 적은 행수를 답으로 삼는다. 행을 만들어 내는 쪽이511
// 해로운 방향이기 때문이다. 끊김과 빈칸이 같은 바이트를 쓰는 자리에서는512
// 한 행 표기를 그대로 두는데, 이는 문장 목록 모형이 이미 적어 둔 충돌이다.513
function 절행수(514
준비: 준비된인쇄상태,515
부모순번: int,516
이전끝: int,517
절순번: int,518
고정바이트: int,519
구분자수: int520
) -> int {521
if 절순번 < 0 { return 0 }522
let 절 = 준비.구조.노드들[절순번]523
if 이전끝 < 0 || 이전끝 > 절.시작 { return 0 }524
// 절 사이 댓글은 제 행을 따로 실어 나르고 그 행은 댓글 생략 경로가 이미525
// 갖고 있다. 두 번 세지 않도록 절을 건드리지 않는다.526
if 댓글수사이(준비, 이전끝, 절.시작) > 0 { return 0 }527
let 부모 = 준비.구조.노드들[부모순번]528
let mut 이전행 = 스칼라정수(부모, "startRow")529
for 후보 in 준비.구조.노드들 {530
if 후보.끝 == 이전끝 { 이전행 = 스칼라정수(후보, "endRow") }531
}532
let 행수 = 스칼라정수(절, "startRow") - 이전행533
if 행수 < 0 { 0 } else { 행수 }534
}536
export function 절접두(537
준비: 준비된인쇄상태,538
부모순번: int,539
이전끝: int,540
절순번: int,541
고정바이트: int,542
구분자수: int,543
한행: string544
) -> string {545
if !준비.오프셋배치 { return 한행 }546
let 행수 = 절행수(준비, 부모순번, 이전끝, 절순번, 고정바이트, 구분자수)547
if 행수 <= 0 { return 한행 }548
줄접두(행수, 노드들여쓰기(준비, 절순번))549
}551
// A literal list element carries its own start column, and the kernel asks the552
// reference printer for that column at line start only. The pad is skipped when553
// the carried column is zero, and the printer's own lazy indentation then554
// applies, so the element's column is the carried column when it is non-zero555
// and @babel/generator's indent level when it is zero.556
export function 리터럴열(준비: 준비된인쇄상태, 순번: int) -> int {557
let 열 = 스칼라정수(준비.구조.노드들[순번], "startColumn")558
if 열 > 0 { 열 } else { 노드들여쓰기(준비, 순번) }559
}561
export function 리터럴첫줄수(준비: 준비된인쇄상태, 부모순번: int, 첫순번: int) -> int {562
let 줄수 = 스칼라정수(준비.구조.노드들[첫순번], "startRow") -563
스칼라정수(준비.구조.노드들[부모순번], "startRow")564
if 줄수 < 0 { 0 } else { 줄수 }565
}567
export function 리터럴사이접두(568
준비: 준비된인쇄상태,569
이전순번: int,570
현재순번: int571
) -> string {572
let 이전 = 준비.구조.노드들[이전순번]573
let 현재 = 준비.구조.노드들[현재순번]574
let 줄수 = 스칼라정수(현재, "startRow") - 스칼라정수(이전, "endRow")575
if 줄수 > 0 { 줄접두(줄수, 리터럴열(준비, 현재순번)) } else { " " }576
}578
// The reference printer catches a literal's closing delimiter up to the node's579
// carried END row and then pads to its carried close column, and it compares580
// both against the printer's REAL position rather than against the position the581
// image carries for the last element - `sourceWithOffset("end", loc, -1)` in582
// @babel/generator and the close-delimiter branch of `tokenChar` in the583
// kernel's structural printer. The emitted body counts its own rows and its own584
// trailing column, so the guest reads the same two facts.585
export function 리터럴닫기접두(586
준비: 준비된인쇄상태,587
부모순번: int,588
몸조각: int589
) -> Result<string, string> {590
let 부모 = 준비.구조.노드들[부모순번]591
let 줄수 = 스칼라정수(부모, "endRow") -592
조각끝행(준비, 스칼라정수(부모, "startRow"), 몸조각)593
let 닫기열 = 스칼라정수(부모, "endColumn") - 1594
if 줄수 > 0 {595
let 들여쓰기값 = if 닫기열 > 0 { 닫기열 } else { 노드들여쓰기(준비, 부모순번) }596
return Ok(줄접두(줄수, 들여쓰기값))597
}598
if 줄수 < 0 || 닫기열 <= 0 { return Ok("") }599
if 인쇄조각행수(준비.조각판, 몸조각) == 0 { return Ok("") }600
let 현재열 = 조각끝열(준비, 몸조각)?601
if 현재열 >= 닫기열 { return Ok("") }602
let mut 공백 = ""603
let mut 남음 = 닫기열 - 현재열604
while 남음 > 0 { 공백 = 공백 + " "; 남음 = 남음 - 1 }605
Ok(공백)606
}608
// The row the printer is on when a call's argument list begins. The reference609
// printer emits the closing `>` of a type-argument list without catching a row610
// up, so the row is the last type argument's end row rather than the list611
// node's; the list's indent decision and its emission read this one function so612
// they cannot disagree about which row the list begins on.613
export function 호출대상끝행(준비: 준비된인쇄상태, 부모순번: int) -> int {614
let 부모 = 준비.구조.노드들[부모순번]615
let 대상순번 = 자식순번(준비, 부모순번, "callee")616
let mut 끝행 = if 대상순번 < 0 { 스칼라정수(부모, "startRow") }617
else { 스칼라정수(준비.구조.노드들[대상순번], "endRow") }618
let 형식인수 = if 자식순번(준비, 부모순번, "typeArguments") >= 0 {619
자식순번(준비, 부모순번, "typeArguments")620
} else { 자식순번(준비, 부모순번, "typeParameters") }621
if 형식인수 >= 0 {622
let 형식들 = 자식순번들(준비, 형식인수, "params")623
끝행 = if 형식들.length == 0 {624
스칼라정수(준비.구조.노드들[형식인수], "endRow")625
} else {626
스칼라정수(준비.구조.노드들[형식들[형식들.length - 1]], "endRow")627
}628
}629
끝행630
}632
export function 함수몸접두(준비: 준비된인쇄상태, 부모순번: int, 몸순번: int) -> string {633
if 몸순번 < 0 { return " " }634
가장자리접두(준비, 몸순번, " ")635
}SHA256 7da2f11dc011bd51e88faacb149d2b0bbeb34210f18db4cb645ac00990f1ada2