forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenEnum.hack
More file actions
143 lines (123 loc) · 3.4 KB
/
CodegenEnum.hack
File metadata and controls
143 lines (123 loc) · 3.4 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
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
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HackCodegen;
use namespace HH\Lib\{C, Vec};
/**
* Generate code for an enum.
*
* ```
* $factory->codegenEnum('Foo', 'int')
* ->setIsAs('int')
* ->addConst('NAME', $value, 'Comment...')
* ->render();
* ```
*/
final class CodegenEnum implements ICodeBuilderRenderer {
use CodegenWithVisibility;
use CodegenWithAttributes;
use HackBuilderRenderer;
private vec<CodegenEnumMember> $members = vec[];
private ?string $isAs = null;
/** Create an instance.
*
* You should use `ICodegenFactory::codegenEnum` instead of directly
* constructing.
*/
public function __construct(
protected IHackCodegenConfig $config,
private string $name,
private string $enumType,
) {
}
/** Make the enum usable directly as the specified type.
*
* For example, `->setIsAs('string')` will declare the enum as `as string`,
* allowing values to be directly passed into functions that take a `string`.
*/
public function setIsAs(string $is_as): this {
invariant($this->isAs === null, 'isAs has already been set');
$this->isAs = $is_as;
return $this;
}
/** @selfdocumenting */
public function getIsAs(): ?string {
return $this->isAs;
}
/** @selfdocumenting */
public function addMember(CodegenEnumMember $member): this {
$this->members[] = $member;
return $this;
}
/** @selfdocumenting */
public function addMembers(
vec<CodegenEnumMember> $members,
): this {
$this->members = Vec\concat($this->members, $members);
return $this;
}
<<__Override>>
public function appendToBuilder(HackBuilder $builder): HackBuilder {
if ($this->docBlock is nonnull) {
$builder->ensureEmptyLine()->addDocBlock($this->docBlock);
}
$builder
->ensureEmptyLine()
->addLine($this->renderAttributes())
->ensureNewLine()
->addWithSuggestedLineBreaksf(
'%s%s%s {',
'enum '.$this->name,
HackBuilder::DELIMITER.': '.$this->enumType,
$this->isAs !== null ? HackBuilder::DELIMITER.'as '.$this->isAs : '',
);
if (!C\is_empty($this->members)) {
$builder->ensureNewLine();
$builder->indent();
foreach ($this->members as $m) {
$m->appendToBuilder($builder);
}
$builder->unindent();
}
$builder->ensureNewLine();
$builder->addLine('}');
return $builder;
}
/** @selfdocumenting */
public function setDocBlock(string $comment): this {
$this->docBlock = $comment;
return $this;
}
protected bool $hasManualFooter = false;
protected bool $hasManualHeader = false;
private ?string $headerName;
private ?string $headerContents;
private ?string $footerName;
private ?string $footerContents;
public function setHasManualHeader(
bool $value = true,
?string $name = null,
?string $contents = null,
): this {
$this->hasManualHeader = $value;
$this->headerName = $name;
$this->headerContents = $contents;
return $this;
}
public function setHasManualFooter(
bool $value = true,
?string $name = null,
?string $contents = null,
): this {
$this->hasManualFooter = $value;
$this->footerName = $name;
$this->footerContents = $contents;
return $this;
}
protected ?string $docBlock;
}