<commit_before><?php

use Latte\Compiler;
use Latte\Macros\MacroSet;
use Latte\MacroTokens;
use Latte\MacroNode;
use Latte\PhpWriter;


/**
 * - n:plugin
 * - n:skip
 */
class PluginMacro extends MacroSet
{

	/** @var string[] */
	private static $code = [];

	/** @var bool|NULL */
	public static $skip = NULL;


	public static function install(Compiler $compiler)
	{
		$me = new self($compiler);

		$me->addMacro('plugin', NULL, [$me, 'macroPluginEnd']);
		$me->addMacro('skip', [$me, 'macroSkip'], [$me, 'macroSkipEnd']);
	}


	public static function installExtended(Compiler $compiler)
	{
		$me = new self($compiler);

		$me->addMacro('target', NULL, [$me, 'macroPluginEnd']);
		$me->addMacro('static', NULL, [$me, 'macroLabelEnd']);
		$me->addMacro('atomic', NULL, [$me, 'macroLabelEnd']);
		$me->addMacro('layer', NULL, [$me, 'macroLayerEnd']);
	}


	/**
	 * @return bool
	 **/
	public static function skipJs()
	{
		return isset(self::$skip) && empty(self::$skip);
	}


	/**
	 * @return bool
	 **/
	public static function skipHtml()
	{
		return empty(self::$skip);
	}


	/**
	 * @param string
	 * @return string
	 */
	public static function checksum($str)
	{
		$encoded = rtrim(base64_encode(md5($str, TRUE)), '=');
		$encoded = strtr($encoded, array(
			'/' => '_',
			'+' => '-',
		));

		return $encoded;
	}


	public function macroPluginEnd(MacroNode $node, PhpWriter $writer)
	{
		$tokenizer = clone $node->tokenizer;
		$tokenizer->reset();

		$code = '';
		$plugin = '';
		$rest = '';
		$format = 0;
		$tokens = NULL;
		$depth = 0;

		while ($token = $tokenizer->nextToken()) {
			$isLast = !$tokenizer->isNext();

			if ($token[0]===';') {
			} elseif (isset($tokens)) {
				if ($token[0]==='[') {
					$depth++;

				} elseif ($token[0]===']') {
					$depth--;
				}

				if ($depth===0 && $token[2]===MacroTokens::T_VARIABLE) {
					if ($tokenizer->isPrev(',') && ($isLast || $tokenizer->isNext(MacroTokens::T_WHITESPACE, ',', ';'))) {
						$tokens->tokens[] = [ltrim($token[0], '$'), $token[1], MacroTokens::T_SYMBOL];
						$tokens->tokens[] = ['=>', $token[1], MacroTokens::T_CHAR];
					}
				}

				$tokens->tokens[] = $token;

			} elseif ($token[0]==='=>') {
				$format = 1;
				$tokens = clone $node->tokenizer;
				$tokens->position = -1;
				$tokens->tokens = $tokenizer->nextUntil(';');
				$token = $tokenizer->nextToken();
				$isLast = !$tokenizer->isNext();

			} elseif ($token[0]===',') {
				$tokenizer->nextAll(MacroTokens::T_WHITESPACE);

				$format = 2;
				$tokens = clone $node->tokenizer;
				$tokens->position = -1;

				$isRest = FALSE;
				if ($tokens->tokens = $tokenizer->nextAll('...')) {
					$isRest = TRUE;

				} elseif (count($tokens->tokens = $tokenizer->nextAll('.'))===3) {
					$isRest = TRUE;
				}

				if ($isRest && $tokenizer->isNext(MacroTokens::T_VARIABLE)) {
					$tokens->tokens = [];
					$rest = $tokenizer->joinUntil(',', ';');
					$token = $tokenizer->nextToken();
					$isLast = !$tokenizer->isNext();
				}

			} elseif (trim($token[0])) {
				$plugin .= trim($token[0]);
			}

			if ($token[0]===';' || $isLast) {
				$code .= "'$plugin' => ";
				$code .= ($rest ? "$rest + " : '');
				$code .= ($format===0 ? 'NULL' : '');
				$code .= ($format===1 ? 'current(' : '');
				$code .= ($format ? $writer->formatArray($tokens) : '');
				$code .= ($format===1 ? ')' : '');<commit_msg>Add support for variables and {php code} in pluginName<commit_after><?php

use Latte\Compiler;
use Latte\Macros\MacroSet;
use Latte\MacroTokens;
use Latte\MacroNode;
use Latte\PhpWriter;


/**
 * - n:plugin
 * - n:skip
 */
class PluginMacro extends MacroSet
{

	/** @var string[] */
	private static $code = [];

	/** @var bool|NULL */
	public static $skip = NULL;


	public static function install(Compiler $compiler)
	{
		$me = new self($compiler);

		$me->addMacro('plugin', NULL, [$me, 'macroPluginEnd']);
		$me->addMacro('skip', [$me, 'macroSkip'], [$me, 'macroSkipEnd']);
	}


	public static function installExtended(Compiler $compiler)
	{
		$me = new self($compiler);

		$me->addMacro('target', NULL, [$me, 'macroPluginEnd']);
		$me->addMacro('static', NULL, [$me, 'macroLabelEnd']);
		$me->addMacro('atomic', NULL, [$me, 'macroLabelEnd']);
		$me->addMacro('layer', NULL, [$me, 'macroLayerEnd']);
	}


	/**
	 * @return bool
	 **/
	public static function skipJs()
	{
		return isset(self::$skip) && empty(self::$skip);
	}


	/**
	 * @return bool
	 **/
	public static function skipHtml()
	{
		return empty(self::$skip);
	}


	/**
	 * @param string
	 * @return string
	 */
	public static function checksum($str)
	{
		$encoded = rtrim(base64_encode(md5($str, TRUE)), '=');
		$encoded = strtr($encoded, array(
			'/' => '_',
			'+' => '-',
		));

		return $encoded;
	}


	public function macroPluginEnd(MacroNode $node, PhpWriter $writer)
	{
		$tokenizer = clone $node->tokenizer;
		$tokenizer->reset();

		$code = '';
		$plugin = '';
		$rest = '';
		$format = 0;
		$tokens = NULL;
		$depth = 0;

		while ($token = $tokenizer->nextToken()) {
			$isLast = !$tokenizer->isNext();

			if ($token[0]===';') {
			} elseif (isset($tokens)) {
				if ($token[0]==='[') {
					$depth++;

				} elseif ($token[0]===']') {
					$depth--;
				}

				if ($depth===0 && $token[2]===MacroTokens::T_VARIABLE) {
					if ($tokenizer->isPrev(',') && ($isLast || $tokenizer->isNext(MacroTokens::T_WHITESPACE, ',', ';'))) {
						$tokens->tokens[] = [ltrim($token[0], '$'), $token[1], MacroTokens::T_SYMBOL];
						$tokens->tokens[] = ['=>', $token[1], MacroTokens::T_CHAR];
					}
				}

				$tokens->tokens[] = $token;

			} elseif ($token[0]==='=>') {
				$format = 1;
				$tokens = clone $node->tokenizer;
				$tokens->position = -1;
				$tokens->tokens = $tokenizer->nextUntil(';');
				$token = $tokenizer->nextToken();
				$isLast = !$tokenizer->isNext();

			} elseif ($token[0]===',') {
				$tokenizer->nextAll(MacroTokens::T_WHITESPACE);

				$format = 2;
				$tokens = clone $node->tokenizer;
				$tokens->position = -1;

				$isRest = FALSE;
				if ($tokens->tokens = $tokenizer->nextAll('...')) {
					$isRest = TRUE;

				} elseif (count($tokens->tokens = $tokenizer->nextAll('.'))===3) {
					$isRest = TRUE;
				}

				if ($isRest && $tokenizer->isNext(MacroTokens::T_VARIABLE)) {
					$tokens->tokens = [];
					$rest = $tokenizer->joinUntil(',', ';');
					$token = $tokenizer->nextToken();
					$isLast = !$tokenizer->isNext();
				}

			} elseif (trim($token[0])) {
				if ($token[0]==='{') {
					$plugin .= '".(';

				} elseif ($token[0]==='}') {
					$plugin .= ')."';

				} else {
					$plugin .= trim($token[0]);
				}
			}

			if ($token[0]===';' || $isLast) {
				$code .= "\"$plugin\" => ";
				$code .= ($rest ? "$rest + " : '');
				$code .= ($format===0 ? 'NULL' : '');
				$code .= ($format===1 ? 'current(' : '');
				$code .= ($format ? $writer->formatArray($tokens) : '');
				$code .= ($format===1 ? ')' : '');
{'fraction_non_alphanumeric': 0.24466075839023682, 'fraction_numerical': 0.007845416242917333, 'mean_word_length': 3.3214061519146263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1160038', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2525, 'n_words': 459}