aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-07-27 16:40:31 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-07-27 16:40:31 +0530
commitc0282d19cc7a749b97925cc5d5237ad6b367b675 (patch)
tree7aacafe822c8344e360eeffd95cd33761816dddb /src/lexer.c
parenta363a4995f834e08b7f5f35a7db76d37bf67b935 (diff)
lexer: implement handle_for()
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c70
1 files changed, 68 insertions, 2 deletions
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;