Add escaping of characters

This commit is contained in:
Sofia 2026-04-16 20:25:00 +03:00
parent 28be145a70
commit 63584525f2
2 changed files with 35 additions and 11 deletions

View File

@ -5,7 +5,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <optional>
static bool iswhitespace(char& character) { static bool iswhitespace(char& character) {
return character == ' ' return character == ' '
@ -14,6 +14,12 @@ static bool iswhitespace(char& character) {
|| character == '\r'; || character == '\r';
} }
static std::optional<char> get_escaped(std::string_view inspected) {
if (inspected == "n")
return '\n';
return {};
}
namespace token { namespace token {
std::string type_name(Type& type) { std::string type_name(Type& type) {
switch (type) { switch (type) {
@ -152,9 +158,27 @@ namespace token {
std::string content{}; std::string content{};
c = text[++i]; // Skip initial " c = text[++i]; // Skip initial "
do { do {
if (c == '\\') {
std::string escaped_content{};
if ((i + 1) >= text_length) break;
auto potential = get_escaped(escaped_content + text[++i]);
std::cout << "\"" << escaped_content << "\"" << std::endl;
while (potential.has_value() && (i + 1) < text_length) {
escaped_content += text[i];
std::cout << "\"" << escaped_content << "\"" << std::endl;
potential = get_escaped(escaped_content + text[++i]);
}
auto escaped = get_escaped(escaped_content);
if (escaped.has_value())
content += *escaped;
c = text[i];
}
else {
content += c; content += c;
if ((i + 1) >= text_length) break; if ((i + 1) >= text_length) break;
c = text[++i]; c = text[++i];
}
} while (c != '\"'); } while (c != '\"');
i++; // Skip second " i++; // Skip second "
tokens.push_back(token::Token{ token::Type::LiteralStr, content, meta + (content.size() + 2) }); tokens.push_back(token::Token{ token::Type::LiteralStr, content, meta + (content.size() + 2) });

14
test.c
View File

@ -13,35 +13,35 @@ void change_first(char otus[5]) {
struct Otus; struct Otus;
void update(struct Otus potus) {
potus.field = 20;
}
struct Otus { struct Otus {
int field; int field;
}; };
void update(struct Otus potus) {
potus.field = 20;
}
void update_ptr(char* ptr) { void update_ptr(char* ptr) {
*ptr = 50; *ptr = 50;
} }
int main() { int main() {
char text[29] = "10th fibonacci number is %d!"; char text[30] = "10th fibonacci number is %d!\n";
printf(text, fibonacci(10)); printf(text, fibonacci(10));
char somelist[5] = { 1, 2, 3, 4, 5 }; char somelist[5] = { 1, 2, 3, 4, 5 };
change_first(somelist); change_first(somelist);
printf(" first element: %d!", somelist[0]); printf("first element: %d!\n", somelist[0]);
struct Otus otus = { 5 }; struct Otus otus = { 5 };
update(otus); update(otus);
printf(" first field: %d!", otus.field); printf("first field: %d!\n", otus.field);
char hello = 10; char hello = 10;
update_ptr(&hello); update_ptr(&hello);
printf(" hello: %d!", hello); printf("hello: %d!\n", hello);
return 0; return 0;
} }