From 178d158932c00809036e43c9256365f43b22bc89 Mon Sep 17 00:00:00 2001 From: Ahmed Harmouche Date: Wed, 7 Aug 2024 23:37:24 +0200 Subject: [PATCH] Source location for: vardecl, function decl, function args --- include/AST/AST.h | 13 ++++++++----- lib/AST/PrinterASTVisitor.cpp | 11 ++++++++--- lib/Parser/Parser.cpp | 25 +++++++++++++++---------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/include/AST/AST.h b/include/AST/AST.h index 3c5d02d..6a34ee7 100644 --- a/include/AST/AST.h +++ b/include/AST/AST.h @@ -363,9 +363,9 @@ class Declaration : public ExternalDeclaration, public Statement { class VariableDeclaration : public Declaration { public: - VariableDeclaration(std::unique_ptr type, const std::string &name, + VariableDeclaration(SourceLocation location, std::unique_ptr type, const std::string &name, std::unique_ptr initializerExpr) - : type(std::move(type)), identifierName(name), + : ASTNode(location), type(std::move(type)), identifierName(name), initializerExpr(std::move(initializerExpr)) {} const std::string &getIdentifierName() const { return identifierName; } @@ -400,14 +400,17 @@ class VariableDeclarationList : public Declaration { std::vector> declarations; }; -class ParameterDeclaration { +class ParameterDeclaration : public ASTNode { public: - ParameterDeclaration(const std::string &name, std::unique_ptr type) - : name(name), type(std::move(type)) {} + ParameterDeclaration(SourceLocation location, const std::string &name, std::unique_ptr type) + : ASTNode(location), name(name), type(std::move(type)) {} const std::string &getName() { return name; } Type *getType() { return type.get(); } + void accept(ASTVisitor *visitor) override { + + } private: std::string name; diff --git a/lib/AST/PrinterASTVisitor.cpp b/lib/AST/PrinterASTVisitor.cpp index 3ab68de..27f1a8f 100644 --- a/lib/AST/PrinterASTVisitor.cpp +++ b/lib/AST/PrinterASTVisitor.cpp @@ -54,7 +54,7 @@ void PrinterASTVisitor::visit(VariableDeclarationList *varDeclList) { void PrinterASTVisitor::visit(VariableDeclaration *varDecl) { auto typeName = varDecl->getType() != nullptr ? varDecl->getType()->toString() : ""; - print("|-VariableDeclaration: name=" + varDecl->getIdentifierName() + ((typeName != "") ? (", type=" + typeName) : "")); + print("|-VariableDeclaration: name=" + varDecl->getIdentifierName() + ((typeName != "") ? (", type=" + typeName) : "") + loc(varDecl->getSourceLocation())); indent(); if (auto exp = varDecl->getInitialzerExpression()) { @@ -276,7 +276,7 @@ void PrinterASTVisitor::visit(FunctionDeclaration *funcDecl) { indent(); for (auto &arg : funcDecl->getParams()) { - print("name=" + arg->getName() + ", type=" + arg->getType()->toString()); + print("name=" + arg->getName() + ", type=" + arg->getType()->toString() + loc(arg->getSourceLocation())); } resetIndent(); @@ -299,7 +299,12 @@ void PrinterASTVisitor::visit(CaseLabel *caseLabel) { std::string PrinterASTVisitor::loc(const SourceLocation &sourceLoc) { std::stringstream ssLoc; - ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << "]->" << "[line: " << sourceLoc.endLine << ", col:" << sourceLoc.endCol << "]"; + if (sourceLoc.startLine == sourceLoc.endLine) { + ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << " -> " << "col:" << sourceLoc.endCol << "]"; + } else { + ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << "] -> " << "[line: " << sourceLoc.endLine << ", col:" << sourceLoc.endCol << "]"; + } + return ssLoc.str(); } diff --git a/lib/Parser/Parser.cpp b/lib/Parser/Parser.cpp index 47b7e1d..7f3ebfb 100644 --- a/lib/Parser/Parser.cpp +++ b/lib/Parser/Parser.cpp @@ -92,24 +92,28 @@ std::unique_ptr Parser::parseFunctionDeclaration() { std::unique_ptr Parser::parseDeclaration() { if (auto type = parseType()) { + SourceLocation startLoc = curToken->getSourceLocation(); advanceToken(); // Only type, no variable name if (curToken->is(TokenKind::semiColon)) { advanceToken(); - return std::make_unique(std::move(type), + return std::make_unique(startLoc, std::move(type), std::string(), nullptr); } const std::string &name = curToken->getIdentifierName(); - + SourceLocation endLoc = curToken->getSourceLocation(); advanceToken(); // Type and variable name, no initializer expression if (curToken->is(TokenKind::semiColon)) { advanceToken(); - return std::make_unique(std::move(type), name, - nullptr); + return std::make_unique( + SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.startLine, endLoc.endCol), + std::move(type), + name, + nullptr); } else if (curToken->is(TokenKind::assign)) { // Initializer expression advanceToken(); @@ -124,7 +128,7 @@ std::unique_ptr Parser::parseDeclaration() { return declarations; } else if (curToken->is(TokenKind::semiColon)) { advanceToken(); - return std::make_unique(std::move(type), name, + return std::make_unique(SourceLocation(), std::move(type), name, std::move(exp)); } else { reportError(ParserErrorKind::UnexpectedToken, "Parser error: unexpected token '" + curToken->getRawData() + "'"); @@ -147,13 +151,12 @@ std::unique_ptr Parser::parseVariableDeclarationList( std::unique_ptr type, const std::string &identifierName, std::unique_ptr initializerExpression) { std::vector> declarations; - declarations.push_back(std::make_unique( + declarations.push_back(std::make_unique(SourceLocation(), nullptr, identifierName, std::move(initializerExpression))); do { advanceToken(); if (!curToken->is(TokenKind::Identifier)) { - reportError(ParserErrorKind::ExpectedToken, "Parser error: expected identifier " + curToken->getRawData()); } @@ -170,10 +173,10 @@ std::unique_ptr Parser::parseVariableDeclarationList( return nullptr; } - declarations.push_back(std::make_unique( + declarations.push_back(std::make_unique(SourceLocation(), nullptr, varName, std::move(exp))); } else { - declarations.push_back(std::make_unique( + declarations.push_back(std::make_unique(SourceLocation(), nullptr, varName, nullptr)); } } while (curToken->is(TokenKind::comma)); @@ -679,11 +682,13 @@ Parser::parseFunctionParameters() { advanceToken(); if (auto type = parseType()) { + SourceLocation startLoc = curToken->getSourceLocation(); advanceToken(); if (curToken->is(TokenKind::Identifier)) { + SourceLocation endLoc = curToken->getSourceLocation(); auto param = std::make_unique( - curToken->getIdentifierName(), std::move(type)); + SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.startLine, endLoc.endCol), curToken->getIdentifierName(), std::move(type)); advanceToken(); params.push_back(std::move(param)); }