Skip to content
Browse files

numerous fixes

  • Loading branch information...
1 parent 9973077 commit 13e30c9a019cc5c9caf6b70cbc0f88888ada3ead @alxlit committed Apr 14, 2012
View
34 src/CoffeeScript/Compiler.php
@@ -17,9 +17,11 @@ class Compiler {
*
* Available options:
*
- * 'file' => The source file, for debugging (formatted into error messages)
- * 'tokens' => Reference to token stream
- * 'trace' => File to write parser trace to
+ * 'filename' => The source file, for debugging (formatted into error messages)
+ * 'header' => Add a header to the generated source (default: TRUE)
+ * 'rewrite' => Enable rewriting token stream (debugging)
+ * 'tokens' => Reference to token stream (debugging)
+ * 'trace' => File to write parser trace to (debugging)
*
* @param string The source CoffeeScript code
* @param array Options (see above)
@@ -30,9 +32,9 @@ static function compile($code, $options = array())
{
$lexer = new Lexer($code, $options);
- if (isset($options['file']))
+ if (isset($options['filename']))
{
- Parser::$FILE = $options['file'];
+ Parser::$FILE = $options['filename'];
}
if (isset($options['tokens']))
@@ -45,14 +47,28 @@ static function compile($code, $options = array())
Parser::Trace(fopen($options['trace'], 'w', TRUE), '> ');
}
- $parser = new Parser();
+ try
+ {
+ $parser = new Parser();
+
+ foreach (($tokens = $lexer->tokenize()) as $token)
+ {
+ $parser->parse($token);
+ }
+
+ $js = $parser->parse(NULL)->compile($options);
+ }
+ catch (\Exception $e)
+ {
+ throw new Error("In {$options['filename']}, ".$e->getMessage());
+ }
- foreach (($tokens = $lexer->tokenize()) as $token)
+ if ( ! isset($options['header']) || $options['header'])
{
- $parser->parse($token);
+ $js = '// Generated by CoffeeScript PHP ' . VERSION . "\n" . $js;
}
- return $parser->parse(NULL)->compile($options);
+ return $js;
}
}
View
2 src/CoffeeScript/Init.php
@@ -2,6 +2,8 @@
namespace CoffeeScript;
+define('VERSION', '1.3.1');
+
class Init {
/**
View
2 src/CoffeeScript/Nodes.php
@@ -26,7 +26,7 @@ class Nodes {
static function multident($code, $tab)
{
$code = preg_replace('/\n/', "\n{$tab}", $code);
- return preg_replace('/\s+$/', '');
+ return preg_replace('/\s+$/', '', $code);
}
static function unfold_soak($options, $parent, $name)
View
2 src/CoffeeScript/Parser.php
@@ -2763,7 +2763,7 @@ function yy_r62(){ $this->_retvalue = yy('Splat', $this->yystack[$this->yyidx +
function yy_r64(){ $this->_retvalue = $this->yystack[$this->yyidx + -1]->minor->add($this->yystack[$this->yyidx + 0]->minor); }
#line 2764 "/var/www/coffeescript-php/grammar.php"
#line 119 "/var/www/coffeescript-php/grammar.y"
- function yy_r65(){ $this->_retvalue = yy('Value', $this->yystack[$this->yyidx + -1]->minor, (array) $this->yystack[$this->yyidx + 0]->minor); }
+ function yy_r65(){ $this->_retvalue = yy('Value', $this->yystack[$this->yyidx + -1]->minor, is_object($this->yystack[$this->yyidx + 0]->minor) ? array($this->yystack[$this->yyidx + 0]->minor) : (array) $this->yystack[$this->yyidx + 0]->minor); }
#line 2767 "/var/www/coffeescript-php/grammar.php"
#line 132 "/var/www/coffeescript-php/grammar.y"
function yy_r75(){ $this->_retvalue = yy('Access', $this->yystack[$this->yyidx + 0]->minor); }
View
4 src/CoffeeScript/Scope.php
@@ -34,6 +34,8 @@ function __construct($parent, $expressions, $method)
function add($name, $type, $immediate = FALSE)
{
+ $name = ''.$name;
+
if ($this->shared && ! $immediate)
{
return $this->parent->add($name, $type, $immediate);
@@ -75,6 +77,8 @@ function assigned_variables()
function check($name, $immediate = FALSE)
{
+ $name = ''.$name;
+
$found = !! $this->type($name);
if ($found || $immediate)
View
14 src/CoffeeScript/yy/Assign.php
@@ -48,7 +48,7 @@ function compile_conditional($options)
throw new Error('the variable "'.$this->left->base->value.'" can\'t be assigned with '.$this->context.' because it has not been defined.');
}
- if (in_array('?', $this->context))
+ if (strpos($this->context, '?') > -1)
{
$options['isExistentialEquals'] = TRUE;
}
@@ -100,7 +100,7 @@ function compile_node($options)
}
}
- if ($this->value instanceof yy_Code && preg_match(METHOD_DEF, $name, $match))
+ if ($this->value instanceof yy_Code && preg_match(METHOD_DEF, ''.$name, $match))
{
if (isset($match[1]) && $match[1])
{
@@ -181,7 +181,10 @@ function compile_pattern_match($options)
$value->properties[] = yy('Index', $idx);
}
- if (in_array($obj->unwrap()->value, Lexer::$COFFEE_RESERVED))
+ $tmp = $obj->unwrap();
+ $tmp = isset($tmp->value) ? $tmp->value : NULL;
+
+ if (in_array($tmp, Lexer::$COFFEE_RESERVED))
{
throw new SyntaxError('assignment to a reserved word: '.$obj->compile($options).' = '.$value->compile($options));
}
@@ -247,7 +250,8 @@ function compile_pattern_match($options)
}
else
{
- $name = $obj->unwrap()->value;
+ $name = $obj->unwrap();
+ $name = isset($name->value) ? $name->value : NULL;
if ($obj instanceof yy_Splat)
{
@@ -333,7 +337,7 @@ function compile_splice($options)
function is_statement($options)
{
- return isset($options['level']) && $options['level'] === LEVEL_TOP && $this->context && in_array('?', $this->context);
+ return isset($options['level']) && $options['level'] === LEVEL_TOP && $this->context && strpos($this->context, '?') > -1;
}
function unfold_soak($options)
View
2 src/CoffeeScript/yy/Base.php
@@ -79,7 +79,6 @@ function compile_closure($options)
{
if ($this->jumps())
{
- var_dump($this);
throw new SyntaxError('cannot use a pure statement in an expression.');
}
@@ -139,7 +138,6 @@ function contains_type($type)
function each_child($func)
{
- return FALSE;
if ( ! ($this->children))
{
return $this;
View
8 src/CoffeeScript/yy/Block.php
@@ -54,9 +54,9 @@ function compile_node($options)
{
$code = "{$code}\n";
}
-
- $codes[] = $code;
}
+
+ $codes[] = $code;
}
else
{
@@ -116,7 +116,7 @@ function compile_root($options)
if ($prelude_exps)
{
- $prelude = $this->compile_node($options + array('indent' => ''))."\n";
+ $prelude = $this->compile_node(array_merge($options, array('indent' => '')))."\n";
}
$this->expressions = $rest;
@@ -146,7 +146,7 @@ function compile_with_declarations($options)
}
}
- $options += array('level' => LEVEL_TOP);
+ $options = array_merge($options, array('level' => LEVEL_TOP));
if ($i)
{
View
12 src/CoffeeScript/yy/Call.php
@@ -131,18 +131,18 @@ function filter_implicit_objects($list)
continue;
}
- $obj = $tmp = NULL;
+ $obj = NULL;
foreach ($node->base->properties as $prop)
{
if (($prop instanceof yy_Assign) || $prop instanceof yy_Comment)
{
if ( ! $obj)
{
- $nodes[] = ($obj = $tmp = yy('Obj', array(), TRUE));
+ $nodes[] = ($obj = yy('Obj', array(), TRUE));
}
- $tmp->properties[] = $prop;
+ $obj->properties[] = $prop;
}
else
{
@@ -177,14 +177,14 @@ function super_reference($options)
if ( ! $method)
{
- throw SyntaxError('cannot call super outside of a function.');
+ throw new SyntaxError('cannot call super outside of a function.');
}
- $name = $method->name;
+ $name = isset($method->name) ? $method->name : NULL;
if ( ! $name)
{
- throw SyntaxError('cannot call super on an anonymous function.');
+ throw new SyntaxError('cannot call super on an anonymous function.');
}
if (isset($method->klass) && $method->klass)
View
36 src/CoffeeScript/yy/Class.php
@@ -73,19 +73,19 @@ function add_properties($node, $name, $options)
{
$func->static = TRUE;
- if ($func->bound)
+ if (isset($func->bound) && $func->bound)
{
$func->context = $name;
}
- else
- {
- $assign->variable = yy('Value', yy('Literal', $name), array(yy('Access', $base, 'proto')));
+ }
+ else
+ {
+ $assign->variable = yy('Value', yy('Literal', $name), array( yy('Access', yy('Literal', 'prototype')), yy('Access', $base) ));
- if ($func instanceof yy_Code && isset($func->bound) && $func->bound)
- {
- $this->bound_funcs[] = $base;
- $func->bound = FALSE;
- }
+ if ($func instanceof yy_Code && isset($func->bound) && $func->bound)
+ {
+ $this->bound_funcs[] = $base;
+ $func->bound = FALSE;
}
}
}
@@ -118,7 +118,7 @@ function compile_node($options)
if ($decl)
{
- array_unshift($this->body->expressions, yy('Assign', yy('Value', yy('Literal', $name), array(yy('Access', yy('Literal', $name)))), yy('Literal', "'{$name}'")));
+ array_unshift($this->body->expressions, yy('Assign', yy('Value', yy('Literal', $name), array(yy('Access', yy('Literal', 'name')))), yy('Literal', "'{$name}'")));
}
$this->body->expressions[] = $lname;
@@ -133,8 +133,17 @@ function compile_node($options)
$this->super_class = yy('Literal', $options['scope']->free_variable('super', FALSE));
array_unshift($this->body->expressions, yy('Extends', $lname, $this->super_class));
$call->args[] = $this->parent;
- $params = isset($call->variables->params) ? $call->variables->params : $call->variables->base->params;
- $params->push(yy('Param', $this->super_class));
+
+ if (isset($call->variable->params))
+ {
+ $params = & $call->variable->params;
+ }
+ else
+ {
+ $params = & $call->variable->base->params;
+ }
+
+ $params[] = yy('Param', $this->super_class);
}
$klass = yy('Parens', $call, TRUE);
@@ -195,6 +204,7 @@ function ensure_constructor($name)
}
$this->ctor->ctor = $this->ctor->name = $name;
+
$this->ctor->klass = NULL;
$this->ctor->no_return = TRUE;
}
@@ -204,7 +214,7 @@ function hoist_directive_prologue()
$index = 0;
$expressions = $this->body->expressions;
- while ((isset($expressions[$index]) && $node = $expressions[$index]) && $node instanceof yy_Comment || $node instanceof yy_Value && $node->is_string())
+ while (isset($expressions[$index]) && ($node = $expressions[$index]) && ( ($node instanceof yy_Comment) || ($node instanceof yy_Value) && $node->is_string() ))
{
$index++;
}
View
2 src/CoffeeScript/yy/Code.php
@@ -133,7 +133,7 @@ function compile_node($options)
{
$this->bound = $this->context = $options['scope']->parent->method->context;
}
- else if ( ! $this->static)
+ else if ( ! (isset($this->static) && $this->static))
{
$options['scope']->parent->assign('_this', 'this');
}
View
4 src/CoffeeScript/yy/For.php
@@ -114,7 +114,7 @@ function compile_node($options)
if ($this->range)
{
- $for_part = $source->compile($options + array('index' => $ivar, $name, $this->step));
+ $for_part = $source->compile(array_merge($options, array('index' => $ivar, 'name' => $name, 'step' => $this->step)));
}
else
{
@@ -188,7 +188,7 @@ function compile_node($options)
}
}
- $body = $body->compile($options + array('indent' => $idt1), LEVEL_TOP);
+ $body = $body->compile(array_merge($options, array('indent' => $idt1)), LEVEL_TOP);
if ($body)
{
View
6 src/CoffeeScript/yy/If.php
@@ -32,7 +32,7 @@ function add_else($else_body)
return $this;
}
- function body_node()
+ function body_node()
{
return $this->body ? $this->body->unwrap() : NULL;
}
@@ -66,7 +66,7 @@ function compile_statement($options)
$cond = $this->condition->compile($options, LEVEL_PAREN);
$options['indent'] .= TAB;
$body = $this->ensure_block($this->body);
- $if_part = "if ({$cond}) {\n".$body->compile($options)."\n{$this->tab}";
+ $if_part = "if ({$cond}) {\n".$body->compile($options)."\n{$this->tab}}";
if ( ! $child)
{
@@ -104,7 +104,7 @@ function ensure_block($node)
{
return $node instanceof yy_Block ? $node : yy('Block', array($node));
}
-
+
function is_chain()
{
return $this->is_chain;
View
3 src/CoffeeScript/yy/Obj.php
@@ -43,7 +43,8 @@ function compile_node($options)
if ($prop)
{
- $prop_name = ''.$prop->unwrap_all()->value;
+ $prop_name = $prop->unwrap_all();
+ $prop_name = isset($prop_name->value) ? $prop_name->value.'' : NULL;
if (in_array($prop_name, $prop_names))
{
View
13 src/CoffeeScript/yy/Op.php
@@ -19,8 +19,6 @@ class yy_Op extends yy_Base
public $operator = NULL;
- // I *think* this is correct, since there is no separation in JavaScript between
- // object functions/data, i.e. invert = function() {} will evaluate to true first.
public $invert = TRUE;
function constructor($op, $first, $second = NULL, $flip = NULL)
@@ -99,12 +97,15 @@ function compile_node($options, $level = NULL)
$this->first->front = $this->front;
}
- if ($this->operator === 'delete' && $options['scope']->check($this->first->unwrap_all()->value))
+ $tmp = $this->first->unwrap_all();
+ $tmp = isset($tmp->value) ? $tmp->value : NULL;
+
+ if ($this->operator === 'delete' && $options['scope']->check($tmp))
{
throw new SyntaxError('delete operand may not be argument or var');
}
- if (in_array($this->operator, array('--', '++')) && in_array($this->first->unwrap_all()->value, Lexer::$STRICT_PROSCRIBED))
+ if (in_array($this->operator, array('--', '++')) && in_array($tmp, Lexer::$STRICT_PROSCRIBED))
{
throw new SyntaxError('prefix increment/decrement may not have eval or arguments operand');
}
@@ -142,7 +143,7 @@ function compile_unary($options)
$parts = array($op = $this->operator);
$plus_minus = in_array($op, array('+', '-'), TRUE);
- if (in_array($op, array('new', 'typeof', 'delete'), TRUE) ||
+ if (in_array($op, array('new', 'typeof', 'delete'), TRUE) ||
$plus_minus &&
$this->first instanceof yy_Op && $this->first->operator === $op)
{
@@ -171,7 +172,7 @@ function is_chainable()
function is_complex()
{
- return ! ($this->is_unary() && ! in_array($this->operator, array('+', '-'))) || $this->first->is_complex();
+ return ! ($this->is_unary() && in_array($this->operator, array('+', '-'))) || $this->first->is_complex();
}
function invert()
View
4 src/CoffeeScript/yy/Param.php
@@ -100,11 +100,11 @@ function names($name = NULL)
}
else if ($obj->is_array() || $obj->is_object())
{
- $names = array_merge($names, $this->names($obj->base));
+ $names = array_merge($names, (array) $this->names($obj->base));
}
else if (isset($obj->this) && $obj->this)
{
- $names = array_merge($names, $at_param($obj));
+ $names = array_merge($names, (array) $at_param($obj));
}
else
{
View
12 src/CoffeeScript/yy/Range.php
@@ -80,7 +80,7 @@ function compile_array($options)
function compile_node($options)
{
- if ( ! $this->from_var)
+ if ( ! (isset($this->from_var) && $this->from_var))
{
$this->compile_variables($options);
}
@@ -102,14 +102,14 @@ function compile_node($options)
$var_part .= ", {$this->to_c}";
}
- if ($this->step !== $this->step_var)
+ if (isset($this->step) && $this->step !== $this->step_var)
{
$var_part .= ", {$this->step}";
}
list($lt, $gt) = array("{$idx} <{$this->equals}", "{$idx} >{$this->equals}");
- if (self::check($this->step_num))
+ if (isset($this->step_num) && self::check($this->step_num))
{
$cond_part = intval($this->step_num) > 0 ? "{$lt} {$this->to_var}" : "{$gt} {$this->to_var}";
}
@@ -124,7 +124,7 @@ function compile_node($options)
$cond_part = "{$cond} ? {$lt} {$this->to_var} : {$gt} {$this->to_var}";
}
- if ($this->step_var)
+ if (isset($this->step_var) && $this->step_var)
{
$step_part = "{$idx} += {$this->step_var}";
}
@@ -195,7 +195,7 @@ function compile_simple($options)
function compile_variables($options)
{
- $options += array('top' => TRUE);
+ $options = array_merge($options, array('top' => TRUE));
list($this->from_c, $this->from_var) = $this->from->cache($options, LEVEL_LIST);
list($this->to_c, $this->to_var) = $this->to->cache($options, LEVEL_LIST);
@@ -207,7 +207,7 @@ function compile_variables($options)
list($this->from_num, $this->to_num) = array(preg_match(SIMPLENUM, $this->from_var), preg_match(SIMPLENUM, $this->to_var));
- if ($this->step_var)
+ if (isset($this->step_var) && $this->step_var)
{
$this->step_num = preg_match(SIMPLENUM, $this->step_var);
}
View
12 src/CoffeeScript/yy/Return.php
@@ -18,23 +18,23 @@ function constructor($expr = NULL)
function compile($options, $level = NULL)
{
- $expr = (isset($this->expression) && $this->expression) ?
- $this->expression->make_return() : NULL;
+ $expr = (isset($this->expression) && $this->expression) ? $this->expression->make_return() : NULL;
if ($expr && ! ($expr instanceof yy_Return))
{
- return $expr->compile($options, $level);
+ $ret = $expr->compile($options, $level);
}
else
{
- return parent::compile($options, $level);
+ $ret = parent::compile($options, $level);
}
+
+ return $ret;
}
function compile_node($options)
{
- return $this->tab.'return'.(isset($this->expression) && $this->expression ?
- ' '.$this->expression->compile($options, LEVEL_PAREN) : '').';';
+ return $this->tab.'return'.(isset($this->expression) && $this->expression ? ' '.$this->expression->compile($options, LEVEL_PAREN) : '').';';
}
function is_statement()
View
2 src/CoffeeScript/yy/Try.php
@@ -46,7 +46,7 @@ function compile_node($options = array())
return
"{$this->tab}try {\n"
- . $this->try_part."\n"
+ . $try_part."\n"
. "{$this->tab}}{$catch_part}{$ensure_part}";
}
View
3 src/CoffeeScript/yy/Value.php
@@ -68,8 +68,7 @@ function cache_reference($options)
$base->add($name);
- return array($base, yy('Value', isset($bref) ? $bref : $base->base,
- array(isset($nref) ? $nref : $name)));
+ return array($base, yy('Value', isset($bref) ? $bref : $base->base, array(isset($nref) ? $nref : $name)));
}
function compile_node($options)

0 comments on commit 13e30c9

Please sign in to comment.
Something went wrong with that request. Please try again.