diff --git a/parser.cpp b/parser.cpp index b1cbc83..c9d77a7 100644 --- a/parser.cpp +++ b/parser.cpp @@ -14,37 +14,35 @@ namespace xos { -Result> Parser::parseStr() { +Result Parser::parseStr() { Result res = lexer_.Lex(); if (res.hasError()) { return res; } const Token &tok = res.get(); if (tok.getKind() != Token::string) { - return Result>::BuildError() + return Result::BuildError() << "Expected string on row " << tok.getRow() << ", col " << tok.getCol(); } - return Result>( - std::make_unique(tok.getVal())); + return Result(tok.getVal()); } -Result> Parser::parseOut() { +Result Parser::parseOut() { Result res = lexer_.Lex(); if (res.hasError()) return res; const Token &tok = res.get(); if (tok.getKind() != Token::out) { - return Result>::BuildError() + return Result::BuildError() << "Expected 'out' on row " << tok.getRow() << ", col " << tok.getCol(); } - Result> sres = parseStr(); + Result sres = parseStr(); if (sres.hasError()) return sres; - std::unique_ptr &str = sres.get(); - return Result>( - std::make_unique(std::move(str))); + std::unique_ptr str(sres.Release()); + return Result(std::move(str)); } } // namespace xos diff --git a/test.cpp b/test.cpp index 8eabeb6..8d68c80 100644 --- a/test.cpp +++ b/test.cpp @@ -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) { @@ -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) { diff --git a/xos.h b/xos.h index b165a96..f8e8dee 100644 --- a/xos.h +++ b/xos.h @@ -93,6 +93,8 @@ class Result { return err_; } + T *Release() { return result_.release(); } + // Example usage: // // Result getTok() { @@ -181,7 +183,7 @@ class Str : public Expr { class Out : public Expr { public: - Out(std::unique_ptr &&expr) : expr_(std::move(expr)) {} + Out(std::unique_ptr expr) : expr_(std::move(expr)) {} const Str &getExpr() const { return *expr_; } private: @@ -193,9 +195,9 @@ class Out : public Expr { class Parser { public: Parser(Lexer &lexer) : lexer_(lexer) {} - Result> parseStr(); - Result> parseOut(); - Result> parseFunc(); + Result parseStr(); + Result parseOut(); + Result parseFunc(); private: Lexer &lexer_;