-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
166 lines (153 loc) · 4.49 KB
/
lexer.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "lexer.hpp"
namespace Dima
{
Token::Token(enum TokenType type){_mType = type;}
Token::Token(){;}
void Token::setText(std::string newText){_mText = newText;}
void Token::setType(enum TokenType newType){_mType = newType;}
void Token::textPush(char symbol){_mText.push_back(symbol);}
void Token::textErase(){_mText.erase();}
const enum TokenType Token::getType() const{return _mType;}
const std::string Token::getText() const{return _mText;}
Tokenizer::Tokenizer(const std::string& Line) //строка разбирается в конструкторе
{
for(char cCh : Line) //cCh = current character
{
if(_cToken.getType() == SPECIAL_SEQUENCE)
{
switch(cCh)
{
case 'd': //дизъюнкция
_cToken.textPush('d');
_cToken.setType(DISJOINT);
break;
case 'c': //конъюнкция
_cToken.textPush('c');
_cToken.setType(CONJUNCT);
break;
case 'n': //отрицание
_cToken.textPush('n');
_cToken.setType(RESISTANCE);
break;
default: //ошибка по умолчанию
throw std::runtime_error("uknkown special symbol!");
break;
}
endToken();
continue;
}
switch(cCh)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if(_cToken.getType() == WHITESPACE || _cToken.getType() == INTEGER_LITERAL)
{
_cToken.setType(INTEGER_LITERAL);
_cToken.textPush(cCh);
}else{throw std::runtime_error("0-9 is not in integer literal");}
break;
case '\\': //выловлено начало спецсимвола
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(SPECIAL_SEQUENCE);
break;
case '(':
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(LBRAC);
_cToken.textPush('(');
endToken();
break;
case ')':
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(RBRAC);
_cToken.textPush(')');
endToken();
break;
case '+':
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(PLUS);
_cToken.textPush('+');
endToken();
break;
case '*':
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(MULTIPLY);
_cToken.textPush('*');
endToken();
break;
case '^':
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(POWER);
_cToken.textPush('^');
endToken();
break;
case '=':
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(EQUAL);
_cToken.textPush('=');
endToken();
break;
case '>':
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(GREATER);
_cToken.textPush('>');
endToken();
break;
case 'x':
if(!_cToken.getText().empty() && _cToken.getText() != "-"){endToken();}
else if(_cToken.getText() == "-")
{
_cToken.setType(INTEGER_LITERAL);
_cToken.textPush('1');
endToken();
}
_cToken.setType(IDENTIFIER);
_cToken.textPush('x');
endToken();
_cToken.setType(IDENTIFIER);
break;
case '-': //x-c -> [x] [+] [-c]
if(_cToken.getType() != WHITESPACE)
{
if(!_cToken.getText().empty()){endToken();}
_cToken.setType(PLUS);
_cToken.textPush('+');
endToken();
}
_cToken.setType(INTEGER_LITERAL);
_cToken.textPush('-');
break;
case '\n':
case '\t':
case ' ':
case '\r':
case '\v':
if(!_cToken.getText().empty()){endToken();}
break;
default:
throw std::runtime_error("unknown symbol!");
break;
}
}
if(_cToken.getType() != IDENTIFIER){endToken();}
}
std::vector<Token> Tokenizer::get_tokens(){return _tokens;}
void Tokenizer::endToken()
{
if(_cToken.getType() != WHITESPACE){_tokens.push_back(_cToken);}
_cToken.setType(WHITESPACE);
_cToken.textErase();
}
void Tokenizer::debugPrint()
{
for(Token cTok : _tokens){std::cout << "Type: " << sTokenTypeStrings[cTok.getType()] << " Name: " << cTok.getText()
<< std::endl;}
}
}