I hate my life so much

This commit is contained in:
Андреев Григорий 2024-08-13 01:06:50 +03:00
parent afe50db78c
commit 2e0dbf8bfe
4 changed files with 104 additions and 7 deletions

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% WRITE arg1 %}</title>
</head>
<body>
<p>{% WRITE arg1 %}</p>
</body>
</html>

View File

@ -5,11 +5,35 @@
#include <functional>
namespace nytl {
/* ============== For parsing =============================*/
struct ParsingContext {
std::string text;
size_t pos = 0;
size_t column = 0;
size_t line = 0;
};
#ifdef EOFVAL
#error Son in my shift
#endif
#define EOFVAL -999
int peep(ParsingContext& ctx);
void skip(ParsingContext& ctx);
void skip(ParsingContext& ctx, char ch);
void skipWhitespace(ParsingContext& ctx);
std::vector<std::string> splitIntoLines(const std::string& str);
std::string concatenateLines(const std::vector<std::string>& lines);
void parse_bare_file(const std::string& filename, const std::string& content,
global_elem_set_t& result);
void parse_special_file(const std::string& filename, const std::string& content,
global_elem_set_t& result);
/* =================== For rendering ====================*/
struct LocalVarValue {
bool is_json = false;
std::string EL_name;

View File

@ -21,6 +21,19 @@ namespace nytl {
str.resize(str.size() - 1);
}
std::string clement_lstrip(const std::string& str) {
size_t gone = 0;
size_t n = str.size();
for (size_t i = 0; i < n; i++) {
if (str[i] == '\n') {
gone = i + 1;
} else if (!isSPACE(str[i])) {
return str.substr(gone);
}
}
return "";
}
void parse_bare_file(const std::string& filename, const std::string& content,
global_elem_set_t& result)
{
@ -42,7 +55,6 @@ namespace nytl {
smallest_tab = tab_sz;
had_nw_line = true;
}
rstrip(current_line);
lines.push_back(current_line);
}
current_line.clear();
@ -66,14 +78,62 @@ namespace nytl {
}
Element& el = result[filename];
el.parts = {ElementPart{element_part_types::code}};
std::string lines_cat;
std::string lines_cat = concatenateLines(lines);
el.parts[0].when_code.lines = mv(lines_cat);
}
int peep(ParsingContext &ctx) {
if (ctx.text.size() <= ctx.pos)
return EOFVAL;
return ctx.text[ctx.pos];
}
void advance(ParsingContext& ctx) {
if (ctx.text[ctx.pos] == '\n') {
ctx.line++;
ctx.column = 0;
} else {
ctx.column++;
}
ctx.pos++;
}
void skip(ParsingContext& ctx) {
ASSERT(ctx.pos < ctx.text.size(), "Unexpected EOF");
advance(ctx);
}
void skip(ParsingContext& ctx, char ch) {
ASSERT(ctx.pos < ctx.text.size(), "Unexpected EOF");
ASSERT(ctx.text[ctx.pos] == ch, "Unexpected character");
advance(ctx);
}
void skipWhitespace(ParsingContext &ctx) {
while (peep(ctx) > 0 && isSPACE((char)peep(ctx)))
skip(ctx);
}
std::vector<std::string> splitIntoLines(const std::string &str) {
std::vector<std::string> result;
for (char ch: str) {
if (ch == '\n')
result.emplace_back();
else
result.back() += ch;
}
return result;
}
std::string concatenateLines(const std::vector<std::string>& lines) {
std::string result;
size_t n = lines.size();
for (size_t i = 0; i < n; i++) {
lines_cat += lines[i];
if (i + 1 < n)
lines_cat += '\n';
if (i)
result += '\n';
result += lines[i];
}
el.parts[0].when_code.lines = mv(lines_cat);
return result;
}
void parse_special_file(const std::string& filename, const std::string& content,

View File

@ -13,5 +13,8 @@ int main(int argc, char** argv) {
templater.update();
std::string answer = templater.render("chat", {});
printf("%s\n<a><f><t><e><r><><l><f>\n", answer.c_str());
std::string answer2 = templater.render("test", {});
printf("%s\n<a><f><t><e><r><><l><f>\n", answer.c_str());
return 0;
}