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). --- Makefile | 2 +- module/vcomponent.scm | 4 ++-- module/vcomponent/primitive.scm | 2 +- src/parse.c | 19 +++++++++++++++---- src/struct.h | 4 ++-- src/struct.scm.c | 40 +++++++++++++++++++++++++--------------- 6 files changed, 46 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index c8e2fd6f..6f25d5c5 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ lib/%.so: $(O_FILES) @mkdir -p lib $(CC) -shared -o $@ $^ $(LDFLAGS) -obj/%.scm.go: %.scm $(SO_FILES) +obj/%.scm.go: %.scm # $(SO_FILES) @mkdir -p obj guild compile $(GUILE_C_FLAGS) -o $@ $< diff --git a/module/vcomponent.scm b/module/vcomponent.scm index fc360486..a106d993 100644 --- a/module/vcomponent.scm +++ b/module/vcomponent.scm @@ -1,5 +1,5 @@ (define-module (vcomponent) - #:use-module ((vcomponent primitive) :select (parse-path make-vcomponent)) + #:use-module ((vcomponent primitive) :select (parse-cal-path make-vcomponent)) #:use-module (vcomponent datetime) #:use-module (vcomponent recurrence) #:use-module (vcomponent timezone) @@ -82,7 +82,7 @@ (define* (make-vcomponent #:optional path) (if (not path) (make-vcomponent) - (let* ((root (parse-path path)) + (let* ((root (parse-cal-path path)) (component (case (string->symbol (or (attr root "X-HNH-SOURCETYPE") "no-type")) ;; == Single ICS file == diff --git a/module/vcomponent/primitive.scm b/module/vcomponent/primitive.scm index e103feae..2cf12508 100644 --- a/module/vcomponent/primitive.scm +++ b/module/vcomponent/primitive.scm @@ -17,7 +17,7 @@ ; %vcomponent-shallow-copy) - (make-vcomponent add-line! add-child! make-vline add-attribute! parse-path) + (make-vcomponent add-line! add-child! make-vline add-attribute! parse-cal-path) ) (load-extension "libguile-calendar" "init_lib") 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) { diff --git a/src/struct.h b/src/struct.h index 838d8180..d39cf471 100644 --- a/src/struct.h +++ b/src/struct.h @@ -8,8 +8,8 @@ #define vcomponent_parent scm_from_uint8(2) #define vcomponent_lines scm_from_uint8(3) -inline SCM scm_component_parent(SCM component) { - return scm_struct_ref (component, vcomponent_parent); } +#define scm_component_parent(component) \ + scm_struct_ref (component, vcomponent_parent) #define vline_value scm_from_uint8(0) #define vline_attributes scm_from_uint8(1) diff --git a/src/struct.scm.c b/src/struct.scm.c index abbbba18..b5aa09c1 100644 --- a/src/struct.scm.c +++ b/src/struct.scm.c @@ -2,6 +2,8 @@ #include +#include "parse.h" + SCM vcomponent_vtable; SCM vline_vtable; @@ -10,27 +12,35 @@ SCM_DEFINE(scm_make_vcomponent, "make-vcomponent", 0, 1, 0, "") { - if (SCM_UNBNDP (type)) type = SCM_BOOL_F; - - if (scm_is_false(type)) type = scm_from_utf8_symbol("VIRTUAL"); + if (SCM_UNBNDP (type)) + type = scm_from_utf8_symbol("VIRTUAL"); - return scm_c_make_struct (vcomponent_vtable, scm_from_int(0), - type, SCM_EOL, SCM_BOOL_F, - scm_make_hash_table(SCM_BOOL_F), - SCM_UNDEFINED); + /* This segfaults */ + return scm_make_struct_no_tail + (vcomponent_vtable, + scm_list_4(type, SCM_EOL, SCM_BOOL_F, + scm_make_hash_table(scm_from_int(0x10)))); } -SCM_DEFINE(scm_parse_cal_path, "parse-path", 1, 0, 0, +SCM_DEFINE(scm_parse_cal_path, "parse-cal-path", 1, 0, 0, (SCM path), "") { - SCM root = scm_make_vcomponent(SCM_BOOL_F); + SCM root = scm_make_vcomponent(SCM_UNSPECIFIED); char* p = scm_to_utf8_stringn(path, NULL); - scm_read_vcalendar(root, p); + // scm_read_vcalendar(root, p); + /* TODO check that path is good? */ + printf("Parsing [%s]\n", p); + FILE* f = fopen(p, "r"); + printf("FILE = %p\n", f); + parse_file (p, f, root); + /* TODO free file */ free(p); + + return root; } SCM_DEFINE(scm_add_line_x, "add-line!", 3, 0, 0, @@ -57,9 +67,9 @@ SCM_DEFINE(scm_add_child_x, "add-child!", 2, 0, 0, SCM_DEFINE(scm_make_vline, "make-vline", 0, 0, 0, (), "") { - return scm_c_make_struct (vline_vtable, scm_from_int(0), - SCM_BOOL_F, scm_make_hash_table(SCM_BOOL_F), - SCM_UNDEFINED); + return scm_make_struct_no_tail + (vline_vtable, + scm_list_2(SCM_BOOL_F, scm_make_hash_table(scm_from_int(0x10)))); } @@ -77,10 +87,10 @@ void init_lib (void) { // init_vcomponent_type(); // content_set_lists = scm_make_weak_key_hash_table (scm_from_uint(0x100)); SCM str = scm_from_utf8_string("pr" "pw" "pw" "pr"); - SCM vcomponent_vtable = scm_make_vtable(str, SCM_BOOL_F); + vcomponent_vtable = scm_make_vtable(str, SCM_BOOL_F); scm_set_struct_vtable_name_x (vcomponent_vtable, scm_from_utf8_symbol("vcomponent")); - SCM vline_vtable = + vline_vtable = scm_make_vtable(scm_from_utf8_string("pw" "pw"), SCM_BOOL_F); scm_set_struct_vtable_name_x (vline_vtable, scm_from_utf8_symbol("vline")); -- cgit v1.2.3