This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gutenberg_post_parser.c
318 lines (247 loc) · 9.79 KB
/
gutenberg_post_parser.c
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
/* gutenberg_post_parser extension for PHP (c) 2018 Ivan Enderlin */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_gutenberg_post_parser.h"
#include "gutenberg_post_parser.h"
/*
* Class entry for `Gutenberg_Parser_Block` and `Gutenberg_Parser_Phrase`.
*/
zend_class_entry *gutenberg_parser_block_class_entry;
zend_class_entry *gutenberg_parser_phrase_class_entry;
zend_object_handlers gutenberg_parser_node_class_entry_handlers;
/*
* Custom object for Gutenberg parser nodes.
*/
typedef struct _gutenberg_parser_block {
zend_object zobj;
} gutenberg_parser_node;
/*
* Function for a `zend_class_entry` to create a Gutenberg parser node object.
*/
static zend_object *create_parser_node_object(zend_class_entry *class_entry)
{
gutenberg_parser_node *gutenberg_parser_node_object;
gutenberg_parser_node_object = ecalloc(1, sizeof(*gutenberg_parser_node_object) + zend_object_properties_size(class_entry));
zend_object_std_init(&gutenberg_parser_node_object->zobj, class_entry);
object_properties_init(&gutenberg_parser_node_object->zobj, class_entry);
gutenberg_parser_node_object->zobj.handlers = &gutenberg_parser_node_class_entry_handlers;
return &gutenberg_parser_node_object->zobj;
}
/*
* Handler for a `zend_class_entry` to destroy (i.e. call the
* destructor on the userland for) a Gutenberg parser node object.
*/
static void destroy_parser_node_object(zend_object *gutenberg_parser_node_object)
{
zend_objects_destroy_object(gutenberg_parser_node_object);
}
/*
* Handler for a `zend_class_entry` to free a Gutenberg parser node object.
*/
static void free_parser_node_object(zend_object *gutenberg_parser_node_object)
{
zend_object_std_dtor(gutenberg_parser_node_object);
}
/*
* Initialize the module.
*/
PHP_MINIT_FUNCTION(gutenberg_post_parser)
{
zend_class_entry class_entry;
//
// Declare `Gutenberg_Parser_Block`.
//
INIT_CLASS_ENTRY(class_entry, "Gutenberg_Parser_Block", NULL);
gutenberg_parser_block_class_entry = zend_register_internal_class(&class_entry TSRMLS_CC);
// Declare the create handler.
gutenberg_parser_block_class_entry->create_object = create_parser_node_object;
// The class is final.
gutenberg_parser_block_class_entry->ce_flags |= ZEND_ACC_FINAL;
// Declare the `namespace` public attribute, with an empty string for the default value.
zend_declare_property_string(gutenberg_parser_block_class_entry, "namespace", sizeof("namespace") - 1, "", ZEND_ACC_PUBLIC);
// Declare the `name` public attribute, with an empty string for the default value.
zend_declare_property_string(gutenberg_parser_block_class_entry, "name", sizeof("name") - 1, "", ZEND_ACC_PUBLIC);
// Declare the `attributes` public attribute, with `NULL` for the default value.
zend_declare_property_null(gutenberg_parser_block_class_entry, "attributes", sizeof("attributes") - 1, ZEND_ACC_PUBLIC);
// Declare the `children` public attribute, with `NULL` for the default value.
zend_declare_property_null(gutenberg_parser_block_class_entry, "children", sizeof("children") - 1, ZEND_ACC_PUBLIC);
//
// Declare `Gutenberg_Parser_Phrase`.
//
INIT_CLASS_ENTRY(class_entry, "Gutenberg_Parser_Phrase", NULL);
gutenberg_parser_phrase_class_entry = zend_register_internal_class(&class_entry TSRMLS_CC);
// Declare the create handler.
gutenberg_parser_phrase_class_entry->create_object = create_parser_node_object;
// The class is final.
gutenberg_parser_phrase_class_entry->ce_flags |= ZEND_ACC_FINAL;
// Declare the `content` public attribute, with an empty string for the default value.
zend_declare_property_string(gutenberg_parser_phrase_class_entry, "content", sizeof("content") - 1, "", ZEND_ACC_PUBLIC);
//
// Declare Gutenberg parser node object handlers.
//
memcpy(&gutenberg_parser_node_class_entry_handlers, zend_get_std_object_handlers(), sizeof(gutenberg_parser_node_class_entry_handlers));
gutenberg_parser_node_class_entry_handlers.offset = XtOffsetOf(gutenberg_parser_node, zobj);
gutenberg_parser_node_class_entry_handlers.dtor_obj = destroy_parser_node_object;
gutenberg_parser_node_class_entry_handlers.free_obj = free_parser_node_object;
return SUCCESS;
}
PHP_RINIT_FUNCTION(gutenberg_post_parser)
{
#if defined(ZTS) && defined(COMPILE_DL_GUTENBERG_POST_PARSER)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/*
* Provide information about the module.
*/
PHP_MINFO_FUNCTION(gutenberg_post_parser)
{
php_info_print_table_start();
php_info_print_table_header(2, "gutenberg_post_parser support", "enabled");
php_info_print_table_end();
}
/*
* Map Rust AST to a PHP array of objects of kind
* `Gutenberg_Parser_Block` and `Gutenberg_Parser_Phrase`.
*/
void into_php_objects(zval *php_array, const Vector_Node* nodes)
{
const uintptr_t number_of_nodes = nodes->length;
if (number_of_nodes == 0) {
return;
}
// Iterate over all nodes.
for (uintptr_t nth = 0; nth < number_of_nodes; ++nth) {
const Node node = nodes->buffer[nth];
// Map [rust] `Node::Block` to [php] `Gutenberg_Parser_Block`.
if (node.tag == Block) {
const Block_Body block = node.block;
zval php_block, php_block_namespace, php_block_name;
// Prepare the PHP strings.
ZVAL_STRINGL(&php_block_namespace, block.namespace.pointer, block.namespace.length);
ZVAL_STRINGL(&php_block_name, block.name.pointer, block.name.length);
// Create the `Gutenberg_Parser_Block` object.
object_init_ex(&php_block, gutenberg_parser_block_class_entry);
// Map [rust] `Node::Block.name.0` to [php] `Gutenberg_Parser_Block->namespace`.
add_property_zval(&php_block, "namespace", &php_block_namespace);
// Map [rust] `Node::Block.name.1` to [php] `Gutenberg_Parser_Block->name`.
add_property_zval(&php_block, "name", &php_block_name);
// Writing the property adds 1 to refcount.
zval_ptr_dtor(&php_block_namespace);
zval_ptr_dtor(&php_block_name);
// Default value for `Gutenberg_Parser_Block->attributes` is `NULL`.
// Allocate a string only if some value.
if (block.attributes.tag == Some) {
Slice_c_char attributes = block.attributes.some._0;
zval php_block_attributes;
// Prepare the PHP string.
ZVAL_STRINGL(&php_block_attributes, attributes.pointer, attributes.length);
// Map [rust] `Node::Block.attributes` to [php] `Gutenberg_Parser_Block->attributes`.
add_property_zval(&php_block, "attributes", &php_block_attributes);
// Writing the property adds 1 to refcount.
zval_ptr_dtor(&php_block_attributes);
}
const Vector_Node* children = (const Vector_Node*) (block.children);
// Default value for `Gutenberg_Parser_Block->children` is `NULL`.
// Allocate an array only if there is children.
if (children->length > 0) {
zval php_children_array;
array_init_size(&php_children_array, children->length);
into_php_objects(&php_children_array, children);
// Map [rust] `Node::Block.children` to [php] `Gutenberg_Parser_Block->children`.
add_property_zval(&php_block, "children", &php_children_array);
Z_DELREF(php_children_array);
}
free((void*) children);
// Insert `Gutenberg_Parser_Block` into the collection.
add_next_index_zval(php_array, &php_block);
}
// Map [rust] `Node::Phrase` to [php] `Gutenberg_Parser_Phrase`.
else if (node.tag == Phrase) {
Slice_c_char phrase = node.phrase._0;
zval php_phrase, php_phrase_content;
// Prepare the PHP string.
ZVAL_STRINGL(&php_phrase_content, phrase.pointer, phrase.length);
// Create the `Gutenberg_Parser_Phrase` object.
object_init_ex(&php_phrase, gutenberg_parser_phrase_class_entry);
// Map [rust] `Node::Phrase(content)` to [php] `Gutenberg_Parser_Phrase->content`.
add_property_zval(&php_phrase, "content", &php_phrase_content);
// Insert `Gutenberg_Parser_Phrase` into the collection.
add_next_index_zval(php_array, &php_phrase);
// Writing the property adds 1 to refcount.
zval_ptr_dtor(&php_phrase_content);
}
}
free((void*) nodes->buffer);
}
/*
* Declare the `gutenberg_post_parse(string $gutenberg_post_as_string): bool | array;` function.
*/
PHP_FUNCTION(gutenberg_post_parse)
{
char *input;
size_t input_len;
// Read the input as a string.
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &input, &input_len) == FAILURE) {
return;
}
// Parse the input.
Result parser_result = parse(input);
// If parsing failed, then return `false`.
if (parser_result.tag == Err) {
RETURN_FALSE;
}
// Else map the Rust AST into a PHP array.
const Vector_Node nodes = parser_result.ok._0;
// Note: `return_value` is a “magic” variable that holds the value to be returned.
//
// Allocate an array.
array_init_size(return_value, nodes.length);
// Map the Rust AST.
into_php_objects(return_value, &nodes);
}
/*
* Provide arginfo.
*/
ZEND_BEGIN_ARG_INFO(arginfo_gutenberg_post_parser, 0)
ZEND_ARG_INFO(0, gutenberg_post_as_string)
ZEND_END_ARG_INFO()
/*
* Declare functions.
*/
static const zend_function_entry gutenberg_post_parser_functions[] = {
PHP_FE(gutenberg_post_parse, arginfo_gutenberg_post_parser)
PHP_FE_END
};
/*
* Declare the module.
*/
zend_module_entry gutenberg_post_parser_module_entry = {
STANDARD_MODULE_HEADER,
"gutenberg_post_parser", /* Extension name */
gutenberg_post_parser_functions, /* zend_function_entry */
PHP_MINIT(gutenberg_post_parser), /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(gutenberg_post_parser), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(gutenberg_post_parser), /* PHP_MINFO - Module info */
PHP_GUTENBERG_POST_PARSER_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_GUTENBERG_POST_PARSER
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(gutenberg_post_parser)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/