work on better matrix error reporting
[pspp] / src / libpspp / message.c
index d71df204ea46a5dd4774246e11b1f2ef25a6e361..526e19f3a016619dbd948c1f0c111db22bc3140e 100644 (file)
@@ -134,6 +134,35 @@ msg_location_destroy (struct msg_location *loc)
     }
 }
 
+static int
+msg_point_compare_3way (const struct msg_point *a, const struct msg_point *b)
+{
+  return (!a->line ? 1
+          : !b->line ? -1
+          : a->line > b->line ? 1
+          : a->line < b->line ? -1
+          : !a->column ? 1
+          : !b->column ? -1
+          : a->column > b->column ? 1
+          : a->column < b->column ? -1
+          : 0);
+}
+
+void
+msg_location_merge (struct msg_location **dstp, const struct msg_location *src)
+{
+  if (dst->file_name != src->file_name)
+    {
+      /* Failure. */
+      return;
+    }
+
+  if (msg_point_compare_3way (&dst->p[0], &src->p[0]) > 0)
+    dst->p[0] = src->p[0];
+  if (msg_point_compare_3way (&dst->p[1], &src->p[1]) < 0)
+    dst->p[1] = src->p[1];
+}
+
 struct msg_location *
 msg_location_dup (const struct msg_location *src)
 {
@@ -143,10 +172,8 @@ msg_location_dup (const struct msg_location *src)
   struct msg_location *dst = xmalloc (sizeof *dst);
   *dst = (struct msg_location) {
     .file_name = intern_new_if_nonnull (src->file_name),
-    .first_line = src->first_line,
-    .last_line = src->last_line,
-    .first_column = src->first_column,
-    .last_column = src->last_column,
+    .p[0] = src->p[0],
+    .p[1] = src->p[1],
   };
   return dst;
 }
@@ -155,8 +182,8 @@ bool
 msg_location_is_empty (const struct msg_location *loc)
 {
   return !loc || (!loc->file_name
-                  && loc->first_line <= 0
-                  && loc->first_column <= 0);
+                  && loc->p[0].line <= 0
+                  && loc->p[0].column <= 0);
 }
 
 void
@@ -168,10 +195,10 @@ msg_location_format (const struct msg_location *loc, struct string *s)
   if (loc->file_name)
     ds_put_cstr (s, loc->file_name);
 
-  int l1 = loc->first_line;
-  int l2 = MAX (loc->first_line, loc->last_line - 1);
-  int c1 = loc->first_column;
-  int c2 = MAX (loc->first_column, loc->last_column - 1);
+  int l1 = loc->p[0].line;
+  int l2 = MAX (l1, loc->p[1].line);
+  int c1 = loc->p[0].column;
+  int c2 = MAX (c1, loc->p[1].column);
 
   if (l1 > 0)
     {