refactor(parser): move parser utility methods to source file

Signed-off-by: erick-alcachofa <erick@artichoke.dev>

Relocate core parsing utility methods from the header to the
implementation file to reduce header bloat and improve compilation
times.

- **Parser API**: Moved the definitions of `consume()`,
  `matchAndConsume()`, and `match()` from `Parser.hpp` to `Parser.cpp`.
- **Cleanup**: Removed an unused `<print>` include in `Types.cpp`
  discovered during the refactor.
- **Organization**: Methods are now declared in the header and defined
  in the source file, maintaining a cleaner separation between interface
  and implementation.
This commit is contained in:
erick-alcachofa 2025-12-25 13:35:50 -06:00
parent f83f7761e7
commit 923b8d7e2d
Signed by: me
GPG Key ID: 6FA5F8643444BAFA
3 changed files with 47 additions and 40 deletions

View File

@ -40,6 +40,15 @@ namespace arti::lang {
Expected<ast::AST> parse();
Expected<Token>
consume(TokenV type, std::string_view message);
Expected<bool>
matchAndConsume(TokenV type);
Expected<bool>
match(TokenV type, std::size_t offset = 0);
Expected<ast::Optional<ast::TopLevelDeclNode>>
parseTopLevelDeclaration();
@ -148,44 +157,6 @@ namespace arti::lang {
Expected<ast::InfLoopStmtNode>
parseInfLoopStatement();
Expected<Token> consume(TokenV type, std::string_view message) {
auto peeked = tokenizer.peekExpect(type, message);
if (! peeked) {
return Unexpected<>{ std::move(peeked).error() };
}
std::ignore = tokenizer.consume();
return peeked;
}
Expected<bool> matchAndConsume(TokenV type) {
auto peeked = tokenizer.peek();
if (! peeked) {
return Unexpected<>{ std::move(peeked).error() };
}
if (peeked->value != type) {
return false;
}
std::ignore = tokenizer.consume();
return true;
}
Expected<bool> match(TokenV type, std::size_t offset = 0) {
auto peeked = tokenizer.peek(offset);
if (! peeked) {
return Unexpected<>{ std::move(peeked).error() };
}
return (peeked->value == type);
}
private:
std::string unitName;
std::string sourceCode;

View File

@ -64,4 +64,42 @@ namespace arti::lang {
return unit;
}
Expected<Token> Parser::consume(TokenV type, std::string_view message) {
auto peeked = tokenizer.peekExpect(type, message);
if (! peeked) {
return Unexpected<>{ std::move(peeked).error() };
}
std::ignore = tokenizer.consume();
return peeked;
}
Expected<bool> Parser::matchAndConsume(TokenV type) {
auto peeked = tokenizer.peek();
if (! peeked) {
return Unexpected<>{ std::move(peeked).error() };
}
if (peeked->value != type) {
return false;
}
std::ignore = tokenizer.consume();
return true;
}
Expected<bool> Parser::match(TokenV type, std::size_t offset) {
auto peeked = tokenizer.peek(offset);
if (! peeked) {
return Unexpected<>{ std::move(peeked).error() };
}
return (peeked->value == type);
}
} // namespace arti::lang

View File

@ -22,8 +22,6 @@
#include <artichoke/Parser/Parser.hpp>
#include <print>
namespace arti::lang {
Expected<ast::NamespacedIdentifierNode> Parser::parseNamespacedIdentifier() {