-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenizer.cpp
More file actions
121 lines (100 loc) · 2.53 KB
/
Tokenizer.cpp
File metadata and controls
121 lines (100 loc) · 2.53 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Tokenizer.cpp
*
* Created on: 29.12.2013
* Author: atamas
*/
#include "Tokenizer.h"
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <limits>
#include "UnEqPredicat.h"
using std::vector;
using std::any_of;
Tokenizer::Tokenizer(std::ifstream & inf):
_inf(inf){
_lineNo = 1;
if(!this->eof()){
_nextToken = readTokenFromFile();
}
}
bool Tokenizer::contents(vector<char> const & arrOfChars, char const charWeAreLookingFor) const{
return any_of(arrOfChars.begin(), arrOfChars.end(), UnEqPredicat<char>(charWeAreLookingFor));
}
bool Tokenizer::eof(){
char nextChar;
if(_inf.get(nextChar)){
_inf.seekg(-1, std::ios_base::cur);
return false;
}else{
return true;
}
}
void Tokenizer::operator >>(Token & token){
token = _nextToken;
if(!this->eof()){
_nextToken = readTokenFromFile();
}
}
Token Tokenizer::readTokenFromFile(){
std::vector<char> const static spaceSeparators = {' ', '\t', '\v'};
std::vector<char> const static binOpFirstChar = {'>', '<', '=', '!'};
std::vector<char> const static allSeparators = {'+', '-', '*', '/', '=', '(', ')', ':', ',', '!', '<', '>', '#', ' ', '\t', '\v', '\n'};
char nextChar = 0;
_inf >> std::noskipws >> nextChar;
while( contents(spaceSeparators,nextChar) && _inf.get(nextChar) ){
}
if(nextChar == '#'){
_inf.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
++_lineNo;
return Token(std::string(1,'\n'), _lineNo-1);
}
if ( contents(binOpFirstChar,nextChar) ){
std::string op;
op.push_back(nextChar);
_inf >> nextChar;
if( nextChar == '=' ){
op.push_back(nextChar);
} else{
_inf.seekg(-1, std::ios_base::cur );
}
return Token(op, _lineNo);
}
if ( contents(allSeparators,nextChar) ){
if ( nextChar == '\n' ){
++_lineNo;
return Token(std::string(1,nextChar), _lineNo-1);
}
return Token(std::string(1,nextChar), _lineNo);
}
std::string token;
token.push_back(nextChar);
while( _inf.get(nextChar) && !contents(allSeparators,nextChar) ){
token.push_back(nextChar);
}
_inf.seekg(-1, std::ios_base::cur);
return Token(token, _lineNo);
}
void Tokenizer::assertNextToken(Token::Type typeToCompare){
Token token;
*this >> token;
token.assertTokenType(typeToCompare);
}
Token Tokenizer::getNextTokenVal(){
return _nextToken;
}
void Tokenizer::skipToken(){
Token token;
*this>>token;
}
void Tokenizer::assertNextToken(std::string strToCompare){
Token token;
*this >> token;
token.assertTokenType(strToCompare);
}
Tokenizer::~Tokenizer() {
_inf.close();
}