Commit 4f1e4aa5 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add token type for stream flags (ASCII/BINARY)

parent 5281dd48
......@@ -191,6 +191,13 @@ bool Foam::UOPstream::write(const token& tok)
switch (tok.type())
{
case token::tokenType::FLAG :
{
// silently consume the flag
return true;
}
case token::tokenType::VERBATIMSTRING :
{
writeToBuffer(char(token::tokenType::VERBATIMSTRING));
......
......@@ -36,6 +36,12 @@ bool Foam::OSstream::write(const token& tok)
switch (tok.type())
{
case token::tokenType::FLAG :
{
// silently consume the flag
return true;
}
case token::tokenType::VERBATIMSTRING :
{
write(char(token::HASH));
......
......@@ -71,6 +71,12 @@ bool Foam::prefixOSstream::write(const token& tok)
switch (tok.type())
{
case token::tokenType::FLAG :
{
// silently consume the flag
return true;
}
case token::tokenType::VERBATIMSTRING :
{
write(char(token::HASH));
......
......@@ -79,6 +79,7 @@ public:
UNDEFINED, //!< An undefined token-type
// Fundamental types
FLAG, //!< stream flag (1-byte bitmask)
PUNCTUATION, //!< single character punctuation
LABEL, //!< label (integer) type
FLOAT_SCALAR, //!< float (single-precision) type
......@@ -97,6 +98,15 @@ public:
};
//- Stream or output control flags (1-byte width)
enum flagType
{
NO_FLAG = 0, //!< No flags
ASCII = 1, //!< ASCII-mode stream
BINARY = 2, //!< BINARY-mode stream
};
//- Standard punctuation tokens (a character)
enum punctuationToken
{
......@@ -137,16 +147,6 @@ public:
bool empty_;
// Private Member Functions
//- Disallow default bitwise copy construct
compound(const compound&) = delete;
//- Disallow default bitwise assignment
void operator=(const compound&) = delete;
public:
//- Runtime type information
......@@ -171,6 +171,9 @@ public:
empty_(false)
{}
//- No default copy construct
compound(const compound&) = delete;
// Selectors
......@@ -202,9 +205,13 @@ public:
virtual void write(Ostream& os) const = 0;
// IOstream Operators
// Operators
//- No default assign operator
compound& operator=(const compound&) = delete;
friend Ostream& operator<<(Ostream&, const compound&);
//- Output operator
friend Ostream& operator<<(Ostream& os, const compound& ct);
};
......@@ -250,6 +257,7 @@ private:
int64_t int64Val;
int32_t int32Val;
int flagVal; // bitmask - stored as int, not enum
punctuationToken punctuationVal;
label labelVal;
floatScalar floatVal;
......@@ -350,7 +358,12 @@ public:
inline ~token();
// Static member functions
// Static Member Functions
//- Create a token with stream flags, no sanity check
//
// \param bitmask the flags to set
inline static token flag(int bitmask);
//- True if the character is a punctuation separator (eg, in ISstream).
// Since it could also start a number, SUBTRACT is not included as
......@@ -395,6 +408,9 @@ public:
//- True if token is ERROR
inline bool error() const;
//- True if token is FLAG
inline bool isFlag() const;
//- True if token is PUNCTUATION
inline bool isPunctuation() const;
......@@ -431,6 +447,10 @@ public:
// Access
//- Return flag bitmask
// Report FatalIOError and return NO_FLAG if token is not FLAG
inline int flagToken() const;
//- Return punctuation character.
// Report FatalIOError and return \b \\0 if token is not PUNCTUATION
inline punctuationToken pToken() const;
......
......@@ -25,6 +25,48 @@ License
#include <algorithm>
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
inline Foam::token Foam::token::flag(int bitmask)
{
token tok;
tok.type_ = tokenType::FLAG;
tok.data_.flagVal = bitmask;
return tok;
}
inline bool Foam::token::isseparator(int c)
{
switch (c)
{
case token::END_STATEMENT :
case token::BEGIN_LIST :
case token::END_LIST :
case token::BEGIN_SQR :
case token::END_SQR :
case token::BEGIN_BLOCK :
case token::END_BLOCK :
case token::COLON :
case token::COMMA :
case token::ASSIGN :
case token::ADD :
// Excluded token::SUBTRACT since it could start a number
case token::MULTIPLY :
case token::DIVIDE :
{
return true;
}
default:
break;
}
return false;
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
inline void Foam::token::setUndefined()
......@@ -322,51 +364,39 @@ inline bool Foam::token::error() const
}
inline bool Foam::token::isPunctuation() const
inline bool Foam::token::isFlag() const
{
return (type_ == tokenType::PUNCTUATION);
return (type_ == tokenType::FLAG);
}
inline Foam::token::punctuationToken Foam::token::pToken() const
inline int Foam::token::flagToken() const
{
if (type_ == tokenType::PUNCTUATION)
if (type_ == tokenType::FLAG)
{
return data_.punctuationVal;
return data_.flagVal;
}
parseError("punctuation character");
return NULL_TOKEN;
parseError("flag bitmask");
return NO_FLAG;
}
inline bool Foam::token::isseparator(int c)
inline bool Foam::token::isPunctuation() const
{
switch (c)
{
case token::END_STATEMENT :
case token::BEGIN_LIST :
case token::END_LIST :
case token::BEGIN_SQR :
case token::END_SQR :
case token::BEGIN_BLOCK :
case token::END_BLOCK :
case token::COLON :
case token::COMMA :
case token::ASSIGN :
case token::ADD :
// Excluded token::SUBTRACT since it could start a number
case token::MULTIPLY :
case token::DIVIDE :
{
return true;
}
return (type_ == tokenType::PUNCTUATION);
}
default:
break;
inline Foam::token::punctuationToken Foam::token::pToken() const
{
if (type_ == tokenType::PUNCTUATION)
{
return data_.punctuationVal;
}
return false;
parseError("punctuation character");
return punctuationToken::NULL_TOKEN;
}
......@@ -712,6 +742,9 @@ inline bool Foam::token::operator==(const token& tok) const
case tokenType::UNDEFINED:
return true;
case tokenType::FLAG:
return data_.flagVal == tok.data_.flagVal;
case tokenType::PUNCTUATION:
return data_.punctuationVal == tok.data_.punctuationVal;
......
......@@ -43,6 +43,10 @@ static OS& printTokenInfo(OS& os, const token& tok)
os << "undefined token";
break;
case token::tokenType::FLAG:
os << "flag '" << int(tok.flagToken()) << '\'';
break;
case token::tokenType::PUNCTUATION:
os << "punctuation '" << tok.pToken() << '\'';
break;
......@@ -117,6 +121,7 @@ Foam::word Foam::token::name() const
switch (type_)
{
case token::tokenType::UNDEFINED: return "undefined";
case token::tokenType::FLAG: return "flag";
case token::tokenType::PUNCTUATION: return "punctuation";
case token::tokenType::LABEL: return "label";
case token::tokenType::FLOAT_SCALAR: return "float";
......@@ -155,6 +160,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
<< "Undefined token" << endl;
break;
case token::tokenType::FLAG:
// Swallow the flag
break;
case token::tokenType::PUNCTUATION:
os << tok.data_.punctuationVal;
break;
......
Markdown is supported
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