From f632ad6805c0cfb2d792a9dec5e2a32b9642183f Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Wed, 11 Jan 2023 14:02:37 +0100 Subject: [PATCH] scriptlang: don't be strict about throwing unwraps --- .clangd | 1 + scriptlang/error.hpp | 5 ++--- scriptlang/parser.cpp | 37 ++++++++++++++++--------------------- utils/result.hpp | 14 +++++++------- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/.clangd b/.clangd index c04fa47..2d53df1 100644 --- a/.clangd +++ b/.clangd @@ -15,6 +15,7 @@ Diagnostics: - readability-identifier-length - bugprone-easily-swappable-parameters - readability-convert-member-functions-to-static + - bugprone-exception-escape CheckOptions: UnusedIncludes: Strict diff --git a/scriptlang/error.hpp b/scriptlang/error.hpp index dccb9f5..028bccf 100644 --- a/scriptlang/error.hpp +++ b/scriptlang/error.hpp @@ -10,9 +10,8 @@ enum class Errors { LexerMultilineCommentNotTerminated, NoLexerOutput, ParserExhausted, - ParserMalformedStringLiteral, - ParserStructNotTerminated, - ParserStructExpectedId, + ParserMalformed, + ParserUnexpected, }; } diff --git a/scriptlang/parser.cpp b/scriptlang/parser.cpp index 95d4ad6..005f164 100644 --- a/scriptlang/parser.cpp +++ b/scriptlang/parser.cpp @@ -17,18 +17,18 @@ auto Parser::parse_struct(bool strictly_values) noexcept -> Result, Errors> { auto values = std::map> {}; - auto first_brace = TRY(lexer.peek()); - if (TRY(lexer.peek()).type == Tokens::LBrace) { - TRY(lexer.next()); - if (TRY(lexer.peek()).type != Tokens::Eof - && TRY(lexer.peek()).type != Tokens::LBrace) { - if (!(TRY(lexer.peek()).type != Tokens::Id)) { - return Errors::ParserStructExpectedId; - } + auto first_brace = *lexer.peek(); + if (first_brace.type == Tokens::LBrace) { + auto name = *lexer.next(); + if (name.type != Tokens::Eof && name.type != Tokens::LBrace) { + if (name.type != Tokens::Id) + return Errors::ParserUnexpected; + if (lexer.next()->type != Tokens::Colon) + return Errors::ParserUnexpected; } - auto last_brace = TRY(lexer.peek()); + auto last_brace = *lexer.peek(); if (last_brace.type != Tokens::RBrace) - return Errors::ParserStructNotTerminated; + return Errors::ParserMalformed; return { std::make_unique( token_span(first_brace, last_brace), std::move(values)), @@ -40,26 +40,22 @@ auto Parser::parse_struct(bool strictly_values) noexcept auto Parser::parse_atom() noexcept -> Result, Errors> { - auto token = TRY(lexer.peek()); + auto token = *lexer.peek(); switch (token.type) { case Tokens::Id: return { std::make_unique(token_span(token, token), - token_text(lexer.peek()->index, lexer.peek()->length)), + token_text(token.index, token.length)), }; case Tokens::Int: return { std::make_unique(token_span(token, token), - std::atol( - token_text(lexer.peek()->index, lexer.peek()->length) - .c_str())), + std::atol(token_text(token.index, token.length).c_str())), }; case Tokens::Float: return { std::make_unique(token_span(token, token), - std::atof( - token_text(lexer.peek()->index, lexer.peek()->length) - .c_str())), + std::atof(token_text(token.index, token.length).c_str())), }; case Tokens::False: return { @@ -72,8 +68,7 @@ auto Parser::parse_atom() noexcept case Tokens::String: return { std::make_unique(token_span(token, token), - *parse_string_value( - token_text(lexer.peek()->index, lexer.peek()->length))), + *parse_string_value(token_text(token.index, token.length))), }; default: return Errors::ParserExhausted; @@ -84,7 +79,7 @@ auto Parser::parse_atom() noexcept -> Result { if (literal.size() < 2) - return Errors::ParserMalformedStringLiteral; + return Errors::ParserMalformed; auto value = std::string {}; auto escaped = false; for (const auto c : literal.substr(1, literal.size() - 2)) { diff --git a/utils/result.hpp b/utils/result.hpp index fdee2f9..95a0f61 100644 --- a/utils/result.hpp +++ b/utils/result.hpp @@ -654,10 +654,10 @@ private: } // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -#define TRY(expr) \ - ({ \ - auto result = (expr); \ - if (result.is_error()) \ - return { std::move(result.unwrap_error()) }; \ - std::move(result.unwrap()); \ - }) +// #define TRY(expr) \ +// ({ \ +// auto result = (expr); \ +// if (result.is_error()) \ +// return { std::move(result.unwrap_error()) }; \ +// std::move(result.unwrap()); \ +// })