segment: Allow '.' as separate token with a line, for macros.
[pspp] / src / language / lexer / lexer.c
index b69bc9a5d0df1c12772afa777b4fb43a4d10bb1c..baebda3f6d89244cdba9eea628b764d65b2aae57 100644 (file)
@@ -679,6 +679,96 @@ lex_force_int (struct lexer *lexer)
     }
 }
 
+/* If the current token is an integer in the range MIN...MAX (inclusive), does
+   nothing and returns true.  Otherwise, reports an error and returns false.
+   If NAME is nonnull, then it is used in the error message. */
+bool
+lex_force_int_range (struct lexer *lexer, const char *name, long min, long max)
+{
+  bool is_integer = lex_is_integer (lexer);
+  bool too_small = is_integer && lex_integer (lexer) < min;
+  bool too_big = is_integer && lex_integer (lexer) > max;
+  if (is_integer && !too_small && !too_big)
+    return true;
+
+  if (min > max)
+    {
+      /* Weird, maybe a bug in the caller.  Just report that we needed an
+         integer. */
+      if (name)
+        lex_error (lexer, _("Integer expected for %s."), name);
+      else
+        lex_error (lexer, _("Integer expected."));
+    }
+  else if (min == max)
+    {
+      if (name)
+        lex_error (lexer, _("Expected %ld for %s."), min, name);
+      else
+        lex_error (lexer, _("Expected %ld."), min);
+    }
+  else if (min + 1 == max)
+    {
+      if (name)
+        lex_error (lexer, _("Expected %ld or %ld for %s."), min, min + 1, name);
+      else
+        lex_error (lexer, _("Expected %ld or %ld."), min, min + 1);
+    }
+  else
+    {
+      bool report_lower_bound = (min > INT_MIN / 2) || too_small;
+      bool report_upper_bound = (max < INT_MAX / 2) || too_big;
+
+      if (report_lower_bound && report_upper_bound)
+        {
+          if (name)
+            lex_error (lexer,
+                       _("Expected integer between %ld and %ld for %s."),
+                       min, max, name);
+          else
+            lex_error (lexer, _("Expected integer between %ld and %ld."),
+                       min, max);
+        }
+      else if (report_lower_bound)
+        {
+          if (min == 0)
+            {
+              if (name)
+                lex_error (lexer, _("Expected non-negative integer for %s."),
+                           name);
+              else
+                lex_error (lexer, _("Expected non-negative integer."));
+            }
+          else if (min == 1)
+            {
+              if (name)
+                lex_error (lexer, _("Expected positive integer for %s."),
+                           name);
+              else
+                lex_error (lexer, _("Expected positive integer."));
+            }
+        }
+      else if (report_upper_bound)
+        {
+          if (name)
+            lex_error (lexer,
+                       _("Expected integer less than or equal to %ld for %s."),
+                       max, name);
+          else
+            lex_error (lexer, _("Expected integer less than or equal to %ld."),
+                       max);
+        }
+      else
+        {
+          if (name)
+            lex_error (lexer, _("Integer expected for %s."), name);
+          else
+            lex_error (lexer, _("Integer expected."));
+        }
+    }
+  return false;
+}
+
 /* If the current token is a number, does nothing and returns true.
    Otherwise, reports an error and returns false. */
 bool
@@ -843,8 +933,8 @@ lex_next_tokcstr (const struct lexer *lexer, int n)
    The string is null-terminated (but the null terminator is not included in
    the returned substring's 'length').
 
-   Only T_ID and T_STRING tokens have meaningful strings.  For other tokens
-   this functions this function will always return NULL.
+   Only T_ID, T_MACRO_ID, T_STRING tokens have meaningful strings.  For other
+   tokens this functions this function will always return NULL.
 
    The UTF-8 encoding of the returned string is correct for variable names and
    other identifiers.  Use filename_to_utf8() to use it as a filename.  Use
@@ -1306,7 +1396,8 @@ lex_source_error_valist (struct lex_source *src, int n0, int n1,
       ds_put_cstr (&s, ": ");
       ds_put_vformat (&s, format, args);
     }
-  ds_put_byte (&s, '.');
+  if (ds_last (&s) != '.')
+    ds_put_byte (&s, '.');
 
   struct msg m = {
     .category = MSG_C_SYNTAX,
@@ -1516,10 +1607,6 @@ lex_source_get__ (const struct lex_source *src_)
                      token->token.string.string);
       break;
 
-    case SCAN_UNEXPECTED_DOT:
-      lex_get_error (src, _("Unexpected `.' in middle of command"));
-      break;
-
     case SCAN_UNEXPECTED_CHAR:
       {
         char c_name[16];