Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
140 lines
4.0 KiB
C++
140 lines
4.0 KiB
C++
#pragma once
|
|
// Spec: molo::app::templates lexer. Splits source into Text, Var ({{ }}),
|
|
// Block ({% %}) tokens; comments ({# #}) are dropped. Whitespace control
|
|
// dashes ({{- , -}}) strip adjacent whitespace runs. Tag close search
|
|
// respects single and double quoted strings.
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace molo::app::templates {
|
|
|
|
class TemplateError : public std::runtime_error {
|
|
public:
|
|
explicit TemplateError(const std::string& msg) : std::runtime_error(msg) {}
|
|
};
|
|
|
|
class UndefinedError : public TemplateError {
|
|
public:
|
|
explicit UndefinedError(const std::string& msg) : TemplateError(msg) {}
|
|
};
|
|
|
|
enum class TokenKind { Text, Var, Block };
|
|
|
|
struct Token {
|
|
TokenKind kind = TokenKind::Text;
|
|
std::string text;
|
|
std::size_t line = 1;
|
|
};
|
|
|
|
inline bool is_space_char(char c) {
|
|
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
|
|
}
|
|
|
|
inline void rstrip_token(std::string& s) {
|
|
while (!s.empty() && is_space_char(s.back())) s.pop_back();
|
|
}
|
|
|
|
inline std::size_t find_tag_close(std::string_view src, std::size_t from, char close_first,
|
|
char close_second) {
|
|
char quote = 0;
|
|
for (std::size_t i = from; i + 1 < src.size(); ++i) {
|
|
char c = src[i];
|
|
if (quote) {
|
|
if (c == '\\') {
|
|
++i;
|
|
continue;
|
|
}
|
|
if (c == quote) quote = 0;
|
|
continue;
|
|
}
|
|
if (c == '\'' || c == '"') {
|
|
quote = c;
|
|
continue;
|
|
}
|
|
if (c == close_first && src[i + 1] == close_second) return i;
|
|
}
|
|
return std::string_view::npos;
|
|
}
|
|
|
|
inline std::vector<Token> lex_template(std::string_view src, std::string_view source_name) {
|
|
std::vector<Token> out;
|
|
std::size_t line = 1;
|
|
std::size_t i = 0;
|
|
auto push_text = [&](std::string_view t) {
|
|
if (t.empty()) return;
|
|
Token tok;
|
|
tok.kind = TokenKind::Text;
|
|
tok.text = std::string(t);
|
|
tok.line = line;
|
|
for (char c : t) {
|
|
if (c == '\n') ++line;
|
|
}
|
|
out.push_back(std::move(tok));
|
|
};
|
|
while (i < src.size()) {
|
|
std::size_t var_at = src.find("{{", i);
|
|
std::size_t blk_at = src.find("{%", i);
|
|
std::size_t com_at = src.find("{#", i);
|
|
std::size_t next = std::string_view::npos;
|
|
if (var_at != std::string_view::npos) next = var_at;
|
|
if (blk_at != std::string_view::npos && (next == std::string_view::npos || blk_at < next)) {
|
|
next = blk_at;
|
|
}
|
|
if (com_at != std::string_view::npos && (next == std::string_view::npos || com_at < next)) {
|
|
next = com_at;
|
|
}
|
|
if (next == std::string_view::npos) {
|
|
push_text(src.substr(i));
|
|
break;
|
|
}
|
|
push_text(src.substr(i, next - i));
|
|
if (next == com_at) {
|
|
std::size_t end = src.find("#}", next + 2);
|
|
if (end == std::string_view::npos) {
|
|
throw TemplateError("unterminated comment in " + std::string(source_name));
|
|
}
|
|
for (std::size_t k = next; k < end + 2; ++k) {
|
|
if (src[k] == '\n') ++line;
|
|
}
|
|
i = end + 2;
|
|
continue;
|
|
}
|
|
bool is_var = (next == var_at);
|
|
char cf = is_var ? '}' : '%';
|
|
std::size_t open_end = next + 2;
|
|
bool lstrip = open_end < src.size() && src[open_end] == '-';
|
|
if (lstrip) ++open_end;
|
|
std::size_t close = find_tag_close(src, open_end, cf, '}');
|
|
if (close == std::string_view::npos) {
|
|
throw TemplateError("unterminated tag in " + std::string(source_name));
|
|
}
|
|
std::size_t inner_end = close;
|
|
bool rstrip = inner_end > open_end && src[inner_end - 1] == '-';
|
|
if (rstrip) --inner_end;
|
|
if (lstrip && !out.empty() && out.back().kind == TokenKind::Text) {
|
|
rstrip_token(out.back().text);
|
|
}
|
|
Token tok;
|
|
tok.kind = is_var ? TokenKind::Var : TokenKind::Block;
|
|
tok.text = std::string(src.substr(open_end, inner_end - open_end));
|
|
tok.line = line;
|
|
for (std::size_t k = next; k < close + 2; ++k) {
|
|
if (src[k] == '\n') ++line;
|
|
}
|
|
out.push_back(std::move(tok));
|
|
i = close + 2;
|
|
if (rstrip) {
|
|
while (i < src.size() && is_space_char(src[i])) {
|
|
if (src[i] == '\n') ++line;
|
|
++i;
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace molo::app::templates
|