Lexer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <ctype.h>
  2. #include <iostream>
  3. #include "Lexer.h"
  4. using namespace std;
  5. char Lexer::nextChar() const
  6. {
  7. if (pos < str.size())
  8. return str[pos];
  9. return 0;
  10. }
  11. char Lexer::getChar()
  12. {
  13. if (pos < str.size())
  14. return str[pos++];
  15. return 0;
  16. }
  17. void Lexer::skipChar()
  18. {
  19. pos++;
  20. }
  21. void Lexer::skipWhiteSpace()
  22. {
  23. if (pos < str.size() && isblank(str[pos]))
  24. pos++;
  25. }
  26. void Lexer::nextToken()
  27. {
  28. token.clear();
  29. type = nothing;
  30. skipWhiteSpace();
  31. char f = getChar();
  32. if (f != 0)
  33. {
  34. token += f;
  35. if (isdigit(f))
  36. {
  37. // token is number
  38. type = integer; // assume "integer"
  39. // number
  40. f = nextChar();
  41. while (isdigit(f) || f == '.')
  42. {
  43. if (f == '.')
  44. {
  45. type = floatingpoint; // -> floating point
  46. }
  47. token += f;
  48. skipChar();
  49. f = nextChar();
  50. }
  51. }
  52. else if (f == '"')
  53. {
  54. // string literal starting with '"'
  55. char delimiter = f;
  56. token = "\"";
  57. type = stringliteral;
  58. f = nextChar();
  59. while (f != delimiter)
  60. {
  61. if (f == 0)
  62. throw Exception("Parsing", "string literal not complete");
  63. token += f;
  64. skipChar();
  65. f = nextChar();
  66. }
  67. token += '"';
  68. skipChar();
  69. }
  70. else if (isalpha(f))
  71. {
  72. // cout << "id"<<endl;
  73. // identifier
  74. type = identifier;
  75. f = nextChar();
  76. // cout << (int)f << " " << f << endl;
  77. while (isalnum(f))
  78. {
  79. token += f;
  80. skipChar();
  81. f = nextChar();
  82. }
  83. }
  84. else
  85. {
  86. type = singlecharacter;
  87. // single character
  88. }
  89. }
  90. skipWhiteSpace();
  91. }
  92. long int Lexer::getInt()
  93. {
  94. // cout << "type: " << type << " token:" << token << endl;
  95. if (type != integer)
  96. throw Exception("getInt", "integer value expected");
  97. int res = stol(token);
  98. nextToken();
  99. return res;
  100. }
  101. double Lexer::getDouble()
  102. {
  103. if (type != floatingpoint)
  104. throw Exception("getDouble", "floating point value expected");
  105. double res = stod(token);
  106. nextToken();
  107. return res;
  108. }
  109. std::string Lexer::getString()
  110. {
  111. if (type != stringliteral)
  112. throw Exception("getString", "string expected");
  113. string res = token.substr(1, token.length() - 2);
  114. nextToken();
  115. return res;
  116. }
  117. std::string Lexer::getWord()
  118. {
  119. if (type != identifier)
  120. throw Exception("getString", "identifier or keyword expected");
  121. string res = token;
  122. nextToken();
  123. return res;
  124. }