String literals
This commit is contained in:
@@ -250,6 +250,10 @@ shared_ptr<Token> Lexer::nextToken() {
|
||||
if (token != nullptr)
|
||||
return token;
|
||||
|
||||
token = matchString();
|
||||
if (token != nullptr)
|
||||
return token;
|
||||
|
||||
// type
|
||||
token = matchType();
|
||||
if (token != nullptr)
|
||||
@@ -405,6 +409,27 @@ shared_ptr<Token> Lexer::matchReal() {
|
||||
return token;
|
||||
}
|
||||
|
||||
shared_ptr<Token> Lexer::matchString() {
|
||||
int nextIndex = currentIndex;
|
||||
|
||||
if (currentIndex >= source.size() || source.at(nextIndex) != '\"')
|
||||
return nullptr;
|
||||
|
||||
bool isClosing = false;
|
||||
do {
|
||||
nextIndex++;
|
||||
isClosing = source.at(nextIndex) == '\"' && source.at(nextIndex - 1) != '\\';
|
||||
} while (nextIndex < source.length() && !isClosing);
|
||||
|
||||
if (!isClosing)
|
||||
return nullptr;
|
||||
|
||||
string lexme = source.substr(currentIndex, nextIndex - currentIndex + 1);
|
||||
shared_ptr<Token> token = make_shared<Token>(TokenKind::STRING, lexme, currentLine, currentColumn);
|
||||
advanceWithToken(token);
|
||||
return token;
|
||||
}
|
||||
|
||||
shared_ptr<Token> Lexer::matchIdentifier() {
|
||||
int nextIndex = currentIndex;
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ private:
|
||||
shared_ptr<Token> matchIntegerBin();
|
||||
shared_ptr<Token> matchIntegerChar();
|
||||
shared_ptr<Token> matchReal();
|
||||
shared_ptr<Token> matchString();
|
||||
shared_ptr<Token> matchType();
|
||||
shared_ptr<Token> matchIdentifier();
|
||||
shared_ptr<Token> matchEnd();
|
||||
|
||||
@@ -41,7 +41,8 @@ vector<TokenKind> Token::tokensLiteral = {
|
||||
TokenKind::INTEGER_HEX,
|
||||
TokenKind::INTEGER_BIN,
|
||||
TokenKind::INTEGER_CHAR,
|
||||
TokenKind::REAL
|
||||
TokenKind::REAL,
|
||||
TokenKind::STRING
|
||||
};
|
||||
|
||||
Token::Token(TokenKind kind, string lexme, int line, int column):
|
||||
|
||||
@@ -41,6 +41,7 @@ enum class TokenKind {
|
||||
INTEGER_BIN,
|
||||
INTEGER_CHAR,
|
||||
REAL,
|
||||
STRING,
|
||||
IDENTIFIER,
|
||||
TYPE,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user