appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::cmd::Token Class Reference

Parsed token container class. More...

#include <Token.h>

Public Member Functions

 Token ()
 Default constructor.
 
 Token (const std::string &strValue)
 Initialization constructor. More...
 
 Token (const std::string &strValue, const size_t lineNum, const size_t posStart, const size_t posEnd)
 Initialization constructor. More...
 
 Token (const Token &src)
 Copy constructor. More...
 
virtual ~Token ()
 Destructor.
 
Tokenoperator= (const Token &rhs)
 Assignment operator. More...
 
const std::string & value () const
 Return token string. More...
 
const size_t linenum () const
 Return input line number where token was located. More...
 
void position (size_t &posStart, size_t &posEnd) const
 Return input line position where token was located. More...
 
std::ostream & printAnnotated (std::ostream &os, const std::string &strLine, const bool bLoc=false)
 Output annotated token embedded location in the input line. More...
 

Public Attributes

std::string m_strValue
 token string value
 
size_t m_lineNum
 line number
 
size_t m_posStart
 line start position of token
 
size_t m_posEnd
 line end position of token
 

Friends

std::ostream & operator<< (std::ostream &os, const Token &tok)
 Insert object into output stream. More...
 
LogBookoperator<< (LogBook &log, const Token &tok)
 Insert object into LogBook pending entry. More...
 

Detailed Description

Parsed token container class.

Definition at line 84 of file Token.h.

Constructor & Destructor Documentation

rnr::cmd::Token::Token ( const std::string &  strValue)

Initialization constructor.

Parameters
strValueToken value.
rnr::cmd::Token::Token ( const std::string &  strValue,
const size_t  lineNum,
const size_t  posStart,
const size_t  posEnd 
)

Initialization constructor.

Parameters
strValueToken value.
lineNumLine number. Set to 0 if unknown or not associated with I/O input.
posStartToken start position in line.
posEndToken end position in line.
Token::Token ( const Token src)

Copy constructor.

Parameters
srcSource object.

Definition at line 104 of file Token.cxx.

References m_lineNum, m_posEnd, m_posStart, and m_strValue.

105 {
106  m_strValue = src.m_strValue;
107  m_lineNum = src.m_lineNum;
108  m_posStart = src.m_posStart;
109  m_posEnd = src.m_posEnd;
110 }
size_t m_lineNum
line number
Definition: Token.h:88
size_t m_posEnd
line end position of token
Definition: Token.h:90
size_t m_posStart
line start position of token
Definition: Token.h:89
std::string m_strValue
token string value
Definition: Token.h:87

Member Function Documentation

const size_t rnr::cmd::Token::linenum ( ) const
inline

Return input line number where token was located.

Returns
Line number.

Definition at line 159 of file Token.h.

References m_lineNum.

160  {
161  return m_lineNum;
162  }
size_t m_lineNum
line number
Definition: Token.h:88
Token & Token::operator= ( const Token rhs)

Assignment operator.

Parameters
rhsRight-hand side object.
Returns
*this

Definition at line 116 of file Token.cxx.

References m_lineNum, m_posEnd, m_posStart, and m_strValue.

117 {
118  m_strValue = rhs.m_strValue;
119  m_lineNum = rhs.m_lineNum;
120  m_posStart = rhs.m_posStart;
121  m_posEnd = rhs.m_posEnd;
122 
123  return *this;
124 }
size_t m_lineNum
line number
Definition: Token.h:88
size_t m_posEnd
line end position of token
Definition: Token.h:90
size_t m_posStart
line start position of token
Definition: Token.h:89
std::string m_strValue
token string value
Definition: Token.h:87
void rnr::cmd::Token::position ( size_t &  posStart,
size_t &  posEnd 
) const
inline

Return input line position where token was located.

Parameters
[out]posStartStarting character position of token.
[out]posEndEnding character position of token.

Definition at line 170 of file Token.h.

References m_posEnd, m_posStart, operator<<, and printAnnotated().

171  {
172  posStart = m_posStart;
173  posEnd = m_posEnd;
174  }
size_t m_posEnd
line end position of token
Definition: Token.h:90
size_t m_posStart
line start position of token
Definition: Token.h:89
ostream & Token::printAnnotated ( std::ostream &  os,
const std::string &  strLine,
const bool  bLoc = false 
)

Output annotated token embedded location in the input line.

Output:
[linenum] line of text where token was generated
Parameters
osOutput stream.
strLineLine whence token was generated.
bLocIf true, then include line number and token characater positions.
Returns
Reference to output stream.

Definition at line 126 of file Token.cxx.

References operator<<(), rnr::str::prettify(), and value().

Referenced by position().

129 {
130  string color(ANSI_FG_BRIGHT_RED);
131  string reset(ANSI_COLOR_RESET);
132 
133  size_t lw = strLine.length(); // line width
134  size_t pos; // working substring starting position
135  size_t len; // working substring length
136 
137  //
138  // Print line number
139  //
140  if( bLoc )
141  {
142  os << "[";
143  if( m_lineNum > 0 )
144  {
145  os << m_lineNum << ":";
146  }
147  os << m_posStart << "," << m_posEnd;
148  os << "] ";
149  }
150 
151  //
152  // Print token embedded in line.
153  //
154  if( m_posStart < lw )
155  {
156  //
157  // Print substring before token in plaintext
158  //
159  if( m_posStart > 0 )
160  {
161  os << strLine.substr(0, m_posStart);
162  }
163 
164  //
165  // Print token substring in color.
166  //
167  len = m_posEnd - m_posStart + 1;
168  if( len > lw )
169  {
170  len = lw - m_posStart;
171  }
172  os << color << strLine.substr(m_posStart, len) << reset;
173 
174  //
175  // Print substring after token in plaintext
176  //
177  pos = m_posEnd + 1;
178  if( pos < lw )
179  {
180  os << strLine.substr(pos);
181  }
182  }
183 
184  //
185  // Print line.
186  //
187  else
188  {
189  os << strLine << endl;
190  }
191 
192  return os;
193 }
size_t m_lineNum
line number
Definition: Token.h:88
size_t m_posEnd
line end position of token
Definition: Token.h:90
size_t m_posStart
line start position of token
Definition: Token.h:89
const std::string& rnr::cmd::Token::value ( ) const
inline

Return token string.

Returns
String.

Definition at line 149 of file Token.h.

References m_strValue.

Referenced by rnr::cmd::CommandLine::peekEq(), and printAnnotated().

150  {
151  return m_strValue;
152  }
std::string m_strValue
token string value
Definition: Token.h:87

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const Token tok 
)
friend

Insert object into output stream.

Parameters
osOutput stream.
tokObject to insert.
Returns
Reference to output stream.

Referenced by position().

LogBook& operator<< ( LogBook log,
const Token tok 
)
friend

Insert object into LogBook pending entry.

Parameters
logLogBook stream.
tokObject to insert.
Returns
Reference to LogBook.

The documentation for this class was generated from the following files: