diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-09 21:02:39 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-09 21:02:39 +0530 |
commit | bcddd5c79306fcd7949c47f64d08400205463beb (patch) | |
tree | 82ec93d6c81e077705082487405b9db1d160d5ac | |
parent | b365f7cdd0882c1fe974e9e08d607341876a0d34 (diff) |
(lexer): parse_file->lex_file and must also add _RAW directives
-rw-r--r-- | include/lexer.h | 13 | ||||
-rw-r--r-- | src/lexer.c | 68 | ||||
-rw-r--r-- | src/template.c | 1 |
3 files changed, 50 insertions, 32 deletions
diff --git a/include/lexer.h b/include/lexer.h index 17a2825..ce761dd 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -1,7 +1,16 @@ #ifndef __LEXER_H #define __LEXER_H -typedef enum { INCLUDE, CONTENT, CONTENTFOR, ENDCONTENT, BODY } directive_e; +#include <list.h> + +typedef enum { + _RAW, + INCLUDE, + CONTENT, + CONTENTFOR, + ENDCONTENT, + BODY +} directive_e; typedef struct { unsigned int offset; @@ -19,7 +28,7 @@ typedef struct { unsigned int length; } contentfor_operand_t; -list_t *parse_file(char *content); +list_t *lex_file(char *buffer); directive_t *find_directive(char *content, key_match_t *match); key_match_t *find_next_key(char *buffer); diff --git a/src/lexer.c b/src/lexer.c index 3237296..ad6d038 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -8,49 +8,57 @@ #include <stdlib.h> #include <string.h> -static list_t * -find_all_keys(char *buffer) +list_t * +lex_file(char *buffer) { - list_t *keys = list_create(sizeof(key_match_t)); - key_match_t *match; - size_t match_offset = 0; + list_t *directives = list_create(sizeof(directive_t)); size_t current_offset = 0; while (true) { - match = find_next_key(buffer); - if (match == NULL) + key_match_t *key = find_next_key(buffer); + if (key == NULL) + break; + + directive_t *directive = find_directive(buffer, key); + /* TODO: Handle unknown directive */ + if (directive == NULL) break; - match_offset = match->offset; + current_offset += key->length + key->offset; - buffer += match->offset + match->length; - match->offset += current_offset; - current_offset += match->length + match_offset; + if (current_offset != 0) { + char *raw_content; + asprintf(&raw_content, "%.*s", (int) key->offset, buffer); - list_add(keys, match); + directive_t *raw_directive = malloc(sizeof(directive_t)); + raw_directive->type = _RAW; + raw_directive->operands = raw_content; + list_add(directives, raw_directive); + } + + buffer += key->offset + key->length; + + list_add(directives, directive); } - return keys; -} + if (strlen(buffer) > 0) { + char *raw_content; + asprintf(&raw_content, "%s", buffer); -list_t * -parse_file(char *content) -{ - list_t *keys = find_all_keys(content); - - for (size_t i = 0; i < keys->size; i++) { - key_match_t *match = list_get(keys, i); -#ifdef DEBUG - printf("%lu: (%u) %.*s\n", - i, - match->length, - match->length, - content + match->offset); -#endif + directive_t *raw_directive = malloc(sizeof(directive_t)); + raw_directive->type = _RAW; + raw_directive->operands = raw_content; + list_add(directives, raw_directive); } - list_delete(keys); - return NULL; + for (size_t i = 0; i < directives->size; i++) { + directive_t *match = list_get(directives, i); + if (match->type == _RAW) { + printf("%lu: %s\n", i, (char *) match->operands); + } + } + + return directives; } key_match_t * diff --git a/src/template.c b/src/template.c index 7a0b64b..7038551 100644 --- a/src/template.c +++ b/src/template.c @@ -45,6 +45,7 @@ template_create(void) case INCLUDE: case CONTENTFOR: case ENDCONTENT: + case _RAW: break; } |