Skip to content

Commit

Permalink
Have parser methods return nodes instead of unique_ptrs
Browse files Browse the repository at this point in the history
  • Loading branch information
PiJoules committed Jan 26, 2024
1 parent e08f42b commit 4a9c076
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
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

0 comments on commit 4a9c076

Please sign in to comment.