<?php
/**
* Harmony
* Copyright (c) 2008 Maxime Bouroumeau-Fuseau
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Harmony
* @package Harmony_Script
* @subpackage Harmony_Script_Context
* @author Maxime Bouroumeau-Fuseau
* @copyright 2008 (c) Maxime Bouroumeau-Fuseau
* @license http://www.opensource.org/licenses/mit-license.php
* @link http://www.harmony-framework.com
*/
/** Harmony_Script_Context */
require_once 'Harmony/Script/Context.php';
/**
* Handles functions
*
* @category Harmony
* @package Harmony_Script
* @subpackage Harmony_Script_Context
* @copyright 2008 (c) Maxime Bouroumeau-Fuseau
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Harmony_Script_Context_Function extends Harmony_Script_Context
{
/**
* Optional arguments
*
* @var array
*/
protected $_optionalArgs = array();
/**
* Whether it's a native function
*
* @var bool
*/
protected $_isNative = false;
/**
* Gets the function name
*/
public function tokenString($text)
{
if ($this->hasParam('method')) {
if ($this->getParam('static')) {
/* static method */
$this->_js .= $text . ' = function';
} else {
/* method */
$this->_js .= $text . ': function';
}
} else {
/* function */
$this->_js .= 'function ' . $text;
}
}
/**
* Arguments list
*/
public function tokenParenthesesStart($text)
{
/* gets arguments */
$args = $this->getParser()->enterContext('Arguments');
$reqArgs = array();
/* stores optional arguments separatly */
for($i = 0, $c = count($args); $i < $c; $i++) {
if (empty($args[$i][1])) {
$reqArgs[] = $args[$i][0];
} else {
$this->_optionalArgs[$i] = $args[$i];
}
}
$this->_js .= '(' . implode(',', $reqArgs) . ")\n";
}
/**
* If it's a native function, the first comment after the
* argument list should be the javascript code
*/
public function tokenComment($text)
{
if ($this->hasParam('docComment')) {
if (!preg_match('/@harmony-native/', $this->getParam('docComment'))) {
return;
}
} else {
return;
}
$text = trim($text, '/*');
$this->_js .= $text;
$this->_isNative = true;
}
/**
* In interfaces, functions end with ;
*/
public function tokenLineEnd($text)
{
/* native function, no more action */
if ($this->_isNative) {
$this->exitContext($this->_js);
return;
}
$this->_js .= '{}';
$this->exitContext($this->_js);
}
/**
* Function core start
*/
public function tokenCurlyBracesStart($text)
{
$block = $this->getParser()->enterContext('Block');
/* native function, no more action */
if ($this->_isNative) {
$this->exitContext($this->_js);
return;
}
$this->_js .= "{\n";
/* defines variables for each optional arguments */
foreach ($this->_optionalArgs as $i => $arg) {
$this->_js .= 'var ' . $arg[0] . ' = arguments.length >= '
. ($i + 1) . ' ? arguments[' . $i . '] : ' . $arg[1] . ";\n";
}
$this->_js .= $block . '}';
$this->exitContext($this->_js);
}
}