SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
Text.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdexcept>
4#include <string_view>
5#include <unordered_map>
6
7class BufferStream;
8
10
11constexpr std::string_view DEFAULT_SINGLE_LINE_COMMENT_START = "//";
12constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_START = "/*";
13constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_END = "*/";
14constexpr std::string_view DEFAULT_STRING_START = "\"";
15constexpr std::string_view DEFAULT_STRING_END = "\"";
16
17using EscapeSequenceMap = std::unordered_map<char, char>;
18
19[[nodiscard]] const EscapeSequenceMap& getDefaultEscapeSequences();
20
21[[nodiscard]] const EscapeSequenceMap& getDefaultEscapeSequencesOrNone(bool useEscapes);
22
28[[nodiscard]] bool isNewLine(char c);
29
35[[nodiscard]] bool isNewLine(std::string_view str);
36
42[[nodiscard]] bool isWhitespace(char c);
43
49[[nodiscard]] bool isWhitespace(std::string_view str);
50
56[[nodiscard]] bool isNumber(char c);
57
63[[nodiscard]] bool isNumber(std::string_view str);
64
71[[nodiscard]] std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap& escapeSequences);
72
79[[nodiscard]] std::string convertEscapesToSpecialChars(std::string_view str, const EscapeSequenceMap& escapeSequences);
80
85void eatWhitespace(BufferStream& stream);
86
92void eatSingleLineComment(BufferStream& stream);
93
100void eatMultiLineComment(BufferStream& stream, std::string_view multiLineCommentEnd = DEFAULT_MULTI_LINE_COMMENT_END);
101
107void eatWhitespaceAndSingleLineComments(BufferStream& stream, std::string_view singleLineCommentStart = DEFAULT_SINGLE_LINE_COMMENT_START);
108
114void eatWhitespaceAndMultiLineComments(BufferStream& stream, std::string_view multiLineCommentStart = DEFAULT_MULTI_LINE_COMMENT_START);
115
122void eatWhitespaceAndComments(BufferStream& stream, std::string_view singleLineCommentStart = DEFAULT_SINGLE_LINE_COMMENT_START, std::string_view multiLineCommentStart = DEFAULT_MULTI_LINE_COMMENT_START);
123
130[[nodiscard]] bool tryToEatChar(BufferStream& stream, char c);
131
141std::string_view readStringToBuffer(BufferStream& stream, BufferStream& backing, std::string_view start = DEFAULT_STRING_START, std::string_view end = DEFAULT_STRING_END, const EscapeSequenceMap& escapeSequences = getDefaultEscapeSequences());
142
150std::string_view readUnquotedStringToBuffer(BufferStream& stream, BufferStream& backing, const EscapeSequenceMap& escapeSequences = getDefaultEscapeSequences());
151
160std::string_view readUnquotedStringToBuffer(BufferStream& stream, BufferStream& backing, std::string_view end, const EscapeSequenceMap& escapeSequences = getDefaultEscapeSequences());
161
162class syntax_error : public std::runtime_error {
163public:
164 using std::runtime_error::runtime_error;
165};
166
167} // namespace sourcepp::parser::text
std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert special characters like \n to escaped special characters like \\n.
Definition: Text.cpp:56
std::string convertEscapesToSpecialChars(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert escaped special characters like \\n to special characters like \n.
Definition: Text.cpp:75
std::unordered_map< char, char > EscapeSequenceMap
Definition: Text.h:17
constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_END
Definition: Text.h:13
void eatMultiLineComment(BufferStream &stream, std::string_view multiLineCommentEnd=DEFAULT_MULTI_LINE_COMMENT_END)
If a multi line comment is detected, eat its contents.
Definition: Text.cpp:102
bool isNewLine(char c)
If a char is a newline character.
Definition: Text.cpp:32
void eatSingleLineComment(BufferStream &stream)
If a single line comment is detected, eat its contents.
Definition: Text.cpp:98
void eatWhitespaceAndSingleLineComments(BufferStream &stream, std::string_view singleLineCommentStart=DEFAULT_SINGLE_LINE_COMMENT_START)
Eat all whitespace and single line comments after the current stream position.
Definition: Text.cpp:108
void eatWhitespace(BufferStream &stream)
Eat all whitespace after the current stream position.
Definition: Text.cpp:93
constexpr std::string_view DEFAULT_SINGLE_LINE_COMMENT_START
Definition: Text.h:11
bool tryToEatChar(BufferStream &stream, char c)
If the given char exists at the current position, skip over it.
Definition: Text.cpp:139
constexpr std::string_view DEFAULT_STRING_END
Definition: Text.h:15
constexpr std::string_view DEFAULT_MULTI_LINE_COMMENT_START
Definition: Text.h:12
constexpr std::string_view DEFAULT_STRING_START
Definition: Text.h:14
const EscapeSequenceMap & getDefaultEscapeSequencesOrNone(bool useEscapes)
Definition: Text.cpp:27
std::string_view readUnquotedStringToBuffer(BufferStream &stream, BufferStream &backing, const EscapeSequenceMap &escapeSequences=getDefaultEscapeSequences())
Read a string starting at the current stream position.
Definition: Text.cpp:180
std::string_view readStringToBuffer(BufferStream &stream, BufferStream &backing, std::string_view start=DEFAULT_STRING_START, std::string_view end=DEFAULT_STRING_END, const EscapeSequenceMap &escapeSequences=getDefaultEscapeSequences())
Read a string starting at the current stream position.
Definition: Text.cpp:147
void eatWhitespaceAndComments(BufferStream &stream, std::string_view singleLineCommentStart=DEFAULT_SINGLE_LINE_COMMENT_START, std::string_view multiLineCommentStart=DEFAULT_MULTI_LINE_COMMENT_START)
Eat all whitespace and comments after the current stream position.
Definition: Text.cpp:117
bool isNumber(char c)
If a char is a numerical character (0-9).
Definition: Text.cpp:48
const EscapeSequenceMap & getDefaultEscapeSequences()
Definition: Text.cpp:10
void eatWhitespaceAndMultiLineComments(BufferStream &stream, std::string_view multiLineCommentStart=DEFAULT_MULTI_LINE_COMMENT_START)
Eat all whitespace and multi line comments after the current stream position.
Definition: Text.cpp:112
bool isWhitespace(char c)
If a char is a whitespace character.
Definition: Text.cpp:40