Files
moloapp/src/app/templates/ast.h
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
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.
2026-10-23 18:20:04 +02:00

117 lines
2.6 KiB
C++

#pragma once
// Spec: molo::app::templates AST. Plain data nodes built by the parser,
// executed by the renderer. Expressions cover literals, names, attribute
// and index access, calls, arithmetic, comparisons, logic, membership,
// filters and tests. Gaps vs Jinja (documented): no ternary, no ** power,
// no string % formatting, no super(), no block-level set.
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "../validate/value.h"
namespace molo::app::templates {
struct Expr;
using ExprPtr = std::shared_ptr<Expr>;
struct Expr {
enum class Kind {
Literal,
Name,
Attr,
Index,
Call,
Unary,
Binary,
Compare,
BoolOp,
Not,
In,
Filter,
Test,
List,
Dict
};
Kind kind = Kind::Literal;
validate::Value literal;
std::string name; // Name, Attr attribute, Call callee, Filter/Test name
ExprPtr left;
ExprPtr right;
ExprPtr operand;
ExprPtr seq; // In: left in seq
std::string op;
bool negated = false; // In (not in), Test (is not)
std::vector<ExprPtr> args;
std::vector<std::pair<std::string, ExprPtr>> kwargs;
std::vector<std::pair<ExprPtr, ExprPtr>> pairs; // Dict
};
struct Node;
using NodePtr = std::shared_ptr<Node>;
struct IfBranch {
ExprPtr cond; // null cond means else
std::vector<NodePtr> body;
};
struct Node {
enum class Kind {
Text,
Output,
If,
For,
Block,
Extends,
Include,
Set,
Macro,
Autoescape
};
Kind kind = Kind::Text;
std::string text; // Text content, Block/Macro/Set name
ExprPtr expr; // Output, Extends, Include, Set value
std::vector<IfBranch> branches; // If
std::vector<std::string> targets; // For
ExprPtr seq; // For
std::vector<NodePtr> body; // For, Block, Macro, Autoescape
std::vector<NodePtr> else_body; // For
std::vector<std::string> params; // Macro
std::vector<std::optional<validate::Value>> defaults; // Macro
bool flag = false; // Autoescape enabled
};
struct MacroDef {
std::string name;
std::vector<std::string> params;
std::vector<std::optional<validate::Value>> defaults;
std::vector<NodePtr> body;
};
inline ExprPtr lit(validate::Value v) {
ExprPtr e = std::make_shared<Expr>();
e->kind = Expr::Kind::Literal;
e->literal = std::move(v);
return e;
}
inline ExprPtr name_expr(std::string n) {
ExprPtr e = std::make_shared<Expr>();
e->kind = Expr::Kind::Name;
e->name = std::move(n);
return e;
}
inline NodePtr text_node(std::string t) {
NodePtr n = std::make_shared<Node>();
n->kind = Node::Kind::Text;
n->text = std::move(t);
return n;
}
} // namespace molo::app::templates