lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / language / utilities / title.c
index 9d5b8261619c0a39c3d318bf8134933c4008422c..398288b812c9d966c6e6de13a76edeffbb784f06 100644 (file)
@@ -52,20 +52,10 @@ cmd_subtitle (struct lexer *lexer, struct dataset *ds UNUSED)
 static int
 parse_title (struct lexer *lexer, enum text_item_type type)
 {
-  if (lex_look_ahead (lexer) == T_STRING)
-    {
-      lex_get (lexer);
-      if (!lex_force_string (lexer))
-       return CMD_FAILURE;
-      set_title (lex_tokcstr (lexer), type);
-      lex_get (lexer);
-      return lex_end_of_command (lexer);
-    }
-  else
-    {
-      set_title (lex_rest_of_line (lexer), type);
-      lex_discard_line (lexer);
-    }
+  if (!lex_force_string (lexer))
+    return CMD_FAILURE;
+  set_title (lex_tokcstr (lexer), type);
+  lex_get (lexer);
   return CMD_SUCCESS;
 }
 
@@ -79,81 +69,49 @@ set_title (const char *title, enum text_item_type type)
 int
 cmd_file_label (struct lexer *lexer, struct dataset *ds)
 {
-  const char *label;
-
-  label = lex_rest_of_line (lexer);
-  lex_discard_line (lexer);
-  while (isspace ((unsigned char) *label))
-    label++;
+  if (!lex_force_string (lexer))
+    return CMD_FAILURE;
 
-  dict_set_label (dataset_dict (ds), label);
+  dict_set_label (dataset_dict (ds), lex_tokcstr (lexer));
+  lex_get (lexer);
 
   return CMD_SUCCESS;
 }
 
-/* Add entry date line to DICT's documents. */
-static void
-add_document_trailer (struct dictionary *dict)
-{
-  char buf[64];
-
-  sprintf (buf, _("   (Entered %s)"), get_start_date ());
-  dict_add_document_line (dict, buf);
-}
-
 /* Performs the DOCUMENT command. */
 int
 cmd_document (struct lexer *lexer, struct dataset *ds)
 {
   struct dictionary *dict = dataset_dict (ds);
-  struct string line = DS_EMPTY_INITIALIZER;
-  bool end_dot;
+  char *trailer;
 
-  do
+  if (!lex_force_string (lexer))
+    return CMD_FAILURE;
+
+  while (lex_is_string (lexer))
     {
-      end_dot = lex_end_dot (lexer);
-      ds_assign_string (&line, lex_entire_line_ds (lexer));
-      if (end_dot)
-        ds_put_byte (&line, '.');
-      dict_add_document_line (dict, ds_cstr (&line));
-
-      lex_discard_line (lexer);
-      lex_get_line (lexer);
+      dict_add_document_line (dict, lex_tokcstr (lexer), true);
+      lex_get (lexer);
     }
-  while (!end_dot);
 
-  add_document_trailer (dict);
-  ds_destroy (&line);
+  trailer = xasprintf (_("   (Entered %s)"), get_start_date ());
+  dict_add_document_line (dict, trailer, true);
+  free (trailer);
 
   return CMD_SUCCESS;
 }
 
-/* Performs the DROP DOCUMENTS command. */
+/* Performs the ADD DOCUMENTS command. */
 int
-cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
+cmd_add_documents (struct lexer *lexer, struct dataset *ds)
 {
-  dict_clear_documents (dataset_dict (ds));
-
-  return lex_end_of_command (lexer);
+  return cmd_document (lexer, ds);
 }
 
-
-/* Performs the ADD DOCUMENTS command. */
+/* Performs the DROP DOCUMENTS command. */
 int
-cmd_add_documents (struct lexer *lexer, struct dataset *ds)
+cmd_drop_documents (struct lexer *lexer UNUSED, struct dataset *ds)
 {
-  struct dictionary *dict = dataset_dict (ds);
-
-  if ( ! lex_force_string (lexer) )
-    return CMD_FAILURE;
-
-  while ( lex_is_string (lexer))
-    {
-      dict_add_document_line (dict, lex_tokcstr (lexer));
-      lex_get (lexer);
-    }
-
-  add_document_trailer (dict);
-
-  return lex_end_of_command (lexer) ;
+  dict_clear_documents (dataset_dict (ds));
+  return CMD_SUCCESS;
 }