Bob/headers/Parser.h
Bobby Lucero 72a31b28af Refactor to ExecutionContext pattern and fix string + none concatenation
- Add none value handling to Value::operator+ for string concatenation
- Replace direct string concatenations with Value::operator+ calls in Interpreter
- Add missing string+none and none+string type combinations
- Standardize token literal generation in Lexer
- Add ExecutionContext support across visitor pattern
- Enhance error reporting integration
- Add new standard library functions
2025-08-05 00:49:59 -04:00

81 lines
2.1 KiB
C++

#pragma once
#include <utility>
#include <vector>
#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<Token> tokens;
int current = 0;
int functionDepth = 0; // Track nesting level of functions
ErrorReporter* errorReporter = nullptr;
public:
explicit Parser(std::vector<Token> tokens) : tokens(std::move(tokens)){};
std::vector<sptr(Stmt)> parse();
void setErrorReporter(ErrorReporter* reporter) { errorReporter = reporter; }
private:
sptr(Expr) expression();
sptr(Expr) logical_or();
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<TokenType>& 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<Stmt> expressionStatement();
std::shared_ptr<Stmt> returnStatement();
std::shared_ptr<Stmt> ifStatement();
std::shared_ptr<Stmt> declaration();
std::shared_ptr<Stmt> varDeclaration();
std::shared_ptr<Stmt> functionDeclaration();
std::shared_ptr<Expr> functionExpression();
sptr(Expr) assignment();
sptr(Expr) increment(); // Parse increment/decrement expressions
sptr(Expr) postfix(); // Parse postfix operators
std::vector<std::shared_ptr<Stmt>> block();
sptr(Expr) finishCall(sptr(Expr) callee);
// 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>& expr);
};