More complex type parsing

This commit is contained in:
Rafał Grodziński
2025-07-08 17:31:56 +09:00
parent 18dd7d05d4
commit 9e7747dcbc
10 changed files with 128 additions and 62 deletions

View File

@@ -13,9 +13,9 @@ vector<shared_ptr<Token>> Lexer::getTokens() {
currentLine = 0;
currentColumn = 0;
tokens.clear();
errors.clear();
vector<shared_ptr<Token>> tokens;
shared_ptr<Token> token;
do {
token = nextToken();
@@ -251,15 +251,7 @@ shared_ptr<Token> Lexer::nextToken() {
return token;
// type
token = match(TokenKind::TYPE, "bool", true);
if (token != nullptr)
return token;
token = match(TokenKind::TYPE, "sint32", true);
if (token != nullptr)
return token;
token = match(TokenKind::TYPE, "real32", true);
token = matchType();
if (token != nullptr)
return token;
@@ -428,6 +420,24 @@ shared_ptr<Token> Lexer::matchIdentifier() {
return token;
}
shared_ptr<Token> Lexer::matchType() {
int nextIndex = currentIndex;
if (tokens.empty() || !tokens.back()->isOfKind({TokenKind::IDENTIFIER, TokenKind::LESS, TokenKind::RIGHT_ARROW}))
return nullptr;
while (nextIndex < source.length() && isIdentifier(nextIndex))
nextIndex++;
if (nextIndex == currentIndex || !isSeparator(nextIndex))
return nullptr;
string lexme = source.substr(currentIndex, nextIndex - currentIndex);
shared_ptr<Token> token = make_shared<Token>(TokenKind::TYPE, lexme, currentLine, currentColumn);
advanceWithToken(token);
return token;
}
shared_ptr<Token> Lexer::matchEnd() {
if (currentIndex >= source.length())
return make_shared<Token>(TokenKind::END, "", currentLine, currentColumn);

View File

@@ -15,6 +15,7 @@ private:
int currentIndex;
int currentLine;
int currentColumn;
vector<shared_ptr<Token>> tokens;
vector<shared_ptr<Error>> errors;
shared_ptr<Token> nextToken();
@@ -24,6 +25,7 @@ private:
shared_ptr<Token> matchIntegerBin();
shared_ptr<Token> matchIntegerChar();
shared_ptr<Token> matchReal();
shared_ptr<Token> matchType();
shared_ptr<Token> matchIdentifier();
shared_ptr<Token> matchEnd();