DATASET: Improve error messages and coding style.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 6 Nov 2022 19:54:48 +0000 (11:54 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 6 Nov 2022 19:54:48 +0000 (11:54 -0800)
src/language/data-io/dataset.c
tests/language/data-io/dataset.at

index a04fec5268ba1df5f564930c5d4b77fa06f7bee5..4e601d767f88ebcf1b9dbd70f99a1a35e4199595 100644 (file)
@@ -45,19 +45,27 @@ parse_window (struct lexer *lexer, unsigned int allowed,
   else if (allowed & (1 << DATASET_HIDDEN) && lex_match_id (lexer, "HIDDEN"))
     return DATASET_HIDDEN;
 
-  lex_error (lexer, NULL);
+  const char *allowed_s[4];
+  size_t n_allowed = 0;
+  if (allowed & (1 << DATASET_MINIMIZED))
+    allowed_s[n_allowed++] = "MINIMIZED";
+  if (allowed & (1 << DATASET_ASIS))
+    allowed_s[n_allowed++] = "ASIS";
+  if (allowed & (1 << DATASET_FRONT))
+    allowed_s[n_allowed++] = "FRONT";
+  if (allowed & (1 << DATASET_HIDDEN))
+    allowed_s[n_allowed++] = "HIDDEN";
+  lex_error_expecting_array (lexer, allowed_s, n_allowed);
   return -1;
 }
 
 static struct dataset *
 parse_dataset_name (struct lexer *lexer, struct session *session)
 {
-  struct dataset *ds;
-
   if (!lex_force_id (lexer))
     return NULL;
 
-  ds = session_lookup_dataset (session, lex_tokcstr (lexer));
+  struct dataset *ds = session_lookup_dataset (session, lex_tokcstr (lexer));
   if (ds != NULL)
     lex_get (lexer);
   else
@@ -68,15 +76,13 @@ parse_dataset_name (struct lexer *lexer, struct session *session)
 int
 cmd_dataset_name (struct lexer *lexer, struct dataset *active)
 {
-  int display;
-
   if (!lex_force_id (lexer))
     return CMD_FAILURE;
   dataset_set_name (active, lex_tokcstr (lexer));
   lex_get (lexer);
 
-  display = parse_window (lexer, (1 << DATASET_ASIS) | (1 << DATASET_FRONT),
-                          DATASET_ASIS);
+  int display = parse_window (lexer, (1 << DATASET_ASIS) | (1 << DATASET_FRONT),
+                              DATASET_ASIS);
   if (display < 0)
     return CMD_FAILURE;
   else if (display != DATASET_ASIS)
@@ -119,28 +125,26 @@ int
 cmd_dataset_copy (struct lexer *lexer, struct dataset *old)
 {
   struct session *session = dataset_session (old);
-  struct dataset *new;
-  int display;
-  char *name;
 
   /* Parse the entire command first.  proc_execute() can attempt to parse
      BEGIN DATA...END DATA and it will fail confusingly if we are in the
      middle of the command at the point.  */
   if (!lex_force_id (lexer))
     return CMD_FAILURE;
-  name = xstrdup (lex_tokcstr (lexer));
+  char *name = xstrdup (lex_tokcstr (lexer));
   lex_get (lexer);
 
-  display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
-                                  | (1 << DATASET_HIDDEN)
-                                  | (1 << DATASET_FRONT)),
-                          DATASET_MINIMIZED);
+  int display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
+                                      | (1 << DATASET_HIDDEN)
+                                      | (1 << DATASET_FRONT)),
+                              DATASET_MINIMIZED);
   if (display < 0)
     {
       free (name);
       return CMD_FAILURE;
     }
 
+  struct dataset *new;
   if (session_lookup_dataset (session, name) == old)
     {
       new = old;
@@ -161,18 +165,16 @@ int
 cmd_dataset_declare (struct lexer *lexer, struct dataset *ds)
 {
   struct session *session = dataset_session (ds);
-  struct dataset *new;
-  int display;
 
   if (!lex_force_id (lexer))
     return CMD_FAILURE;
 
-  new = session_lookup_dataset (session, lex_tokcstr (lexer));
+  struct dataset *new = session_lookup_dataset (session, lex_tokcstr (lexer));
   if (new == NULL)
     new = dataset_create (session, lex_tokcstr (lexer));
   lex_get (lexer);
 
-  display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
+  int display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
                                   | (1 << DATASET_HIDDEN)
                                   | (1 << DATASET_FRONT)),
                           DATASET_MINIMIZED);
index c484f16b8114fb98a7a6c6be42bbf16b88c6f4c9..d86a0000565adaa2fc8dce4fd1467beaefc04766 100644 (file)
@@ -330,4 +330,67 @@ Variable,Record,Columns,Format
 y,1,2-4,F3.0
 ])
 
+AT_CLEANUP
+
+AT_SETUP([DATASET syntax errors])
+AT_DATA([dataset.sps], [dnl
+DATASET NAME **.
+DATASET NAME xyzzy WINDOW **.
+
+DATASET NAME xyzzy.
+DATASET ACTIVATE **.
+DATASET ACTIVATE xyzzy WINDOW **.
+
+DATASET COPY **.
+DATASET COPY quux WINDOW **.
+
+DATASET DECLARE **.
+DATASET DECLARE foo WINDOW **.
+
+DATASET CLOSE **.
+])
+AT_CHECK([pspp dataset.sps], [1], [dnl
+dataset.sps:1.14-1.15: error: DATASET NAME: Syntax error expecting identifier.
+    1 | DATASET NAME **.
+      |              ^~
+
+dataset.sps:2.27-2.28: error: DATASET NAME: Syntax error expecting ASIS or
+FRONT.
+    2 | DATASET NAME xyzzy WINDOW **.
+      |                           ^~
+
+dataset.sps:5.18-5.19: error: DATASET ACTIVATE: Syntax error expecting
+identifier.
+    5 | DATASET ACTIVATE **.
+      |                  ^~
+
+dataset.sps:6.31-6.32: error: DATASET ACTIVATE: Syntax error expecting ASIS or
+FRONT.
+    6 | DATASET ACTIVATE xyzzy WINDOW **.
+      |                               ^~
+
+dataset.sps:8.14-8.15: error: DATASET COPY: Syntax error expecting identifier.
+    8 | DATASET COPY **.
+      |              ^~
+
+dataset.sps:9.26-9.27: error: DATASET COPY: Syntax error expecting MINIMIZED,
+FRONT, or HIDDEN.
+    9 | DATASET COPY quux WINDOW **.
+      |                          ^~
+
+dataset.sps:11.17-11.18: error: DATASET DECLARE: Syntax error expecting
+identifier.
+   11 | DATASET DECLARE **.
+      |                 ^~
+
+dataset.sps:12.28-12.29: error: DATASET DECLARE: Syntax error expecting
+MINIMIZED, FRONT, or HIDDEN.
+   12 | DATASET DECLARE foo WINDOW **.
+      |                            ^~
+
+dataset.sps:14.15-14.16: error: DATASET CLOSE: Syntax error expecting
+identifier.
+   14 | DATASET CLOSE **.
+      |               ^~
+])
 AT_CLEANUP
\ No newline at end of file