From f0e2fe43a5e5e22342a13139815556ae3b373d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 1 Oct 2019 23:03:11 +0200 Subject: Start moving to scheme structs. --- src/parse.c | 225 ++++++++++++++++++++++++++---------------------------------- 1 file changed, 96 insertions(+), 129 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 565e1d6c..e79231cb 100644 --- a/src/parse.c +++ b/src/parse.c @@ -9,6 +9,10 @@ #include "err.h" +#include +#include "struct.h" +#include "guile_type_helpers.h" + // #define TYPE vcomponent // #include "linked_list.inc.h" // #undef TYPE @@ -20,10 +24,33 @@ #undef T #undef V +/* + +-------------------------------------------------------+ + v | + BEGIN → key -------------------------------→ ':' → value → CRLF -+-→ EOF + | ^ + v | + ';' → param-key → ':' → param-value --+ + ^ | + +------------------------------------+ + + + vcomponent := map> + line := pair + attributes := map> + + + */ + +#define string_eq(a, b) scm_string_eq(a, b, SCM_BOOL_F,SCM_BOOL_F,SCM_BOOL_F,SCM_BOOL_F) + /* * name *(";" param) ":" value CRLF */ int parse_file(char* filename, FILE* f, vcomponent* root) { + scm_c_use_module ("(vcomponent struct)"); + + part_context p_ctx = p_key; SNEW(parse_ctx, ctx, f, filename); @@ -36,9 +63,18 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * {cline,param}_key is also temporary register used during * parsing. */ - SNEW(content_line, cline); - SNEW(strbuf, cline_key); - SNEW(strbuf, param_key); + // SNEW(content_line, cline); + // SNEW(strbuf, param_key); + // SNEW(strbuf, param_val); + // SNEW(strbuf, attr_key); + // SNEW(strbuf, attr_val); + + SNEW(strbuf, str); + SCM component; /* TODO init to root */ + SCM line = scm_make_vline(); + SCM attr_key; /* string */ + SCM line_key; /* string */ + SCM param_set; /* hashtable */ char c; while ( (c = fgetc(f)) != EOF) { @@ -48,21 +84,48 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (fold(&ctx, c) > 0) { /* Actuall end of line, handle value */ - TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); - handle_kv(&cline_key, &cline, &ctx); + // TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); + /* + * The key being BEGIN means that we decend into a new component. + */ + if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { + /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ + SCM child = scm_make_vcomponent(scm_from_strbuf(&str)); + scm_add_child_x (component, child); + component = child; + + } else if (string_eq(line_key, scm_from_utf8_string("END"))) { + // TODO make current component be parent of current component? + component = scm_component_parent(component); + + /* + * A regular key, value pair. Push it into to the current + * component. + */ + } else { + scm_set_value_x(line, scm_from_strbuf(&str)); + scm_add_line_x(component, line_key, line); + line = scm_make_vline(); + } + + strbuf_soft_reset (&str); p_ctx = p_key; } /* Else continue on current line */ /* We have an escaped character */ } else if (c == '\\') { - handle_escape (&ctx); + char esc = handle_escape (&ctx); + strbuf_append(&str, esc); /* Border between param {key, value} */ } else if (p_ctx == p_param_name && c == '=') { /* Save the current parameter key */ - TRANSFER (¶m_key, &ctx.str); + // TODO + // TRANSFER (¶m_key, &ctx.str); + attr_key = scm_from_strbuf(&str); p_ctx = p_param_value; + strbuf_soft_reset (&str); /* * One of four cases: @@ -77,15 +140,8 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * the current parameter set. */ if (p_ctx == p_param_value) { /* save current parameter value. */ - - NEW(strbuf, s); - TRANSFER(s, &ctx.str); - - NEW(param_set, ps); - PUSH(param_set)(ps, s); - - PUSH(TRIE(param_set))(CLINE_CUR_PARAMS(&cline), param_key.mem, ps); - strbuf_soft_reset (¶m_key); + scm_add_attribute_x(line, line_key, scm_from_strbuf(&str)); + strbuf_soft_reset (&str); } /* @@ -96,10 +152,12 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { */ if (p_ctx == p_key) { - TRANSFER(&cline_key, &ctx.str); + // TRANSFER(&cline_key, &ctx.str); - NEW(content_set, p); - PUSH(LLIST(content_set))(&cline, p); + // NEW(content_set, p); + // PUSH(LLIST(content_set))(&cline, p); + attr_key = scm_from_strbuf(&str); + strbuf_soft_reset (&str); } if (c == ':') p_ctx = p_value; @@ -110,7 +168,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * the current string. */ } else { - strbuf_append(&ctx.str, c); + strbuf_append(&str, c); ++ctx.column; ++ctx.pcolumn; @@ -121,21 +179,24 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { ERR("Error parsing"); } /* Check to see if empty line */ - else if (ctx.str.ptr != 0) { + else if (str.ptr != 0) { /* * The standard (3.4, l. 2675) says that each icalobject must * end with CRLF. My files however does not, so we also parse * the end here. */ - TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); - handle_kv(&cline_key, &cline, &ctx); + // TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); + // TODO + // handle_kv(&cline_key, &cline, &ctx); } - FREE(content_line)(&cline); - FREE(strbuf)(&cline_key); - FREE(strbuf)(¶m_key); + // FREE(content_line)(&cline); + // FREE(strbuf)(&cline_key); + // FREE(strbuf)(¶m_key); + + FREE(strbuf)(&str); assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); @@ -146,102 +207,6 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { return 0; } -/* - * We have a complete key value pair. - */ -int handle_kv ( - strbuf* key, - content_line* cline, - parse_ctx* ctx - ) { - - /* - * The key being BEGIN means that we decend into a new component. - */ - if (strbuf_c(key, "BEGIN")) { - /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - - /* - * Take a copy of the name of the entered component, and store - * it on the stack of component names. - */ - NEW(strbuf, s); - DEEP_COPY(strbuf)(s, CLINE_CUR_VAL(cline)); - PUSH(LLIST(strbuf))(&ctx->key_stack, s); - - /* Clear the value list in the parse content_line */ - RESET(LLIST(content_set))(cline); - - /* - * Create the new curent component, link it with the current - * component in a parent/child relationship. - * Finally push the new component on to the top of the - * component stack. - */ - NEW(vcomponent, e, - s->mem, - ctx->filename); - vcomponent* parent = PEEK(LLIST(vcomponent))(&ctx->comp_stack); - PUSH(vcomponent)(parent, e); - - PUSH(LLIST(vcomponent))(&ctx->comp_stack, e); - - /* - * The end of a component, go back along the stack to the previous - * component. - */ - } else if (strbuf_c(key, "END")) { - strbuf* expected_key = POP(LLIST(strbuf))(&ctx->key_stack); - - if (strbuf_cmp(expected_key, CLINE_CUR_VAL(cline)) != 0) { - - ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line", - expected_key->mem, - CLINE_CUR_VAL(cline)->mem, - vcomponent_get_val( - PEEK(LLIST(vcomponent))(&ctx->comp_stack), - "X-HNH-FILENAME")); - PUSH(LLIST(strbuf))(&ctx->key_stack, expected_key); - - return -1; - - } else { - FFREE(strbuf, expected_key); - POP(LLIST(vcomponent))(&ctx->comp_stack); - } - - /* - * A regular key, value pair. Push it into to the current - * component. - */ - } else { - - /* - * cline is the value store used during parsing, meaning that - * its values WILL mutate at a later point. Therefore we take - * a copy of it here. - */ - NEW(content_line, c); - DEEP_COPY(content_line)(c, cline); - - /* - * The PUSH(TRIE(T)) method handles collisions by calling - * RESOLVE(T). content_line resolves by merging the new value - * into the old value, and freeing the new value's container. - * - * This means that |c| declared above might be destroyed - * here. - */ - PUSH(TRIE(content_line))( - &PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines, - key->mem, c); - - RESET(LLIST(content_set))(cline); - } - - return 0; -} - int fold(parse_ctx* ctx, char c) { int retval; @@ -289,7 +254,7 @@ INIT_F(parse_ctx, FILE* f, char* filename) { self->pline = 1; self->pcolumn = 1; - INIT(strbuf, &self->str); + // INIT(strbuf, &self->str); return 0; } @@ -302,12 +267,12 @@ FREE_F(parse_ctx) { self->line = 0; self->column = 0; - FREE(strbuf)(&self->str); + // FREE(strbuf)(&self->str); return 0; } -int handle_escape (parse_ctx* ctx) { +char handle_escape (parse_ctx* ctx) { char esc = fgetc(ctx->f); /* @@ -340,11 +305,13 @@ int handle_escape (parse_ctx* ctx) { ERR_P(ctx, "Non escapable character '%c' (%i)", esc, esc); } - /* save escapade character as a normal character */ - strbuf_append(&ctx->str, esc); - ++ctx->column; ++ctx->pcolumn; - return 0; + return esc; + + /* save escapade character as a normal character */ + // strbuf_append(&ctx->str, esc); + + // return 0; } -- cgit v1.2.3 From 7539f8c8804849294e100c5442e0397f4f4d2c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 1 Oct 2019 23:39:00 +0200 Subject: Disabled bunch of old stuff, new stuff kinda builds. --- src/parse.c | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index e79231cb..46f9644e 100644 --- a/src/parse.c +++ b/src/parse.c @@ -5,7 +5,7 @@ #include #include "macro.h" -#include "vcal.h" +// #include "vcal.h" #include "err.h" @@ -17,12 +17,12 @@ // #include "linked_list.inc.h" // #undef TYPE -#define T strbuf -#define V strbuf -#include "pair.h" -#include "pair.inc.h" -#undef T -#undef V +// #define T strbuf +// #define V strbuf +// #include "pair.h" +// #include "pair.inc.h" +// #undef T +// #undef V /* +-------------------------------------------------------+ @@ -47,14 +47,12 @@ /* * name *(";" param) ":" value CRLF */ -int parse_file(char* filename, FILE* f, vcomponent* root) { - scm_c_use_module ("(vcomponent struct)"); - +int parse_file(char* filename, FILE* f, SCM root) { part_context p_ctx = p_key; SNEW(parse_ctx, ctx, f, filename); - PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); + // PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); /* * Create a content_line which we use as storage while we are @@ -70,11 +68,10 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { // SNEW(strbuf, attr_val); SNEW(strbuf, str); - SCM component; /* TODO init to root */ + SCM component = root; SCM line = scm_make_vline(); SCM attr_key; /* string */ - SCM line_key; /* string */ - SCM param_set; /* hashtable */ + SCM line_key = scm_from_utf8_string(""); /* string */ char c; while ( (c = fgetc(f)) != EOF) { @@ -103,7 +100,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * component. */ } else { - scm_set_value_x(line, scm_from_strbuf(&str)); + scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); scm_add_line_x(component, line_key, line); line = scm_make_vline(); } @@ -140,7 +137,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { * the current parameter set. */ if (p_ctx == p_param_value) { /* save current parameter value. */ - scm_add_attribute_x(line, line_key, scm_from_strbuf(&str)); + scm_add_attribute_x(line, attr_key, scm_from_strbuf(&str)); strbuf_soft_reset (&str); } @@ -198,9 +195,9 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { FREE(strbuf)(&str); - assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); - assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); - assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack)); + // assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); + // assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); + // assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack)); FREE(parse_ctx)(&ctx); @@ -242,8 +239,8 @@ int fold(parse_ctx* ctx, char c) { INIT_F(parse_ctx, FILE* f, char* filename) { - INIT(LLIST(strbuf), &self->key_stack); - INIT(LLIST(vcomponent), &self->comp_stack); + // INIT(LLIST(strbuf), &self->key_stack); + // INIT(LLIST(vcomponent), &self->comp_stack); self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); strcpy(self->filename, filename); self->f = f; @@ -261,8 +258,8 @@ INIT_F(parse_ctx, FILE* f, char* filename) { FREE_F(parse_ctx) { - FREE(LLIST(strbuf))(&self->key_stack); - FREE(LLIST(vcomponent))(&self->comp_stack); + // FREE(LLIST(strbuf))(&self->key_stack); + // FREE(LLIST(vcomponent))(&self->comp_stack); free(self->filename); self->line = 0; -- cgit v1.2.3 From 1c3bd94c328df0c8b4293bc42a25b2d7c851fd0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Wed, 2 Oct 2019 23:05:01 +0200 Subject: Made parser work again (for single files). --- src/parse.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 46f9644e..0e243234 100644 --- a/src/parse.c +++ b/src/parse.c @@ -42,7 +42,7 @@ */ -#define string_eq(a, b) scm_string_eq(a, b, SCM_BOOL_F,SCM_BOOL_F,SCM_BOOL_F,SCM_BOOL_F) +#define string_eq(a, b) scm_is_true(scm_string_eq(a, b, SCM_UNDEFINED,SCM_UNDEFINED,SCM_UNDEFINED,SCM_UNDEFINED)) /* * name *(";" param) ":" value CRLF @@ -73,11 +73,15 @@ int parse_file(char* filename, FILE* f, SCM root) { SCM attr_key; /* string */ SCM line_key = scm_from_utf8_string(""); /* string */ + INFO("Starting parsing"); char c; + INFO("here"); while ( (c = fgetc(f)) != EOF) { + INFO_F("LOOP %c", c); /* We have a linebreak */ if (c == '\r' || c == '\n') { + INFO("EOL"); if (fold(&ctx, c) > 0) { /* Actuall end of line, handle value */ @@ -87,12 +91,14 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - SCM child = scm_make_vcomponent(scm_from_strbuf(&str)); + INFO("Creating child"); + SCM child = scm_make_vcomponent(scm_string_to_symbol(scm_from_strbuf(&str))); scm_add_child_x (component, child); component = child; } else if (string_eq(line_key, scm_from_utf8_string("END"))) { // TODO make current component be parent of current component? + INFO("back to parent"); component = scm_component_parent(component); /* @@ -100,6 +106,7 @@ int parse_file(char* filename, FILE* f, SCM root) { * component. */ } else { + INFO("Adding attribute"); scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); scm_add_line_x(component, line_key, line); line = scm_make_vline(); @@ -120,6 +127,7 @@ int parse_file(char* filename, FILE* f, SCM root) { /* Save the current parameter key */ // TODO // TRANSFER (¶m_key, &ctx.str); + INFO("Param key"); attr_key = scm_from_strbuf(&str); p_ctx = p_param_value; strbuf_soft_reset (&str); @@ -136,6 +144,7 @@ int parse_file(char* filename, FILE* f, SCM root) { /* We got a parameter value, push the current string to * the current parameter set. */ if (p_ctx == p_param_value) { + INFO("param value"); /* save current parameter value. */ scm_add_attribute_x(line, attr_key, scm_from_strbuf(&str)); strbuf_soft_reset (&str); @@ -149,11 +158,13 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (p_ctx == p_key) { + INFO("key"); // TRANSFER(&cline_key, &ctx.str); // NEW(content_set, p); // PUSH(LLIST(content_set))(&cline, p); - attr_key = scm_from_strbuf(&str); + // attr_key + line_key = scm_from_strbuf(&str); strbuf_soft_reset (&str); } @@ -173,7 +184,7 @@ int parse_file(char* filename, FILE* f, SCM root) { } if (! feof(f)) { - ERR("Error parsing"); + ERR_F("Error parsing errno = %i", errno); } /* Check to see if empty line */ else if (str.ptr != 0) { -- cgit v1.2.3 From e940c6e74114830fb41c061035b5a160e0c3b6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Wed, 2 Oct 2019 23:11:56 +0200 Subject: Restore directory parsing. --- src/parse.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 0e243234..dd8066ed 100644 --- a/src/parse.c +++ b/src/parse.c @@ -73,15 +73,15 @@ int parse_file(char* filename, FILE* f, SCM root) { SCM attr_key; /* string */ SCM line_key = scm_from_utf8_string(""); /* string */ - INFO("Starting parsing"); + INFO_F("Parsing [%s]", filename); + char c; - INFO("here"); while ( (c = fgetc(f)) != EOF) { - INFO_F("LOOP %c", c); + // INFO_F("LOOP %c", c); /* We have a linebreak */ if (c == '\r' || c == '\n') { - INFO("EOL"); + // INFO("EOL"); if (fold(&ctx, c) > 0) { /* Actuall end of line, handle value */ @@ -91,14 +91,14 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - INFO("Creating child"); + // INFO("Creating child"); SCM child = scm_make_vcomponent(scm_string_to_symbol(scm_from_strbuf(&str))); scm_add_child_x (component, child); component = child; } else if (string_eq(line_key, scm_from_utf8_string("END"))) { // TODO make current component be parent of current component? - INFO("back to parent"); + // INFO("back to parent"); component = scm_component_parent(component); /* @@ -106,7 +106,7 @@ int parse_file(char* filename, FILE* f, SCM root) { * component. */ } else { - INFO("Adding attribute"); + // INFO("Adding attribute"); scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); scm_add_line_x(component, line_key, line); line = scm_make_vline(); @@ -127,7 +127,7 @@ int parse_file(char* filename, FILE* f, SCM root) { /* Save the current parameter key */ // TODO // TRANSFER (¶m_key, &ctx.str); - INFO("Param key"); + // INFO("Param key"); attr_key = scm_from_strbuf(&str); p_ctx = p_param_value; strbuf_soft_reset (&str); @@ -144,7 +144,7 @@ int parse_file(char* filename, FILE* f, SCM root) { /* We got a parameter value, push the current string to * the current parameter set. */ if (p_ctx == p_param_value) { - INFO("param value"); + // INFO("param value"); /* save current parameter value. */ scm_add_attribute_x(line, attr_key, scm_from_strbuf(&str)); strbuf_soft_reset (&str); @@ -158,7 +158,7 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (p_ctx == p_key) { - INFO("key"); + // INFO("key"); // TRANSFER(&cline_key, &ctx.str); // NEW(content_set, p); -- cgit v1.2.3 From e13f6bb201dff690208b9cc951b5c098b0d63356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 3 Oct 2019 00:46:01 +0200 Subject: Slowly going through and fixing everything. --- src/parse.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index dd8066ed..06d8707c 100644 --- a/src/parse.c +++ b/src/parse.c @@ -91,14 +91,14 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - // INFO("Creating child"); + INFO("Creating child"); SCM child = scm_make_vcomponent(scm_string_to_symbol(scm_from_strbuf(&str))); scm_add_child_x (component, child); component = child; } else if (string_eq(line_key, scm_from_utf8_string("END"))) { // TODO make current component be parent of current component? - // INFO("back to parent"); + INFO("back to parent"); component = scm_component_parent(component); /* @@ -106,7 +106,8 @@ int parse_file(char* filename, FILE* f, SCM root) { * component. */ } else { - // INFO("Adding attribute"); + strbuf_cap(&str); // TODO remove + INFO_F("Adding attribute [%s]", str.mem); scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); scm_add_line_x(component, line_key, line); line = scm_make_vline(); @@ -127,7 +128,7 @@ int parse_file(char* filename, FILE* f, SCM root) { /* Save the current parameter key */ // TODO // TRANSFER (¶m_key, &ctx.str); - // INFO("Param key"); + INFO_F("Param key [%s]", str.mem); attr_key = scm_from_strbuf(&str); p_ctx = p_param_value; strbuf_soft_reset (&str); @@ -144,7 +145,7 @@ int parse_file(char* filename, FILE* f, SCM root) { /* We got a parameter value, push the current string to * the current parameter set. */ if (p_ctx == p_param_value) { - // INFO("param value"); + INFO_F("param value [%s]", str.mem); /* save current parameter value. */ scm_add_attribute_x(line, attr_key, scm_from_strbuf(&str)); strbuf_soft_reset (&str); @@ -158,7 +159,8 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (p_ctx == p_key) { - // INFO("key"); + strbuf_cap(&str); // TODO remove + INFO_F("key [%s]", str.mem); // TRANSFER(&cline_key, &ctx.str); // NEW(content_set, p); @@ -193,6 +195,7 @@ int parse_file(char* filename, FILE* f, SCM root) { * end with CRLF. My files however does not, so we also parse * the end here. */ + ERR("Not implemented"); // TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); // TODO -- cgit v1.2.3 From 785f70a3d16e549e36b8ef17f081829fe492a193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 3 Oct 2019 22:02:03 +0200 Subject: Locate bug with DTEND. --- src/parse.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 06d8707c..48b58b95 100644 --- a/src/parse.c +++ b/src/parse.c @@ -94,6 +94,16 @@ int parse_file(char* filename, FILE* f, SCM root) { INFO("Creating child"); SCM child = scm_make_vcomponent(scm_string_to_symbol(scm_from_strbuf(&str))); scm_add_child_x (component, child); + + /* TODO it should be possible to create this object once + at the top of this function + */ + SCM templine = scm_make_vline(); + scm_struct_set_x(templine, vline_value, + scm_from_utf8_stringn(filename, strlen(filename))); + scm_add_line_x(child, scm_from_utf8_string("X-HNH-FILENAME"), + templine); + component = child; } else if (string_eq(line_key, scm_from_utf8_string("END"))) { @@ -195,7 +205,7 @@ int parse_file(char* filename, FILE* f, SCM root) { * end with CRLF. My files however does not, so we also parse * the end here. */ - ERR("Not implemented"); + ERR("Handling of missing trailing endline not reimplemented."); // TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); // TODO -- cgit v1.2.3 From 60d51e5700a55bc3ae17e34f9f3da1d4653a3026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 3 Oct 2019 23:56:59 +0200 Subject: Everything seems to parse now. --- src/parse.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 48b58b95..8c370958 100644 --- a/src/parse.c +++ b/src/parse.c @@ -69,7 +69,7 @@ int parse_file(char* filename, FILE* f, SCM root) { SNEW(strbuf, str); SCM component = root; - SCM line = scm_make_vline(); + SCM line = scm_make_vline(SCM_UNDEFINED); SCM attr_key; /* string */ SCM line_key = scm_from_utf8_string(""); /* string */ @@ -98,9 +98,8 @@ int parse_file(char* filename, FILE* f, SCM root) { /* TODO it should be possible to create this object once at the top of this function */ - SCM templine = scm_make_vline(); - scm_struct_set_x(templine, vline_value, - scm_from_utf8_stringn(filename, strlen(filename))); + SCM templine = + scm_make_vline(scm_from_utf8_stringn(filename, strlen(filename))); scm_add_line_x(child, scm_from_utf8_string("X-HNH-FILENAME"), templine); @@ -120,7 +119,7 @@ int parse_file(char* filename, FILE* f, SCM root) { INFO_F("Adding attribute [%s]", str.mem); scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); scm_add_line_x(component, line_key, line); - line = scm_make_vline(); + line = scm_make_vline(SCM_UNDEFINED); } strbuf_soft_reset (&str); -- cgit v1.2.3 From 3521ad64ef664f8303fa93ac237212b97dd0f69c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 4 Oct 2019 00:01:27 +0200 Subject: Remove debug prints.. --- src/parse.c | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 8c370958..996c0a92 100644 --- a/src/parse.c +++ b/src/parse.c @@ -73,16 +73,10 @@ int parse_file(char* filename, FILE* f, SCM root) { SCM attr_key; /* string */ SCM line_key = scm_from_utf8_string(""); /* string */ - INFO_F("Parsing [%s]", filename); - char c; while ( (c = fgetc(f)) != EOF) { - // INFO_F("LOOP %c", c); - /* We have a linebreak */ if (c == '\r' || c == '\n') { - // INFO("EOL"); - if (fold(&ctx, c) > 0) { /* Actuall end of line, handle value */ // TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); @@ -91,7 +85,6 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - INFO("Creating child"); SCM child = scm_make_vcomponent(scm_string_to_symbol(scm_from_strbuf(&str))); scm_add_child_x (component, child); @@ -107,7 +100,6 @@ int parse_file(char* filename, FILE* f, SCM root) { } else if (string_eq(line_key, scm_from_utf8_string("END"))) { // TODO make current component be parent of current component? - INFO("back to parent"); component = scm_component_parent(component); /* @@ -115,8 +107,6 @@ int parse_file(char* filename, FILE* f, SCM root) { * component. */ } else { - strbuf_cap(&str); // TODO remove - INFO_F("Adding attribute [%s]", str.mem); scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); scm_add_line_x(component, line_key, line); line = scm_make_vline(SCM_UNDEFINED); @@ -135,9 +125,6 @@ int parse_file(char* filename, FILE* f, SCM root) { } else if (p_ctx == p_param_name && c == '=') { /* Save the current parameter key */ - // TODO - // TRANSFER (¶m_key, &ctx.str); - INFO_F("Param key [%s]", str.mem); attr_key = scm_from_strbuf(&str); p_ctx = p_param_value; strbuf_soft_reset (&str); @@ -154,7 +141,6 @@ int parse_file(char* filename, FILE* f, SCM root) { /* We got a parameter value, push the current string to * the current parameter set. */ if (p_ctx == p_param_value) { - INFO_F("param value [%s]", str.mem); /* save current parameter value. */ scm_add_attribute_x(line, attr_key, scm_from_strbuf(&str)); strbuf_soft_reset (&str); @@ -168,8 +154,6 @@ int parse_file(char* filename, FILE* f, SCM root) { */ if (p_ctx == p_key) { - strbuf_cap(&str); // TODO remove - INFO_F("key [%s]", str.mem); // TRANSFER(&cline_key, &ctx.str); // NEW(content_set, p); -- cgit v1.2.3 From 6d5a32fc3bf707bafc19e239dd60371bece90fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 4 Oct 2019 00:05:46 +0200 Subject: General cleanup in parse. --- src/parse.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 996c0a92..fbfb2387 100644 --- a/src/parse.c +++ b/src/parse.c @@ -73,6 +73,9 @@ int parse_file(char* filename, FILE* f, SCM root) { SCM attr_key; /* string */ SCM line_key = scm_from_utf8_string(""); /* string */ + SCM scm_filename = scm_from_utf8_stringn(filename, strlen(filename)); + SCM filename_key = scm_from_utf8_string("X-HNH-FILENAME"); + char c; while ( (c = fgetc(f)) != EOF) { /* We have a linebreak */ @@ -91,15 +94,11 @@ int parse_file(char* filename, FILE* f, SCM root) { /* TODO it should be possible to create this object once at the top of this function */ - SCM templine = - scm_make_vline(scm_from_utf8_stringn(filename, strlen(filename))); - scm_add_line_x(child, scm_from_utf8_string("X-HNH-FILENAME"), - templine); + scm_add_line_x(child, filename_key, scm_make_vline(scm_filename)); component = child; } else if (string_eq(line_key, scm_from_utf8_string("END"))) { - // TODO make current component be parent of current component? component = scm_component_parent(component); /* @@ -153,12 +152,6 @@ int parse_file(char* filename, FILE* f, SCM root) { * parameters. */ if (p_ctx == p_key) { - - // TRANSFER(&cline_key, &ctx.str); - - // NEW(content_set, p); - // PUSH(LLIST(content_set))(&cline, p); - // attr_key line_key = scm_from_strbuf(&str); strbuf_soft_reset (&str); } -- cgit v1.2.3 From 2782987949ffbd8b7ac92cd92a3ab65c78865cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 4 Oct 2019 00:18:40 +0200 Subject: Comments in parser. --- src/parse.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index fbfb2387..81312967 100644 --- a/src/parse.c +++ b/src/parse.c @@ -180,12 +180,12 @@ int parse_file(char* filename, FILE* f, SCM root) { * The standard (3.4, l. 2675) says that each icalobject must * end with CRLF. My files however does not, so we also parse * the end here. + * + * Actually we don't any more. + * Since the last thing in a file should always be END:VCALENDAR + * it might be a good idea to verify that. Or we could just, you + * know, not. */ - ERR("Handling of missing trailing endline not reimplemented."); - - // TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); - // TODO - // handle_kv(&cline_key, &cline, &ctx); } -- cgit v1.2.3 From 77791305d6e1483fa5ae46f26616242c00f99989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 4 Oct 2019 21:02:17 +0200 Subject: HTML output seems to work in full now. --- src/parse.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 81312967..3a5907c8 100644 --- a/src/parse.c +++ b/src/parse.c @@ -91,9 +91,6 @@ int parse_file(char* filename, FILE* f, SCM root) { SCM child = scm_make_vcomponent(scm_string_to_symbol(scm_from_strbuf(&str))); scm_add_child_x (component, child); - /* TODO it should be possible to create this object once - at the top of this function - */ scm_add_line_x(child, filename_key, scm_make_vline(scm_filename)); component = child; -- cgit v1.2.3 From ab964d17145114eda93cda35f69a4b1e1779e242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 6 Oct 2019 13:41:00 +0200 Subject: Cleanup in C code. --- src/parse.c | 53 +++-------------------------------------------------- 1 file changed, 3 insertions(+), 50 deletions(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 3a5907c8..586a43b4 100644 --- a/src/parse.c +++ b/src/parse.c @@ -5,7 +5,6 @@ #include #include "macro.h" -// #include "vcal.h" #include "err.h" @@ -13,17 +12,6 @@ #include "struct.h" #include "guile_type_helpers.h" -// #define TYPE vcomponent -// #include "linked_list.inc.h" -// #undef TYPE - -// #define T strbuf -// #define V strbuf -// #include "pair.h" -// #include "pair.inc.h" -// #undef T -// #undef V - /* +-------------------------------------------------------+ v | @@ -42,7 +30,6 @@ */ -#define string_eq(a, b) scm_is_true(scm_string_eq(a, b, SCM_UNDEFINED,SCM_UNDEFINED,SCM_UNDEFINED,SCM_UNDEFINED)) /* * name *(";" param) ":" value CRLF @@ -52,26 +39,12 @@ int parse_file(char* filename, FILE* f, SCM root) { part_context p_ctx = p_key; SNEW(parse_ctx, ctx, f, filename); - // PUSH(LLIST(vcomponent))(&ctx.comp_stack, root); - - /* - * Create a content_line which we use as storage while we are - * parsing. This object is constantly broken down and rebuilt. - * - * {cline,param}_key is also temporary register used during - * parsing. - */ - // SNEW(content_line, cline); - // SNEW(strbuf, param_key); - // SNEW(strbuf, param_val); - // SNEW(strbuf, attr_key); - // SNEW(strbuf, attr_val); SNEW(strbuf, str); SCM component = root; SCM line = scm_make_vline(SCM_UNDEFINED); SCM attr_key; /* string */ - SCM line_key = scm_from_utf8_string(""); /* string */ + SCM line_key = scm_from_utf8_string(""); SCM scm_filename = scm_from_utf8_stringn(filename, strlen(filename)); SCM filename_key = scm_from_utf8_string("X-HNH-FILENAME"); @@ -82,13 +55,12 @@ int parse_file(char* filename, FILE* f, SCM root) { if (c == '\r' || c == '\n') { if (fold(&ctx, c) > 0) { /* Actuall end of line, handle value */ - // TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); /* * The key being BEGIN means that we decend into a new component. */ if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - SCM child = scm_make_vcomponent(scm_string_to_symbol(scm_from_strbuf(&str))); + SCM child = scm_make_vcomponent(scm_from_strbuf_symbol(&str)); scm_add_child_x (component, child); scm_add_line_x(child, filename_key, scm_make_vline(scm_filename)); @@ -186,16 +158,8 @@ int parse_file(char* filename, FILE* f, SCM root) { } - // FREE(content_line)(&cline); - // FREE(strbuf)(&cline_key); - // FREE(strbuf)(¶m_key); - FREE(strbuf)(&str); - // assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); - // assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); - // assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack)); - FREE(parse_ctx)(&ctx); return 0; @@ -236,8 +200,6 @@ int fold(parse_ctx* ctx, char c) { INIT_F(parse_ctx, FILE* f, char* filename) { - // INIT(LLIST(strbuf), &self->key_stack); - // INIT(LLIST(vcomponent), &self->comp_stack); self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); strcpy(self->filename, filename); self->f = f; @@ -248,20 +210,15 @@ INIT_F(parse_ctx, FILE* f, char* filename) { self->pline = 1; self->pcolumn = 1; - // INIT(strbuf, &self->str); - return 0; } FREE_F(parse_ctx) { - // FREE(LLIST(strbuf))(&self->key_stack); - // FREE(LLIST(vcomponent))(&self->comp_stack); free(self->filename); self->line = 0; self->column = 0; - // FREE(strbuf)(&self->str); return 0; } @@ -302,10 +259,6 @@ char handle_escape (parse_ctx* ctx) { ++ctx->column; ++ctx->pcolumn; + /* Returns the escaped char, for appending to the current string */ return esc; - - /* save escapade character as a normal character */ - // strbuf_append(&ctx->str, esc); - - // return 0; } -- cgit v1.2.3 From 04b31c9b820e6756043a87027458c0b8d0546d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 1 Nov 2019 21:10:06 +0100 Subject: Start port of parse to scheme. --- src/parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c index 586a43b4..3edbd874 100644 --- a/src/parse.c +++ b/src/parse.c @@ -18,7 +18,7 @@ BEGIN → key -------------------------------→ ':' → value → CRLF -+-→ EOF | ^ v | - ';' → param-key → ':' → param-value --+ + ';' → param-key → '=' → param-value --+ ^ | +------------------------------------+ -- cgit v1.2.3 From 3a1d3898c3d42d43645b79586f0b26ab4f8ff331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sat, 2 Nov 2019 22:29:47 +0100 Subject: Remove ALL c code. --- src/parse.c | 264 ------------------------------------------------------------ 1 file changed, 264 deletions(-) delete mode 100644 src/parse.c (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c deleted file mode 100644 index 3edbd874..00000000 --- a/src/parse.c +++ /dev/null @@ -1,264 +0,0 @@ -#include "parse.h" - -#include -#include -#include - -#include "macro.h" - -#include "err.h" - -#include -#include "struct.h" -#include "guile_type_helpers.h" - -/* - +-------------------------------------------------------+ - v | - BEGIN → key -------------------------------→ ':' → value → CRLF -+-→ EOF - | ^ - v | - ';' → param-key → '=' → param-value --+ - ^ | - +------------------------------------+ - - - vcomponent := map> - line := pair - attributes := map> - - - */ - - -/* - * name *(";" param) ":" value CRLF - */ -int parse_file(char* filename, FILE* f, SCM root) { - - part_context p_ctx = p_key; - - SNEW(parse_ctx, ctx, f, filename); - - SNEW(strbuf, str); - SCM component = root; - SCM line = scm_make_vline(SCM_UNDEFINED); - SCM attr_key; /* string */ - SCM line_key = scm_from_utf8_string(""); - - SCM scm_filename = scm_from_utf8_stringn(filename, strlen(filename)); - SCM filename_key = scm_from_utf8_string("X-HNH-FILENAME"); - - char c; - while ( (c = fgetc(f)) != EOF) { - /* We have a linebreak */ - if (c == '\r' || c == '\n') { - if (fold(&ctx, c) > 0) { - /* Actuall end of line, handle value */ - /* - * The key being BEGIN means that we decend into a new component. - */ - if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { - /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - SCM child = scm_make_vcomponent(scm_from_strbuf_symbol(&str)); - scm_add_child_x (component, child); - - scm_add_line_x(child, filename_key, scm_make_vline(scm_filename)); - - component = child; - - } else if (string_eq(line_key, scm_from_utf8_string("END"))) { - component = scm_component_parent(component); - - /* - * A regular key, value pair. Push it into to the current - * component. - */ - } else { - scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); - scm_add_line_x(component, line_key, line); - line = scm_make_vline(SCM_UNDEFINED); - } - - strbuf_soft_reset (&str); - p_ctx = p_key; - } /* Else continue on current line */ - - /* We have an escaped character */ - } else if (c == '\\') { - char esc = handle_escape (&ctx); - strbuf_append(&str, esc); - - /* Border between param {key, value} */ - } else if (p_ctx == p_param_name && c == '=') { - - /* Save the current parameter key */ - attr_key = scm_from_strbuf(&str); - p_ctx = p_param_value; - strbuf_soft_reset (&str); - - /* - * One of four cases: - * 1) end of key , start of value - * 2) ,, key , ,, param - * 3) ,, param, ,, param - * 4) ,, param, ,, value - */ - } else if ((p_ctx == p_key || p_ctx == p_param_value) && (c == ':' || c == ';')) { - - /* We got a parameter value, push the current string to - * the current parameter set. */ - if (p_ctx == p_param_value) { - /* save current parameter value. */ - scm_add_attribute_x(line, attr_key, scm_from_strbuf(&str)); - strbuf_soft_reset (&str); - } - - /* - * Top level key. - * Copy the key into the current cline, and create a - * content_set for the upcomming value and (possible) - * parameters. - */ - if (p_ctx == p_key) { - line_key = scm_from_strbuf(&str); - strbuf_soft_reset (&str); - } - - if (c == ':') p_ctx = p_value; - else if (c == ';') p_ctx = p_param_name; - - /* - * Nothing interesting happened, append the read character to - * the current string. - */ - } else { - strbuf_append(&str, c); - - ++ctx.column; - ++ctx.pcolumn; - } - } - - if (! feof(f)) { - ERR_F("Error parsing errno = %i", errno); - } - /* Check to see if empty line */ - else if (str.ptr != 0) { - /* - * The standard (3.4, l. 2675) says that each icalobject must - * end with CRLF. My files however does not, so we also parse - * the end here. - * - * Actually we don't any more. - * Since the last thing in a file should always be END:VCALENDAR - * it might be a good idea to verify that. Or we could just, you - * know, not. - */ - - } - - FREE(strbuf)(&str); - - FREE(parse_ctx)(&ctx); - - return 0; -} - -int fold(parse_ctx* ctx, char c) { - int retval; - - char buf[2] = { - (c == '\n' ? '\n' : (char) fgetc(ctx->f)), - (char) fgetc(ctx->f) - }; - - ctx->pcolumn = 1; - - if (buf[0] != '\n') { - ERR_P(ctx, "expected new_line after CR"); - retval = -1; - - } else if (buf[1] == ' ' || buf[1] == '\t') { - retval = 0; - ctx->pcolumn++; - - } else if (ungetc(buf[1], ctx->f) != buf[1]) { - ERR_P(ctx, "Failed to put character back on FILE"); - retval = -2; - - } else { - retval = 1; - ++ctx->line; - ctx->column = 0; - } - - ++ctx->pline; - - return retval; -} - - -INIT_F(parse_ctx, FILE* f, char* filename) { - self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); - strcpy(self->filename, filename); - self->f = f; - - self->line = 0; - self->column = 0; - - self->pline = 1; - self->pcolumn = 1; - - return 0; -} - -FREE_F(parse_ctx) { - - free(self->filename); - - self->line = 0; - self->column = 0; - - return 0; -} - -char handle_escape (parse_ctx* ctx) { - char esc = fgetc(ctx->f); - - /* - * Escape character '\' and escaped token sepparated by a newline - * (since the standard for some reason allows that (!!!)) - * We are at least guaranteed that it's a folded line, so just - * unfold it and continue trying to find a token to escape. - */ - if (esc == '\r' || esc == '\n') { - int ret; - if ( (ret = fold(ctx, esc)) != 0) { - if (ret == 1) ERR_P(ctx, "ESC before not folded line"); - else ERR_P(ctx, "other error: val = %i", ret); - exit (2); - } else { - esc = fgetc(ctx->f); - } - } - - /* Escaped new_line */ - if (esc == 'n' || esc == 'N') { - esc = '\n'; - - /* "Standard" escaped character */ - } else if (esc == ';' || esc == ',' || esc == '\\') { - /* esc already contains character, do nothing */ - - /* Invalid escaped character */ - } else { - ERR_P(ctx, "Non escapable character '%c' (%i)", esc, esc); - } - - ++ctx->column; - ++ctx->pcolumn; - - /* Returns the escaped char, for appending to the current string */ - return esc; -} -- cgit v1.2.3