1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
/**
* @file Scanner.h
*
* @author <a href="mailto:mellmann@informatik.hu-berlin.de">Heinrich Mellmann</a>
* Implementation of class Scanner
*/

#include "Scanner.h"

bool Scanner::endOfFile()
{
  return nextChar == EOF;
}//end eof


void Scanner::getNextToken()
{
  skipSpace();
  bufferLength = 0;

  if(nextChar == EOF) 
  {
    buffer[bufferLength++] = nextChar;
    buffer[bufferLength] = '\0';
    return;
  }//end if

  if(!isTokenChar(nextChar))
  {
    buffer[bufferLength++] = nextChar;
    buffer[bufferLength] = '\0';
    getNextChar();
    return;
  }//end if

  while(isTokenChar(nextChar))
  {
    buffer[bufferLength++] = nextChar;
    buffer[bufferLength] = '\0';
    getNextChar();
  }//end while
}//end getNextToken

/*
 * Return true if c is a token character, 
 * i.e., 0-1, a-z, A-Z, or one of the character '.','-' or '_'
 */
bool Scanner::isTokenChar(char c)
{
  return isalnum(c) || c == '.' || c == '-' || c == '_';
}//end isTolenChar


bool Scanner::isToken(std::string token)
{
  return token == buffer;
}//end isToken

bool Scanner::eof()
{
  return nextChar == EOF;
}//end eof

void Scanner::skipSpace()
{
  while(isspace(nextChar) || nextChar == '#')
  {
    if(nextChar == '\n')
      lineNumber++;

    if(nextChar == '#') 
      skipLine();
    else
      getNextChar();
  }
}//end skipSpace


void Scanner::skipLine()
{
  while(nextChar != EOF && nextChar != '\n')
  {
    getNextChar();    
  }//end while
  
  getNextChar(); 
  lineNumber++;
}//end skipLine


void Scanner::getNextChar()
{
  if(inputStream.good())
    nextChar = (char) inputStream.get();
  else
    nextChar = EOF;
}//end getNextChar


int Scanner::getLineNumber()
{
  return lineNumber;
}//end getLineNumber