output: Move text_item and group_item usage closer to the SPV model.
[pspp] / src / language / lexer / lexer.c
index fb45465f1943aa1d2e811aecd8ef41fce5e2282e..11de4897724f1e4f8631e41c715e88f54c61ebd5 100644 (file)
@@ -1257,7 +1257,6 @@ lex_source_error_valist (struct lex_source *src, int n0, int n1,
 {
   const struct lex_token *token;
   struct string s;
-  struct msg m;
 
   ds_init_empty (&s);
 
@@ -1285,14 +1284,16 @@ lex_source_error_valist (struct lex_source *src, int n0, int n1,
     }
   ds_put_byte (&s, '.');
 
-  m.category = MSG_C_SYNTAX;
-  m.severity = MSG_S_ERROR;
-  m.file_name = src->reader->file_name;
-  m.first_line = lex_source_get_first_line_number (src, n0);
-  m.last_line = lex_source_get_last_line_number (src, n1);
-  m.first_column = lex_source_get_first_column (src, n0);
-  m.last_column = lex_source_get_last_column (src, n1);
-  m.text = ds_steal_cstr (&s);
+  struct msg m = {
+    .category = MSG_C_SYNTAX,
+    .severity = MSG_S_ERROR,
+    .file_name = src->reader->file_name,
+    .first_line = lex_source_get_first_line_number (src, n0),
+    .last_line = lex_source_get_last_line_number (src, n1),
+    .first_column = lex_source_get_first_column (src, n0),
+    .last_column = lex_source_get_last_column (src, n1),
+    .text = ds_steal_cstr (&s),
+  };
   msg_emit (&m);
 }
 
@@ -1414,20 +1415,31 @@ lex_source_get__ (const struct lex_source *src_)
     }
   for (int i = 0; i < n_lines; i++)
     {
+      /* Beginning of line. */
       const char *line = &src->buffer[src->journal_pos - src->tail];
-      const char *newline = rawmemchr (line, '\n');
-      size_t line_len = newline - line;
-      if (line_len > 0 && line[line_len - 1] == '\r')
-        line_len--;
 
-      char *syntax = malloc (line_len + 2);
-      memcpy (syntax, line, line_len);
-      syntax[line_len] = '\n';
-      syntax[line_len + 1] = '\0';
+      /* Calculate line length, including \n or \r\n end-of-line if present.
+
+         We use src->head even though that may be beyond what we've actually
+         converted to tokens (which is only through state.line_pos).  That's
+         because, if we're emitting the line due to SEG_END_COMMAND, we want to
+         take the whole line through the newline, not just through the '.'. */
+      size_t max_len = src->head - src->journal_pos;
+      const char *newline = memchr (line, '\n', max_len);
+      size_t line_len = newline ? newline - line + 1 : max_len;
+
+      /* Calculate line length excluding end-of-line. */
+      size_t copy_len = line_len;
+      if (copy_len > 0 && line[copy_len - 1] == '\n')
+        copy_len--;
+      if (copy_len > 0 && line[copy_len - 1] == '\r')
+        copy_len--;
 
-      text_item_submit (text_item_create_nocopy (TEXT_ITEM_SYNTAX, syntax));
+      /* Submit the line as syntax. */
+      text_item_submit (text_item_create_nocopy (TEXT_ITEM_SYNTAX,
+                                                 xmemdup0 (line, copy_len)));
 
-      src->journal_pos += newline - line + 1;
+      src->journal_pos += line_len;
     }
 
   token->token_len = state.seg_pos - src->seg_pos;