SORT CASES: Improve error messages and coding style.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 19 Sep 2022 02:31:47 +0000 (19:31 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 19 Sep 2022 02:31:47 +0000 (19:31 -0700)
src/data/subcase.h
src/language/stats/sort-cases.c
src/language/stats/sort-criteria.c
tests/language/stats/sort-cases.at

index 4e5043648c4728fbbacfe01fecb5ab3f0cf0173a..027a6395420bf90e52999a496e0c0d4cb14f30f9 100644 (file)
@@ -47,6 +47,7 @@ struct subcase
 
     struct caseproto *proto;    /* Created lazily. */
   };
+#define SUBCASE_EMPTY_INITIALIZER { .fields = NULL }
 
 void subcase_init_empty (struct subcase *);
 void subcase_init_vars (struct subcase *,
index 4b6b1ebec3e0adb7b951a67be2655a9f2ea8db2f..41c7662413c498a902291077e4260d8c339dd3d6 100644 (file)
 int
 cmd_sort_cases (struct lexer *lexer, struct dataset *ds)
 {
-  struct subcase ordering;
-  struct casereader *output;
+  struct subcase ordering = SUBCASE_EMPTY_INITIALIZER;
   bool ok = false;
 
   lex_match (lexer, T_BY);
 
   proc_cancel_temporary_transformations (ds);
-  subcase_init_empty (&ordering);
   if (!parse_sort_criteria (lexer, dataset_dict (ds), &ordering, NULL, NULL))
     return CMD_CASCADING_FAILURE;
 
   if (settings_get_testing_mode () && lex_match (lexer, T_SLASH))
     {
-      if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, T_EQUALS)
-          || !lex_force_int_range (lexer, "BUFFERS", 2, INT_MAX))
+      if (!lex_force_match_id (lexer, "BUFFERS"))
+        goto done;
+      lex_match (lexer, T_EQUALS);
+      if (!lex_force_int_range (lexer, "BUFFERS", 2, INT_MAX))
         goto done;
-
       min_buffers = max_buffers = lex_integer (lexer);
-
       lex_get (lexer);
     }
 
   proc_discard_output (ds);
-  output = sort_execute (proc_open_filtering (ds, false), &ordering);
+  struct casereader *output = sort_execute (proc_open_filtering (ds, false),
+                                            &ordering);
   ok = proc_commit (ds);
   ok = dataset_set_source (ds, output) && ok;
 
index 36466cd410f9bfdb89a2db6588fbbe6ba3c6a21d..92c80640460895a5bdb3dfda143d6240b4ab2f9b 100644 (file)
@@ -51,18 +51,18 @@ parse_sort_criteria (struct lexer *lexer, const struct dictionary *dict,
   if (saw_direction != NULL)
     *saw_direction = false;
 
+  int start_ofs = lex_ofs (lexer);
   do
     {
       size_t prev_n_vars = n_vars;
-      enum subcase_direction direction;
-      size_t i;
 
       /* Variables. */
       if (!parse_variables_const (lexer, dict, vars, &n_vars,
-                                  PV_APPEND | PV_NO_SCRATCH))
+                                  PV_APPEND | PV_DUPLICATE | PV_NO_SCRATCH))
         goto error;
 
       /* Sort direction. */
+      enum subcase_direction direction;
       if (lex_match (lexer, T_LPAREN))
        {
          if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN"))
@@ -82,12 +82,13 @@ parse_sort_criteria (struct lexer *lexer, const struct dictionary *dict,
       else
         direction = SC_ASCEND;
 
-      for (i = prev_n_vars; i < n_vars; i++)
+      for (size_t i = prev_n_vars; i < n_vars; i++)
         {
           const struct variable *var = (*vars)[i];
           if (!subcase_add_var (ordering, var, direction))
-            msg (SW, _("Variable %s specified twice in sort criteria."),
-                 var_get_name (var));
+            lex_ofs_msg (lexer, SW, start_ofs, lex_ofs (lexer) - 1,
+                         _("Variable %s specified twice in sort criteria."),
+                         var_get_name (var));
         }
     }
   while (lex_token (lexer) == T_ID
@@ -97,6 +98,8 @@ parse_sort_criteria (struct lexer *lexer, const struct dictionary *dict,
   return true;
 
 error:
+  subcase_uninit (ordering);
+  subcase_init_empty (ordering);
   free (local_vars);
   if (vars)
     *vars = NULL;
index 8d839f9dabeacf7dfa87459e629e23d719af37a8..6c21efa5eb8a80d761d15d837035579cda1d71cf 100644 (file)
@@ -135,3 +135,37 @@ x,mod2
 5.00,1.00
 ])
 AT_CLEANUP
+
+AT_SETUP([SORT CASES syntax errors])
+AT_DATA([sort-cases.sps], [dnl
+DATA LIST LIST NOTABLE/x y z.
+SORT CASES BY **.
+SORT CASES BY x(**).
+SORT CASES BY x(D**).
+SORT CASES BY x(A) x(D) y(**).
+])
+AT_DATA([insert.sps], [dnl
+INSERT FILE='sort-cases.sps' ERROR=IGNORE.
+])
+AT_CHECK([pspp --testing-mode -O format=csv insert.sps], [1], [dnl
+"sort-cases.sps:2.15-2.16: error: SORT CASES: Syntax error expecting variable name.
+    2 | SORT CASES BY **.
+      |               ^~"
+
+"sort-cases.sps:3.17-3.18: error: SORT CASES: Syntax error expecting A or D.
+    3 | SORT CASES BY x(**).
+      |                 ^~"
+
+"sort-cases.sps:4.18-4.19: error: SORT CASES: Syntax error expecting `)'.
+    4 | SORT CASES BY x(D**).
+      |                  ^~"
+
+"sort-cases.sps:5.15-5.23: warning: SORT CASES: Variable x specified twice in sort criteria.
+    5 | SORT CASES BY x(A) x(D) y(**).
+      |               ^~~~~~~~~"
+
+"sort-cases.sps:5.27-5.28: error: SORT CASES: Syntax error expecting A or D.
+    5 | SORT CASES BY x(A) x(D) y(**).
+      |                           ^~"
+])
+AT_CLEANUP
\ No newline at end of file