Add escaping of characters
This commit is contained in:
parent
28be145a70
commit
63584525f2
@ -5,7 +5,7 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <optional>
|
||||
|
||||
static bool iswhitespace(char& character) {
|
||||
return character == ' '
|
||||
@ -14,6 +14,12 @@ static bool iswhitespace(char& character) {
|
||||
|| character == '\r';
|
||||
}
|
||||
|
||||
static std::optional<char> get_escaped(std::string_view inspected) {
|
||||
if (inspected == "n")
|
||||
return '\n';
|
||||
return {};
|
||||
}
|
||||
|
||||
namespace token {
|
||||
std::string type_name(Type& type) {
|
||||
switch (type) {
|
||||
@ -152,9 +158,27 @@ namespace token {
|
||||
std::string content{};
|
||||
c = text[++i]; // Skip initial "
|
||||
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;
|
||||
if ((i + 1) >= text_length) break;
|
||||
c = text[++i];
|
||||
}
|
||||
} while (c != '\"');
|
||||
i++; // Skip second "
|
||||
tokens.push_back(token::Token{ token::Type::LiteralStr, content, meta + (content.size() + 2) });
|
||||
|
||||
14
test.c
14
test.c
@ -13,35 +13,35 @@ void change_first(char otus[5]) {
|
||||
|
||||
struct Otus;
|
||||
|
||||
void update(struct Otus potus) {
|
||||
potus.field = 20;
|
||||
}
|
||||
struct Otus {
|
||||
int field;
|
||||
};
|
||||
void update(struct Otus potus) {
|
||||
potus.field = 20;
|
||||
}
|
||||
|
||||
void update_ptr(char* ptr) {
|
||||
*ptr = 50;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char text[29] = "10th fibonacci number is %d!";
|
||||
char text[30] = "10th fibonacci number is %d!\n";
|
||||
printf(text, fibonacci(10));
|
||||
|
||||
char somelist[5] = { 1, 2, 3, 4, 5 };
|
||||
|
||||
change_first(somelist);
|
||||
|
||||
printf(" first element: %d!", somelist[0]);
|
||||
printf("first element: %d!\n", somelist[0]);
|
||||
|
||||
struct Otus otus = { 5 };
|
||||
update(otus);
|
||||
|
||||
printf(" first field: %d!", otus.field);
|
||||
printf("first field: %d!\n", otus.field);
|
||||
|
||||
char hello = 10;
|
||||
update_ptr(&hello);
|
||||
printf(" hello: %d!", hello);
|
||||
printf("hello: %d!\n", hello);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user