diff options
-rw-r--r-- | compromyse.xyz/index.html | 3 | ||||
-rw-r--r-- | include/lexer.h | 5 | ||||
-rw-r--r-- | src/lexer.c | 70 |
3 files changed, 76 insertions, 2 deletions
diff --git a/compromyse.xyz/index.html b/compromyse.xyz/index.html index c60ce75..3dc4fc5 100644 --- a/compromyse.xyz/index.html +++ b/compromyse.xyz/index.html @@ -1,3 +1,6 @@ +{{ for post : posts }}KSFAISO + ASHIAUHSFI +HERE{{ endfor }} <div class="p-16"> <div class="flex flex-wrap gap-8"> diff --git a/include/lexer.h b/include/lexer.h index 448d302..b9dbf1c 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -26,6 +26,7 @@ typedef struct { void *operands; } directive_t; +/* TODO: strlen(content) instead of length? */ typedef struct { char *key; char *content; @@ -56,5 +57,9 @@ void lexer_handle_content(directive_t *directive, key_match_t *match, char *buffer, size_t n); +void lexer_handle_for(directive_t *directive, + key_match_t *match, + char *buffer, + size_t n); #endif diff --git a/src/lexer.c b/src/lexer.c index 4a4c195..a04f667 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -7,6 +7,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <util.h> list_t * lex(char *buffer) @@ -129,8 +130,6 @@ lexer_handle_contentfor(directive_t *directive, buffer = content + match->length + match->offset; - size_t content_length = 0; - key_match_t *new_match; directive_t *new_directive; @@ -172,6 +171,71 @@ lexer_handle_contentfor(directive_t *directive, } void +lexer_handle_for(directive_t *directive, + key_match_t *match, + char *buffer, + size_t n) +{ + directive->type = FOR; + for_operand_t *operands = malloc(sizeof(for_operand_t)); + + char *tempbuffer = strdup(buffer); + /* For free() */ + void *orig = tempbuffer; + + tempbuffer += n; + tempbuffer += strlen("for"); + operands->key = strdup(trim(strtok(tempbuffer, ":"))); + operands->source = strdup(trim(strtok(NULL, "}"))); + + free(orig); + + buffer += match->length; + + key_match_t *new_match; + directive_t *new_directive; + + while (true) { + new_match = find_next_key(buffer, 0); + if (new_match == NULL) { + printf("Cannot find endfor\n"); + free(new_directive); + free(new_match); + free(directive); + /* TODO: Handle early returns */ + return; + } + + new_directive = find_directive(buffer, new_match); + if (new_directive == NULL) { + printf("Cannot find directive: %.*s\n", + new_match->length, + buffer + new_match->offset); + free(new_directive); + free(new_match); + free(directive); + return; + } + + if (new_directive->type == ENDFOR) { + break; + } + } + + operands->content = strndup(buffer, new_match->offset); + + free(new_directive); + free(new_match); + + printf("KEY: %s\n", operands->key); + printf("SOURCE: %s\n", operands->source); + printf("CONTENT: %s\n", operands->content); + exit(1); + + directive->operands = operands; +} + +void lexer_handle_content(directive_t *directive, key_match_t *match, char *buffer, @@ -231,6 +295,8 @@ found_start: lexer_handle_contentfor(directive, match, buffer, content, n); } else if (DIRECTIVE_IS("content")) { lexer_handle_content(directive, match, buffer, n); + } else if (DIRECTIVE_IS("for")) { + lexer_handle_for(directive, match, buffer, n); } else { free(directive); return NULL; |