KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.7KB

  1. <?php
  2. /**
  3. * Twig::Compiler
  4. * ~~~~~~~~~~~~~~
  5. *
  6. * This module implements the Twig compiler.
  7. *
  8. * :copyright: 2008 by Armin Ronacher.
  9. * :license: BSD.
  10. */
  11. // mark the compiler as being included. This use used by the public
  12. // `twig_load_compiler` function that loads the compiler system.
  13. define('TWIG_COMPILER_INCLUDED', true);
  14. require TWIG_BASE . '/lexer.php';
  15. require TWIG_BASE . '/parser.php';
  16. require TWIG_BASE . '/ast.php';
  17. function twig_compile($node, $fp=null)
  18. {
  19. if (!is_null($fp))
  20. $compiler = new Twig_FileCompiler($fp);
  21. else
  22. $compiler = new Twig_StringCompiler();
  23. $node->compile($compiler);
  24. if (is_null($fp))
  25. return $compiler->getCode();
  26. }
  27. class Twig_Compiler
  28. {
  29. private $last_lineno;
  30. public function __construct()
  31. {
  32. $this->last_lineno = NULL;
  33. }
  34. public function format()
  35. {
  36. $arguments = func_get_args();
  37. $this->raw(call_user_func_array('sprintf', $arguments));
  38. }
  39. public function string($value)
  40. {
  41. $this->format('"%s"', addcslashes($value, "\t\""));
  42. }
  43. public function repr($value)
  44. {
  45. if (is_int($value) || is_float($value))
  46. $this->raw($value);
  47. else if (is_null($value))
  48. $this->raw('NULL');
  49. else if (is_bool($value))
  50. $this->raw($value ? 'true' : 'false');
  51. else if (is_array($value)) {
  52. $this->raw('array(');
  53. $i = 0;
  54. foreach ($value as $key => $value) {
  55. if ($i++)
  56. $this->raw(', ');
  57. $this->repr($key);
  58. $this->raw(' => ');
  59. $this->repr($value);
  60. }
  61. $this->raw(')');
  62. }
  63. else
  64. $this->string($value);
  65. }
  66. public function pushContext()
  67. {
  68. $this->raw('$context[\'::parent\'] = $parent = $context;'. "\n");
  69. }
  70. public function popContext()
  71. {
  72. $this->raw('$context = $context[\'::parent\'];'. "\n");
  73. }
  74. public function addDebugInfo($node)
  75. {
  76. if ($node->lineno != $this->last_lineno) {
  77. $this->last_lineno = $node->lineno;
  78. $this->raw("/* LINE:$node->lineno */\n");
  79. }
  80. }
  81. }
  82. class Twig_FileCompiler extends Twig_Compiler
  83. {
  84. private $fp;
  85. public function __construct($fp)
  86. {
  87. parent::__construct();
  88. $this->fp = $fp;
  89. }
  90. public function raw($string)
  91. {
  92. fwrite($this->fp, $string);
  93. }
  94. }
  95. class Twig_StringCompiler extends Twig_Compiler
  96. {
  97. private $source;
  98. public function __construct()
  99. {
  100. parent::__construct();
  101. $this->source = '';
  102. }
  103. public function raw($string)
  104. {
  105. $this->source .= $string;
  106. }
  107. public function getCode()
  108. {
  109. return $this->source;
  110. }
  111. }