Lexer daha anlaşılır hale getirildi

This commit is contained in:
abdussamedulutas 2025-12-28 14:35:18 +03:00
parent 8adeabaff1
commit 0f33740a07
1 changed files with 347 additions and 313 deletions

View File

@ -20,11 +20,34 @@ 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)
{ {
@ -35,12 +58,12 @@ 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)
{ {
@ -51,16 +74,16 @@ 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)
@ -80,7 +103,7 @@ public:
}; };
} }
} }
std::string getPositionRange() std::string Lexer::getPositionRange()
{ {
int *A = this->positionRange(); int *A = this->positionRange();
std::string mem; std::string mem;
@ -92,7 +115,7 @@ public:
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++)
@ -124,16 +147,16 @@ public:
}; };
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)
@ -144,7 +167,18 @@ public:
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)
{ {
@ -152,7 +186,7 @@ public:
}; };
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)
{ {
@ -160,11 +194,11 @@ public:
}; };
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)
{ {
@ -185,7 +219,7 @@ public:
} }
} }
bool isNumeric() bool Lexer::isNumeric()
{ {
char c = this->getchar(); char c = this->getchar();
switch (c) switch (c)
@ -208,7 +242,8 @@ public:
} }
} }
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