Fix memory leak related to lexer skipping whitespace.

This commit is contained in:
Deukhoofd 2019-05-24 19:18:03 +02:00
parent 4a034bc051
commit b2fa857c9b
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 10 additions and 8 deletions

View File

@ -6,7 +6,7 @@
#include "Lexer.hpp" #include "Lexer.hpp"
Lexer::Lexer(string scriptString, class Script* script) { Lexer::Lexer(string scriptString, class Script* script) {
this -> ScriptString = std::move(scriptString); this -> _scriptString = std::move(scriptString);
this -> ScriptData = script; this -> ScriptData = script;
this -> Position = 0; this -> Position = 0;
} }
@ -18,6 +18,8 @@ vector<IToken*> Lexer::Lex() {
auto nextKind = next -> GetKind(); auto nextKind = next -> GetKind();
if (nextKind != TokenKind::WhiteSpace) if (nextKind != TokenKind::WhiteSpace)
tokens.push_back(next); tokens.push_back(next);
else
delete next;
if (nextKind == TokenKind::EndOfFile) if (nextKind == TokenKind::EndOfFile)
break; break;
} }
@ -25,9 +27,9 @@ vector<IToken*> Lexer::Lex() {
} }
char Lexer::Peek(){ char Lexer::Peek(){
if (Lexer::Position > this -> ScriptString.length()) if (Lexer::Position > this -> _scriptString.length())
return '\0'; return '\0';
return this -> ScriptString[Lexer::Position]; return this -> _scriptString[Lexer::Position];
} }
char Lexer::Next(){ char Lexer::Next(){
@ -164,7 +166,7 @@ IToken * Lexer::LexIdentifierOrKeyword() {
} }
} }
string s = this -> ScriptString.substr(start, end - start + 1); string s = this -> _scriptString.substr(start, end - start + 1);
switch (const_hash(s.c_str())){ switch (const_hash(s.c_str())){
case const_hash("and"): return new SimpleToken(TokenKind::AndKeyword, start, 3); case const_hash("and"): return new SimpleToken(TokenKind::AndKeyword, start, 3);
case const_hash("break"): return new SimpleToken(TokenKind::BreakKeyword, start, 5); case const_hash("break"): return new SimpleToken(TokenKind::BreakKeyword, start, 5);
@ -222,7 +224,7 @@ IToken* Lexer::LexString(char c){
return new SimpleToken(TokenKind::BadToken, start, end -start + 1); return new SimpleToken(TokenKind::BadToken, start, end -start + 1);
} }
string s = this -> ScriptString.substr(start + 1, end - start); string s = this -> _scriptString.substr(start + 1, end - start);
stringstream stream; stringstream stream;
for (int i = 0; i < s.size(); i++){ for (int i = 0; i < s.size(); i++){
c = s[i]; c = s[i];

View File

@ -8,7 +8,7 @@
using namespace std; using namespace std;
class Lexer { class Lexer {
string ScriptString; string _scriptString;
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
public: public:
#endif #endif
@ -17,8 +17,8 @@ public:
char Next(); char Next();
IToken* LexNext(char c); IToken* LexNext(char c);
IToken* LexNumber(char c); IToken* LexNumber(char c);
IToken *LexIdentifierOrKeyword(); IToken* LexIdentifierOrKeyword();
IToken *LexString(char c); IToken* LexString(char c);
public: public:
Script* ScriptData; Script* ScriptData;