message: Introduce underlining for error message regions.
[pspp] / tests / output / pivot-table-test.c
index 41ff52445cc66753e7b2f8248fcbfabc4a388574..7aef458d2479fa4f44451f9b3af67e3fd69357d0 100644 (file)
 #include "libpspp/i18n.h"
 #include "libpspp/string-map.h"
 #include "output/driver.h"
-#include "output/message-item.h"
 #include "output/options.h"
+#include "output/output-item.h"
 #include "output/pivot-table.h"
-#include "output/table-item.h"
 
 #include "gl/error.h"
 #include "gl/progname.h"
@@ -51,13 +50,10 @@ static char *box;
 /* -o, --output: Base name for output files. */
 static const char *output_base = "render";
 
-/* --dump: Print table dump to stdout? */
-static bool dump;
-
 static const char *parse_options (int argc, char **argv);
 static void usage (void) NO_RETURN;
-static struct pivot_table *read_table (struct lexer *);
-static void output_msg (const struct msg *, void *);
+static void read_table (struct lexer *);
+static void output_msg (const struct msg *, struct lexer *);
 
 int
 main (int argc, char **argv)
@@ -72,13 +68,13 @@ main (int argc, char **argv)
   settings_init ();
 
   struct lex_reader *reader = lex_reader_for_file (input_file_name, NULL,
-                                                   LEX_SYNTAX_AUTO,
+                                                   SEG_MODE_AUTO,
                                                    LEX_ERROR_CONTINUE);
   if (!reader)
     exit (1);
 
   struct lexer *lexer = lex_create ();
-  msg_set_handler (output_msg, lexer);
+  lex_set_message_handler (lexer, output_msg);
   lex_include (lexer, reader);
   lex_get (lexer);
 
@@ -89,10 +85,7 @@ main (int argc, char **argv)
       if (lex_match (lexer, T_STOP))
         break;
 
-      struct pivot_table *pt = read_table (lexer);
-      if (dump)
-        pivot_table_dump (pt, 0);
-      pivot_table_submit (pt);
+      read_table (lexer);
     }
 
   lex_destroy (lexer);
@@ -135,7 +128,6 @@ configure_drivers (int width, int length UNUSED, int min_break)
   register_driver (&options, "-");
 
 
-#ifdef HAVE_CAIRO
   /* Render to <base>.pdf. */
   string_map_insert (&options, "top-margin", "0");
   string_map_insert (&options, "bottom-margin", "0");
@@ -144,9 +136,13 @@ configure_drivers (int width, int length UNUSED, int min_break)
   string_map_insert (&options, "paper-size", "99x99in");
   string_map_insert (&options, "trim", "true");
   register_driver (&options, "%s.pdf", output_base);
-#endif
 
+  string_map_insert (&options, "box", "unicode");
   register_driver (&options, "%s.txt", output_base);
+
+  string_map_insert (&options, "box", "ascii");
+  register_driver (&options, "%s-ascii.txt", output_base);
+
   register_driver (&options, "%s.csv", output_base);
   register_driver (&options, "%s.odt", output_base);
   register_driver (&options, "%s.spv", output_base);
@@ -172,7 +168,6 @@ parse_options (int argc, char **argv)
         OPT_EMPHASIS,
         OPT_BOX,
         OPT_TABLE_LOOK,
-        OPT_DUMP,
         OPT_HELP
       };
       static const struct option options[] =
@@ -184,7 +179,6 @@ parse_options (int argc, char **argv)
           {"box", required_argument, NULL, OPT_BOX},
           {"output", required_argument, NULL, 'o'},
           {"table-look", required_argument, NULL, OPT_TABLE_LOOK},
-          {"dump", no_argument, NULL, OPT_DUMP},
           {"help", no_argument, NULL, OPT_HELP},
           {NULL, 0, NULL, 0},
         };
@@ -230,10 +224,6 @@ parse_options (int argc, char **argv)
           }
           break;
 
-        case OPT_DUMP:
-          dump = true;
-          break;
-
         case OPT_HELP:
           usage ();
 
@@ -608,16 +598,16 @@ read_value_option (struct lexer *lexer, const struct pivot_table *pt,
   if (lex_match_id (lexer, "SUBSCRIPTS"))
     {
       lex_match (lexer, T_EQUALS);
-      size_t allocated_subscripts = value->n_subscripts;
+
+      struct pivot_value_ex *ex = pivot_value_ex_rw (value);
+      size_t allocated_subscripts = ex->n_subscripts;
       while (lex_token (lexer) == T_STRING)
         {
-          if (value->n_subscripts >= allocated_subscripts)
-            value->subscripts = x2nrealloc (value->subscripts,
-                                            &allocated_subscripts,
-                                            sizeof *value->subscripts);
+          if (ex->n_subscripts >= allocated_subscripts)
+            ex->subscripts = x2nrealloc (ex->subscripts, &allocated_subscripts,
+                                         sizeof *ex->subscripts);
 
-          value->subscripts[value->n_subscripts++] = xstrdup (
-            lex_tokcstr (lexer));
+          ex->subscripts[ex->n_subscripts++] = xstrdup (lex_tokcstr (lexer));
           lex_get (lexer);
         }
       return;
@@ -627,12 +617,13 @@ read_value_option (struct lexer *lexer, const struct pivot_table *pt,
     {
       lex_match (lexer, T_EQUALS);
 
-      if (!value->font_style)
+      struct pivot_value_ex *ex = pivot_value_ex_rw (value);
+      if (!ex->font_style)
         {
-          value->font_style = xmalloc (sizeof *value->font_style);
-          font_style_copy (NULL, value->font_style, &base_style->font_style);
+          ex->font_style = xmalloc (sizeof *ex->font_style);
+          font_style_copy (NULL, ex->font_style, &base_style->font_style);
         }
-      read_font_style (lexer, value->font_style);
+      read_font_style (lexer, ex->font_style);
       return;
     }
 
@@ -640,12 +631,13 @@ read_value_option (struct lexer *lexer, const struct pivot_table *pt,
     {
       lex_match (lexer, T_EQUALS);
 
-      if (!value->cell_style)
+      struct pivot_value_ex *ex = pivot_value_ex_rw (value);
+      if (!ex->cell_style)
         {
-          value->cell_style = xmalloc (sizeof *value->cell_style);
-          *value->cell_style = base_style->cell_style;
+          ex->cell_style = xmalloc (sizeof *ex->cell_style);
+          *ex->cell_style = base_style->cell_style;
         }
-      read_cell_style (lexer, value->cell_style);
+      read_cell_style (lexer, ex->cell_style);
       return;
     }
 
@@ -687,8 +679,10 @@ read_value (struct lexer *lexer, const struct pivot_table *pt,
     {
       value = xmalloc (sizeof *value);
       *value = (struct pivot_value) {
-        .type = PIVOT_VALUE_STRING,
-        .string = { .s = xstrdup (lex_tokcstr (lexer)) },
+        .string = {
+          .type = PIVOT_VALUE_STRING,
+          .s = xstrdup (lex_tokcstr (lexer))
+        },
       };
       lex_get (lexer);
     }
@@ -696,8 +690,10 @@ read_value (struct lexer *lexer, const struct pivot_table *pt,
     {
       value = xmalloc (sizeof *value);
       *value = (struct pivot_value) {
-        .type = PIVOT_VALUE_VARIABLE,
-        .variable = { .var_name = xstrdup (lex_tokcstr (lexer)) },
+        .variable = {
+          .type = PIVOT_VALUE_VARIABLE,
+          .var_name = xstrdup (lex_tokcstr (lexer))
+        },
       };
       lex_get (lexer);
     }
@@ -943,7 +939,8 @@ read_footnote (struct lexer *lexer, struct pivot_table *pt)
   else
     marker = NULL;
 
-  pivot_table_create_footnote__ (pt, idx, marker, content);
+  bool show = !lex_match_id (lexer, "HIDE");
+  pivot_table_create_footnote__ (pt, idx, marker, content)->show = show;
 }
 
 static void
@@ -1129,13 +1126,16 @@ read_current_layer (struct lexer *lexer, struct pivot_table *table)
     }
 }
 
-static struct pivot_table *
+static void
 read_table (struct lexer *lexer)
 {
+  bool displayed = false;
+
   struct pivot_table *pt = pivot_table_create ("Default Title");
   while (lex_match (lexer, T_SLASH))
     {
       assert (!pivot_table_is_shared (pt));
+      displayed = false;
 
       if (lex_match_id (lexer, "ROW"))
         read_dimension (lexer, pt, PIVOT_AXIS_ROW,
@@ -1212,6 +1212,7 @@ read_table (struct lexer *lexer)
         {
           pivot_table_submit (pivot_table_ref (pt));
           pt = pivot_table_unshare (pt);
+          displayed = true;
         }
       else
         {
@@ -1220,26 +1221,30 @@ read_table (struct lexer *lexer)
         }
     }
 
+  if (!displayed)
+    pivot_table_submit (pt);
+  else
+    pivot_table_unref (pt);
+
   force_match (lexer, T_ENDCMD);
-  return pt;
 }
 
 static void
-output_msg (const struct msg *m_, void *lexer_)
+output_msg (const struct msg *m_, struct lexer *lexer)
 {
-  struct lexer *lexer = lexer_;
-  struct msg m = *m_;
-
-  if (m.file_name == NULL)
-    {
-      m.file_name = CONST_CAST (char *, lex_get_file_name (lexer));
-      m.first_line = lex_get_first_line_number (lexer, 0);
-      m.last_line = lex_get_last_line_number (lexer, 0);
-    }
-
-  m.command_name = output_get_command_name ();
+  struct msg m = {
+    .category = m_->category,
+    .severity = m_->severity,
+    .location = (m_->location ? m_->location
+                 : lexer ? lex_get_location (lexer, 0, 0)
+                 : NULL),
+    .command_name = output_get_uppercase_command_name (),
+    .text = m_->text,
+  };
 
-  message_item_submit (message_item_create (&m));
+  output_item_submit (message_item_create (&m));
 
   free (m.command_name);
+  if (m.location != m_->location)
+    msg_location_destroy (m.location);
 }