5#include <BufferStream.h>
23 return defaultEscapeSequences;
36 return this->
tokens.size();
44 this->
head = std::clamp<uint64_t>(head_, 0, this->
tokens.size());
49 throw std::overflow_error{
""};
56 throw std::overflow_error{
""};
91 BufferStreamReadOnly stream{
text};
93 backingString.resize(
text.length());
94 BufferStream backing{backingString};
98 while (stream.tell() < stream.size()) {
114 }
catch (
const std::overflow_error&) {}
122 specialSequences[special] = normal;
126 for (
int i = 0; i < str.length(); i++) {
127 if (specialSequences.contains(str[i])) {
129 out += specialSequences[str[i]];
139 for (
int i = 0; i < str.length(); i++) {
140 if (!
escapeSequences.empty() && str[i] ==
'\\' && i < str.length() - 1) {
156 if (stream.tell() == stream.size() &&
string::isWhitespace(stream.seek(-1, std::ios::cur).read<
char>()))
return;
157 stream.seek(-1, std::ios::cur);
161 while (stream.tell() < stream.size() && !
string::isNewLine(stream.read<
char>())) {}
202 if (stream.tell() == stream.size() || stream.peek<
char>() != c) {
212 bool stopAtWhitespace =
true;
213 std::pair<char, char> quoteUsed;
214 auto c = stream.read<
char>();
215 for (
const auto& quote : quotes) {
216 if (c == quote.first) {
217 stopAtWhitespace =
false;
222 if (stopAtWhitespace) {
226 for (c = stream.read<
char>(); (stopAtWhitespace && !
string::isWhitespace(c)) || (!stopAtWhitespace && quoteUsed.second != c); stream >> c) {
228 auto n = stream.read<
char>();
234 }
else if (!stopAtWhitespace && quoteUsed.second == c) {
249 const auto startSpan = backing.tell();
251 bool stopAtWhitespace =
true;
252 std::pair<char, char> quoteUsed;
253 auto c = stream.read<
char>();
254 for (
const auto& quote : quotes) {
255 if (c == quote.first) {
256 stopAtWhitespace =
false;
261 if (stopAtWhitespace) {
265 for (c = stream.read<
char>(); (stopAtWhitespace && !
string::isWhitespace(c)) || (!stopAtWhitespace && quoteUsed.second != c); stream >> c) {
267 auto n = stream.read<
char>();
273 }
else if (!stopAtWhitespace && quoteUsed.second == c) {
285 return {
reinterpret_cast<const char*
>(backing.data()) + startSpan,
static_cast<std::string_view::size_type
>(backing.tell() - 1 - startSpan)};
299 for (
char c = stream.read<
char>(); !
string::isWhitespace(c) && end.find(c) == std::string_view::npos; c = stream.read<
char>()) {
318 const auto startSpan = backing.tell();
320 for (
char c = stream.read<
char>(); !
string::isWhitespace(c) && end.find(c) == std::string_view::npos; c = stream.read<
char>()) {
336 return {
reinterpret_cast<const char*
>(backing.data()) + startSpan,
static_cast<std::string_view::size_type
>(backing.tell() - 1 - startSpan)};
std::vector< std::string_view > tokens
const std::vector< std::string_view > & getTokens() const
void seek(uint64_t head_)
std::string_view peek() const
Tokenizer & setEscapeSequences(const EscapeSequenceMap &escapeSequences_)
Tokenizer & setMultiLineCommentStart(std::string_view multiLineCommentStart_)
std::string_view singleLineCommentStart
Result parse(std::string_view text, std::string &backingString) const
EscapeSequenceMap escapeSequences
Tokenizer & setSingleLineCommentStart(std::string_view singleLineCommentStart_)
Tokenizer & setMultiLineCommentEnd(std::string_view multiLineCommentEnd_)
std::string_view multiLineCommentStart
std::string_view multiLineCommentEnd
std::string convertSpecialCharsToEscapes(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert special characters like \n to escaped special characters like \\n.
std::string convertEscapesToSpecialChars(std::string_view str, const EscapeSequenceMap &escapeSequences)
Convert escaped special characters like \\n to special characters like \n.
void eatMultiLineComment(BufferStream &stream, std::string_view multiLineCommentEnd=DEFAULT_MULTI_LINE_COMMENT_END)
If a multi line comment is detected, eat its contents.
std::string readString(BufferStream &stream, std::initializer_list< std::pair< char, char > > quotes={DEFAULT_QUOTES}, const EscapeSequenceMap &escapeSequences=getDefaultEscapeSequences())
Read a string starting at the current stream position.
std::unordered_map< char, char > EscapeSequenceMap
void eatSingleLineComment(BufferStream &stream)
If a single line comment is detected, eat its contents.
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.
void eatWhitespace(BufferStream &stream)
Eat all whitespace after the current stream position.
bool tryToEatChar(BufferStream &stream, char c)
If the given char exists at the current position, skip over it.
constexpr std::pair DEFAULT_QUOTES
const EscapeSequenceMap & getDefaultEscapeSequencesOrNone(bool useEscapes)
std::string_view readUnquotedStringToBuffer(BufferStream &stream, BufferStream &backing, const EscapeSequenceMap &escapeSequences=getDefaultEscapeSequences())
Read a string starting at the current stream position.
std::string readUnquotedString(BufferStream &stream, const EscapeSequenceMap &escapeSequences=getDefaultEscapeSequences())
Read a string starting at the current stream position.
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.
std::string_view readStringToBuffer(BufferStream &stream, BufferStream &backing, std::initializer_list< std::pair< char, char > > quotes={DEFAULT_QUOTES}, const EscapeSequenceMap &escapeSequences=getDefaultEscapeSequences())
Read a string starting at the current stream position.
const EscapeSequenceMap & getDefaultEscapeSequences()
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.
bool isWhitespace(char c)
If a char is a whitespace character.
bool isNewLine(char c)
If a char is a newline character.