forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartiallyGeneratedCode.php
More file actions
193 lines (175 loc) · 6.16 KB
/
PartiallyGeneratedCode.php
File metadata and controls
193 lines (175 loc) · 6.16 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
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
<?hh // strict
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/**
* Manage partially generated code. The main operation is to merge existing
* code (that probably has some handwritten code) with generated code.
*/
final class PartiallyGeneratedCode {
private string $code;
private static string $manualBegin = '/* BEGIN MANUAL SECTION %s */';
private static string $manualEnd = '/* END MANUAL SECTION */';
public function __construct(string $code) {
$this->code = $code;
}
public static function getBeginManualSection(string $id): string {
return sprintf('/* BEGIN MANUAL SECTION %s */', $id);
}
private static function getBeginManualSectionRegex(string $regex): string {
// this needs to be kept in sync with getBeginManualSection.
return sprintf('|/\* BEGIN MANUAL SECTION %s \*/|', $regex);
}
public static function getEndManualSection(): string {
return '/* END MANUAL SECTION */';
}
public static function containsManualSection(string $code): bool {
return strpos($code, self::getEndManualSection()) !== false;
}
/**
* Merge the code with the existing code. The manual sections of
* the existing code will be merged into the corresponding sections
* of the new code.
*
* If rekeys is specified, we will attempt to pull code from sections
* with different names, as specified by the mapping.
*/
public function merge(
string $existing_code,
?Map<string, Vector<string>> $rekeys = null): string {
$merged = array();
$existing = $this->extractManualCode($existing_code);
$generated = $this->iterateCodeSections($this->code);
foreach ($generated as $section) {
list($id, $chunk) = $section;
if ($id === null) {
// Autogenerated section, add it as it is
$merged[] = $chunk;
} else {
if ($existing->containsKey($id)) {
// This manual section was present in the existing code, so insert it
$merged[] = $existing[$id];
} else {
$content = array();
if ($rekeys !== null) {
if ($rekeys->containsKey($id)) {
foreach ($rekeys[$id] as $old_id) {
if ($existing->containsKey($old_id)) {
$content[] = $existing[$old_id];
}
}
}
}
if ($content) {
$merged[] = implode("\n\n", $content);
} else {
// This manual section is new, so insert inside it the chunk from
// the generated code (e.g. the generated code can have a comment
// saying what that manual section should be used for)
$merged[] = $chunk;
}
}
}
}
return implode("\n", array_filter($merged));
}
/**
* Extract manually generated code and returns a map of ids to chunks of code
*/
private function extractManualCode(string $code): Map<string, string> {
$manual = Map {};
foreach ($this->iterateCodeSections($code) as $section) {
list($id, $chunk) = $section;
if ($id !== null) {
$manual[$id] = $chunk;
}
}
return $manual;
}
/**
* Extract the generated code and returns it as a string.
*/
public function extractGeneratedCode(): string {
$generated = array();
foreach ($this->iterateCodeSections($this->code) as $section) {
list($id, $chunk) = $section;
if ($id === null) {
$generated[] = $chunk;
}
}
return implode("\n", $generated);
}
/**
* Validate the manual sections and throws PartiallyGeneratedCodeException
* if there are any errors (e.g. unfinished manual section, nested
* manual sections, duplicated ids, etc)
*/
public function assertValidManualSections(): void {
foreach ($this->iterateCodeSections($this->code) as $section) {}
}
/**
* Iterates through the code yielding tuples of ($id, $chunk), where
* $id is the id of the manual section or null if it's an auto-generated
* section, and chunk is the code belonging to that section.
* The lines containing begin/end of manual section belong to the
* autogenerated sections.
*/
private function iterateCodeSections(
string $code
): Generator<int, (?string, string), void> {
// Regular expression to match the beginning of a manual section
$quoted = preg_quote(self::$manualBegin, '/');
$begin = self::getBeginManualSectionRegex('(.*)');
$valid_begin = self::getBeginManualSectionRegex('([A-Za-z0-9:_]+)');
$seen_ids = Set {};
$current_id = null;
$chunk = array();
$manual = array();
$lines = explode("\n", $code);
foreach ($lines as $line) {
if (strpos($line, self::$manualEnd) !== false) {
yield tuple($current_id, implode("\n", $chunk));
$chunk = array($line);
$current_id = null;
} else if (preg_match($begin, $line) === 1) {
if ($current_id !== null) {
throw new PartiallyGeneratedCodeException(
"The manual section $current_id was open before ".
"the previous one was closed"
);
}
if (!preg_match($valid_begin, $line)) {
throw new PartiallyGeneratedCodeException(
"Invalid id specified: " . $line
);
}
$chunk[] = $line;
yield tuple(null, implode("\n", $chunk));
$chunk = array();
$current_id = trim(preg_replace($begin, '\\1', $line));
if ($seen_ids->contains($current_id)) {
throw new PartiallyGeneratedCodeException(
"Duplicate manual section id: $current_id"
);
}
$seen_ids->add($current_id);
} else {
$chunk[] = $line;
}
}
if ($current_id !== null) {
throw new PartiallyGeneratedCodeException(
"The manual section $current_id was not closed at the end of code"
);
}
if ($code) {
yield tuple(null, implode("\n", $chunk));
}
}
}
final class PartiallyGeneratedCodeException extends Exception {};