xiiregexbuilder

FPGA-Accelerated Regular Expression Matching Engine
commit b37fdb4f722532ac3630160ba9fc41fdea36e368
parent 491dc5bb2a0e3001b1a2f55203861697610ce06e
Author: Pranav Prabhu Kumble <kumblepranavprabhu@gmail.com>
Date:   Tue, 31 Mar 2026 00:46:09 +0530

Week 2: Implement AST Parser (Part 3: Finalizing)

Diffstat:
Msrc/parser.cpp | 30++++++++++++++++++++++++++++++
Msrc/parser.h | 25+++++++++++++++++++++++++
2 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/src/parser.cpp b/src/parser.cpp @@ -98,3 +98,33 @@ std::unique_ptr<ASTNode> Parser::parseAtom() { return nullptr; // Should not reach here } +const Token& Parser::peek() const { + return tokens[pos]; +} + +const Token& Parser::advance() { + if (!isAtEnd()) pos++; + return tokens[pos - 1]; +} + +bool Parser::match(TokenType type) { + if (check(type)) { + advance(); + return true; + } + return false; +} + +bool Parser::check(TokenType type) const { + if (isAtEnd()) return false; + return peek().type == type; +} + +bool Parser::isAtEnd() const { + return pos >= tokens.size() || tokens[pos].type == TokenType::END_OF_INPUT; +} + +void Parser::error(const std::string& message) const { + const Token& t = peek(); + throw std::runtime_error(message + " at line " + std::to_string(t.line) + ", column " + std::to_string(t.column)); +} diff --git a/src/parser.h b/src/parser.h @@ -80,3 +80,28 @@ public: explicit OptionalNode(std::unique_ptr<ASTNode> i) : ASTNode(ASTNodeType::OPTIONAL), inner(std::move(i)) {} }; + +class Parser { +public: + explicit Parser(const std::vector<Token>& tokens); + std::unique_ptr<ASTNode> parse(); + +private: + const std::vector<Token>& tokens; + size_t pos; + + std::unique_ptr<ASTNode> parseExpression(); // Union + std::unique_ptr<ASTNode> parseTerm(); // Concatenation + std::unique_ptr<ASTNode> parseFactor(); // Quantifiers + std::unique_ptr<ASTNode> parseAtom(); // Literals, Dot, Parens + + const Token& peek() const; + const Token& advance(); + bool match(TokenType type); + bool check(TokenType type) const; + bool isAtEnd() const; + + void error(const std::string& message) const; +}; + +#endif // PARSER_H