runtime.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. #pragma once
  2. #include "lexer.h"
  3. #include "value.h"
  4. #include <cassert>
  5. #include <ctime>
  6. #include <memory>
  7. #include <sstream>
  8. #include <string>
  9. #include <vector>
  10. #define JJ_DEBUG(msg, ...) do { if (g_jinja_debug) printf("%s:%-3d : " msg "\n", FILENAME, __LINE__, __VA_ARGS__); } while (0)
  11. extern bool g_jinja_debug;
  12. namespace jinja {
  13. struct statement;
  14. using statement_ptr = std::unique_ptr<statement>;
  15. using statements = std::vector<statement_ptr>;
  16. // Helpers for dynamic casting and type checking
  17. template<typename T>
  18. struct extract_pointee_unique {
  19. using type = T;
  20. };
  21. template<typename U>
  22. struct extract_pointee_unique<std::unique_ptr<U>> {
  23. using type = U;
  24. };
  25. template<typename T>
  26. bool is_stmt(const statement_ptr & ptr) {
  27. return dynamic_cast<const T*>(ptr.get()) != nullptr;
  28. }
  29. template<typename T>
  30. T * cast_stmt(statement_ptr & ptr) {
  31. return dynamic_cast<T*>(ptr.get());
  32. }
  33. template<typename T>
  34. const T * cast_stmt(const statement_ptr & ptr) {
  35. return dynamic_cast<const T*>(ptr.get());
  36. }
  37. // End Helpers
  38. // not thread-safe
  39. void enable_debug(bool enable);
  40. struct context {
  41. std::shared_ptr<std::string> src; // for debugging; use shared_ptr to avoid copying on scope creation
  42. std::time_t current_time; // for functions that need current time
  43. bool is_get_stats = false; // whether to collect stats
  44. // src is optional, used for error reporting
  45. context(std::string src = "") : src(std::make_shared<std::string>(std::move(src))) {
  46. env = mk_val<value_object>();
  47. env->has_builtins = false; // context object has no builtins
  48. env->insert("true", mk_val<value_bool>(true));
  49. env->insert("True", mk_val<value_bool>(true));
  50. env->insert("false", mk_val<value_bool>(false));
  51. env->insert("False", mk_val<value_bool>(false));
  52. env->insert("none", mk_val<value_none>());
  53. env->insert("None", mk_val<value_none>());
  54. current_time = std::time(nullptr);
  55. }
  56. ~context() = default;
  57. context(const context & parent) : context() {
  58. // inherit variables (for example, when entering a new scope)
  59. auto & pvar = parent.env->as_ordered_object();
  60. for (const auto & pair : pvar) {
  61. set_val(pair.first, pair.second);
  62. }
  63. current_time = parent.current_time;
  64. is_get_stats = parent.is_get_stats;
  65. src = parent.src;
  66. }
  67. value get_val(const std::string & name) {
  68. auto it = env->val_obj.unordered.find(name);
  69. if (it != env->val_obj.unordered.end()) {
  70. return it->second;
  71. } else {
  72. return mk_val<value_undefined>(name);
  73. }
  74. }
  75. void set_val(const std::string & name, const value & val) {
  76. env->insert(name, val);
  77. }
  78. void print_vars() const {
  79. printf("Context Variables:\n%s\n", value_to_json(env, 2).c_str());
  80. }
  81. private:
  82. value_object env;
  83. };
  84. /**
  85. * Base class for all nodes in the AST.
  86. */
  87. struct statement {
  88. size_t pos; // position in source, for debugging
  89. virtual ~statement() = default;
  90. virtual std::string type() const { return "Statement"; }
  91. // execute_impl must be overridden by derived classes
  92. virtual value execute_impl(context &) { throw std::runtime_error("cannot exec " + type()); }
  93. // execute is the public method to execute a statement with error handling
  94. value execute(context &);
  95. };
  96. // Type Checking Utilities
  97. template<typename T>
  98. static void chk_type(const statement_ptr & ptr) {
  99. if (!ptr) return; // Allow null for optional fields
  100. assert(dynamic_cast<T *>(ptr.get()) != nullptr);
  101. }
  102. template<typename T, typename U>
  103. static void chk_type(const statement_ptr & ptr) {
  104. if (!ptr) return;
  105. assert(dynamic_cast<T *>(ptr.get()) != nullptr || dynamic_cast<U *>(ptr.get()) != nullptr);
  106. }
  107. // Base Types
  108. /**
  109. * Expressions will result in a value at runtime (unlike statements).
  110. */
  111. struct expression : public statement {
  112. std::string type() const override { return "Expression"; }
  113. };
  114. // Statements
  115. struct program : public statement {
  116. statements body;
  117. program() = default;
  118. explicit program(statements && body) : body(std::move(body)) {}
  119. std::string type() const override { return "Program"; }
  120. value execute_impl(context &) override {
  121. throw std::runtime_error("Cannot execute program directly, use jinja::runtime instead");
  122. }
  123. };
  124. struct if_statement : public statement {
  125. statement_ptr test;
  126. statements body;
  127. statements alternate;
  128. if_statement(statement_ptr && test, statements && body, statements && alternate)
  129. : test(std::move(test)), body(std::move(body)), alternate(std::move(alternate)) {
  130. chk_type<expression>(this->test);
  131. }
  132. std::string type() const override { return "If"; }
  133. value execute_impl(context & ctx) override;
  134. };
  135. struct identifier;
  136. struct tuple_literal;
  137. /**
  138. * Loop over each item in a sequence
  139. * https://jinja.palletsprojects.com/en/3.0.x/templates/#for
  140. */
  141. struct for_statement : public statement {
  142. statement_ptr loopvar; // Identifier | TupleLiteral
  143. statement_ptr iterable;
  144. statements body;
  145. statements default_block; // if no iteration took place
  146. for_statement(statement_ptr && loopvar, statement_ptr && iterable, statements && body, statements && default_block)
  147. : loopvar(std::move(loopvar)), iterable(std::move(iterable)),
  148. body(std::move(body)), default_block(std::move(default_block)) {
  149. chk_type<identifier, tuple_literal>(this->loopvar);
  150. chk_type<expression>(this->iterable);
  151. }
  152. std::string type() const override { return "For"; }
  153. value execute_impl(context & ctx) override;
  154. };
  155. struct break_statement : public statement {
  156. std::string type() const override { return "Break"; }
  157. struct signal : public std::exception {
  158. const char* what() const noexcept override {
  159. return "Break statement executed";
  160. }
  161. };
  162. value execute_impl(context &) override {
  163. throw break_statement::signal();
  164. }
  165. };
  166. struct continue_statement : public statement {
  167. std::string type() const override { return "Continue"; }
  168. struct signal : public std::exception {
  169. const char* what() const noexcept override {
  170. return "Continue statement executed";
  171. }
  172. };
  173. value execute_impl(context &) override {
  174. throw continue_statement::signal();
  175. }
  176. };
  177. // do nothing
  178. struct noop_statement : public statement {
  179. std::string type() const override { return "Noop"; }
  180. value execute_impl(context &) override {
  181. return mk_val<value_undefined>();
  182. }
  183. };
  184. struct set_statement : public statement {
  185. statement_ptr assignee;
  186. statement_ptr val;
  187. statements body;
  188. set_statement(statement_ptr && assignee, statement_ptr && value, statements && body)
  189. : assignee(std::move(assignee)), val(std::move(value)), body(std::move(body)) {
  190. chk_type<expression>(this->assignee);
  191. chk_type<expression>(this->val);
  192. }
  193. std::string type() const override { return "Set"; }
  194. value execute_impl(context & ctx) override;
  195. };
  196. struct macro_statement : public statement {
  197. statement_ptr name;
  198. statements args;
  199. statements body;
  200. macro_statement(statement_ptr && name, statements && args, statements && body)
  201. : name(std::move(name)), args(std::move(args)), body(std::move(body)) {
  202. chk_type<identifier>(this->name);
  203. for (const auto& arg : this->args) chk_type<expression>(arg);
  204. }
  205. std::string type() const override { return "Macro"; }
  206. value execute_impl(context & ctx) override;
  207. };
  208. struct comment_statement : public statement {
  209. std::string val;
  210. explicit comment_statement(const std::string & v) : val(v) {}
  211. std::string type() const override { return "Comment"; }
  212. value execute_impl(context &) override {
  213. return mk_val<value_undefined>();
  214. }
  215. };
  216. // Expressions
  217. struct member_expression : public expression {
  218. statement_ptr object;
  219. statement_ptr property;
  220. bool computed; // true if obj[expr] and false if obj.prop
  221. member_expression(statement_ptr && object, statement_ptr && property, bool computed)
  222. : object(std::move(object)), property(std::move(property)), computed(computed) {
  223. chk_type<expression>(this->object);
  224. chk_type<expression>(this->property);
  225. }
  226. std::string type() const override { return "MemberExpression"; }
  227. value execute_impl(context & ctx) override;
  228. };
  229. struct call_expression : public expression {
  230. statement_ptr callee;
  231. statements args;
  232. call_expression(statement_ptr && callee, statements && args)
  233. : callee(std::move(callee)), args(std::move(args)) {
  234. chk_type<expression>(this->callee);
  235. for (const auto& arg : this->args) chk_type<expression>(arg);
  236. }
  237. std::string type() const override { return "CallExpression"; }
  238. value execute_impl(context & ctx) override;
  239. };
  240. /**
  241. * Represents a user-defined variable or symbol in the template.
  242. */
  243. struct identifier : public expression {
  244. std::string val;
  245. explicit identifier(const std::string & val) : val(val) {}
  246. std::string type() const override { return "Identifier"; }
  247. value execute_impl(context & ctx) override;
  248. };
  249. // Literals
  250. struct integer_literal : public expression {
  251. int64_t val;
  252. explicit integer_literal(int64_t val) : val(val) {}
  253. std::string type() const override { return "IntegerLiteral"; }
  254. value execute_impl(context &) override {
  255. return mk_val<value_int>(val);
  256. }
  257. };
  258. struct float_literal : public expression {
  259. double val;
  260. explicit float_literal(double val) : val(val) {}
  261. std::string type() const override { return "FloatLiteral"; }
  262. value execute_impl(context &) override {
  263. return mk_val<value_float>(val);
  264. }
  265. };
  266. struct string_literal : public expression {
  267. std::string val;
  268. explicit string_literal(const std::string & val) : val(val) {}
  269. std::string type() const override { return "StringLiteral"; }
  270. value execute_impl(context &) override {
  271. return mk_val<value_string>(val);
  272. }
  273. };
  274. struct array_literal : public expression {
  275. statements val;
  276. explicit array_literal(statements && val) : val(std::move(val)) {
  277. for (const auto& item : this->val) chk_type<expression>(item);
  278. }
  279. std::string type() const override { return "ArrayLiteral"; }
  280. value execute_impl(context & ctx) override {
  281. auto arr = mk_val<value_array>();
  282. for (const auto & item_stmt : val) {
  283. arr->push_back(item_stmt->execute(ctx));
  284. }
  285. return arr;
  286. }
  287. };
  288. struct tuple_literal : public array_literal {
  289. explicit tuple_literal(statements && val) : array_literal(std::move(val)) {}
  290. std::string type() const override { return "TupleLiteral"; }
  291. };
  292. struct object_literal : public expression {
  293. std::vector<std::pair<statement_ptr, statement_ptr>> val;
  294. explicit object_literal(std::vector<std::pair<statement_ptr, statement_ptr>> && val)
  295. : val(std::move(val)) {
  296. for (const auto & pair : this->val) {
  297. chk_type<expression>(pair.first);
  298. chk_type<expression>(pair.second);
  299. }
  300. }
  301. std::string type() const override { return "ObjectLiteral"; }
  302. value execute_impl(context & ctx) override;
  303. };
  304. // Complex Expressions
  305. /**
  306. * An operation with two sides, separated by an operator.
  307. * Note: Either side can be a Complex Expression, with order
  308. * of operations being determined by the operator.
  309. */
  310. struct binary_expression : public expression {
  311. token op;
  312. statement_ptr left;
  313. statement_ptr right;
  314. binary_expression(token op, statement_ptr && left, statement_ptr && right)
  315. : op(std::move(op)), left(std::move(left)), right(std::move(right)) {
  316. chk_type<expression>(this->left);
  317. chk_type<expression>(this->right);
  318. }
  319. std::string type() const override { return "BinaryExpression"; }
  320. value execute_impl(context & ctx) override;
  321. };
  322. /**
  323. * An operation with two sides, separated by the | operator.
  324. * Operator precedence: https://github.com/pallets/jinja/issues/379#issuecomment-168076202
  325. */
  326. struct filter_expression : public expression {
  327. // either an expression or a value is allowed
  328. statement_ptr operand;
  329. value_string val; // will be set by filter_statement
  330. statement_ptr filter;
  331. filter_expression(statement_ptr && operand, statement_ptr && filter)
  332. : operand(std::move(operand)), filter(std::move(filter)) {
  333. chk_type<expression>(this->operand);
  334. chk_type<identifier, call_expression>(this->filter);
  335. }
  336. filter_expression(value_string && val, statement_ptr && filter)
  337. : val(std::move(val)), filter(std::move(filter)) {
  338. chk_type<identifier, call_expression>(this->filter);
  339. }
  340. std::string type() const override { return "FilterExpression"; }
  341. value execute_impl(context & ctx) override;
  342. };
  343. struct filter_statement : public statement {
  344. statement_ptr filter;
  345. statements body;
  346. filter_statement(statement_ptr && filter, statements && body)
  347. : filter(std::move(filter)), body(std::move(body)) {
  348. chk_type<identifier, call_expression>(this->filter);
  349. }
  350. std::string type() const override { return "FilterStatement"; }
  351. value execute_impl(context & ctx) override;
  352. };
  353. /**
  354. * An operation which filters a sequence of objects by applying a test to each object,
  355. * and only selecting the objects with the test succeeding.
  356. *
  357. * It may also be used as a shortcut for a ternary operator.
  358. */
  359. struct select_expression : public expression {
  360. statement_ptr lhs;
  361. statement_ptr test;
  362. select_expression(statement_ptr && lhs, statement_ptr && test)
  363. : lhs(std::move(lhs)), test(std::move(test)) {
  364. chk_type<expression>(this->lhs);
  365. chk_type<expression>(this->test);
  366. }
  367. std::string type() const override { return "SelectExpression"; }
  368. value execute_impl(context & ctx) override {
  369. auto predicate = test->execute_impl(ctx);
  370. if (!predicate->as_bool()) {
  371. return mk_val<value_undefined>();
  372. }
  373. return lhs->execute_impl(ctx);
  374. }
  375. };
  376. /**
  377. * An operation with two sides, separated by the "is" operator.
  378. * NOTE: "value is something" translates to function call "test_is_something(value)"
  379. */
  380. struct test_expression : public expression {
  381. statement_ptr operand;
  382. bool negate;
  383. statement_ptr test;
  384. test_expression(statement_ptr && operand, bool negate, statement_ptr && test)
  385. : operand(std::move(operand)), negate(negate), test(std::move(test)) {
  386. chk_type<expression>(this->operand);
  387. chk_type<identifier, call_expression>(this->test);
  388. }
  389. std::string type() const override { return "TestExpression"; }
  390. value execute_impl(context & ctx) override;
  391. };
  392. /**
  393. * An operation with one side (operator on the left).
  394. */
  395. struct unary_expression : public expression {
  396. token op;
  397. statement_ptr argument;
  398. unary_expression(token op, statement_ptr && argument)
  399. : op(std::move(op)), argument(std::move(argument)) {
  400. chk_type<expression>(this->argument);
  401. }
  402. std::string type() const override { return "UnaryExpression"; }
  403. value execute_impl(context & ctx) override;
  404. };
  405. struct slice_expression : public expression {
  406. statement_ptr start_expr;
  407. statement_ptr stop_expr;
  408. statement_ptr step_expr;
  409. slice_expression(statement_ptr && start_expr, statement_ptr && stop_expr, statement_ptr && step_expr)
  410. : start_expr(std::move(start_expr)), stop_expr(std::move(stop_expr)), step_expr(std::move(step_expr)) {
  411. chk_type<expression>(this->start_expr);
  412. chk_type<expression>(this->stop_expr);
  413. chk_type<expression>(this->step_expr);
  414. }
  415. std::string type() const override { return "SliceExpression"; }
  416. value execute_impl(context &) override {
  417. throw std::runtime_error("must be handled by MemberExpression");
  418. }
  419. };
  420. struct keyword_argument_expression : public expression {
  421. statement_ptr key;
  422. statement_ptr val;
  423. keyword_argument_expression(statement_ptr && key, statement_ptr && val)
  424. : key(std::move(key)), val(std::move(val)) {
  425. chk_type<identifier>(this->key);
  426. chk_type<expression>(this->val);
  427. }
  428. std::string type() const override { return "KeywordArgumentExpression"; }
  429. value execute_impl(context & ctx) override;
  430. };
  431. struct spread_expression : public expression {
  432. statement_ptr argument;
  433. explicit spread_expression(statement_ptr && argument) : argument(std::move(argument)) {
  434. chk_type<expression>(this->argument);
  435. }
  436. std::string type() const override { return "SpreadExpression"; }
  437. };
  438. struct call_statement : public statement {
  439. statement_ptr call;
  440. statements caller_args;
  441. statements body;
  442. call_statement(statement_ptr && call, statements && caller_args, statements && body)
  443. : call(std::move(call)), caller_args(std::move(caller_args)), body(std::move(body)) {
  444. chk_type<call_expression>(this->call);
  445. for (const auto & arg : this->caller_args) chk_type<expression>(arg);
  446. }
  447. std::string type() const override { return "CallStatement"; }
  448. };
  449. struct ternary_expression : public expression {
  450. statement_ptr condition;
  451. statement_ptr true_expr;
  452. statement_ptr false_expr;
  453. ternary_expression(statement_ptr && condition, statement_ptr && true_expr, statement_ptr && false_expr)
  454. : condition(std::move(condition)), true_expr(std::move(true_expr)), false_expr(std::move(false_expr)) {
  455. chk_type<expression>(this->condition);
  456. chk_type<expression>(this->true_expr);
  457. chk_type<expression>(this->false_expr);
  458. }
  459. std::string type() const override { return "Ternary"; }
  460. value execute_impl(context & ctx) override {
  461. value cond_val = condition->execute(ctx);
  462. if (cond_val->as_bool()) {
  463. return true_expr->execute(ctx);
  464. } else {
  465. return false_expr->execute(ctx);
  466. }
  467. }
  468. };
  469. struct raised_exception : public std::exception {
  470. std::string message;
  471. raised_exception(const std::string & msg) : message(msg) {}
  472. const char* what() const noexcept override {
  473. return message.c_str();
  474. }
  475. };
  476. // Used to rethrow exceptions with modified messages
  477. struct rethrown_exception : public std::exception {
  478. std::string message;
  479. rethrown_exception(const std::string & msg) : message(msg) {}
  480. const char* what() const noexcept override {
  481. return message.c_str();
  482. }
  483. };
  484. //////////////////////
  485. static void gather_string_parts_recursive(const value & val, value_string & parts) {
  486. // TODO: probably allow print value_none as "None" string? currently this breaks some templates
  487. if (is_val<value_string>(val)) {
  488. const auto & str_val = cast_val<value_string>(val)->val_str;
  489. parts->val_str.append(str_val);
  490. } else if (is_val<value_int>(val) || is_val<value_float>(val) || is_val<value_bool>(val)) {
  491. std::string str_val = val->as_string().str();
  492. parts->val_str.append(str_val);
  493. } else if (is_val<value_array>(val)) {
  494. auto items = cast_val<value_array>(val)->as_array();
  495. for (const auto & item : items) {
  496. gather_string_parts_recursive(item, parts);
  497. }
  498. }
  499. }
  500. static std::string render_string_parts(const value_string & parts) {
  501. std::ostringstream oss;
  502. for (const auto & part : parts->val_str.parts) {
  503. oss << part.val;
  504. }
  505. return oss.str();
  506. }
  507. struct runtime {
  508. context & ctx;
  509. explicit runtime(context & ctx) : ctx(ctx) {}
  510. value_array execute(const program & prog) {
  511. value_array results = mk_val<value_array>();
  512. for (const auto & stmt : prog.body) {
  513. value res = stmt->execute(ctx);
  514. results->push_back(std::move(res));
  515. }
  516. return results;
  517. }
  518. static value_string gather_string_parts(const value & val) {
  519. value_string parts = mk_val<value_string>();
  520. gather_string_parts_recursive(val, parts);
  521. // join consecutive parts with the same type
  522. auto & p = parts->val_str.parts;
  523. for (size_t i = 1; i < p.size(); ) {
  524. if (p[i].is_input == p[i - 1].is_input) {
  525. p[i - 1].val += p[i].val;
  526. p.erase(p.begin() + i);
  527. } else {
  528. i++;
  529. }
  530. }
  531. return parts;
  532. }
  533. };
  534. } // namespace jinja