<commit_before><?php

namespace org\octris\core\tpl\compiler {
    /****c* compiler/macro
     * NAME
     *      macro
     * FUNCTION
     *      Library for handling template macros. This is a static class.
     * COPYRIGHT
     *      copyright (c) 2010 by <NAME>
     * AUTHOR
     *      <NAME> <<EMAIL>>
     ****
     */

    class macro {
        /****v* macro/$registry
         * SYNOPSIS
         */
        protected static $registry = array();
        /*
         * FUNCTION
         *      macro registry
         ****
         */
        
        /****v* macro/$last_error
         * SYNOPSIS
         */
        protected static $last_error = '';
        /*
         * FUNCTION
         *      last occured error
         ****
         */

        /*
         * static class cannot be instantiated
         */
        protected function __construct() {}
        protected function __clone() {}
        
        /****m* macro/getError
         * SYNOPSIS
         */
        public static function getError()
        /*
         * FUNCTION
         *      return last occured error
         * OUTPUTS
         *      (string) -- last occured error
         ****
         */
        {
            return self::$last_error;
        }
        
        /****m* macro/setError
         * SYNOPSIS
         */
        protected static function setError($name, $msg)
        /*
         * FUNCTION
         *      set error
         * INPUTS
         *      * $name (string) -- name of macro the error occured for
         *      * $msg (string) -- additional error message
         ****
         */
        {
            self::$last_error = sprintf('"%s" -- %s', $name, $msg);
        }
        
        /****m* macro/registerMacro
         * SYNOPSIS
         */
        public static function registerMacro($name, $callback, array $args)
        /*
         * FUNCTION
         *      register a macro
         * INPUTS
         *      * $name (string) -- name of macro to register
         *      * $callback (mixed) -- callback to call when macro is executed
         *      * $args (array) -- for testing arguments
         ****
         */
        {
            self::$registry[strtolower($name)] = array(
                'callback' => $callback,
                'args'     => array_merge(array('min' => 0, 'max' => 0), $args)
            );
        }
        
        /****m* macro/execMacro
         * SYNOPSIS
         */
        public static function execMacro($name, $args, array $options = array())
        /*
         * FUNCTION
         *      execute specified macro with specified arguments
         * INPUTS
         *      * $name (string) -- name of macro to execute
         *      * $args (array) -- arguments for macro
         *      * $options (array) -- additional options for macro
         * OUTPUTS
         *      (mixed) -- output of macro
         ****
         */
        {
            self::$last_error = '';
            
            $name = strtolower($name);
            
            if (!isset(self::$registry[$name])) {
                self::setError($name, 'unknown macro');
            } elseif (!is_callable(self::$registry[$name]['callback'])) {
                self::setError($name, 'unable to execute macro');
            } elseif (count($args) < self::$registry[$name]['args']['min']) {
                self::setError($name, 'not enough arguments');
            } elseif (count($args) > self::$registry[$name]['args']['max']) {
                self::setError($name, 'too many arguments');
            } else {
                return call_user_func_array(self::$registry[$name]['callback'], array($args, $options));
            }
        }
    }

    /*
     * register "import" macro
     */
    macro::registerMacro(
        'import',
        function($args, array $options = array()) {
            $ret = '';
            $err = '';
            
            if (($file = \org\octris\core\tpl\compiler\searchpath::findFile($args[0])) !== false) {
                $c   = new \org\octris\core\tpl\compiler();
                $ret = $c->process($file);
            } else {
                $err = sprintf(
                    'unable to locate file "%s" in "%s"', 
                    $args[0],
                    implode(':', \org\octris\core\tpl\compiler\searchpath::getPath())
                );<commit_msg>- added support for error messages reported by macro<commit_after><?php

namespace org\octris\core\tpl\compiler {
    require_once('searchpath.class.php');
    
    /****c* compiler/macro
     * NAME
     *      macro
     * FUNCTION
     *      Library for handling template macros. This is a static class.
     * COPYRIGHT
     *      copyright (c) 2010 by <NAME>
     * AUTHOR
     *      <NAME> <<EMAIL>>
     ****
     */

    class macro {
        /****v* macro/$registry
         * SYNOPSIS
         */
        protected static $registry = array();
        /*
         * FUNCTION
         *      macro registry
         ****
         */
        
        /****v* macro/$last_error
         * SYNOPSIS
         */
        protected static $last_error = '';
        /*
         * FUNCTION
         *      last occured error
         ****
         */

        /*
         * static class cannot be instantiated
         */
        protected function __construct() {}
        protected function __clone() {}
        
        /****m* macro/getError
         * SYNOPSIS
         */
        public static function getError()
        /*
         * FUNCTION
         *      return last occured error
         * OUTPUTS
         *      (string) -- last occured error
         ****
         */
        {
            return self::$last_error;
        }
        
        /****m* macro/setError
         * SYNOPSIS
         */
        protected static function setError($name, $msg)
        /*
         * FUNCTION
         *      set error
         * INPUTS
         *      * $name (string) -- name of macro the error occured for
         *      * $msg (string) -- additional error message
         ****
         */
        {
            self::$last_error = sprintf('"%s" -- %s', $name, $msg);
        }
        
        /****m* macro/registerMacro
         * SYNOPSIS
         */
        public static function registerMacro($name, $callback, array $args)
        /*
         * FUNCTION
         *      register a macro
         * INPUTS
         *      * $name (string) -- name of macro to register
         *      * $callback (mixed) -- callback to call when macro is executed
         *      * $args (array) -- for testing arguments
         ****
         */
        {
            self::$registry[strtolower($name)] = array(
                'callback' => $callback,
                'args'     => array_merge(array('min' => 0, 'max' => 0), $args)
            );
        }
        
        /****m* macro/execMacro
         * SYNOPSIS
         */
        public static function execMacro($name, $args, array $options = array())
        /*
         * FUNCTION
         *      execute specified macro with specified arguments
         * INPUTS
         *      * $name (string) -- name of macro to execute
         *      * $args (array) -- arguments for macro
         *      * $options (array) -- additional options for macro
         * OUTPUTS
         *      (mixed) -- output of macro
         ****
         */
        {
            self::$last_error = '';
            
            $name = strtolower($name);
            
            if (!isset(self::$registry[$name])) {
                self::setError($name, 'unknown macro');
            } elseif (!is_callable(self::$registry[$name]['callback'])) {
                self::setError($name, 'unable to execute macro');
            } elseif (count($args) < self::$registry[$name]['args']['min']) {
                self::setError($name, 'not enough arguments');
            } elseif (count($args) > self::$registry[$name]['args']['max']) {
                self::setError($name, 'too many arguments');
            } else {
                list($ret, $err) = call_user_func_array(self::$registry[$name]['callback'], array($args, $options));
                
                if ($err) {
                    self::setError($name, $err);
                }
                
                return $ret
            }
        }
    }

    /*
     * register "import" macro
     */
    macro::registerMacro(
        'import',
        function($args, array $options = array()) {
            $ret = '';
            $err = '';
            
            if (($file = \org\octris\core\tpl\compiler\searchpath::findFile($args[0])) !== false) {
                $c   = new \org\octris\core\tpl\compiler();
                $ret = $c->process($file);
            } else {
                $err = sprintf(
                    'unable to locate file "%s" in "%s"', 
                    $args[0],
                    implode(':', \org\octris\core\tpl\compiler\searchpath::getPath())
                );
{'fraction_non_alphanumeric': 0.13829194967152877, 'fraction_numerical': 0.0017815388041420776, 'mean_word_length': 1.2590543259557343, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '226295', 'n_tokens_mistral': 2404, 'n_tokens_neox': 2376, 'n_words': 548}