php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
token.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2020 Alexander Borisov
3 *
4 * Author: Alexander Borisov <borisov@lexbor.com>
5 */
6
7#include "lexbor/core/shs.h"
8#include "lexbor/core/conv.h"
10#include "lexbor/core/print.h"
11
12#include "lexbor/css/parser.h"
16
17#define LXB_CSS_SYNTAX_TOKEN_RES_NAME_SHS_MAP
19
20#define LEXBOR_STR_RES_MAP_HEX
21#define LEXBOR_STR_RES_ANSI_REPLACEMENT_CHARACTER
22#include "lexbor/core/str_res.h"
23
24
27
31
32
33typedef struct {
36}
38
39
40static lxb_status_t
41lxb_css_syntax_token_str_cb(const lxb_char_t *data, size_t len, void *ctx);
42
43
46{
47 if (tkz->cache_pos < tkz->cache->length
48 && (tkz->prepared == 0 || tkz->cache_pos < tkz->prepared))
49 {
50 return tkz->cache->list[tkz->cache_pos];
51 }
52
54}
55
61
62void
64{
66
67 if (tkz->cache_pos < tkz->cache->length) {
68 if (tkz->prepared != 0 && tkz->cache_pos >= tkz->prepared) {
69 return;
70 }
71
72 token = tkz->cache->list[tkz->cache_pos];
73
75 lexbor_dobject_free(tkz->tokens, token);
76
77 tkz->cache_pos += 1;
78
79 if (tkz->cache_pos >= tkz->cache->length) {
80 tkz->cache->length = 0;
81 tkz->cache_pos = 0;
82 }
83 }
84}
85
86void
88{
89 while (count != 0) {
90 count--;
92 }
93}
94
97 lexbor_str_t *str, lexbor_mraw_t *mraw)
98{
99 size_t length;
100
101 length = token->length + 1;
102
103 if (length > str->length) {
104 if (str->data == NULL) {
105 str->data = lexbor_mraw_alloc(mraw, length);
106 if (str->data == NULL) {
108 }
109
110 str->length = 0;
111 }
112 else {
113 if (lexbor_str_realloc(str, mraw, length) == NULL) {
115 }
116 }
117 }
118
119 /* + 1 = '\0' */
120 memcpy(str->data, token->data, length);
121
122 str->length = token->length;
123
124 return LXB_STATUS_OK;
125}
126
130{
132 lxb_css_syntax_token_string_t *token_string;
133
134 if (token->type >= LXB_CSS_SYNTAX_TOKEN_IDENT
136 {
137 token_string = lxb_css_syntax_token_string(token);
138 goto copy;
139 }
140 else if (token->type == LXB_CSS_SYNTAX_TOKEN_DIMENSION) {
141 token_string = lxb_css_syntax_token_dimension_string(token);
142 goto copy;
143 }
144
145 return LXB_STATUS_OK;
146
147copy:
148
149 data = lexbor_mraw_alloc(tkz->mraw, token_string->length + 1);
150 if (data == NULL) {
152 return tkz->status;
153 }
154
155 /* + 1 = '\0' */
156 memcpy(data, token_string->data, token_string->length + 1);
157
158 token_string->data = data;
159 token->cloned = true;
160
161 return LXB_STATUS_OK;
162}
163
166{
169
170 token = lexbor_dobject_alloc(tkz->tokens);
171 if (token == NULL) {
173 return NULL;
174 }
175
177 if (status != LXB_STATUS_OK) {
178 tkz->status = status;
179 return NULL;
180 }
181
182 token->cloned = false;
183
184 return token;
185}
186
187
188void
191{
192 lxb_css_syntax_token_string_t *token_string;
193
194 if (token->cloned) {
195 if (token->type == LXB_CSS_SYNTAX_TOKEN_DIMENSION) {
196 token_string = lxb_css_syntax_token_dimension_string(token);
197 }
198 else {
199 token_string = lxb_css_syntax_token_string(token);
200 }
201
202 lexbor_mraw_free(tkz->mraw, (lxb_char_t *) token_string->data);
203 }
204}
205
206const lxb_char_t *
208{
209 switch (type) {
211 return (lxb_char_t *) "ident";
213 return (lxb_char_t *) "function";
215 return (lxb_char_t *) "at-keyword";
217 return (lxb_char_t *) "hash";
219 return (lxb_char_t *) "string";
221 return (lxb_char_t *) "bad-string";
223 return (lxb_char_t *) "url";
225 return (lxb_char_t *) "bad-url";
227 return (lxb_char_t *) "delim";
229 return (lxb_char_t *) "number";
231 return (lxb_char_t *) "percentage";
233 return (lxb_char_t *) "dimension";
235 return (lxb_char_t *) "whitespace";
237 return (lxb_char_t *) "CDO";
239 return (lxb_char_t *) "CDC";
241 return (lxb_char_t *) "colon";
243 return (lxb_char_t *) "semicolon";
245 return (lxb_char_t *) "comma";
247 return (lxb_char_t *) "left-square-bracket";
249 return (lxb_char_t *) "right-square-bracket";
251 return (lxb_char_t *) "left-parenthesis";
253 return (lxb_char_t *) "right-parenthesis";
255 return (lxb_char_t *) "left-curly-bracket";
257 return (lxb_char_t *) "right-curly-bracket";
259 return (lxb_char_t *) "comment";
261 return (lxb_char_t *) "end-of-file";
263 return (lxb_char_t *) "end";
264 default:
265 return (lxb_char_t *) "undefined";
266 }
267}
268
271{
272 const lexbor_shs_entry_t *entry;
273
274 entry = lexbor_shs_entry_get_lower_static(lxb_css_syntax_token_res_name_shs_map,
275 type_name, len);
276 if (entry == NULL) {
278 }
279
280 return (lxb_css_syntax_token_type_t) (uintptr_t) entry->value;
281}
282
283
286 lxb_css_syntax_token_cb_f cb, void *ctx)
287{
288 size_t len;
290 lxb_char_t buf[128];
293
294 switch (token->type) {
296 buf[0] = token->types.delim.character;
297 buf[1] = 0x00;
298
299 return cb(buf, 1, ctx);
300
303 buf, (sizeof(buf) - 1));
304
305 buf[len] = 0x00;
306
307 return cb(buf, len, ctx);
308
311 buf, (sizeof(buf) - 1));
312
313 buf[len] = 0x00;
314
315 status = cb(buf, len, ctx);
316 if (status != LXB_STATUS_OK) {
317 return status;
318 }
319
320 return cb((lxb_char_t *) "%", 1, ctx);
321
323 return cb((lxb_char_t *) "<!--", 4, ctx);
324
326 return cb((lxb_char_t *) "-->", 3, ctx);
327
329 return cb((lxb_char_t *) ":", 1, ctx);
330
332 return cb((lxb_char_t *) ";", 1, ctx);
333
335 return cb((lxb_char_t *) ",", 1, ctx);
336
338 return cb((lxb_char_t *) "[", 1, ctx);
339
341 return cb((lxb_char_t *) "]", 1, ctx);
342
344 return cb((lxb_char_t *) "(", 1, ctx);
345
347 return cb((lxb_char_t *) ")", 1, ctx);
348
350 return cb((lxb_char_t *) "{", 1, ctx);
351
353 return cb((lxb_char_t *) "}", 1, ctx);
354
356 status = cb((lxb_char_t *) "#", 1, ctx);
357 if (status != LXB_STATUS_OK) {
358 return status;
359 }
360
361 str = &token->types.string;
362
363 return cb(str->data, str->length, ctx);
364
366 status = cb((lxb_char_t *) "@", 1, ctx);
367 if (status != LXB_STATUS_OK) {
368 return status;
369 }
370
371 str = &token->types.string;
372
373 return cb(str->data, str->length, ctx);
374
377 str = &token->types.string;
378
379 return cb(str->data, str->length, ctx);
380
382 str = &token->types.string;
383
384 status = cb(str->data, str->length, ctx);
385 if (status != LXB_STATUS_OK) {
386 return status;
387 }
388
389 return cb((lxb_char_t *) "(", 1, ctx);
390
393 status = cb((lxb_char_t *) "\"", 1, ctx);
394 if (status != LXB_STATUS_OK) {
395 return status;
396 }
397
398 const lxb_char_t *begin = token->types.string.data;
399 const lxb_char_t *end = begin + token->types.string.length;
400
401 const lxb_char_t *ptr = begin;
402
403 for (; begin < end; begin++) {
404 /* 0x5C; '\'; Inverse/backward slash */
405 if (*begin == 0x5C) {
406 begin += 1;
407
408 status = cb(ptr, (begin - ptr), ctx);
409 if (status != LXB_STATUS_OK) {
410 return status;
411 }
412
413 if (begin == end) {
414 status = cb((const lxb_char_t *) "\\", 1, ctx);
415 if (status != LXB_STATUS_OK) {
416 return status;
417 }
418
419 ptr = begin;
420
421 break;
422 }
423
424 begin -= 1;
425 ptr = begin;
426 }
427 /* 0x22; '"'; Only quotes above */
428 else if (*begin == 0x22) {
429 if (ptr != begin) {
430 status = cb(ptr, (begin - ptr), ctx);
431 if (status != LXB_STATUS_OK) {
432 return status;
433 }
434 }
435
436 status = cb((const lxb_char_t *) "\\", 1, ctx);
437 if (status != LXB_STATUS_OK) {
438 return status;
439 }
440
441 ptr = begin;
442 }
443 }
444
445 if (ptr != begin) {
446 status = cb(ptr, (begin - ptr), ctx);
447 if (status != LXB_STATUS_OK) {
448 return status;
449 }
450 }
451
452 return cb((const lxb_char_t *) "\"", 1, ctx);
453 }
454
457 status = cb((lxb_char_t *) "url(", 4, ctx);
458 if (status != LXB_STATUS_OK) {
459 return status;
460 }
461
462 str = &token->types.string;
463
464 status = cb(str->data, str->length, ctx);
465 if (status != LXB_STATUS_OK) {
466 return status;
467 }
468
469 return cb((lxb_char_t *) ")", 1, ctx);
470
472 status = cb((lxb_char_t *) "/*", 2, ctx);
473 if (status != LXB_STATUS_OK) {
474 return status;
475 }
476
477 str = &token->types.string;
478
479 status = cb(str->data, str->length, ctx);
480 if (status != LXB_STATUS_OK) {
481 return status;
482 }
483
484 return cb((lxb_char_t *) "*/", 2, ctx);
485
488 buf, (sizeof(buf) - 1));
489
490 buf[len] = 0x00;
491
492 status = cb(buf, len, ctx);
493 if (status != LXB_STATUS_OK) {
494 return status;
495 }
496
497 dim = &token->types.dimension;
498
499 return cb(dim->str.data, dim->str.length, ctx);
500
502 return cb((lxb_char_t *) "END-OF-FILE", 11, ctx);
503
505 return cb((lxb_char_t *) "END", 3, ctx);
506
507 default:
508 return LXB_STATUS_ERROR;
509 }
510}
511
514 lexbor_str_t *str, lexbor_mraw_t *mraw)
515{
517
518 ctx.str = str;
519 ctx.mraw = mraw;
520
521 if (str->data == NULL) {
522 lexbor_str_init(str, mraw, 1);
523 if (str->data == NULL) {
525 }
526 }
527
528 return lxb_css_syntax_token_serialize(token, lxb_css_syntax_token_str_cb,
529 &ctx);
530}
531
532static lxb_status_t
533lxb_css_syntax_token_str_cb(const lxb_char_t *data, size_t len, void *cb_ctx)
534{
537
538 ptr = lexbor_str_append(ctx->str, ctx->mraw, data, len);
539 if (ptr == NULL) {
541 }
542
543 return LXB_STATUS_OK;
544}
545
546
549 size_t *out_length)
550{
551 size_t length = 0;
553 lexbor_str_t str;
554
556 &length);
557 if (status != LXB_STATUS_OK) {
558 goto failed;
559 }
560
561 /* + 1 == '\0' */
562 str.data = lexbor_malloc(length + 1);
563 if (str.data == NULL) {
564 goto failed;
565 }
566
567 str.length = 0;
568
570 &str);
571 if (status != LXB_STATUS_OK) {
572 lexbor_free(str.data);
573 goto failed;
574 }
575
576 str.data[str.length] = '\0';
577
578 if (out_length != NULL) {
579 *out_length = str.length;
580 }
581
582 return str.data;
583
584failed:
585
586 if (out_length != NULL) {
587 *out_length = 0;
588 }
589
590 return NULL;
591}
592
595 const lxb_css_syntax_token_t *token,
596 const char *module_name)
597{
600
601 static const char unexpected[] = "%s. Unexpected token: %s";
602
604 if (name == NULL) {
605 return NULL;
606 }
607
608 msg = lxb_css_log_format(parser->log, LXB_CSS_LOG_SYNTAX_ERROR, unexpected,
609 module_name, name);
610
612
613 return msg;
614}
615
616/*
617 * No inline functions for ABI.
618 */
624
625void
630
637
638const lxb_char_t *
643
size_t len
Definition apprentice.c:174
char * cb
Definition assert.c:26
copy(string $from, string $to, $context=null)
count(Countable|array $value, int $mode=COUNT_NORMAL)
size_t lexbor_conv_float_to_data(double num, lxb_char_t *buf, size_t len)
Definition conv.c:16
@ LXB_STATUS_ERROR_MEMORY_ALLOCATION
Definition base.h:51
@ LXB_STATUS_OK
Definition base.h:49
@ LXB_STATUS_ERROR
Definition base.h:50
struct lxb_css_syntax_token lxb_css_syntax_token_t
Definition base.h:46
struct lxb_css_syntax_tokenizer lxb_css_syntax_tokenizer_t
Definition base.h:45
struct lxb_css_parser lxb_css_parser_t
Definition base.h:41
lxb_css_syntax_token_t * lxb_css_syntax_token_create_noi(lexbor_dobject_t *dobj)
Definition token.c:620
lxb_status_t lxb_css_syntax_token_string_make(lxb_css_syntax_tokenizer_t *tkz, lxb_css_syntax_token_t *token)
Definition token.c:128
const lxb_char_t * lxb_css_syntax_token_type_name_by_id(lxb_css_syntax_token_type_t type)
Definition token.c:207
lxb_css_log_message_t * lxb_css_syntax_token_error(lxb_css_parser_t *parser, const lxb_css_syntax_token_t *token, const char *module_name)
Definition token.c:594
lxb_char_t * lxb_css_syntax_token_serialize_char(const lxb_css_syntax_token_t *token, size_t *out_length)
Definition token.c:548
lxb_css_syntax_token_type_t lxb_css_syntax_token_type_id_by_name(const lxb_char_t *type_name, size_t len)
Definition token.c:270
lxb_status_t lxb_css_syntax_tokenizer_cache_push(lxb_css_syntax_tokenizer_cache_t *cache, lxb_css_syntax_token_t *value)
Definition tokenizer.c:246
void lxb_css_syntax_token_string_free(lxb_css_syntax_tokenizer_t *tkz, lxb_css_syntax_token_t *token)
Definition token.c:189
lxb_css_syntax_token_t * lxb_css_syntax_token_cached_create(lxb_css_syntax_tokenizer_t *tkz)
Definition token.c:165
const lxb_char_t * lxb_css_syntax_token_type_name_noi(lxb_css_syntax_token_t *token)
Definition token.c:639
lxb_css_syntax_token_t * lxb_css_syntax_tokenizer_token(lxb_css_syntax_tokenizer_t *tkz)
Definition tokenizer.c:278
lxb_css_syntax_token_t * lxb_css_syntax_token(lxb_css_syntax_tokenizer_t *tkz)
Definition token.c:45
lxb_status_t lxb_css_syntax_token_serialize_str(const lxb_css_syntax_token_t *token, lexbor_str_t *str, lexbor_mraw_t *mraw)
Definition token.c:513
lxb_css_syntax_token_t * lxb_css_syntax_token_next(lxb_css_syntax_tokenizer_t *tkz)
Definition token.c:57
lxb_css_syntax_token_t * lxb_css_syntax_token_destroy_noi(lxb_css_syntax_token_t *token, lexbor_dobject_t *dobj)
Definition token.c:632
void lxb_css_syntax_token_consume_n(lxb_css_syntax_tokenizer_t *tkz, unsigned count)
Definition token.c:87
lxb_css_syntax_token_type_t lxb_css_syntax_token_type_noi(lxb_css_syntax_token_t *token)
Definition token.c:645
lxb_status_t lxb_css_syntax_token_serialize(const lxb_css_syntax_token_t *token, lxb_css_syntax_token_cb_f cb, void *ctx)
Definition token.c:285
lxb_status_t lxb_css_syntax_token_string_dup(lxb_css_syntax_token_string_t *token, lexbor_str_t *str, lexbor_mraw_t *mraw)
Definition token.c:96
void lxb_css_syntax_token_consume(lxb_css_syntax_tokenizer_t *tkz)
Definition token.c:63
void lxb_css_syntax_token_clean_noi(lxb_css_syntax_token_t *token)
Definition token.c:626
lxb_inline lxb_css_syntax_token_type_t lxb_css_syntax_token_type(const lxb_css_syntax_token_t *token)
Definition token.h:277
struct lxb_css_syntax_token_dimension lxb_css_syntax_token_dimension_t
lxb_inline lxb_css_syntax_token_t * lxb_css_syntax_token_create(lexbor_dobject_t *dobj)
Definition token.h:252
lxb_inline lxb_css_syntax_token_t * lxb_css_syntax_token_destroy(lxb_css_syntax_token_t *token, lexbor_dobject_t *dobj)
Definition token.h:264
#define lxb_css_syntax_token_string(token)
Definition token.h:25
lxb_status_t(* lxb_css_syntax_token_cb_f)(const lxb_char_t *data, size_t len, void *ctx)
Definition token.h:58
lxb_inline const lxb_char_t * lxb_css_syntax_token_type_name(const lxb_css_syntax_token_t *token)
Definition token.h:271
lxb_css_syntax_token_type_t
Definition token.h:68
@ LXB_CSS_SYNTAX_TOKEN_FUNCTION
Definition token.h:73
@ LXB_CSS_SYNTAX_TOKEN_LS_BRACKET
Definition token.h:95
@ LXB_CSS_SYNTAX_TOKEN_BAD_URL
Definition token.h:79
@ LXB_CSS_SYNTAX_TOKEN_RC_BRACKET
Definition token.h:100
@ LXB_CSS_SYNTAX_TOKEN__EOF
Definition token.h:101
@ LXB_CSS_SYNTAX_TOKEN_NUMBER
Definition token.h:88
@ LXB_CSS_SYNTAX_TOKEN_BAD_STRING
Definition token.h:77
@ LXB_CSS_SYNTAX_TOKEN_CDO
Definition token.h:90
@ LXB_CSS_SYNTAX_TOKEN_COMMA
Definition token.h:94
@ LXB_CSS_SYNTAX_TOKEN_CDC
Definition token.h:91
@ LXB_CSS_SYNTAX_TOKEN_DELIM
Definition token.h:87
@ LXB_CSS_SYNTAX_TOKEN_COLON
Definition token.h:92
@ LXB_CSS_SYNTAX_TOKEN_UNDEF
Definition token.h:69
@ LXB_CSS_SYNTAX_TOKEN_DIMENSION
Definition token.h:84
@ LXB_CSS_SYNTAX_TOKEN_RS_BRACKET
Definition token.h:96
@ LXB_CSS_SYNTAX_TOKEN_AT_KEYWORD
Definition token.h:74
@ LXB_CSS_SYNTAX_TOKEN_STRING
Definition token.h:76
@ LXB_CSS_SYNTAX_TOKEN_URL
Definition token.h:78
@ LXB_CSS_SYNTAX_TOKEN_IDENT
Definition token.h:72
@ LXB_CSS_SYNTAX_TOKEN_WHITESPACE
Definition token.h:81
@ LXB_CSS_SYNTAX_TOKEN_LC_BRACKET
Definition token.h:99
@ LXB_CSS_SYNTAX_TOKEN_L_PARENTHESIS
Definition token.h:97
@ LXB_CSS_SYNTAX_TOKEN_HASH
Definition token.h:75
@ LXB_CSS_SYNTAX_TOKEN_PERCENTAGE
Definition token.h:89
@ LXB_CSS_SYNTAX_TOKEN_COMMENT
Definition token.h:80
@ LXB_CSS_SYNTAX_TOKEN_R_PARENTHESIS
Definition token.h:98
@ LXB_CSS_SYNTAX_TOKEN_SEMICOLON
Definition token.h:93
@ LXB_CSS_SYNTAX_TOKEN__END
Definition token.h:103
lxb_inline void lxb_css_syntax_token_clean(lxb_css_syntax_token_t *token)
Definition token.h:258
struct lxb_css_syntax_token_string lxb_css_syntax_token_string_t
#define lxb_css_syntax_token_dimension_string(token)
Definition token.h:34
DNS_STATUS status
Definition dns_win32.c:49
void * lexbor_dobject_free(lexbor_dobject_t *dobject, void *data)
Definition dobject.c:135
void * lexbor_dobject_alloc(lexbor_dobject_t *dobject)
Definition dobject.c:91
int begin
Definition eaw_table.h:20
zend_ffi_type * type
Definition ffi.c:3812
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
#define NULL
Definition gdcache.h:45
LXB_API void * lexbor_free(void *dst)
Definition memory.c:33
LXB_API void * lexbor_malloc(size_t size)
Definition memory.c:15
lxb_css_log_message_t * lxb_css_log_format(lxb_css_log_t *log, lxb_css_log_type_t type, const char *format,...)
Definition log.c:140
@ LXB_CSS_LOG_SYNTAX_ERROR
Definition log.h:24
void * lexbor_mraw_free(lexbor_mraw_t *mraw, void *data)
Definition mraw.c:392
void * lexbor_mraw_alloc(lexbor_mraw_t *mraw, size_t size)
Definition mraw.c:180
unsigned const char * end
Definition php_ffi.h:51
char * msg
Definition phpdbg.h:289
zend_constant * data
lxb_status_t lexbor_serialize_length_cb(const lxb_char_t *data, size_t length, void *ctx)
Definition serialize.c:12
lxb_status_t lexbor_serialize_copy_cb(const lxb_char_t *data, size_t length, void *ctx)
Definition serialize.c:19
const lexbor_shs_entry_t * lexbor_shs_entry_get_lower_static(const lexbor_shs_entry_t *root, const lxb_char_t *key, size_t key_len)
Definition shs.c:63
lxb_char_t * lexbor_str_realloc(lexbor_str_t *str, lexbor_mraw_t *mraw, size_t new_size)
Definition str.c:94
lxb_char_t * lexbor_str_append(lexbor_str_t *str, lexbor_mraw_t *mraw, const lxb_char_t *buff, size_t length)
Definition str.c:131
lxb_char_t * lexbor_str_init(lexbor_str_t *str, lexbor_mraw_t *mraw, size_t size)
Definition str.c:22
Definition shs.h:19
void * value
Definition shs.h:21
lxb_char_t * data
Definition str.h:47
size_t length
Definition str.h:48
lxb_css_log_t * log
Definition parser.h:168
lexbor_mraw_t * mraw
Definition token.c:35
lexbor_str_t * str
Definition token.c:34
const lxb_char_t * data
Definition token.h:128
lxb_css_syntax_token_type_t type
Definition token.h:192
union lxb_css_syntax_token::lxb_css_syntax_token_u types
lxb_css_syntax_token_t ** list
Definition tokenizer.h:37
lexbor_mraw_t * mraw
Definition tokenizer.h:57
lexbor_dobject_t * tokens
Definition tokenizer.h:45
lxb_css_syntax_tokenizer_cache_t * cache
Definition tokenizer.h:44
unsigned int lxb_status_t
Definition types.h:28
unsigned char lxb_char_t
Definition types.h:27
lxb_css_syntax_token_delim_t delim
Definition token.h:178
lxb_css_syntax_token_number_t number
Definition token.h:172
lxb_css_syntax_token_dimension_t dimension
Definition token.h:173
lxb_css_syntax_token_string_t string
Definition token.h:176
zval * dim
zend_string * name
value