-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtoken.c
602 lines (485 loc) · 11.8 KB
/
token.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright (C) 2011-2014 YesLogic Pty. Ltd.
// Released as Open Source (see COPYING.txt for details)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "token.h"
#include "util.h"
/*------------------------------------------------------------------------*/
token *create_doctype_token(unsigned char ch)
{
token *tk;
tk = malloc(sizeof(token));
tk->type = TOKEN_DOCTYPE;
tk->dt.doctype_name = string_append(NULL, ch);
tk->dt.doctype_public_identifier = NULL; //meaning: missing
tk->dt.doctype_system_identifier = NULL; //meaning: missing
tk->dt.force_quirks_flag = OFF;
return tk;
}
token *create_start_tag_token(unsigned char ch)
{
token *tk;
tk = malloc(sizeof(token));
tk->type = TOKEN_START_TAG;
tk->stt.tag_name = string_append(NULL, ch);
tk->stt.self_closing_flag = UNSET;
tk->stt.attributes = NULL;
return tk;
}
token *create_end_tag_token(unsigned char ch)
{
token *tk;
tk = malloc(sizeof(token));
tk->type = TOKEN_END_TAG;
tk->ett.tag_name = string_append(NULL, ch);
return tk;
}
token *create_comment_token(void)
{
token *tk;
tk = malloc(sizeof(token));
tk->type = TOKEN_COMMENT;
tk->cmt.comment= string_append(NULL, '\0');
return tk;
}
token *create_multi_char_token(unsigned char *mch, long char_count)
{
token *tk;
tk = malloc(sizeof(token));
tk->type = TOKEN_MULTI_CHAR;
tk->mcht.mch = mch;
tk->mcht.char_count = char_count;
return tk;
}
token *create_eof_token(void)
{
token *tk;
tk = malloc(sizeof(token));
tk->type = TOKEN_EOF;
return tk;
}
void free_token(token *tk)
{
//attribute_list *attrs, *temp_ptr;
if(tk != NULL)
{
switch (tk->type)
{
case TOKEN_DOCTYPE:
free(tk->dt.doctype_name);
free(tk->dt.doctype_public_identifier);
free(tk->dt.doctype_system_identifier);
break;
case TOKEN_START_TAG:
free(tk->stt.tag_name);
/*
attrs = tk->stt.attributes;
while(attrs != NULL)
{
free(attrs->name);
free(attrs->value);
temp_ptr = attrs;
attrs = attrs->tail;
free(temp_ptr);
}*/
break;
case TOKEN_END_TAG:
free(tk->ett.tag_name);
break;
case TOKEN_COMMENT:
free(tk->cmt.comment);
break;
case TOKEN_MULTI_CHAR:
break;
case TOKEN_EOF:
break;
default:
break;
}
free(tk);
}
}
/*-------------------------------------------------------------*/
void free_attributes(attribute_list *list)
{
attribute_list *temp_ptr;
while(list != NULL)
{
free(list->name);
free(list->value);
temp_ptr = list;
list = list->tail;
free(temp_ptr);
}
}
/*-------------------------------------------------------------*/
attribute_list *clone_attributes(attribute_list *list)
{
attribute_list *clone;
attribute_list *new_node, *last_node;
clone = NULL;
while(list != NULL)
{
new_node = (attribute_list *)malloc(sizeof(attribute_list));
new_node->name = strdup(list->name);
new_node->value = strdup(list->value);
new_node->attr_ns = list->attr_ns ;
new_node->tail = NULL;
if(clone == NULL)
{
clone = new_node;
}
else
{
last_node->tail = new_node;
}
last_node = new_node;
list = list->tail ;
}
return clone;
}
/*-------------------------------------------------------------*/
/*add the name-value pair to the end of attr_list*/
attribute_list *html_attribute_list_cons(unsigned char *name,
unsigned char *value,
namespace_type attr_name_space,
attribute_list *attr_list)
{
attribute_list *list;
if((name == NULL) || (value == NULL))
{
return attr_list;
}
list = (attribute_list *)malloc(sizeof(attribute_list));
list->name = strdup(name);
list->value = strdup(value);
list->attr_ns = attr_name_space;
list->tail = NULL;
if(attr_list == NULL)
{
return list;
}
else
{
attribute_list *temp_ptr = attr_list;
while(temp_ptr->tail != NULL)
{
temp_ptr = temp_ptr->tail;
}
temp_ptr->tail = list;
return attr_list;
}
}
/*-------------------------------------------------------------*/
/*returns 0 if attr1 and attr2 are identical:
Attr1 and attr2 have the same number of name-value pairs and the name-value pairs are identical.
The order of the name-value pairs does not matter.
Otherwise, return 1.
*/
int attr_list_comp(attribute_list *attr1, attribute_list *attr2)
{
attribute_list *temp_attr1, *temp_attr2;
temp_attr1 = attr1;
temp_attr2 = attr2;
while(temp_attr1 != NULL)
{
if(attribute_in_list(temp_attr1->name, temp_attr1->value, attr2) != 1)
{
return 1; //attr1 and attr2 NOT identical.
}
temp_attr1 = temp_attr1->tail;
}
while(temp_attr2 != NULL)
{
if(attribute_in_list(temp_attr2->name, temp_attr2->value, attr1) != 1)
{
return 1; //attr1 and attr2 NOT identical.
}
temp_attr2 = temp_attr2->tail;
}
return 0; //attr1 and attr2 are identical, including the case where both are NULL.
}
/*-------------------------------------------------------------*/
/*returns 1 if name and value is a name-value pair in list.
Otherwise returns 0.*/
int attribute_in_list(unsigned char *name, unsigned char *value, attribute_list *list)
{
if((name == NULL) || (value == NULL))
{
return 0;
}
while(list != NULL)
{
if((list->name != NULL) && (list->value != NULL))
{
if((strcmp(list->name, name) == 0) && (strcmp(list->value, value) == 0))
{
return 1;
}
}
list = list->tail;
}
return 0;
}
/*-------------------------------------------------------------*/
/*returns 1 if attr_name exists in list, whatever value it might have*/
/*returns 0 if list is NULL, or attr_name does not exist in list*/
int attribute_name_in_list(unsigned char *attr_name, attribute_list *list)
{
assert(attr_name != NULL);
while(list != NULL)
{
if(list->name != NULL)
{
if(strcmp(list->name, attr_name) == 0)
{
return 1;
}
}
list = list->tail;
}
return 0;
}
/*-------------------------------------------------------------*/
/*return a copy of the value of attr_name if attr_name exists in list and its value is not NULL*/
/*return NULL if list is NULL, or attr_name does not exist in list, or its value is NULL*/
unsigned char *get_attribute_value(unsigned char *attr_name, attribute_list *list)
{
assert(attr_name != NULL);
while(list != NULL)
{
if(list->name != NULL)
{
if(strcmp(list->name, attr_name) == 0)
{
if(list->value != NULL)
{
unsigned char *attr_value;
attr_value = strdup(list->value);
return attr_value;
}
else
{
return NULL;
}
}
}
list = list->tail;
}
return NULL;
}
/*-------------------------------------------------------------*/
/*if list is NULL, return NULL*/
/*otherwise if attr_name exists in list, remove it and return the list with attr_name removed*/
/*if attr_name does not exist in list, return list unmodified*/
attribute_list *remove_attribute(unsigned char *attr_name, attribute_list *list)
{
assert(attr_name != NULL);
if(list == NULL)
{
return NULL;
}
else
{
if((list->name != NULL) && (strcmp(list->name, attr_name) == 0))
{
attribute_list *list_tail = list->tail;
free(list->name);
free(list->value);
free(list);
return list_tail;
}
else
{
attribute_list *previous_node, *temp_list = list;
previous_node = temp_list;
temp_list = temp_list->tail;
while(temp_list != NULL)
{
if((temp_list->name != NULL) && (strcmp(temp_list->name, attr_name) == 0))
{
free(temp_list->name);
free(temp_list->value);
previous_node->tail = temp_list->tail;
free(temp_list);
break;
}
previous_node = temp_list;
temp_list = temp_list->tail;
}
return list;
}
}
}
/*-------------------------------------------------------------*/
token_list *html_token_list_nil(void)
{
return NULL;
}
token_list *html_token_list_cons(token *tk, token_list *tail)
{
token_list *list;
list = (token_list *)malloc(sizeof(token_list));
list->tk = tk;
list->tail = tail;
return list;
}
token_list *html_token_list_reverse(token_list *tk_list)
{
token_list *rev_list = NULL, *temp_ptr;
while(tk_list != NULL)
{
rev_list = html_token_list_cons(tk_list->tk, rev_list);
temp_ptr = tk_list;
tk_list = tk_list->tail;
free(temp_ptr);
}
return rev_list;
}
token *html_token_list_get_first_token(token_list *tk_list)
{
if(tk_list != NULL)
{
return tk_list->tk;
}
else
{
return NULL;
}
}
token_list *html_token_list_get_tail(token_list *tk_list)
{
if(tk_list != NULL)
{
return tk_list->tail;
}
else
{
return NULL;
}
}
/*-------------------------------------------------------------*/
void html_print_token(token *tk)
{
switch (tk->type)
{
case TOKEN_DOCTYPE:
printf("Token type: DOCTYPE.\n");
printf("Doctype name: %s\n", tk->dt.doctype_name);
printf("Public identifier: %s\n", tk->dt.doctype_public_identifier);
printf("System identifier: %s\n", tk->dt.doctype_system_identifier);
html_print_force_quirks_flag(tk->dt.force_quirks_flag);
break;
case TOKEN_START_TAG:
printf("Token type: START TAG.\n");
printf("Start tag name: %s\n", tk->stt.tag_name);
html_print_self_closing_flag(tk->stt.self_closing_flag);
html_print_attribute_list(tk->stt.attributes);
break;
case TOKEN_END_TAG:
printf("Token type: END TAG.\n");
printf("End tag name: %s\n", tk->ett.tag_name);
break;
case TOKEN_COMMENT:
printf("Token type: COMMENT.\n");
printf("Comment: %s\n", tk->cmt.comment);
break;
case TOKEN_MULTI_CHAR:
printf("Token type: MULTI_CHAR.\n");
printf("First character: %c\n", *(tk->mcht.mch));
printf("Character count: %d\n", tk->mcht.char_count);
break;
case TOKEN_EOF:
printf("Token type: END OF FILE.\n");
break;
default:
break;
}
printf("\n");
}
/*-------------------------------------------------------------*/
void html_print_force_quirks_flag(force_quirks_flag_type flag)
{
if(flag == ON)
{
printf("Force quirks flag: on\n");
}
else if(flag == OFF)
{
printf("Force quirks flag: off\n");
}
else
{
printf("Invalid force quirks flag value.\n");
}
}
/*-------------------------------------------------------------*/
void html_print_attribute_list(attribute_list *attributes)
{
printf("Attributes: [");
while (attributes != NULL)
{
printf("%s=%s", attributes->name, attributes->value);
switch (attributes->attr_ns)
{
case HTML:
printf("(ns-html)");
break;
case MATHML:
printf("(ns-mathml)");
break;
case SVG:
printf("(ns-svg)");
break;
case XLINK:
printf("(ns-xlink)");
break;
case XML:
printf("(ns-xml)");
break;
case XMLNS:
printf("(ns-xmlns)");
break;
case DEFAULT:
printf("(ns-default)");
break;
default:
break;
}
if (attributes->tail != NULL)
{
printf(", ");
}
attributes = attributes->tail;
}
putchar(']');
printf("\n");
}
/*-------------------------------------------------------------*/
void html_print_self_closing_flag(self_closing_flag_type flag)
{
if(flag == SET)
{
printf("Self closing flag: set\n");
}
else if(flag == UNSET)
{
printf("Self closing flag: unset\n");
}
else
{
printf("Invalid self closing flag value.\n");
}
}
/*-------------------------------------------------------------*/
void html_print_token_list(token_list *tk_list)
{
while (tk_list != NULL)
{
html_print_token(tk_list->tk);
putchar('\n');
tk_list = tk_list->tail;
}
}