forked from fletcher/peg-multimarkdown
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsing_functions.h
132 lines (109 loc) · 3.22 KB
/
parsing_functions.h
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
/* parsing_functions.h - Functions for parsing markdown and
* freeing element lists. */
int yyparse(void);
static void free_element_contents(element elt);
/* free_element_list - free list of elements recursively */
void free_element_list(element * elt) {
element * next = NULL;
while (elt != NULL) {
next = elt->next;
free_element_contents(*elt);
if (elt->children != NULL) {
free_element_list(elt->children);
elt->children = NULL;
}
free(elt);
elt = next;
}
}
/* free_element_contents - free element contents depending on type */
static void free_element_contents(element elt) {
switch (elt.key) {
case STR:
case SPACE:
case RAW:
case HTMLBLOCK:
case HTML:
case VERBATIM:
case CODE:
case NOTE:
case AUTOLABEL:
case CITATION:
case TERM:
case TABLESEPARATOR:
case ATTRKEY:
case GLOSSARY:
case GLOSSARYTERM:
case NOTELABEL:
free(elt.contents.str);
elt.contents.str = NULL;
break;
case LINK:
case IMAGE:
case REFERENCE:
free(elt.contents.link->url);
elt.contents.link->url = NULL;
free(elt.contents.link->title);
elt.contents.link->title = NULL;
free_element_list(elt.contents.link->label);
free(elt.contents.link->identifier);
elt.contents.link->identifier = NULL;
/* free_element_list(elt.contents.link->attr);*/
free(elt.contents.link);
elt.contents.link = NULL;
break;
default:
;
}
}
/* free_element - free element and contents */
void free_element(element *elt) {
free_element_contents(*elt);
free(elt);
}
element * parse_references(char *string, int extensions) {
char *oldcharbuf;
syntax_extensions = extensions;
oldcharbuf = charbuf;
charbuf = string;
yyparsefrom(yy_References); /* first pass, just to collect references */
charbuf = oldcharbuf;
return references;
}
element * parse_notes(char *string, int extensions, element *reference_list) {
char *oldcharbuf;
notes = NULL;
syntax_extensions = extensions;
if (extension(EXT_NOTES)) {
references = reference_list;
oldcharbuf = charbuf;
charbuf = string;
yyparsefrom(yy_Notes); /* second pass for notes */
charbuf = oldcharbuf;
}
return notes;
}
element * parse_labels(char *string, int extensions, element *reference_list, element *note_list) {
char *oldcharbuf;
syntax_extensions = extensions;
references = reference_list;
notes = note_list;
labels = NULL;
oldcharbuf = charbuf;
charbuf = string;
yyparsefrom(yy_AutoLabels); /* third pass, to collect labels */
charbuf = oldcharbuf;
return labels;
}
element * parse_markdown(char *string, int extensions, element *reference_list, element *note_list, element *label_list) {
char *oldcharbuf;
syntax_extensions = extensions;
references = reference_list;
notes = note_list;
labels = label_list;
oldcharbuf = charbuf;
charbuf = string;
yyparsefrom(yy_Doc);
charbuf = oldcharbuf; /* restore charbuf to original value */
return parse_result;
}