scriptlang: don't be strict about throwing unwraps
This commit is contained in:
parent
65fbfd26e2
commit
f632ad6805
1
.clangd
1
.clangd
@ -15,6 +15,7 @@ Diagnostics:
|
|||||||
- readability-identifier-length
|
- readability-identifier-length
|
||||||
- bugprone-easily-swappable-parameters
|
- bugprone-easily-swappable-parameters
|
||||||
- readability-convert-member-functions-to-static
|
- readability-convert-member-functions-to-static
|
||||||
|
- bugprone-exception-escape
|
||||||
|
|
||||||
CheckOptions:
|
CheckOptions:
|
||||||
UnusedIncludes: Strict
|
UnusedIncludes: Strict
|
||||||
|
@ -10,9 +10,8 @@ enum class Errors {
|
|||||||
LexerMultilineCommentNotTerminated,
|
LexerMultilineCommentNotTerminated,
|
||||||
NoLexerOutput,
|
NoLexerOutput,
|
||||||
ParserExhausted,
|
ParserExhausted,
|
||||||
ParserMalformedStringLiteral,
|
ParserMalformed,
|
||||||
ParserStructNotTerminated,
|
ParserUnexpected,
|
||||||
ParserStructExpectedId,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,18 +17,18 @@ auto Parser::parse_struct(bool strictly_values) noexcept
|
|||||||
-> Result<std::unique_ptr<Expression>, Errors>
|
-> Result<std::unique_ptr<Expression>, Errors>
|
||||||
{
|
{
|
||||||
auto values = std::map<std::string, std::unique_ptr<Expression>> {};
|
auto values = std::map<std::string, std::unique_ptr<Expression>> {};
|
||||||
auto first_brace = TRY(lexer.peek());
|
auto first_brace = *lexer.peek();
|
||||||
if (TRY(lexer.peek()).type == Tokens::LBrace) {
|
if (first_brace.type == Tokens::LBrace) {
|
||||||
TRY(lexer.next());
|
auto name = *lexer.next();
|
||||||
if (TRY(lexer.peek()).type != Tokens::Eof
|
if (name.type != Tokens::Eof && name.type != Tokens::LBrace) {
|
||||||
&& TRY(lexer.peek()).type != Tokens::LBrace) {
|
if (name.type != Tokens::Id)
|
||||||
if (!(TRY(lexer.peek()).type != Tokens::Id)) {
|
return Errors::ParserUnexpected;
|
||||||
return Errors::ParserStructExpectedId;
|
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)
|
if (last_brace.type != Tokens::RBrace)
|
||||||
return Errors::ParserStructNotTerminated;
|
return Errors::ParserMalformed;
|
||||||
return {
|
return {
|
||||||
std::make_unique<Struct>(
|
std::make_unique<Struct>(
|
||||||
token_span(first_brace, last_brace), std::move(values)),
|
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
|
auto Parser::parse_atom() noexcept
|
||||||
-> Result<std::unique_ptr<Expression>, Errors>
|
-> Result<std::unique_ptr<Expression>, Errors>
|
||||||
{
|
{
|
||||||
auto token = TRY(lexer.peek());
|
auto token = *lexer.peek();
|
||||||
switch (token.type) {
|
switch (token.type) {
|
||||||
case Tokens::Id:
|
case Tokens::Id:
|
||||||
return {
|
return {
|
||||||
std::make_unique<Id>(token_span(token, token),
|
std::make_unique<Id>(token_span(token, token),
|
||||||
token_text(lexer.peek()->index, lexer.peek()->length)),
|
token_text(token.index, token.length)),
|
||||||
};
|
};
|
||||||
case Tokens::Int:
|
case Tokens::Int:
|
||||||
return {
|
return {
|
||||||
std::make_unique<Int>(token_span(token, token),
|
std::make_unique<Int>(token_span(token, token),
|
||||||
std::atol(
|
std::atol(token_text(token.index, token.length).c_str())),
|
||||||
token_text(lexer.peek()->index, lexer.peek()->length)
|
|
||||||
.c_str())),
|
|
||||||
};
|
};
|
||||||
case Tokens::Float:
|
case Tokens::Float:
|
||||||
return {
|
return {
|
||||||
std::make_unique<Float>(token_span(token, token),
|
std::make_unique<Float>(token_span(token, token),
|
||||||
std::atof(
|
std::atof(token_text(token.index, token.length).c_str())),
|
||||||
token_text(lexer.peek()->index, lexer.peek()->length)
|
|
||||||
.c_str())),
|
|
||||||
};
|
};
|
||||||
case Tokens::False:
|
case Tokens::False:
|
||||||
return {
|
return {
|
||||||
@ -72,8 +68,7 @@ auto Parser::parse_atom() noexcept
|
|||||||
case Tokens::String:
|
case Tokens::String:
|
||||||
return {
|
return {
|
||||||
std::make_unique<String>(token_span(token, token),
|
std::make_unique<String>(token_span(token, token),
|
||||||
*parse_string_value(
|
*parse_string_value(token_text(token.index, token.length))),
|
||||||
token_text(lexer.peek()->index, lexer.peek()->length))),
|
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
return Errors::ParserExhausted;
|
return Errors::ParserExhausted;
|
||||||
@ -84,7 +79,7 @@ auto Parser::parse_atom() noexcept
|
|||||||
-> Result<std::string, Errors>
|
-> Result<std::string, Errors>
|
||||||
{
|
{
|
||||||
if (literal.size() < 2)
|
if (literal.size() < 2)
|
||||||
return Errors::ParserMalformedStringLiteral;
|
return Errors::ParserMalformed;
|
||||||
auto value = std::string {};
|
auto value = std::string {};
|
||||||
auto escaped = false;
|
auto escaped = false;
|
||||||
for (const auto c : literal.substr(1, literal.size() - 2)) {
|
for (const auto c : literal.substr(1, literal.size() - 2)) {
|
||||||
|
@ -654,10 +654,10 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
|
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
|
||||||
#define TRY(expr) \
|
// #define TRY(expr) \
|
||||||
({ \
|
// ({ \
|
||||||
auto result = (expr); \
|
// auto result = (expr); \
|
||||||
if (result.is_error()) \
|
// if (result.is_error()) \
|
||||||
return { std::move(result.unwrap_error()) }; \
|
// return { std::move(result.unwrap_error()) }; \
|
||||||
std::move(result.unwrap()); \
|
// std::move(result.unwrap()); \
|
||||||
})
|
// })
|
||||||
|
Loading…
Reference in New Issue
Block a user