Skip to content

Commit ad83fed

Browse files
committed
Clean up preprocessor code
1 parent e42d17f commit ad83fed

File tree

10 files changed

+281
-76
lines changed

10 files changed

+281
-76
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "utils"]
22
path = utils
3-
url = https://github.com/EmbedJournal/c-utils.git
3+
url = https://github.com/GoToMain/c-utils.git

src/fluid.c

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,93 @@ void fluid_render_data(fluid_t *ctx)
8888

8989
void fluid_explode_sub_ctx(fluid_t *ctx, lexer_block_t *blk, fluid_t *sub_ctx)
9090
{
91-
list_insert_nodes(&ctx->lex_blocks, &blk->node, &sub_ctx->lex_blocks);
92-
list_remove_node(&ctx->lex_blocks, &blk->node);
93-
lexer_free_block(blk);
91+
list_insert_nodes(&ctx->lex_blocks, &blk->node,
92+
sub_ctx->lex_blocks.head, sub_ctx->lex_blocks.tail);
93+
94+
lexer_remove_block(ctx, blk);
9495

9596
/* empty sub_ctx blocks to prevent lexer_teardown() from free-ing them */
9697
sub_ctx->lex_blocks.head = NULL;
9798
sub_ctx->lex_blocks.tail = NULL;
9899
}
99100

100-
int fluid_handle_includes(fluid_t *ctx)
101+
int fluid_remove_blocks(fluid_t *ctx, lexer_block_t *start, lexer_block_t *end)
102+
{
103+
if (list_remove_nodes(&ctx->lex_blocks, &start->node, &end->node)) {
104+
printf("fluid: block remove failed\n");
105+
return -1;
106+
}
107+
108+
while (start != end) {
109+
lexer_remove_block(ctx, start);
110+
start = CONTAINER_OF(start->node.next, lexer_block_t, node);
111+
}
112+
lexer_remove_block(ctx, end);
113+
return 0;
114+
}
115+
116+
void fluid_handle_raw_blocks(fluid_t *ctx, lexer_block_t *start, lexer_block_t *end)
117+
{
118+
lexer_block_t *p;
119+
120+
p = CONTAINER_OF(start->node.next, lexer_block_t, node);
121+
lexer_remove_block(ctx, start);
122+
while (p != end) {
123+
lexer_block_cast_to_data(p);
124+
p = CONTAINER_OF(p->node.next, lexer_block_t, node);
125+
}
126+
lexer_remove_block(ctx, end);
127+
}
128+
129+
int fluid_preprocessor(fluid_t *ctx)
101130
{
102-
fluid_t *sub_ctx = NULL;
103-
lexer_block_t *blk;
104131
node_t *p;
132+
lexer_block_t *blk;
133+
fluid_t *sub_ctx;
134+
lexer_block_t *comment_start = NULL;
135+
lexer_block_t *raw_start = NULL;
105136

106137
p = ctx->lex_blocks.head;
107138
while (p) {
108139
blk = CONTAINER_OF(p, lexer_block_t, node);
109140
p = p->next;
141+
142+
/* strip comments */
143+
if (comment_start) {
144+
if (blk->type == LEXER_BLOCK_TAG &&
145+
blk->tok.tag.keyword == LIQ_KW_ENDCOMMENT) {
146+
if (fluid_remove_blocks(ctx, comment_start, blk))
147+
return -1;
148+
comment_start = NULL;
149+
}
150+
continue;
151+
}
152+
else if (blk->type == LEXER_BLOCK_TAG &&
153+
blk->tok.tag.keyword == LIQ_KW_COMMENT) {
154+
comment_start = blk;
155+
continue;
156+
}
157+
158+
/* force mark raw blocks as data blocks */
159+
if (raw_start) {
160+
if (blk->type == LEXER_BLOCK_TAG &&
161+
blk->tok.tag.keyword == LIQ_KW_ENDRAW) {
162+
fluid_handle_raw_blocks(ctx, raw_start, blk);
163+
raw_start = NULL;
164+
}
165+
continue;
166+
}
167+
else if (blk->type == LEXER_BLOCK_TAG &&
168+
blk->tok.tag.keyword == LIQ_KW_RAW) {
169+
raw_start = blk;
170+
continue;
171+
}
172+
173+
/* include files */
110174
if (blk->type == LEXER_BLOCK_TAG &&
111-
liquid_get_kw(blk->tok.tag.keyword) == LIQ_KW_INCLUDE &&
112-
blk->tok.tag.tokens && blk->tok.tag.tokens[0]) {
175+
blk->tok.tag.keyword == LIQ_KW_INCLUDE &&
176+
blk->tok.tag.tokens && blk->tok.tag.tokens[0])
177+
{
113178
sub_ctx = fluid_load(ctx->dirname, blk->tok.tag.tokens[0]);
114179
if (sub_ctx == NULL) {
115180
return -1;
@@ -118,14 +183,21 @@ int fluid_handle_includes(fluid_t *ctx)
118183
if (lexer_lex(sub_ctx) != 0) {
119184
return -1;
120185
}
121-
if (fluid_handle_includes(sub_ctx)) {
186+
if (fluid_preprocessor(sub_ctx)) {
122187
return -1;
123188
}
124189
fluid_explode_sub_ctx(ctx, blk, sub_ctx);
125190
lexer_teardown(sub_ctx);
126191
fluid_destroy_context(sub_ctx);
127192
}
128193
}
194+
195+
/**
196+
* After preprocessing there may be consecutive data blocks
197+
* so call lexer_grabage_collect() to squash them.
198+
*/
199+
lexer_grabage_collect(ctx);
200+
129201
return 0;
130202
}
131203

@@ -148,7 +220,7 @@ int main(int argc, char *argv[])
148220
return -1;
149221
}
150222

151-
if (fluid_handle_includes(ctx)) {
223+
if (fluid_preprocessor(ctx)) {
152224
return -1;
153225
}
154226

@@ -160,7 +232,7 @@ int main(int argc, char *argv[])
160232
fluid_render_data(ctx);
161233

162234
lexer_teardown(ctx);
163-
parseer_teardown(ctx);
235+
parser_teardown(ctx);
164236
fluid_destroy_context(ctx);
165237
return 0;
166238
}

0 commit comments

Comments
 (0)