-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathTryStatement.ts
More file actions
68 lines (57 loc) · 1.87 KB
/
TryStatement.ts
File metadata and controls
68 lines (57 loc) · 1.87 KB
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
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { printSeparatedItem } from '../slang-printers/print-separated-item.js';
import { extractVariant } from '../slang-utils/extract-variant.js';
import { SlangNode } from './SlangNode.js';
import { Expression } from './Expression.js';
import { ReturnsDeclaration } from './ReturnsDeclaration.js';
import { Block } from './Block.js';
import { CatchClauses } from './CatchClauses.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { Doc, ParserOptions } from 'prettier';
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
import type { PrintableNode } from './types.d.ts';
const { line } = doc.builders;
export class TryStatement extends SlangNode {
readonly kind = NonterminalKind.TryStatement;
expression: Expression['variant'];
returns?: ReturnsDeclaration;
body: Block;
catchClauses: CatchClauses;
constructor(
ast: ast.TryStatement,
collected: CollectedMetadata,
options: ParserOptions<PrintableNode>
) {
super(ast, collected);
this.expression = extractVariant(
new Expression(ast.expression, collected, options)
);
if (ast.returns) {
this.returns = new ReturnsDeclaration(ast.returns, collected, options);
}
this.body = new Block(ast.body, collected, options);
this.catchClauses = new CatchClauses(ast.catchClauses, collected, options);
this.updateMetadata(
this.expression,
this.returns,
this.body,
this.catchClauses
);
}
print(print: PrintFunction): Doc {
const returnsDoc = print('returns');
return [
'try',
printSeparatedItem(print('expression'), {
firstSeparator: line
}),
[
returnsDoc ? [returnsDoc, ' '] : returnsDoc,
print('body'),
' ',
print('catchClauses')
]
];
}
}