123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <ctype.h>
- #include <iostream>
- #include "Lexer.h"
- using namespace std;
- char Lexer::nextChar() const
- {
- if (pos < str.size())
- return str[pos];
- return 0;
- }
- char Lexer::getChar()
- {
- if (pos < str.size())
- return str[pos++];
- return 0;
- }
- void Lexer::skipChar()
- {
- pos++;
- }
- void Lexer::skipWhiteSpace()
- {
- if (pos < str.size() && isblank(str[pos]))
- pos++;
- }
- void Lexer::nextToken()
- {
- token.clear();
- type = nothing;
- skipWhiteSpace();
- char f = getChar();
- if (f != 0)
- {
- token += f;
- if (isdigit(f))
- {
- // token is number
- type = integer; // assume "integer"
- // number
- f = nextChar();
- while (isdigit(f) || f == '.')
- {
- if (f == '.')
- {
- type = floatingpoint; // -> floating point
- }
- token += f;
- skipChar();
- f = nextChar();
- }
- }
- else if (f == '"')
- {
- // string literal starting with '"'
- char delimiter = f;
- token = "\"";
- type = stringliteral;
- f = nextChar();
- while (f != delimiter)
- {
- if (f == 0)
- throw Exception("Parsing", "string literal not complete");
- token += f;
- skipChar();
- f = nextChar();
- }
- token += '"';
- skipChar();
- }
- else if (isalpha(f))
- {
- // cout << "id"<<endl;
- // identifier
- type = identifier;
- f = nextChar();
- // cout << (int)f << " " << f << endl;
- while (isalnum(f))
- {
- token += f;
- skipChar();
- f = nextChar();
- }
- }
- else
- {
- type = singlecharacter;
- // single character
- }
- }
- skipWhiteSpace();
- }
- long int Lexer::getInt()
- {
- // cout << "type: " << type << " token:" << token << endl;
- if (type != integer)
- throw Exception("getInt", "integer value expected");
- int res = stol(token);
- nextToken();
- return res;
- }
- double Lexer::getDouble()
- {
- if (type != floatingpoint)
- throw Exception("getDouble", "floating point value expected");
- double res = stod(token);
- nextToken();
- return res;
- }
- std::string Lexer::getString()
- {
- if (type != stringliteral)
- throw Exception("getString", "string expected");
- string res = token.substr(1, token.length() - 2);
- nextToken();
- return res;
- }
- std::string Lexer::getWord()
- {
- if (type != identifier)
- throw Exception("getString", "identifier or keyword expected");
- string res = token;
- nextToken();
- return res;
- }
|