working on parser
[taskasaur.git] / parser.c
index 0f476b4..6f597f2 100644 (file)
--- a/parser.c
+++ b/parser.c
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
-typedef struct TodoItem {
-    char* name;
-    char* description;
-    char* due;
-    char** items;
-} TodoItem;
-
-void parse(FILE* file, int* length);
+#include "headers/parser.h"
+
+typedef struct State {
+    Board* board;
+    TodoList* cur_todolist;
+    TodoItem* cur_todoitem;
+    /* for use during processing */
+    char* last_block_text;
+} State;
+
+char* read_file(char* file_name, long* size);
+
+/* processing */
+void enter_todolist();
+void exit_todolist();
+
+void enter_todoitem();
+void exit_todoitem();
+
+/* callbacks to parser */
+int enter_block(MD_BLOCKTYPE type, void* detail, void* userdata);
+int leave_block(MD_BLOCKTYPE type, void* detail, void* userdata);
+int enter_span(MD_SPANTYPE type, void* detail, void* userdata);
+int leave_span(MD_SPANTYPE type, void* detail, void* userdata);
+int text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata);
+void debug_log(const char* msg, void* userdata);
+void syntax(void);
+
+const MD_PARSER parser = {
+    0,
+    MD_DIALECT_COMMONMARK,
+    &enter_block,
+    &leave_block,
+    &enter_span,
+    &leave_span,
+    &text,
+    &debug_log,
+    &syntax
+};
+
+char*
+read_file(char* file_name, long* size) 
+{ // apparently using seek isnt the greatest, may change to chunk reading later
+    FILE* file;
+    char* output;
+    long fsize;
+
+    file = fopen(file_name, "r");
+    if (file == NULL) {
+        printf("Something went wrong opening file\n");
+        return NULL;
+    }
 
+    fseek(file, 0, SEEK_END);
+    fsize = ftell(file);
+    fseek(file, 0, SEEK_SET);
+
+    output = malloc(fsize+1); // maybe error check malloc
+    fread(output, 1, fsize, file);
+    if (ferror(file)) {
+        printf("Something went wrong reading file\n");
+        fclose(file);
+        free(output);
+        return NULL;
+    }
+    output[fsize] = 0; // add null terminator
+    
+    /* clean up */
+    fclose(file);
 
-static char task_md[] = "###";
+    /* return */
+    *size = fsize;
+    return output;
+}
 
-int
-main(int argc, char** argv)
+Board*
+begin_parse(char* board_path)
 {
-    FILE* input_file;
-    int todo_length;
+    const char* input_buffer;
+    long input_size;
+    State* state;
 
-    input_file = fopen("test_board.md", "r");     
-    if (!input_file) {
-        printf("Something went wrong opening file");
-        return 1;
-    }
+    /* read entire file */
+    input_buffer = read_file(board_path, &input_size);
+
+    /* setup state */
+    state = malloc(sizeof(State));
+    state->cur_todolist = NULL;
+    state->cur_todoitem = NULL;
+
+    state->board = malloc(sizeof(Board));
+    state->board->todolist_list = malloc(0);
+    state->board->todolist_count = 0;
+
+    md_parse(input_buffer, input_size, &parser, state);
 
-    parse(input_file, &todo_length);
+    free((char*)input_buffer);
 
-    return 0;    
+    return state->board;
 }
 
-/* TodoItem** */ 
-void
-parse(FILE* file, int* length) 
+int
+enter_block(MD_BLOCKTYPE type, void* detail, void* userdata)
 {
-    TodoItem** out_arr;
-    int out_len;
-    char* lineptr;
-    size_t len;
-    ssize_t nread;
-
-    out_arr = NULL;
-    out_len = 0;
-    lineptr = NULL;
-    len = 0;
+    return 0;
+}
 
-    while ((nread = getline(&lineptr, &len, file)) != -1) {
+int
+leave_block(MD_BLOCKTYPE type, void* detail, void* userdata)
+{
+    State* state;
+    state = (State*)userdata;
 
-        lineptr[strcspn(lineptr, "\n")] = 0; // remove newline
-        
-        if (strcmp(lineptr, "") == 0) {
-            printf("found empty line\n");
-            lineptr = NULL;
-            continue;
-        } 
+    switch (type) {
 
-        // found a task
-        if (strlen(lineptr) > 3 && strncmp(lineptr, task_md, 3) == 0) {
-            printf("found_task\n");
-            lineptr = NULL;
-            continue;
-        }
+        case MD_BLOCK_H:
 
+            switch(((MD_BLOCK_H_DETAIL*)detail)->level) {
 
-        
+                case 1:
+                    printf("leave h1, %s\n", state->last_block_text);
+                
+                case 2:
+                    printf("leave h2, %s\n", state->last_block_text);
+                    break;
+                case 3:
+                    printf("leave h3, %s\n", state->last_block_text);
+                    break;
 
-        /* out_arr = realloc(out_arr, (sizeof(char*))*out_len); // bad to keep resizing? */
+            }
 
-        /* out_arr[out_len-1] = lineptr; */
+            break;
 
-        lineptr = NULL;
+        // no need for default case for now :>
     }
+    return 0;
+}
+
+int
+enter_span(MD_SPANTYPE type, void* detail, void* userdata)
+{
+    return 0;
+}
+
+int
+leave_span(MD_SPANTYPE type, void* detail, void* userdata)
+{
+    return 0;
+}
+
+int
+text(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata){
+
+    State* state;
+    char* content;
+
+    state = (State*)userdata;
+
+    content = malloc(size*sizeof(char)+1);
+    memcpy(content, text, size);
+    content[size] = 0;
+
+    /* printf("%s\n", content); */
+    state->last_block_text = content;
     
-    *length = out_len;
-    /* return out_arr; */
+    return 0;
+}
+
+void
+debug_log(const char* msg, void* userdata)
+{
+    printf(msg);
+    return;
+}
+
+void 
+syntax(void)
+{
+    return;
 }