Skip to content

Commit

Permalink
refactor(parser.hpp): change optional pointer to pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 1, 2024
1 parent a071f97 commit eab71e1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
7 changes: 4 additions & 3 deletions include/myxml/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace myxml
std::optional<char> peek();
std::optional<std::string_view> peekN(int);
std::optional<char> afterN(int);
// m characters after n characters
std::optional<std::string_view> afterNM(int, int);
std::optional<char> take();
std::optional<std::string_view> takeN(int);
Expand Down Expand Up @@ -64,7 +65,7 @@ namespace myxml
/**
* @returns `std::nullopt` if not start with `<!CDATA[`
*/
std::optional<std::shared_ptr<CData>> parseCData();
std::shared_ptr<CData> parseCData();
/**
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError`
Expand All @@ -80,13 +81,13 @@ namespace myxml
std::optional<Declaration> parseDeclaration();

public:
std::optional<std::shared_ptr<Element>> ParseElement();
std::shared_ptr<Element> ParseElement();
/**
* @returns std::nullopt if no heading `<`
* @throws `SyntaxError` if the heading character is `<` and the trailing characters are in incorrect format
* @throws `UnexpectedEndOfInput` if missing name
*/
std::optional<ElementTag> ParseTag();
std::optional<ElementTag> ParseElementTag();
/**
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError`
Expand Down
2 changes: 1 addition & 1 deletion src/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace myxml

std::shared_ptr<Element> Element::Parse(std::string_view buf)
{
return Parser(buf).ParseElement().value();
return Parser(buf).ParseElement();
}

std::optional<std::string_view> Element::GetAttribute(std::string_view name)
Expand Down
18 changes: 9 additions & 9 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ namespace myxml
return std::shared_ptr<Text>(new Text(*this->takeN(len)));
}

std::optional<std::shared_ptr<CData>> Parser::parseCData()
std::shared_ptr<CData> Parser::parseCData()
{
if (this->peekN(9) != "<![CDATA[")
{
return std::nullopt;
return nullptr;
}
this->takeN(9);
std::size_t len = 0;
Expand All @@ -151,7 +151,7 @@ namespace myxml
return std::make_shared<CData>(it);
}

std::optional<ElementTag> Parser::ParseTag()
std::optional<ElementTag> Parser::ParseElementTag()
{
if (this->take() != '<')
{
Expand Down Expand Up @@ -200,10 +200,10 @@ namespace myxml
{
if (auto cdata = this->parseCData(); cdata)
{
elem->InsertAtEnd(*cdata);
elem->InsertAtEnd(cdata);
continue;
}
auto tag = this->ParseTag(); // impossible to be std::nullopt
auto tag = this->ParseElementTag(); // impossible to be std::nullopt
assert(tag);
switch (tag->type)
{
Expand Down Expand Up @@ -248,10 +248,10 @@ namespace myxml
throw UnexpectedEndOfInput();
}

std::optional<std::shared_ptr<Element>> Parser::ParseElement()
std::shared_ptr<Element> Parser::ParseElement()
{
this->skipWhiteSpaces();
if (auto tag = this->ParseTag(); tag)
if (auto tag = this->ParseElementTag(); tag)
{
if (tag->type == ElementTag::ClosingType::Closed)
{
Expand All @@ -274,7 +274,7 @@ namespace myxml
}
else
{
return std::nullopt;
return nullptr;
}
}

Expand Down Expand Up @@ -314,7 +314,7 @@ namespace myxml
}
if (auto root = this->ParseElement(); root)
{
document.SetRoot(*root);
document.SetRoot(root);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions tests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
TEST_CASE("Parsing tag", "parser")
{
std::string open = "<tag>";
auto tag = myxml::Parser(open).ParseTag();
auto tag = myxml::Parser(open).ParseElementTag();
REQUIRE(tag->name == "tag");
REQUIRE(tag->type == myxml::ElementTag::ClosingType::Open);

std::string closed = "<tag/>";
tag = myxml::Parser(closed).ParseTag();
tag = myxml::Parser(closed).ParseElementTag();
REQUIRE(tag->name == "tag");
REQUIRE(tag->type == myxml::ElementTag::ClosingType::Closed);

std::string closing = "</tag>";
tag = myxml::Parser(closing).ParseTag();
tag = myxml::Parser(closing).ParseElementTag();
REQUIRE(tag->name == "tag");
REQUIRE(tag->type == myxml::ElementTag::ClosingType::Closing);
}
Expand Down

0 comments on commit eab71e1

Please sign in to comment.