Lexer daha anlaşılır hale getirildi
This commit is contained in:
parent
8adeabaff1
commit
0f33740a07
136
core/Lexer.cpp
136
core/Lexer.cpp
|
|
@ -20,12 +20,35 @@ public:
|
||||||
int size = 0;
|
int size = 0;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
std::vector<int> offsetMap;
|
std::vector<int> offsetMap;
|
||||||
void beginPosition()
|
void beginPosition();
|
||||||
{
|
int getLastPosition();
|
||||||
|
void acceptPosition();
|
||||||
|
void setLastPosition(int);
|
||||||
|
bool isEnd();
|
||||||
|
void rejectPosition();
|
||||||
|
int * positionRange();
|
||||||
|
std::string getPositionRange();
|
||||||
|
bool include(std::string,bool);
|
||||||
|
int getOffset();
|
||||||
|
int setOffset(int);
|
||||||
|
char getchar(int);
|
||||||
|
char getchar();
|
||||||
|
void nextChar();
|
||||||
|
void toChar(int);
|
||||||
|
void setText(std::string);
|
||||||
|
void skipWhiteSpace();
|
||||||
|
bool isNumeric();
|
||||||
|
INumber readNumeric();
|
||||||
|
};
|
||||||
|
|
||||||
|
void Lexer::beginPosition()
|
||||||
|
{
|
||||||
this->offsetMap.push_back(this->getLastPosition());
|
this->offsetMap.push_back(this->getLastPosition());
|
||||||
}
|
}
|
||||||
int getLastPosition()
|
|
||||||
{
|
|
||||||
|
int Lexer::getLastPosition()
|
||||||
|
{
|
||||||
if(this->offsetMap.size() == 0)
|
if(this->offsetMap.size() == 0)
|
||||||
{
|
{
|
||||||
return this->offset;
|
return this->offset;
|
||||||
|
|
@ -34,14 +57,14 @@ public:
|
||||||
{
|
{
|
||||||
return this->offsetMap[this->offsetMap.size() - 1];
|
return this->offsetMap[this->offsetMap.size() - 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void acceptPosition()
|
void Lexer::acceptPosition()
|
||||||
{
|
{
|
||||||
int T = this->offsetMap[this->offsetMap.size() - 1];
|
int T = this->offsetMap[this->offsetMap.size() - 1];
|
||||||
this->setLastPosition(T);
|
this->setLastPosition(T);
|
||||||
}
|
}
|
||||||
void setLastPosition(int n)
|
void Lexer::setLastPosition(int n)
|
||||||
{
|
{
|
||||||
if(this->offsetMap.size() == 0)
|
if(this->offsetMap.size() == 0)
|
||||||
{
|
{
|
||||||
this->offset = n;
|
this->offset = n;
|
||||||
|
|
@ -50,18 +73,18 @@ public:
|
||||||
{
|
{
|
||||||
this->offsetMap[this->offsetMap.size() - 1] = n;
|
this->offsetMap[this->offsetMap.size() - 1] = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool isEnd()
|
bool Lexer::isEnd()
|
||||||
{
|
{
|
||||||
bool result = this->size <= this->getOffset();
|
bool result = this->size <= this->getOffset();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
void rejectPosition()
|
void Lexer::rejectPosition()
|
||||||
{
|
{
|
||||||
this->offsetMap.pop_back();
|
this->offsetMap.pop_back();
|
||||||
}
|
}
|
||||||
int * positionRange()
|
int * Lexer::positionRange()
|
||||||
{
|
{
|
||||||
int len = this->offsetMap.size();
|
int len = this->offsetMap.size();
|
||||||
if(len == 0)
|
if(len == 0)
|
||||||
{
|
{
|
||||||
|
|
@ -79,9 +102,9 @@ public:
|
||||||
this->offsetMap[len - 1]
|
this->offsetMap[len - 1]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string getPositionRange()
|
std::string Lexer::getPositionRange()
|
||||||
{
|
{
|
||||||
int *A = this->positionRange();
|
int *A = this->positionRange();
|
||||||
std::string mem;
|
std::string mem;
|
||||||
|
|
||||||
|
|
@ -90,10 +113,10 @@ public:
|
||||||
mem.push_back(this->input.at(i));
|
mem.push_back(this->input.at(i));
|
||||||
}
|
}
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool include(std::string word,bool accept = true)
|
bool Lexer::include(std::string word,bool accept = true)
|
||||||
{
|
{
|
||||||
this->beginPosition();
|
this->beginPosition();
|
||||||
for (int i = 0; i < word.size(); i++)
|
for (int i = 0; i < word.size(); i++)
|
||||||
{
|
{
|
||||||
|
|
@ -123,18 +146,18 @@ public:
|
||||||
this->rejectPosition();
|
this->rejectPosition();
|
||||||
};
|
};
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
int getOffset()
|
int Lexer::getOffset()
|
||||||
{
|
{
|
||||||
return this->getLastPosition();
|
return this->getLastPosition();
|
||||||
}
|
}
|
||||||
int setOffset(int n)
|
int Lexer::setOffset(int n)
|
||||||
{
|
{
|
||||||
this->setLastPosition(n);
|
this->setLastPosition(n);
|
||||||
return this->getLastPosition();
|
return this->getLastPosition();
|
||||||
}
|
}
|
||||||
char getchar(int additionalOffset = 0)
|
char Lexer::getchar(int additionalOffset)
|
||||||
{
|
{
|
||||||
int target = this->getOffset() + additionalOffset;
|
int target = this->getOffset() + additionalOffset;
|
||||||
if(this->size - 1 < target)
|
if(this->size - 1 < target)
|
||||||
{
|
{
|
||||||
|
|
@ -143,29 +166,40 @@ public:
|
||||||
}else{
|
}else{
|
||||||
return this->input.at(target);
|
return this->input.at(target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void nextChar()
|
char Lexer::getchar()
|
||||||
|
{
|
||||||
|
int target = this->getOffset();
|
||||||
|
if(this->size - 1 < target)
|
||||||
{
|
{
|
||||||
|
std::cerr << "Hata yanlış erişim\n";
|
||||||
|
return '\0';
|
||||||
|
}else{
|
||||||
|
return this->input.at(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Lexer::nextChar()
|
||||||
|
{
|
||||||
if(this->isEnd() == true)
|
if(this->isEnd() == true)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
this->setOffset(this->getOffset() + 1);
|
this->setOffset(this->getOffset() + 1);
|
||||||
}
|
}
|
||||||
void toChar(int n)
|
void Lexer::toChar(int n)
|
||||||
{
|
{
|
||||||
if(this->isEnd() == true)
|
if(this->isEnd() == true)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
this->setOffset(this->getOffset() + n);
|
this->setOffset(this->getOffset() + n);
|
||||||
}
|
}
|
||||||
void setText(std::string input) {
|
void Lexer::setText(std::string input) {
|
||||||
this->input = input;
|
this->input = input;
|
||||||
this->size = input.length();
|
this->size = input.length();
|
||||||
}
|
}
|
||||||
void skipWhiteSpace()
|
void Lexer::skipWhiteSpace()
|
||||||
{
|
{
|
||||||
while(this->isEnd() == false)
|
while(this->isEnd() == false)
|
||||||
{
|
{
|
||||||
switch(this->getchar())
|
switch(this->getchar())
|
||||||
|
|
@ -183,10 +217,10 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isNumeric()
|
bool Lexer::isNumeric()
|
||||||
{
|
{
|
||||||
char c = this->getchar();
|
char c = this->getchar();
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
|
|
@ -206,9 +240,10 @@ public:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
INumber readNumeric(){
|
INumber Lexer::readNumeric()
|
||||||
|
{
|
||||||
INumber numberToken;
|
INumber numberToken;
|
||||||
numberToken.start = this->getLastPosition();
|
numberToken.start = this->getLastPosition();
|
||||||
if(this->getchar() == '-')
|
if(this->getchar() == '-')
|
||||||
|
|
@ -386,6 +421,5 @@ public:
|
||||||
}
|
}
|
||||||
numberToken.end = this->getLastPosition();
|
numberToken.end = this->getLastPosition();
|
||||||
return numberToken;
|
return numberToken;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue