1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2
3 /* libcroco - Library for parsing and applying CSS
4 * Copyright (C) 2006-2019 Free Software Foundation, Inc.
5 *
6 * This file is not part of the GNU gettext program, but is used with
7 * GNU gettext.
8 *
9 * The original copyright notice is as follows:
10 */
11
12 /*
13 * This file is part of The Croco Library
14 *
15 * Copyright (C) 2003-2004 Dodji Seketeli. All Rights Reserved.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of version 2.1 of the GNU Lesser General Public
19 * License as published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 * USA
30 *
31 * Author: Dodji Seketeli
32 */
33
34 /**
35 *@file
36 *The definition of the #CRToken class.
37 *Abstracts a css2 token.
38 */
39
40 #include <config.h>
41 #include <string.h>
42 #include "cr-token.h"
43
44 /*
45 *TODO: write a CRToken::to_string() method.
46 */
47
48 /**
49 *Frees the attributes of the current instance
50 *of #CRtoken.
51 *@param a_this the current instance of #CRToken.
52 */
53 static void
cr_token_clear(CRToken * a_this)54 cr_token_clear (CRToken * a_this)
55 {
56 g_return_if_fail (a_this);
57
58 switch (a_this->type) {
59 case S_TK:
60 case CDO_TK:
61 case CDC_TK:
62 case INCLUDES_TK:
63 case DASHMATCH_TK:
64 case PAGE_SYM_TK:
65 case MEDIA_SYM_TK:
66 case FONT_FACE_SYM_TK:
67 case CHARSET_SYM_TK:
68 case IMPORT_SYM_TK:
69 case IMPORTANT_SYM_TK:
70 case SEMICOLON_TK:
71 case NO_TK:
72 case DELIM_TK:
73 case CBO_TK:
74 case CBC_TK:
75 case BO_TK:
76 case BC_TK:
77 break;
78
79 case STRING_TK:
80 case IDENT_TK:
81 case HASH_TK:
82 case URI_TK:
83 case FUNCTION_TK:
84 case COMMENT_TK:
85 case ATKEYWORD_TK:
86 if (a_this->u.str) {
87 cr_string_destroy (a_this->u.str);
88 a_this->u.str = NULL;
89 }
90 break;
91
92 case EMS_TK:
93 case EXS_TK:
94 case LENGTH_TK:
95 case ANGLE_TK:
96 case TIME_TK:
97 case FREQ_TK:
98 case PERCENTAGE_TK:
99 case NUMBER_TK:
100 case PO_TK:
101 case PC_TK:
102 if (a_this->u.num) {
103 cr_num_destroy (a_this->u.num);
104 a_this->u.num = NULL;
105 }
106 break;
107
108 case DIMEN_TK:
109 if (a_this->u.num) {
110 cr_num_destroy (a_this->u.num);
111 a_this->u.num = NULL;
112 }
113
114 if (a_this->dimen) {
115 cr_string_destroy (a_this->dimen);
116 a_this->dimen = NULL;
117 }
118
119 break;
120
121 case RGB_TK:
122 if (a_this->u.rgb) {
123 cr_rgb_destroy (a_this->u.rgb) ;
124 a_this->u.rgb = NULL ;
125 }
126 break ;
127
128 case UNICODERANGE_TK:
129 /*not supported yet. */
130 break;
131
132 default:
133 cr_utils_trace_info ("I don't know how to clear this token\n") ;
134 break;
135 }
136
137 a_this->type = NO_TK;
138 }
139
140 /**
141 *Default constructor of
142 *the #CRToken class.
143 *@return the newly built instance of #CRToken.
144 */
145 CRToken *
cr_token_new(void)146 cr_token_new (void)
147 {
148 CRToken *result = NULL;
149
150 result = g_try_malloc (sizeof (CRToken));
151
152 if (result == NULL) {
153 cr_utils_trace_info ("Out of memory");
154 return NULL;
155 }
156
157 memset (result, 0, sizeof (CRToken));
158
159 return result;
160 }
161
162 /**
163 *Sets the type of curren instance of
164 *#CRToken to 'S_TK' (S in the css2 spec)
165 *@param a_this the current instance of #CRToken.
166 *@return CR_OK upon successfull completion, an error
167 *code otherwise.
168 */
169 enum CRStatus
cr_token_set_s(CRToken * a_this)170 cr_token_set_s (CRToken * a_this)
171 {
172 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
173
174 cr_token_clear (a_this);
175
176 a_this->type = S_TK;
177
178 return CR_OK;
179 }
180
181 /**
182 *Sets the type of the current instance of
183 *#CRToken to 'CDO_TK' (CDO as said by the css2 spec)
184 *@param a_this the current instance of #CRToken.
185 *@return CR_OK upon successfull completion, an error
186 *code otherwise.
187 */
188 enum CRStatus
cr_token_set_cdo(CRToken * a_this)189 cr_token_set_cdo (CRToken * a_this)
190 {
191 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
192
193 cr_token_clear (a_this);
194
195 a_this->type = CDO_TK;
196
197 return CR_OK;
198 }
199
200 /**
201 *Sets the type of the current token to
202 *CDC_TK (CDC as said by the css2 spec).
203 *@param a_this the current instance of #CRToken.
204 *@return CR_OK upon successfull completion, an error
205 *code otherwise.
206 */
207 enum CRStatus
cr_token_set_cdc(CRToken * a_this)208 cr_token_set_cdc (CRToken * a_this)
209 {
210 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
211
212 cr_token_clear (a_this);
213
214 a_this->type = CDC_TK;
215
216 return CR_OK;
217 }
218
219 /**
220 *Sets the type of the current instance of
221 *#CRToken to INCLUDES_TK (INCLUDES as said by the css2 spec).
222 *@param a_this the current instance of #CRToken.
223 *@return CR_OK upon successfull completion, an error
224 *code otherwise.
225 */
226 enum CRStatus
cr_token_set_includes(CRToken * a_this)227 cr_token_set_includes (CRToken * a_this)
228 {
229 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
230
231 cr_token_clear (a_this);
232
233 a_this->type = INCLUDES_TK;
234
235 return CR_OK;
236 }
237
238 /**
239 *Sets the type of the current instance of
240 *#CRToken to DASHMATCH_TK (DASHMATCH as said by the css2 spec).
241 *@param a_this the current instance of #CRToken.
242 *@return CR_OK upon successfull completion, an error
243 *code otherwise.
244 */
245 enum CRStatus
cr_token_set_dashmatch(CRToken * a_this)246 cr_token_set_dashmatch (CRToken * a_this)
247 {
248 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
249
250 cr_token_clear (a_this);
251
252 a_this->type = DASHMATCH_TK;
253
254 return CR_OK;
255 }
256
257 enum CRStatus
cr_token_set_comment(CRToken * a_this,CRString * a_str)258 cr_token_set_comment (CRToken * a_this, CRString * a_str)
259 {
260 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
261
262 cr_token_clear (a_this);
263 a_this->type = COMMENT_TK;
264 a_this->u.str = a_str ;
265 return CR_OK;
266 }
267
268 enum CRStatus
cr_token_set_string(CRToken * a_this,CRString * a_str)269 cr_token_set_string (CRToken * a_this, CRString * a_str)
270 {
271 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
272
273 cr_token_clear (a_this);
274
275 a_this->type = STRING_TK;
276
277 a_this->u.str = a_str ;
278
279 return CR_OK;
280 }
281
282 enum CRStatus
cr_token_set_ident(CRToken * a_this,CRString * a_ident)283 cr_token_set_ident (CRToken * a_this, CRString * a_ident)
284 {
285 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
286
287 cr_token_clear (a_this);
288 a_this->type = IDENT_TK;
289 a_this->u.str = a_ident;
290 return CR_OK;
291 }
292
293
294 enum CRStatus
cr_token_set_function(CRToken * a_this,CRString * a_fun_name)295 cr_token_set_function (CRToken * a_this, CRString * a_fun_name)
296 {
297 g_return_val_if_fail (a_this,
298 CR_BAD_PARAM_ERROR);
299
300 cr_token_clear (a_this);
301 a_this->type = FUNCTION_TK;
302 a_this->u.str = a_fun_name;
303 return CR_OK;
304 }
305
306 enum CRStatus
cr_token_set_hash(CRToken * a_this,CRString * a_hash)307 cr_token_set_hash (CRToken * a_this, CRString * a_hash)
308 {
309 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
310
311 cr_token_clear (a_this);
312 a_this->type = HASH_TK;
313 a_this->u.str = a_hash;
314
315 return CR_OK;
316 }
317
318 enum CRStatus
cr_token_set_rgb(CRToken * a_this,CRRgb * a_rgb)319 cr_token_set_rgb (CRToken * a_this, CRRgb * a_rgb)
320 {
321 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
322
323 cr_token_clear (a_this);
324 a_this->type = RGB_TK;
325 a_this->u.rgb = a_rgb;
326
327 return CR_OK;
328 }
329
330 enum CRStatus
cr_token_set_import_sym(CRToken * a_this)331 cr_token_set_import_sym (CRToken * a_this)
332 {
333 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
334
335 cr_token_clear (a_this);
336
337 a_this->type = IMPORT_SYM_TK;
338
339 return CR_OK;
340 }
341
342 enum CRStatus
cr_token_set_page_sym(CRToken * a_this)343 cr_token_set_page_sym (CRToken * a_this)
344 {
345 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
346
347 cr_token_clear (a_this);
348
349 a_this->type = PAGE_SYM_TK;
350
351 return CR_OK;
352 }
353
354 enum CRStatus
cr_token_set_media_sym(CRToken * a_this)355 cr_token_set_media_sym (CRToken * a_this)
356 {
357 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
358
359 cr_token_clear (a_this);
360
361 a_this->type = MEDIA_SYM_TK;
362
363 return CR_OK;
364 }
365
366 enum CRStatus
cr_token_set_font_face_sym(CRToken * a_this)367 cr_token_set_font_face_sym (CRToken * a_this)
368 {
369 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
370
371 cr_token_clear (a_this);
372 a_this->type = FONT_FACE_SYM_TK;
373
374 return CR_OK;
375 }
376
377 enum CRStatus
cr_token_set_charset_sym(CRToken * a_this)378 cr_token_set_charset_sym (CRToken * a_this)
379 {
380 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
381
382 cr_token_clear (a_this);
383 a_this->type = CHARSET_SYM_TK;
384
385 return CR_OK;
386 }
387
388 enum CRStatus
cr_token_set_atkeyword(CRToken * a_this,CRString * a_atname)389 cr_token_set_atkeyword (CRToken * a_this, CRString * a_atname)
390 {
391 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
392
393 cr_token_clear (a_this);
394 a_this->type = ATKEYWORD_TK;
395 a_this->u.str = a_atname;
396 return CR_OK;
397 }
398
399 enum CRStatus
cr_token_set_important_sym(CRToken * a_this)400 cr_token_set_important_sym (CRToken * a_this)
401 {
402 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
403 cr_token_clear (a_this);
404 a_this->type = IMPORTANT_SYM_TK;
405 return CR_OK;
406 }
407
408 enum CRStatus
cr_token_set_ems(CRToken * a_this,CRNum * a_num)409 cr_token_set_ems (CRToken * a_this, CRNum * a_num)
410 {
411 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
412 cr_token_clear (a_this);
413 a_this->type = EMS_TK;
414 a_this->u.num = a_num;
415 return CR_OK;
416 }
417
418 enum CRStatus
cr_token_set_exs(CRToken * a_this,CRNum * a_num)419 cr_token_set_exs (CRToken * a_this, CRNum * a_num)
420 {
421 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
422 cr_token_clear (a_this);
423 a_this->type = EXS_TK;
424 a_this->u.num = a_num;
425 return CR_OK;
426 }
427
428 enum CRStatus
cr_token_set_length(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)429 cr_token_set_length (CRToken * a_this, CRNum * a_num,
430 enum CRTokenExtraType a_et)
431 {
432 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
433
434 cr_token_clear (a_this);
435
436 a_this->type = LENGTH_TK;
437 a_this->extra_type = a_et;
438 a_this->u.num = a_num;
439
440 return CR_OK;
441 }
442
443 enum CRStatus
cr_token_set_angle(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)444 cr_token_set_angle (CRToken * a_this, CRNum * a_num,
445 enum CRTokenExtraType a_et)
446 {
447 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
448
449 cr_token_clear (a_this);
450
451 a_this->type = ANGLE_TK;
452 a_this->extra_type = a_et;
453 a_this->u.num = a_num;
454
455 return CR_OK;
456 }
457
458 enum CRStatus
cr_token_set_time(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)459 cr_token_set_time (CRToken * a_this, CRNum * a_num,
460 enum CRTokenExtraType a_et)
461 {
462 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
463
464 cr_token_clear (a_this);
465
466 a_this->type = TIME_TK;
467 a_this->extra_type = a_et;
468 a_this->u.num = a_num;
469
470 return CR_OK;
471 }
472
473 enum CRStatus
cr_token_set_freq(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)474 cr_token_set_freq (CRToken * a_this, CRNum * a_num,
475 enum CRTokenExtraType a_et)
476 {
477 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
478
479 cr_token_clear (a_this);
480
481 a_this->type = FREQ_TK;
482 a_this->extra_type = a_et;
483 a_this->u.num = a_num;
484
485 return CR_OK;
486 }
487
488 enum CRStatus
cr_token_set_dimen(CRToken * a_this,CRNum * a_num,CRString * a_dim)489 cr_token_set_dimen (CRToken * a_this, CRNum * a_num,
490 CRString * a_dim)
491 {
492 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
493 cr_token_clear (a_this);
494 a_this->type = DIMEN_TK;
495 a_this->u.num = a_num;
496 a_this->dimen = a_dim;
497 return CR_OK;
498
499 }
500
501 enum CRStatus
cr_token_set_percentage(CRToken * a_this,CRNum * a_num)502 cr_token_set_percentage (CRToken * a_this, CRNum * a_num)
503 {
504 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
505
506 cr_token_clear (a_this);
507
508 a_this->type = PERCENTAGE_TK;
509 a_this->u.num = a_num;
510
511 return CR_OK;
512 }
513
514 enum CRStatus
cr_token_set_number(CRToken * a_this,CRNum * a_num)515 cr_token_set_number (CRToken * a_this, CRNum * a_num)
516 {
517 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
518
519 cr_token_clear (a_this);
520
521 a_this->type = NUMBER_TK;
522 a_this->u.num = a_num;
523 return CR_OK;
524 }
525
526 enum CRStatus
cr_token_set_uri(CRToken * a_this,CRString * a_uri)527 cr_token_set_uri (CRToken * a_this, CRString * a_uri)
528 {
529 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
530
531 cr_token_clear (a_this);
532
533 a_this->type = URI_TK;
534 a_this->u.str = a_uri;
535
536 return CR_OK;
537 }
538
539 enum CRStatus
cr_token_set_delim(CRToken * a_this,guint32 a_char)540 cr_token_set_delim (CRToken * a_this, guint32 a_char)
541 {
542 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
543
544 cr_token_clear (a_this);
545
546 a_this->type = DELIM_TK;
547 a_this->u.unichar = a_char;
548
549 return CR_OK;
550 }
551
552 enum CRStatus
cr_token_set_semicolon(CRToken * a_this)553 cr_token_set_semicolon (CRToken * a_this)
554 {
555 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
556
557 cr_token_clear (a_this);
558
559 a_this->type = SEMICOLON_TK;
560
561 return CR_OK;
562 }
563
564 enum CRStatus
cr_token_set_cbo(CRToken * a_this)565 cr_token_set_cbo (CRToken * a_this)
566 {
567 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
568
569 cr_token_clear (a_this);
570
571 a_this->type = CBO_TK;
572
573 return CR_OK;
574 }
575
576 enum CRStatus
cr_token_set_cbc(CRToken * a_this)577 cr_token_set_cbc (CRToken * a_this)
578 {
579 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
580
581 cr_token_clear (a_this);
582
583 a_this->type = CBC_TK;
584
585 return CR_OK;
586 }
587
588 enum CRStatus
cr_token_set_po(CRToken * a_this)589 cr_token_set_po (CRToken * a_this)
590 {
591 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
592
593 cr_token_clear (a_this);
594
595 a_this->type = PO_TK;
596
597 return CR_OK;
598 }
599
600 enum CRStatus
cr_token_set_pc(CRToken * a_this)601 cr_token_set_pc (CRToken * a_this)
602 {
603 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
604
605 cr_token_clear (a_this);
606
607 a_this->type = PC_TK;
608
609 return CR_OK;
610 }
611
612 enum CRStatus
cr_token_set_bo(CRToken * a_this)613 cr_token_set_bo (CRToken * a_this)
614 {
615 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
616
617 cr_token_clear (a_this);
618
619 a_this->type = BO_TK;
620
621 return CR_OK;
622 }
623
624 enum CRStatus
cr_token_set_bc(CRToken * a_this)625 cr_token_set_bc (CRToken * a_this)
626 {
627 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
628
629 cr_token_clear (a_this);
630
631 a_this->type = BC_TK;
632
633 return CR_OK;
634 }
635
636 /**
637 *The destructor of the #CRToken class.
638 *@param a_this the current instance of #CRToken.
639 */
640 void
cr_token_destroy(CRToken * a_this)641 cr_token_destroy (CRToken * a_this)
642 {
643 g_return_if_fail (a_this);
644
645 cr_token_clear (a_this);
646
647 g_free (a_this);
648 }
649