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:
parent
f83f7761e7
commit
923b8d7e2d
@ -40,6 +40,15 @@ namespace arti::lang {
|
|||||||
|
|
||||||
Expected<ast::AST> parse();
|
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>>
|
Expected<ast::Optional<ast::TopLevelDeclNode>>
|
||||||
parseTopLevelDeclaration();
|
parseTopLevelDeclaration();
|
||||||
|
|
||||||
@ -148,44 +157,6 @@ namespace arti::lang {
|
|||||||
Expected<ast::InfLoopStmtNode>
|
Expected<ast::InfLoopStmtNode>
|
||||||
parseInfLoopStatement();
|
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:
|
private:
|
||||||
std::string unitName;
|
std::string unitName;
|
||||||
std::string sourceCode;
|
std::string sourceCode;
|
||||||
|
|||||||
@ -64,4 +64,42 @@ namespace arti::lang {
|
|||||||
return unit;
|
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
|
} // namespace arti::lang
|
||||||
|
|||||||
@ -22,8 +22,6 @@
|
|||||||
|
|
||||||
#include <artichoke/Parser/Parser.hpp>
|
#include <artichoke/Parser/Parser.hpp>
|
||||||
|
|
||||||
#include <print>
|
|
||||||
|
|
||||||
namespace arti::lang {
|
namespace arti::lang {
|
||||||
|
|
||||||
Expected<ast::NamespacedIdentifierNode> Parser::parseNamespacedIdentifier() {
|
Expected<ast::NamespacedIdentifierNode> Parser::parseNamespacedIdentifier() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user