Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have parser methods return nodes instead of unique_ptrs #45

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,35 @@

namespace xos {

Result<std::unique_ptr<ast::Str>> Parser::parseStr() {
Result<ast::Str> Parser::parseStr() {
Result<Token> res = lexer_.Lex();
if (res.hasError()) {
return res;
}
const Token &tok = res.get();
if (tok.getKind() != Token::string) {
return Result<std::unique_ptr<ast::Str>>::BuildError()
return Result<ast::Str>::BuildError()
<< "Expected string on row " << tok.getRow() << ", col "
<< tok.getCol();
}
return Result<std::unique_ptr<ast::Str>>(
std::make_unique<ast::Str>(tok.getVal()));
return Result<ast::Str>(tok.getVal());
}

Result<std::unique_ptr<ast::Out>> Parser::parseOut() {
Result<ast::Out> Parser::parseOut() {
Result<Token> res = lexer_.Lex();
if (res.hasError()) return res;

const Token &tok = res.get();
if (tok.getKind() != Token::out) {
return Result<std::unique_ptr<ast::Out>>::BuildError()
return Result<ast::Out>::BuildError()
<< "Expected 'out' on row " << tok.getRow() << ", col "
<< tok.getCol();
}

Result<std::unique_ptr<ast::Str>> sres = parseStr();
Result<ast::Str> sres = parseStr();
if (sres.hasError()) return sres;
std::unique_ptr<ast::Str> &str = sres.get();
return Result<std::unique_ptr<ast::Out>>(
std::make_unique<ast::Out>(std::move(str)));
std::unique_ptr<ast::Str> str(sres.Release());
return Result<ast::Out>(std::move(str));
}

} // namespace xos
8 changes: 4 additions & 4 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ TEST(Parser, ParseStr) {
xos::Parser parser(lexer);

auto result = parser.parseStr();
ASSERT_NE(result.get(), nullptr);
ASSERT_EQ(result.get()->getStr(), "Hello World");
ASSERT_FALSE(result.hasError());
ASSERT_EQ(result.get().getStr(), "Hello World");
}

TEST(Parser, ParseStrError) {
Expand All @@ -73,8 +73,8 @@ TEST(Parser, ParseOut) {
xos::Parser parser(lexer);

auto result = parser.parseOut();
ASSERT_NE(result.get(), nullptr);
ASSERT_EQ(result.get()->getExpr(), xos::ast::Str("Hello World"));
ASSERT_FALSE(result.hasError());
ASSERT_EQ(result.get().getExpr(), xos::ast::Str("Hello World"));
}

TEST(Result, ErrorBuilder) {
Expand Down
10 changes: 6 additions & 4 deletions xos.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class Result {
return err_;
}

T *Release() { return result_.release(); }

// Example usage:
//
// Result<Token> getTok() {
Expand Down Expand Up @@ -181,7 +183,7 @@ class Str : public Expr {

class Out : public Expr {
public:
Out(std::unique_ptr<Str> &&expr) : expr_(std::move(expr)) {}
Out(std::unique_ptr<Str> expr) : expr_(std::move(expr)) {}
const Str &getExpr() const { return *expr_; }

private:
Expand All @@ -193,9 +195,9 @@ class Out : public Expr {
class Parser {
public:
Parser(Lexer &lexer) : lexer_(lexer) {}
Result<std::unique_ptr<ast::Str>> parseStr();
Result<std::unique_ptr<ast::Out>> parseOut();
Result<std::unique_ptr<ast::Func>> parseFunc();
Result<ast::Str> parseStr();
Result<ast::Out> parseOut();
Result<ast::Func> parseFunc();

private:
Lexer &lexer_;
Expand Down
Loading