Skip to content

Commit

Permalink
refactor: naming parser, buffer and cdata
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Sep 5, 2024
1 parent a4154ef commit f7b745a
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 259 deletions.
3 changes: 3 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ root->set_name("far");
#### Attributes
```C++
using namespace myxml;
// create an element with name 'root'
element elem("root");
// or get an element from parsing
Expand All @@ -49,6 +50,7 @@ elem["hello"]; // == elem["hello"] = "";
#### Children

```C++
using namespace myxml;
// element root;
element child = root.first_elem();
// or first element with name "child"
Expand All @@ -62,6 +64,7 @@ std::vector<element> children = root.elems("child");
### Text

```C++
using namespace myxml;
// create from string
text txt = "Hello";
// or from query
Expand Down
34 changes: 17 additions & 17 deletions include/myxml/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace myxml
{
/**
* ADT. Used by Parser.
* ADT. Used by parser.
* Implement by StringBuffer and XMLFile
*/
class Buffer
class buffer
{
private:
std::size_t offset = 0;
Expand All @@ -21,34 +21,34 @@ namespace myxml
// @returns {pointer to data, data length}
virtual std::tuple<const char *, std::size_t> base() const = 0;
// update line and column
void updateLocation(char);
void update_loc(char);
// update line and column
void updateLocation(std::string_view);
void update_loc(std::string_view);

public:
virtual ~Buffer() = default;
std::optional<char> Peek() const;
std::optional<std::string_view> PeekN(int) const;
std::optional<char> AfterN(int) const;
std::optional<std::string_view> AfterNM(int, int) const;
std::optional<char> Take();
std::optional<std::string_view> TakeN(int);
virtual ~buffer() = default;
std::optional<char> peek() const;
std::optional<std::string_view> peek_n(int) const;
std::optional<char> after_n(int) const;
std::optional<std::string_view> after_n_m(int, int) const;
std::optional<char> take();
std::optional<std::string_view> take_n(int);
// @returns {line, column}
std::tuple<std::size_t, std::size_t> CurrentLocation();
std::tuple<std::size_t, std::size_t> cur_loc();
};

class StringBuffer : public Buffer
class string_buffer : public buffer
{
private:
std::variant<std::string, std::string_view> inner;
std::string_view getView() const;
std::string_view view() const;

/** Implement Buffer */
virtual std::tuple<const char *, std::size_t> base() const override;

public:
StringBuffer(std::string_view);
StringBuffer(std::string &&);
StringBuffer(const char *);
string_buffer(std::string_view);
string_buffer(std::string &&);
string_buffer(const char *);
};
}
32 changes: 25 additions & 7 deletions include/myxml/cdata.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
#pragma once
#include "myxml/node.hpp"
#include "myxml/printable.hpp"

namespace myxml
{
class CData : public Node
class cdata_impl;

class cdata : public printable
{
private:
std::string inner;
std::shared_ptr<cdata_impl> _impl;

cdata(std::shared_ptr<cdata_impl>);

public:
cdata(std::string &&);
cdata(std::string_view);

virtual void entity_encoding(bool) override {}
virtual void platform_specific_newline(bool) override {}
virtual void print(std::ostream &) const override;
};

class cdata_impl : public Node
{
public:
explicit CData(std::string);
std::string inner;

cdata_impl() {};
explicit cdata_impl(std::string_view);
explicit cdata_impl(std::string &&);
virtual ~cdata_impl() = default;

virtual ~CData() = default;
// virtual std::string ExportRaw() const override;
// virtual std::string ExportFormatted(int indentLevel = 0, int indentSize = 4) const override;
virtual void entity_encoding(bool) override;
virtual void entity_encoding(bool) override {}
virtual void platform_specific_newline(bool) override {}
virtual void print(std::ostream &) const override;
};
Expand Down
20 changes: 10 additions & 10 deletions include/myxml/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace myxml
{
class ParseError : public std::exception
class parse_error : public std::exception
{
private:
virtual const char *prefix() const = 0;
Expand All @@ -16,7 +16,7 @@ namespace myxml
std::size_t column;

public:
ParseError(std::string message, std::size_t line, std::size_t column);
parse_error(std::string message, std::size_t line, std::size_t column);

virtual const char *what() const noexcept override;
};
Expand All @@ -27,46 +27,46 @@ namespace myxml
* 2. Unexpected token. Encounter a token that is not expected in the context. For example: extra semicolon.
* ...
*/
class SyntaxError : public ParseError
class syntax_error : public parse_error
{
private:
virtual const char *prefix() const;

public:
SyntaxError(std::string, std::size_t line, std::size_t column);
syntax_error(std::string, std::size_t line, std::size_t column);
};

/**
*
*/
class SemanticError : public ParseError
class semantic_error : public parse_error
{
private:
virtual const char *prefix() const;

public:
SemanticError(std::string, std::size_t line, std::size_t column);
semantic_error(std::string, std::size_t line, std::size_t column);
};

/**
* e.g. EOF
*/
class UnexpectedEndOfInput : public ParseError
class unexpected_eof : public parse_error
{
private:
virtual const char *prefix() const;

public:
UnexpectedEndOfInput(std::size_t line, std::size_t column);
unexpected_eof(std::size_t line, std::size_t column);
};

class IOError : public std::exception
class io_error : public std::exception
{
private:
std::string message;

public:
IOError(std::string);
io_error(std::string);

virtual const char *what() const noexcept override;
};
Expand Down
60 changes: 30 additions & 30 deletions include/myxml/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@

namespace myxml
{
struct ElementTag
struct element_tag
{
enum class ClosingType
enum class closing_type
{
Open, // <tag>
Closed, // <tag/>
Closing // </tag>
};

std::string name;
ElementTag::ClosingType type = ClosingType::Open;
std::map<std::string, std::string> attris;
element_tag::closing_type type = closing_type::Open;
std::map<std::string, std::string> attrs;
};

class Parser
class parser
{
private:
std::shared_ptr<Buffer> buffer;
std::shared_ptr<buffer> _buffer;

std::optional<char> peek();
std::optional<std::string_view> peekN(int);
std::optional<char> afterN(int);
std::optional<std::string_view> peek_n(int);
std::optional<char> after_n(int);
// m characters after n characters
std::optional<std::string_view> afterNM(int, int);
std::optional<std::string_view> after_n_m(int, int);
std::optional<char> take();
std::optional<std::string_view> takeN(int);
void skipWhiteSpaces();
std::tuple<std::size_t, std::size_t> currentLoc();
std::optional<std::string_view> take_n(int);
void skip_whitespaces();
std::tuple<std::size_t, std::size_t> cur_loc();

public:
/**
* For all parsing method,
* return std::nullopt means `not this one` and will not consume buffer.
Expand All @@ -46,66 +47,65 @@ namespace myxml
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError` if an invalid character occurs.
*/
std::string parseIdent();
std::string parse_ident();
/**
* Parse a string literal
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError` if missing any of `"`
*/
std::string parseStringLiteral();
std::string parse_str_literal();
/**
* @returns std::nullopt if find no attribute
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError` if the following chars do not confront to `key="value"` format
*/
std::optional<std::pair<std::string, std::string>> parseAttribute();
std::optional<std::pair<std::string, std::string>> parse_attribute();
/**
* @throws `SyntaxError` if faild to find `<`
*/
std::shared_ptr<text_impl> parseText();
std::shared_ptr<text_impl> parse_text();
/**
* @returns `std::nullopt` if not start with `<!CDATA[`
*/
std::shared_ptr<CData> parseCData();
std::shared_ptr<cdata_impl> parse_cdata();
/**
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError`
* @throws `SemanticError`
*/
std::shared_ptr<element_impl> parseElementWithHeader(ElementTag header);
std::shared_ptr<element_impl> parse_element_with_header(element_tag header);
/**
* @returns std::nullopt if not starts with `<?xml`
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError`
* @throws `SemanticError`
*/
std::optional<declaration> parseDeclaration();
std::optional<declaration> parse_declaration();

public:
std::shared_ptr<element_impl> ParseElement();
std::shared_ptr<element_impl> parse_element();
/**
* @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> ParseElementTag();
std::optional<element_tag> parse_element_tag();
/**
* @throws `UnexpectedEndOfInput`
* @throws `SyntaxError`
* @throws `SemanticError`
*/
document ParseDocument();
Parser() = delete;
explicit Parser(std::string_view);
explicit Parser(std::string &&);
document parse_document();
parser() = delete;
explicit parser(std::string_view);
explicit parser(std::string &&);

template <typename T, typename = std::enable_if_t<std::is_base_of_v<Buffer, T>>>
explicit Parser(std::shared_ptr<T> buffer)
: buffer(buffer) {}
template <typename T, typename = std::enable_if_t<std::is_base_of_v<buffer, T>>>
explicit parser(std::shared_ptr<T> buffer)
: _buffer(buffer) {}
};

namespace util
{
bool isValidXmlChar(char ch);
bool is_valid_xml_char(char ch);
}
}
4 changes: 2 additions & 2 deletions include/myxml/xmlfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace myxml
{
class XMLFile : public Buffer
class XMLFile : public buffer
{
private:
XMLFile();
Expand All @@ -18,7 +18,7 @@ namespace myxml

std::size_t offset;

/* Implement Buffer*/
/* Implement buffer*/
virtual std::tuple<const char *, std::size_t> base() const;

public:
Expand Down
Loading

0 comments on commit f7b745a

Please sign in to comment.