Improve the way we handle the various parsing "states". Until now
[pspp-builds.git] / src / language / lexer / lexer.c
index 391fa79a3e1332fb324e4aa2871be3dba8d0b9c8..4cb3f593095f5c9f4c18d82a0e54a53ccbfbb0b0 100644 (file)
 
 #include <config.h>
 #include "lexer.h"
-#include "message.h"
+#include <libpspp/message.h>
 #include <ctype.h>
 #include <errno.h>
 #include <limits.h>
 #include <math.h>
 #include <stdarg.h>
 #include <stdlib.h>
-#include "alloc.h"
-#include "command.h"
-#include "message.h"
-#include "line-buffer.h"
-#include "magic.h"
-#include "settings.h"
-#include "str.h"
+#include <libpspp/alloc.h>
+#include <language/command.h>
+#include <libpspp/message.h>
+#include <language/line-buffer.h>
+#include <libpspp/magic.h>
+#include <data/settings.h>
+#include <libpspp/str.h>
+
+#include "size_max.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -125,7 +127,7 @@ restore_token (void)
 {
   assert (put_token != 0);
   token = put_token;
-  ds_replace (&tokstr, ds_c_str (&put_tokstr));
+  ds_assign_string (&tokstr, &put_tokstr);
   str_copy_trunc (tokid, sizeof tokid, ds_c_str (&tokstr));
   tokval = put_tokval;
   put_token = 0;
@@ -137,7 +139,7 @@ static void
 save_token (void) 
 {
   put_token = token;
-  ds_replace (&put_tokstr, ds_c_str (&tokstr));
+  ds_assign_string (&put_tokstr, &tokstr);
   put_tokval = tokval;
 }
 
@@ -449,7 +451,7 @@ lex_end_of_command (void)
   if (token != '.')
     {
       lex_error (_("expecting end of command"));
-      return CMD_TRAILING_GARBAGE;
+      return CMD_FAILURE;
     }
   else
     return CMD_SUCCESS;
@@ -693,7 +695,7 @@ lex_put_back_id (const char *id)
   assert (lex_id_to_token (id, strlen (id)) == T_ID);
   save_token ();
   token = T_ID;
-  ds_replace (&tokstr, id);
+  ds_assign_c_str (&tokstr, id);
   str_copy_trunc (tokid, sizeof tokid, ds_c_str (&tokstr));
 }
 \f
@@ -734,6 +736,25 @@ lex_set_prog (char *p)
 {
   prog = p;
 }
+
+/* Discards the rest of the current command.
+   When we're reading commands from a file, we skip tokens until
+   a terminal dot or EOF.
+   When we're reading commands interactively from the user,
+   that's just discarding the current line, because presumably
+   the user doesn't want to finish typing a command that will be
+   ignored anyway. */
+void
+lex_discard_rest_of_command (void) 
+{
+  if (!getl_is_interactive ())
+    {
+      while (token != T_STOP && token != '.')
+       lex_get ();
+    }
+  else 
+    lex_discard_line (); 
+}
 \f
 /* Weird line reading functions. */
 
@@ -930,7 +951,7 @@ lex_negative_to_dash (void)
     {
       token = T_POS_NUM;
       tokval = -tokval;
-      ds_replace (&tokstr, ds_c_str (&tokstr) + 1);
+      ds_assign_substring (&tokstr, &tokstr, 1, SIZE_MAX);
       save_token ();
       token = '-';
     }