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 /src | |
parent | b365f7cdd0882c1fe974e9e08d607341876a0d34 (diff) |
(lexer): parse_file->lex_file and must also add _RAW directives
Diffstat (limited to 'src')
-rw-r--r-- | src/lexer.c | 68 | ||||
-rw-r--r-- | src/template.c | 1 |
2 files changed, 39 insertions, 30 deletions
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; } |