The TextParser is a helper class to facilitate parsing a text protocol from a string. It makes sure that parsing doesn't go outside of the string bounds, and emits human readable errors on parse failures. */
| 19 | readable errors on parse failures. |
| 20 | */ |
| 21 | class TextParser { |
| 22 | public: |
| 23 | explicit TextParser(std::string_view str); |
| 24 | ~TextParser(); |
| 25 | |
| 26 | bool hasError() const; |
| 27 | Error getError() const; |
| 28 | |
| 29 | size_t position() const; |
| 30 | size_t end() const; |
| 31 | |
| 32 | bool isAtEnd() const; |
| 33 | bool ensureIsAtEnd(); |
| 34 | bool ensureNotAtEnd(); |
| 35 | |
| 36 | std::string_view substr(size_t from, size_t to) const; |
| 37 | |
| 38 | bool tryParse(char c); |
| 39 | bool parse(char c); |
| 40 | bool peek(char c) const; |
| 41 | char peek() const; |
| 42 | |
| 43 | bool tryParse(std::string_view term); |
| 44 | bool parse(std::string_view term); |
| 45 | |
| 46 | bool skip(); |
| 47 | bool skipCount(size_t count); |
| 48 | |
| 49 | bool tryParseWhitespaces(); |
| 50 | |
| 51 | std::optional<double> parseDouble(); |
| 52 | std::optional<int> parseInt(); |
| 53 | std::optional<uint32_t> parseUInt(); |
| 54 | std::optional<long long> parseHexLong(); |
| 55 | std::optional<std::string_view> parseIdentifier(); |
| 56 | bool parseDouble(double& output); |
| 57 | bool parseInt(int& output); |
| 58 | bool parseUInt(uint32_t& output); |
| 59 | bool parseHexLong(long long& output); |
| 60 | bool parseIdentifier(std::string_view& output); |
| 61 | |
| 62 | /** |
| 63 | Read the string while the given predicate is true. |
| 64 | Returns a string containing the read characters. |
| 65 | */ |
| 66 | template<typename Pred> |
| 67 | std::string_view readWhile(Pred&& pred) { |
| 68 | auto start = position(); |
| 69 | while (tryParsePredicate(pred)) { |
| 70 | } |
| 71 | return substr(start, position()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | Read the string until the given character is found or the end of the buffer is reached. |
| 76 | Parser will be positioned right after the character was found. |
| 77 | */ |
| 78 | std::string_view readUntilCharacter(char c); |
no outgoing calls