Browse Source

Write new contexts and interfaces

Andrew Young 8 years ago
parent
commit
c10786c46d
8 changed files with 288 additions and 15 deletions
  1. 29 14
      src/doc_elt.h
  2. 1 1
      src/doc_elt_ops.h
  3. 60 0
      src/merge_ctxt.c
  4. 38 0
      src/merge_ctxt.h
  5. 46 0
      src/parse_ctxt.c
  6. 31 0
      src/parse_ctxt.h
  7. 45 0
      src/print_ctxt.c
  8. 38 0
      src/print_ctxt.h

+ 29 - 14
src/doc_elt.h

@@ -11,13 +11,20 @@
 #include "doc_elt_ops.h"
 #include "doc_stream.h"
 
+#include "merge_ctxt.h"
+#include "print_ctxt.h"
+#include "parse_ctxt.h"
+
 /* search merger type, from smerger.h */
 typedef struct smerger smerger;
 
 enum doc_type {
+  NO_TYPE = 0,
   ORG_DOCUMENT,
   ORG_HEADING,
-  ORG_TEXT
+  ORG_TEXT,
+  ORG_PROPERTY,
+  ORG_DRAWER,
 };
 
 typedef struct doc_elt
@@ -26,11 +33,6 @@ typedef struct doc_elt
   size_t type;
 } doc_elt;
 
-typedef struct merge_ctxt
-{
-  smerger *global_smerger;
-} merge_ctxt;
-
 typedef struct doc_key
 {
   size_t length;
@@ -72,8 +74,8 @@ doc_elt_print (doc_ref* ref, print_ctxt *context, doc_stream *out);
 
 /**
  * @brief Compare two org_elements.
- * @self Compare this element. Uses this elements operations.
- * @other_element The element to compare with.
+ * @param self Compare this element. Uses this elements operations.
+ * @param other_element The element to compare with.
  *
  * two org_elements, returning TRUE if they match each other by some
  * distiguishing factor, false otherwise.  It is okay to compare two
@@ -81,32 +83,45 @@ doc_elt_print (doc_ref* ref, print_ctxt *context, doc_stream *out);
  * equal and doc_elt_compare will always return false.
  */
 static inline bool
-doc_elt_isrelated (doc_ref *ref_a, doc_ref *ref_b, void *context);
+doc_elt_isrelated (doc_ref *ref_a, doc_ref *ref_b, merge_ctxt *context);
 
+/**
+ * @brief Test if an element is updated.
+ * @param elt_a 
+ */
 static inline bool
 doc_elt_isupdated (doc_ref *elt_a);
 
 /**
  * @brief Compare two org_elements.
- * @self Compare this element. Uses this elements operations.
- * @other_element The element to compare with.
+ * @param self Compare this element. Uses this elements operations.
+ * @param other_element The element to compare with.
  */
 static inline int
 doc_elt_compare (doc_elt *elt_a, doc_src s1, doc_elt *elt_b, doc_src s2);
 
 /**
- * @brief Merge one element into the next
- * This function permanently changes the first element
+ * @brief Merge one element into the next.
+ * This function permanently changes the first element.
  */
 static void
 doc_elt_merge (doc_ref *ref_a, doc_ref *ref_b, merge_ctxt *ctxt);
 
+/**
+ * @brief Obtain the doc_key that uniquely identifies the element.
+ */
 static doc_key *
 doc_elt_get_key (doc_elt *elt);
 
+/**
+ * @brief Signal to the elements stored in REF that they are deleted.
+ */
 static void
 doc_elt_note_delete (doc_ref *ref, merge_ctxt *ctxt);
 
+/**
+ * @brief Signal to the elements stored in REF that they are inserted.
+ */
 static void
 doc_elt_note_insert (doc_ref *ref, merge_ctxt *ctxt);
 
@@ -146,7 +161,7 @@ doc_elt_print (doc_ref* ref, print_ctxt *context, doc_stream *out)
 }
 
 static inline bool
-doc_elt_isrelated (doc_ref *ref_a, doc_ref *ref_b, void *context)
+doc_elt_isrelated (doc_ref *ref_a, doc_ref *ref_b, merge_ctxt *context)
 {
   bool status = false;
   doc_elt *elt_a = doc_ref_get_elt (ref_a);

+ 1 - 1
src/doc_elt_ops.h

@@ -30,7 +30,7 @@ typedef void (doc_elt_ops_print) (doc_ref*, print_ctxt *, doc_stream *);
 
 /* comparing */
 typedef int (doc_elt_ops_compare) (doc_elt *, doc_src, doc_elt*, doc_src);
-typedef bool (doc_elt_ops_isrelated) (doc_ref *, doc_ref *, void*);
+typedef bool (doc_elt_ops_isrelated) (doc_ref *, doc_ref *, merge_ctxt *);
 
 /* merging */
 typedef void (doc_elt_ops_merge) (doc_ref *, doc_ref *, merge_ctxt *);

+ 60 - 0
src/merge_ctxt.c

@@ -0,0 +1,60 @@
+#include "stdlib.h"
+#include "string.h"
+#include "config.h"
+#include "gl_list.h"
+#include "gl_array_list.h"
+#include "smerger.h"
+#include "merge_ctxt.h"
+
+size_t default_priorities_count = 3;
+const char *default_priorities[] = { "A", "B", "C" };
+
+/**
+ * @brief Create the default set of accepted priorities.
+ */
+static gl_list_t merge_ctxt_create_default_priorities ();
+
+void
+merge_ctxt_init (merge_ctxt *ctxt)
+{
+  ctxt->global_smerger = NULL;
+  ctxt->priorities = NULL;
+  return;
+}
+
+void
+merge_ctxt_free (merge_ctxt *ctxt)
+{
+  smerger_free (ctxt->global_smerger);
+  gl_list_free (ctxt->priorities);
+  return;
+}
+
+void
+merge_ctxt_set_defaults (merge_ctxt *ctxt)
+{
+  if (ctxt->global_smerger == NULL)
+    {
+      ctxt->global_smerger = smerger_create ();
+    }
+  if (ctxt->priorities == NULL)
+    {
+      ctxt->priorities = merge_ctxt_create_default_priorities ();
+    }
+  return;
+}
+
+static gl_list_t
+merge_ctxt_create_default_priorities ()
+{
+  gl_list_t list =
+    gl_list_nx_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
+
+  int i = 0;
+  for (i = 0; i < default_priorities_count; i++)
+    {
+      gl_list_nx_add_last (list, default_priorities[i]);
+    }
+
+  return list;
+}

+ 38 - 0
src/merge_ctxt.h

@@ -0,0 +1,38 @@
+/**
+ * @file merge_ctxt.h
+ *
+ * Defines a context structure containing information used during the
+ * matching and merging process, along with all associated functions.
+ */
+
+#ifndef MERGE_CTXT_H
+#define MERGE_CTXT_H
+
+struct gl_list_impl;
+typedef struct gl_list_impl * gl_list_t;
+
+struct smerger;
+typedef struct smerger smerger;
+
+typedef struct merge_ctxt
+{
+  smerger *global_smerger;
+  gl_list_t priorities;
+} merge_ctxt;
+
+/**
+ * @brief Initialize a merge context.
+ */
+void merge_ctxt_init (merge_ctxt *ctxt);
+
+/**
+ * @brief Free a merge context and all it's data.
+ */
+void merge_ctxt_free (merge_ctxt *ctxt);
+
+/**
+ * @brief Set any unset values to their defaults.
+ */
+void merge_ctxt_set_defaults (merge_ctxt *ctxt);
+
+#endif

+ 46 - 0
src/parse_ctxt.c

@@ -0,0 +1,46 @@
+#include "stdlib.h"
+#include "string.h"
+#include "config.h"
+#include "debug.h"
+#include "gl_list.h"
+#include "gl_array_list.h"
+#include "parse_ctxt.h"
+
+
+static const size_t default_todo_states_size = 2;
+static const char* default_todo_states[] = {"TODO", "DONE"};
+
+gl_list_t parse_ctxt_create_default_todo_states ();
+
+void
+parse_ctxt_init (parse_ctxt *ctxt)
+{
+  ctxt->todo_states = NULL;
+  return;
+}
+
+void
+parse_ctxt_set_defaults (parse_ctxt *ctxt)
+{
+  if (ctxt->todo_states == NULL)
+    {
+      ctxt->todo_states = parse_ctxt_create_default_todo_states ();
+    }
+
+  return;
+}
+
+gl_list_t
+parse_ctxt_create_default_todo_states ()
+{
+  gl_list_t list =
+    gl_list_nx_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
+
+  int i = 0;
+  for (i = 0; i < default_todo_states_size; i++)
+    {
+      gl_list_nx_add_last (list, default_todo_states[i]);
+    }
+
+  return list;
+}

+ 31 - 0
src/parse_ctxt.h

@@ -0,0 +1,31 @@
+/**
+ * @file parse_ctxt.h
+ *
+ * Defines a context storing state while parsing, and all it's
+ * associated functions.  The parse context also stores configuration
+ * variables controlling the parse behaviour of the lexer, parser, and
+ * the doc_elts.
+ */
+
+#ifndef PARSE_CTXT
+#define PARSE_CTXT
+
+struct gl_list_impl;
+typedef struct gl_list_impl * gl_list_t;
+
+typedef struct parse_ctxt
+{
+  gl_list_t todo_states;
+} parse_ctxt;
+
+/**
+ * @brief Initialize a parse context.
+ */
+void parse_ctxt_init (parse_ctxt *ctxt);
+
+/**
+ * @brief Set any unset data to a default value.
+ */
+void parse_ctxt_set_defaults (parse_ctxt *ctxt);
+
+#endif

+ 45 - 0
src/print_ctxt.c

@@ -0,0 +1,45 @@
+#include "config.h"
+#include "debug.h"
+#include "print.h"
+#include "stdlib.h"
+#include "print_ctxt.h"
+
+/**
+ * @brief Set all unset values in CTXT to defaults.
+ */
+void print_ctxt_set_defaults (print_ctxt *ctxt);
+
+print_ctxt *
+print_ctxt_create_empty (void)
+{
+  print_ctxt *ctxt = calloc (1, sizeof (print_ctxt));
+  return ctxt;
+}
+
+void
+print_ctxt_init (print_ctxt *ctxt)
+{
+  ctxt->depth = 0;
+  ctxt->rmargin = 0;
+  ctxt->tab_width = 0;
+  ctxt->use_tabs = 0;
+  ctxt->print_state = print_merged;
+  ctxt->nested_conflicts = no_conflict;
+  ctxt->structure_conflict = no_conflict;
+  ctxt->content_conflict = no_conflict;
+  return;
+}
+
+void
+print_ctxt_set_defaults (print_ctxt *ctxt)
+{
+  if (ctxt->rmargin == 0)
+    {
+      ctxt->rmargin = 77;
+    }
+  if (ctxt->tab_width == 0)
+    {
+      ctxt->tab_width = 8;
+    }
+  return;
+}

+ 38 - 0
src/print_ctxt.h

@@ -0,0 +1,38 @@
+/**
+ * @file print_ctxt.h
+ *
+ * Defines a context used during element printing and all it's
+ * associated functions.
+ */
+
+#ifndef PRINT_CTXT_H
+#define PRINT_CTXT_H
+#include "print.h"
+#include "stdbool.h"
+
+/**
+ * @brief A context for printing doc_elt's in a tree.
+ */
+typedef struct print_ctxt
+{
+  int            depth;              /*< The current depth in a document tree */
+  size_t         rmargin;            /*< The column of the doc's right margin */
+  size_t         tab_width;          /*< The column width of a tab character  */
+  bool           use_tabs;           /*< Should generated output use tabs?    */
+  print_state    print_state;        /*< the current printing mode            */
+  bool           nested_conflicts;   /*< if there are nested conflicts        */
+  conflict_state structure_conflict; /*< the current state of conflicts       */
+  conflict_state content_conflict;   /*< the current state of conflicts       */
+} print_ctxt;
+
+/**
+ * @brief Create and initialize a print context.
+ */
+print_ctxt* print_ctxt_create_empty (void);
+
+/**
+ * @brief Initialize a print_ctxt.
+ */
+void print_ctxt_init (print_ctxt *ctxt);
+
+#endif