Skip to content

Commit

Permalink
Optimization (#53)
Browse files Browse the repository at this point in the history
* #50 More debug infos
* Enable isInline on every nodes
  • Loading branch information
kylekatarnls authored Aug 9, 2016
1 parent 702b8e6 commit 745ddc5
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 173 deletions.
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ engines:
- a5e6f6215d8cc84a0b7750eaafaf8105
# else here is the simpliest way to do it
- 311e57c70997007dce22492a1426d448
# False positive due to dynamic variable name
- dd33258e7e716128f54e50df078b03ea
checks:
# Allow static access such as CommonUtils ones
CleanCode/StaticAccess:
Expand Down
8 changes: 7 additions & 1 deletion src/Jade/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ class Compiler extends MixinVisitor
*/
protected $quote;

/**
* @var string
*/
protected $filename;

/**
* @param array/Jade $options
* @param array $filters
*/
public function __construct($options = array(), array $filters = array())
public function __construct($options = array(), array $filters = array(), $filename = null)
{
$this->options = $this->setOptions($options);
$this->filters = $filters;
$this->filename = $filename;
}

/**
Expand Down
78 changes: 48 additions & 30 deletions src/Jade/Compiler/CodeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,53 @@ protected function getVarname($separator)
: $varname;
}

protected function parseArrayString(&$argument, $match, $consume, &$quote, &$key, &$value)
{
$quote = $quote
? CommonUtils::escapedEnd($match[1])
? $quote
: null
: $match[2];
${is_null($value) ? 'key' : 'value'} .= $match[0];
$consume($argument, $match[0]);
}

protected function parseArrayAssign(&$argument, $match, $consume, &$quote, &$key, &$value)
{
if ($quote) {
${is_null($value) ? 'key' : 'value'} .= $match[0];
$consume($argument, $match[0]);

return;
}

if (!is_null($value)) {
throw new \ErrorException('Parse error on ' . substr($argument, strlen($match[1])), 15);
}

$key .= $match[1];
$value = '';
$consume($argument, $match[0]);
}

protected function parseArrayElement(&$argument, $match, $consume, &$quote, &$key, &$value)
{
switch ($match[2]) {
case '"':
case "'":
$this->parseArrayString($argument, $match, $consume, $quote, $key, $value);
break;
case ':':
case '=>':
$this->parseArrayAssign($argument, $match, $consume, $quote, $key, $value);
break;
case ',':
${is_null($value) ? 'key' : 'value'} .= $match[0];
$consume($argument, $match[0]);
break;
}
}

protected function parseArray($input, $subCodeHandler)
{
$output = array();
Expand All @@ -101,36 +148,7 @@ protected function parseArray($input, $subCodeHandler)
$argument = ltrim($argument, '$');
$quote = null;
while (preg_match('/^(.*?)(=>|[\'",:])/', $argument, $match)) {
switch ($match[2]) {
case '"':
case "'":
$quote = $quote
? CommonUtils::escapedEnd($match[1])
? $quote
: null
: $match[2];
${is_null($value) ? 'key' : 'value'} .= $match[0];
$consume($argument, $match[0]);
break;
case ':':
case '=>':
if ($quote) {
${is_null($value) ? 'key' : 'value'} .= $match[0];
$consume($argument, $match[0]);
break;
}
if (!is_null($value)) {
throw new \ErrorException('Parse error on ' . substr($argument, strlen($match[1])), 15);
}
$key .= $match[1];
$value = '';
$consume($argument, $match[0]);
break;
case ',':
${is_null($value) ? 'key' : 'value'} .= $match[0];
$consume($argument, $match[0]);
break;
}
$this->parseArrayElement($argument, $match, $consume, $quote, $key, $value);
}
${is_null($value) ? 'key' : 'value'} .= $argument;
$addToOutput();
Expand Down
26 changes: 22 additions & 4 deletions src/Jade/Compiler/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,28 @@ protected function visitNode(Node $node)
{
$fqn = get_class($node);
$parts = explode('\\', $fqn);
$name = end($parts);
$method = 'visit' . ucfirst(strtolower($name));

return $this->$method($node);
$name = strtolower(end($parts));
$method = 'visit' . ucfirst($name);

try {
return $this->$method($node);
} catch (\ErrorException $e) {
if (!in_array($e->getCode(), array(8, 33))) {
throw $e;
}

throw new \ErrorException(
'Error on the ' . $name .
(isset($node->name) ? ' "' . $node->name . '"' : '') .
($this->filename ? ' in ' . $this->filename : '') .
' line ' . $node->line . ":\n" . $e->getMessage(),
34,
1,
__FILE__,
__LINE__,
$e
);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Jade/Jade.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function setCustomOptions(array $options)
public function compile($input)
{
$parser = new Parser($input, null, $this->options);
$compiler = new Compiler($this->options, $this->filters);
$compiler = new Compiler($this->options, $this->filters, $parser->getFilename());
$php = $compiler->compile($parser->parse());
if (version_compare(PHP_VERSION, '7.0.0') < 0) {
$php = preg_replace_callback('/(' . preg_quote('\\Jade\\Compiler::getPropertyFromAnything', '/') . '\\(((?>[^()]+)|(?-2))*\\))[ \t]*(\\(((?>[^()]+)|(?-2))*\\))/', function ($match) {
Expand Down
Loading

0 comments on commit 745ddc5

Please sign in to comment.