forked from jgm/peg-markdown
-
Notifications
You must be signed in to change notification settings - Fork 55
/
parsing_functions.c
222 lines (173 loc) · 5.18 KB
/
parsing_functions.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
/* parsing_functions.c - Functions for parsing markdown and
* freeing element lists. */
/* These yy_* functions come from markdown_parser.c which is
* generated from markdown_parser.leg
* */
typedef int (*yyrule)();
extern int yyparse();
extern int yyparsefrom(yyrule);
extern int yy_References();
extern int yy_Notes();
extern int yy_Doc();
extern int yy_AutoLabels();
extern int yy_DocWithMetaData();
extern int yy_MetaDataOnly();
extern int yy_DocForOPML();
#include "utility_functions.h"
#include "parsing_functions.h"
#include "markdown_peg.h"
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 METAKEY:
case METAVALUE:
case TABLESEPARATOR:
case ATTRKEY:
case GLOSSARY:
case GLOSSARYTERM:
case NOTELABEL:
case CELLSPAN:
case EMDASH:
case ENDASH:
case GLOSSARYSORTKEY:
case MATHSPAN:
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 */
/* if (parse_aborted) {
free_element_list(parse_result);
return NULL;
}*/
return parse_result;
}
element * parse_markdown_with_metadata(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;
start_time = clock();
yyparsefrom(yy_DocWithMetaData);
charbuf = oldcharbuf; /* restore charbuf to original value */
/* reset start_time for subsequent passes */
start_time = 0;
if (parse_aborted) {
parse_aborted = 0;
free_element_list(parse_result);
return NULL;
}
return parse_result;
}
element * parse_metadata_only(char *string, int extensions) {
char *oldcharbuf;
syntax_extensions = extensions;
oldcharbuf = charbuf;
charbuf = string;
yyparsefrom(yy_MetaDataOnly);
charbuf = oldcharbuf; /* restore charbuf to original value */
return parse_result;
}
element * parse_markdown_for_opml(char *string, int extensions) {
char *oldcharbuf;
syntax_extensions = extensions;
oldcharbuf = charbuf;
charbuf = string;
yyparsefrom(yy_DocForOPML);
charbuf = oldcharbuf; /* restore charbuf to original value */
return parse_result;
}