#pragma once #include #include #include "Lexer.h" #include "Expression.h" #include "Statement.h" #include "TypeWrapper.h" #include "helperFunctions/ShortHands.h" #include "ErrorReporter.h" class Parser { private: const std::vector tokens; int current = 0; int functionDepth = 0; // Track nesting level of functions ErrorReporter* errorReporter = nullptr; public: explicit Parser(std::vector tokens) : tokens(std::move(tokens)){}; std::vector parse(); void setErrorReporter(ErrorReporter* reporter) { errorReporter = reporter; } private: sptr(Expr) expression(); sptr(Expr) logical_or(); sptr(Expr) ternary(); sptr(Expr) logical_and(); sptr(Expr) bitwise_or(); sptr(Expr) bitwise_xor(); sptr(Expr) bitwise_and(); sptr(Expr) shift(); sptr(Expr) equality(); sptr(Expr) comparison(); sptr(Expr) term(); sptr(Expr) factor(); sptr(Expr) unary(); sptr(Expr) primary(); bool match(const std::vector& types); bool check(TokenType type); bool isAtEnd(); Token advance(); Token peek(); Token previous(); Token consume(TokenType type, const std::string& message); sptr(Stmt) statement(); void sync(); std::shared_ptr expressionStatement(); std::shared_ptr returnStatement(); std::shared_ptr ifStatement(); std::shared_ptr whileStatement(); std::shared_ptr doWhileStatement(); std::shared_ptr forStatement(); std::shared_ptr breakStatement(); std::shared_ptr continueStatement(); std::shared_ptr declaration(); std::shared_ptr varDeclaration(); std::shared_ptr functionDeclaration(); std::shared_ptr functionExpression(); std::shared_ptr assignmentStatement(); sptr(Expr) assignment(); sptr(Expr) assignmentExpression(); // For for loop increment clauses sptr(Expr) increment(); // Parse increment/decrement expressions sptr(Expr) postfix(); // Parse postfix operators std::vector> block(); sptr(Expr) finishCall(sptr(Expr) callee); sptr(Expr) finishArrayIndex(sptr(Expr) array); sptr(Expr) finishArrayAssign(sptr(Expr) array, sptr(Expr) index, sptr(Expr) value); sptr(Expr) arrayLiteral(); sptr(Expr) call(); // Handle call chains (function calls and array indexing) // Helper methods for function scope tracking void enterFunction() { functionDepth++; } void exitFunction() { functionDepth--; } bool isInFunction() const { return functionDepth > 0; } // Helper method for tail call detection bool isTailCall(const std::shared_ptr& expr); };