Commit 9407443e authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent e905f9b0
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment