From 9407443e69a440d5ca10a81b56215c04862b604e Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Sun, 9 Aug 2009 15:43:49 +0200
Subject: [PATCH] improvments to tokenizing Scalar

- for alphanumeric sequences (optionally with [-+.]) that don't look
  like a float or int -> return as a word

  This means that '0patch' now looks like a <word> (not <label>)
  and '1.end' looks like a <word> and not <scalar>

  Something like '1-e' still does get treated as a bad <scalar> though
---
 src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C | 97 +++++++++++--------
 1 file changed, 59 insertions(+), 38 deletions(-)

diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C
index a05a5e72c4c..39637a6dc01 100644
--- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C
+++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C
@@ -174,7 +174,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
 
         // Numbers: do not distinguish at this point between Types.
         //
-        // we ideally wish to match the equivalent of this regular expression
+        // ideally match the equivalent of this regular expression
         //
         //    /^[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([Ee][-+]?[0-9]+)?$/
         //
@@ -183,75 +183,96 @@ Foam::Istream& Foam::ISstream::read(token& t)
         case '0' : case '1' : case '2' : case '3' : case '4' :
         case '5' : case '6' : case '7' : case '8' : case '9' :
         {
-            // has a floating point or digit
-            bool isScalar = (c == '.');
+            // has a digit
             bool hasDigit = isdigit(c);
 
+            // has a decimal point - cannot be label
+            bool notLabel = (c == '.');
+
+            // has contents that cannot be scalar
+            bool notScalar = false;
+
             unsigned int nChar = 0;
             charBuffer[nChar++] = c;
 
-            while
-            (
-                is_.get(c)
-             && (
-                    isdigit(c)
+            while (is_.get(c))
+            {
+                if (isdigit(c))
+                {
+                    hasDigit = true;
+                }
+                else if
+                (
+                    c == '+'
+                 || c == '-'
                  || c == '.'
-                 || c == 'e'
                  || c == 'E'
-                 || c == '+'
-                 || c == '-'
+                 || c == 'e'
                 )
-            )
-            {
-                charBuffer[nChar++] = c;
-                if (nChar >= sizeof(charBuffer))
                 {
-                    nChar--;
-                    break;
+                    notLabel = true;
                 }
-
-                if (isdigit(c))
+                else if (isalpha(c))
                 {
-                    hasDigit = true;
+                    notLabel = notScalar = true;
                 }
                 else
                 {
-                    isScalar = true;
+                    break;
+                }
+
+                charBuffer[nChar++] = c;
+                if (nChar >= sizeof(charBuffer))
+                {
+                    // runaway argument
+                    t.setBad();
+                    return *this;
                 }
             }
             charBuffer[nChar] = '\0';
 
+            if (!hasDigit)
+            {
+                notLabel = notScalar = true;
+            }
+
             setState(is_.rdstate());
 
             if (!is_.bad())
             {
                 is_.putback(c);
 
-                if (hasDigit)
+                if (nChar == 1 && charBuffer[0] == '-')
                 {
-                    if (!isScalar)
-                    {
-                        long lt = atol(charBuffer);
-                        t = label(lt);
-
-                        // return as a scalar if doesn't fit in a label
-                        isScalar = (t.labelToken() != lt);
-                    }
+                    // a single '-' is punctuation
+                    t = token::punctuationToken(token::SUBTRACT);
+                }
+                else if (notScalar)
+                {
+                    // not label or scalar: must be a word
+                    t = new word(charBuffer);
+                }
+                else if (notLabel)
+                {
+                    // not label: must be a scalar
+                    t = scalar(atof(charBuffer));
+                }
+                else if (hasDigit)
+                {
+                    // has digit: treat as a label
+                    long lt = atol(charBuffer);
+                    t = label(lt);
 
-                    if (isScalar)
+                    // return as a scalar if doesn't fit in a label
+                    if (t.labelToken() != lt)
                     {
                         t = scalar(atof(charBuffer));
                     }
                 }
-                else if (nChar == 1 && charBuffer[0] == '-')
-                {
-                    // a single '-' is punctuation
-                    t = token::punctuationToken(token::SUBTRACT);
-                }
                 else
                 {
-                    // some really bad sequence - eg, ".+E"
-                    t.setBad();
+                    // some else - treat as word
+                    t = new word(charBuffer);
                 }
             }
             else
-- 
GitLab