//
// token.cpp
// Token hierarchy implementation
// F.J. Alberti
// Created: 17/07/2001
// Last Modified: 17/07/2001
//
// FA   07/09/2001    Token lexeme is allocated in the heap
//

#include "token.h"
#include <string.h>


Token::Token()
  : _kind (kNone),
    _len  (0)
{
  _lexeme = new char[kMaxStringLiteralLength+1];
//assert(_lexeme != 0);
  _lexeme[0] = '\0';
}


Token::Token(const Token& tok)
  : _kind (tok._kind),
    _len  (tok._len),
    _span (tok._span)
{
  _lexeme = new char[kMaxStringLiteralLength+1];
//assert(_lexeme != 0);
  strcpy(_lexeme, tok._lexeme);
}


Token::~Token()
{
  if (_lexeme)
    delete [] _lexeme;
}


Token& Token::operator =(const Token& tok)
{
  if (this != &tok) {
    _kind = tok._kind;
    strcpy(_lexeme, tok._lexeme);
    _len  = tok._len;
    _span = tok._span;
  }
  return *this;
}


void Token::nullify()
{
  _kind          = kNone;
  _lexeme[0]     = '\0';
  _len           = 0;
  _span.beg.line = 0;
  _span.beg.off  = 0;
  _span.end.line = 0;
  _span.end.off  = 0;
}


void Token::append(int c)
{
//assert(_len <= kMaxStringLiteralLength);
  _lexeme[_len++] = c;
  _lexeme[_len]   = '\0';
}


