pspp-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pre-lexer-2 4/4] lexer: Get rid of lex_negative_to_dash().


From: Ben Pfaff
Subject: [pre-lexer-2 4/4] lexer: Get rid of lex_negative_to_dash().
Date: Fri, 24 Sep 2010 21:17:54 -0700

This function stands in the way of replacing the lexer in a
straightforward way, because it disrupts the stream of tokens: the
lexer can't tell in advance whether, e.g., "-4" is one token or
two.  Fortunately, it is not used in very many places and is
relatively easy to remove.
---
 src/language/data-io/placement-parser.c |   18 +++++++-------
 src/language/expressions/parse.c        |   36 ++++++++++++++++++++----------
 src/language/lexer/lexer.c              |   26 +--------------------
 src/language/lexer/lexer.h              |    1 -
 4 files changed, 35 insertions(+), 46 deletions(-)

diff --git a/src/language/data-io/placement-parser.c 
b/src/language/data-io/placement-parser.c
index e9fe337..820560d 100644
--- a/src/language/data-io/placement-parser.c
+++ b/src/language/data-io/placement-parser.c
@@ -293,12 +293,10 @@ execute_placement_format (const struct fmt_spec *format,
    stores a 1-based column number into *COLUMN if successful,
    otherwise emits an error message and returns false. */
 static bool
-parse_column (struct lexer *lexer, int base, int *column)
+parse_column (int value, int base, int *column)
 {
   assert (base == 0 || base == 1);
-  if (!lex_force_int (lexer))
-    return false;
-  *column = lex_integer (lexer) - base + 1;
+  *column = value - base + 1;
   if (*column < 1)
     {
       if (base == 1)
@@ -307,7 +305,6 @@ parse_column (struct lexer *lexer, int base, int *column)
         msg (SE, _("Column positions for fields must not be negative."));
       return false;
     }
-  lex_get (lexer);
   return true;
 }
 
@@ -330,15 +327,18 @@ parse_column_range (struct lexer *lexer, int base,
                     bool *range_specified)
 {
   /* First column. */
-  if (!parse_column (lexer, base, first_column))
+  if (!lex_force_int (lexer)
+      || !parse_column (lex_integer (lexer), base, first_column))
     return false;
+  lex_get (lexer);
 
   /* Last column. */
-  lex_negative_to_dash (lexer);
-  if (lex_match (lexer, T_DASH))
+  if (lex_is_integer (lexer) && lex_integer (lexer) < 0)
     {
-      if (!parse_column (lexer, base, last_column))
+      if (!parse_column (-lex_integer (lexer), base, last_column))
         return false;
+      lex_get (lexer);
+
       if (*last_column < *first_column)
        {
          msg (SE, _("The ending column for a field must be "
diff --git a/src/language/expressions/parse.c b/src/language/expressions/parse.c
index 90de07f..a523ec2 100644
--- a/src/language/expressions/parse.c
+++ b/src/language/expressions/parse.c
@@ -499,16 +499,14 @@ match_operator (struct lexer *lexer, const struct 
operator ops[], size_t op_cnt,
   const struct operator *op;
 
   for (op = ops; op < ops + op_cnt; op++)
-    {
-      if (op->token == T_DASH)
-        lex_negative_to_dash (lexer);
-      if (lex_match (lexer, op->token))
-        {
-          if (operator != NULL)
-            *operator = op;
-          return true;
-        }
-    }
+    if (lex_token (lexer) == op->token)
+      {
+        if (op->token != T_NEG_NUM)
+          lex_get (lexer);
+        if (operator != NULL)
+          *operator = op;
+        return true;
+      }
   if (operator != NULL)
     *operator = NULL;
   return false;
@@ -710,6 +708,7 @@ parse_add (struct lexer *lexer, struct expression *e)
     {
       { T_PLUS, OP_ADD, "addition (`+')" },
       { T_DASH, OP_SUB, "subtraction (`-')" },
+      { T_NEG_NUM, OP_ADD, "subtraction (`-')" },
     };
 
   return parse_binary_operators (lexer, e, parse_mul (lexer, e),
@@ -752,8 +751,21 @@ parse_exp (struct lexer *lexer, struct expression *e)
       "That is, `a**b**c' equals `(a**b)**c', not as `a**(b**c)'.  "
       "To disable this warning, insert parentheses.");
 
-  return parse_binary_operators (lexer, e, parse_primary (lexer, e), &op, 1,
-                                 parse_primary, chain_warning);
+  union any_node *lhs, *node;
+  bool negative = false;
+
+  if (lex_token (lexer) == T_NEG_NUM)
+    {
+      lhs = expr_allocate_number (e, -lex_tokval (lexer));
+      negative = true;
+      lex_get (lexer);
+    }
+  else
+    lhs = parse_primary (lexer, e);
+
+  node = parse_binary_operators (lexer, e, lhs, &op, 1,
+                                  parse_primary, chain_warning);
+  return negative ? expr_allocate_unary (e, OP_NEG, node) : node;
 }
 
 /* Parses system variables. */
diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c
index 6a7e9c6..9c17c18 100644
--- a/src/language/lexer/lexer.c
+++ b/src/language/lexer/lexer.c
@@ -215,13 +215,8 @@ lex_get (struct lexer *lexer)
          {
            char *tail;
 
-           /* `-' can introduce a negative number, or it can be a
-              token by itself.  If it is not followed by a digit or a
-              decimal point, it is definitely not a number.
-              Otherwise, it might be either, but most of the time we
-              want it as a number.  When the syntax calls for a `-'
-              token, lex_negative_to_dash() must be used to break
-              negative numbers into two tokens. */
+           /* `-' can introduce a negative number, or it can be a token by
+              itself. */
            if (*lexer->prog == '-')
              {
                ds_put_char (&lexer->tokstr, *lexer->prog++);
@@ -1135,23 +1130,6 @@ lex_token_representation (struct lexer *lexer)
 
 /* Really weird functions. */
 
-/* Most of the time, a `-' is a lead-in to a negative number.  But
-   sometimes it's actually part of the syntax.  If a dash can be part
-   of syntax then this function is called to rip it off of a
-   number. */
-void
-lex_negative_to_dash (struct lexer *lexer)
-{
-  if (lexer->token == T_NEG_NUM)
-    {
-      lexer->token = T_POS_NUM;
-      lexer->tokval = -lexer->tokval;
-      ds_assign_substring (&lexer->tokstr, ds_substr (&lexer->tokstr, 1, 
SIZE_MAX));
-      save_token (lexer);
-      lexer->token = T_DASH;
-    }
-}
-
 /* Skip a COMMENT command. */
 void
 lex_skip_comment (struct lexer *lexer)
diff --git a/src/language/lexer/lexer.h b/src/language/lexer/lexer.h
index efdef8a..82f3fe2 100644
--- a/src/language/lexer/lexer.h
+++ b/src/language/lexer/lexer.h
@@ -97,7 +97,6 @@ const char *lex_tokid (const struct lexer *);
 const struct string *lex_tokstr (const struct lexer *);
 
 /* Really weird functions. */
-void lex_negative_to_dash (struct lexer *);
 void lex_skip_comment (struct lexer *);
 
 #endif /* !lexer_h */
-- 
1.7.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]