6#include <BufferStream.h>
24 return defaultEscapeSequences;
33 return c ==
'\n' || c ==
'\r';
37 return std::ranges::all_of(str, [](
char c) {
return isNewLine(c); });
41 return c ==
' ' || c ==
'\a' || c ==
'\f' || c ==
'\t' || c ==
'\v' ||
isNewLine(c);
45 return std::ranges::all_of(str, [](
char c) {
return isWhitespace(c); });
49 return std::isdigit(c);
53 return std::ranges::all_of(str, [](
char c) {
return isNumber(c); });
59 for (
const auto& [normal, special] : escapeSequences) {
60 specialSequences[special] = normal;
64 for (
int i = 0; i < str.length(); i++) {
65 if (specialSequences.contains(str[i])) {
67 out += specialSequences[str[i]];
77 for (
int i = 0; i < str.length(); i++) {
78 if (!escapeSequences.empty() && str[i] ==
'\\' && i < str.length() - 1) {
80 if (escapeSequences.contains(n)) {
81 out += escapeSequences.at(n);
95 stream.seek(-1, std::ios::cur);
99 while (!
isNewLine(stream.read<
char>())) {}
103 while (!std::ranges::equal(stream.read_span<
char>(multiLineCommentEnd.length()), multiLineCommentEnd)) {
104 stream.seek(-
static_cast<int64_t
>(multiLineCommentEnd.length() - 1), std::ios::cur);
120 if (!singleLineCommentStart.empty()) {
121 if (std::ranges::equal(stream.read_span<
char>(singleLineCommentStart.length()), singleLineCommentStart)) {
126 stream.seek(-
static_cast<int64_t
>(singleLineCommentStart.length()), std::ios::cur);
129 if (!multiLineCommentStart.empty()) {
130 if (std::ranges::equal(stream.read_span<
char>(multiLineCommentStart.length()), multiLineCommentStart)) {
135 stream.seek(-
static_cast<int64_t
>(multiLineCommentStart.length()), std::ios::cur);
140 if (stream.peek<
char>() != c) {
148 const auto startSpan = backing.tell();
150 bool stopAtWhitespace =
true;
151 char c = stream.read<
char>();
152 if (start.find(c) != std::string_view::npos) {
153 stopAtWhitespace =
false;
158 for (c = stream.read<
char>(); (stopAtWhitespace && !
isWhitespace(c)) || (!stopAtWhitespace && end.find(c) == std::string_view::npos); c = stream.read<
char>()) {
159 if (!escapeSequences.empty() && c ==
'\\') {
160 auto n = stream.read<
char>();
164 if (escapeSequences.contains(n)) {
165 backing << escapeSequences.at(n);
166 }
else if (!stopAtWhitespace && end.find(n) != std::string_view::npos) {
177 return {
reinterpret_cast<const char*
>(backing.data()) + startSpan, backing.tell() - 1 - startSpan};
185 const auto startSpan = backing.tell();
187 for (
char c = stream.read<
char>(); !
isWhitespace(c) && end.find(c) == std::string_view::npos; c = stream.read<
char>()) {
188 if (!escapeSequences.empty() && c ==
'\\') {
189 auto n = stream.read<
char>();
190 if (escapeSequences.contains(n)) {
191 backing << escapeSequences.at(n);
192 }
else if (
isWhitespace(n) || end.find(n) != std::string_view::npos) {
203 return {
reinterpret_cast<const char*
>(backing.data()) + startSpan, backing.tell() - 1 - startSpan};
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.
std::unordered_map< char, char > EscapeSequenceMap
void eatMultiLineComment(BufferStream &stream, std::string_view multiLineCommentEnd=DEFAULT_MULTI_LINE_COMMENT_END)
If a multi line comment is detected, eat its contents.
bool isNewLine(char c)
If a char is a newline character.
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.
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_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.
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.
bool isNumber(char c)
If a char is a numerical character (0-9).
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.