Skip to content

Commit

Permalink
More fixes and micro-optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
smuuf committed Aug 23, 2020
1 parent 33bd80e commit a54ec29
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lib/hafriedlander/Peg/Compiler/PHPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class PHPBuilder {

public $needsStack = false;
public $needsStack = \false;

static function build () {
return new PHPBuilder();
Expand All @@ -28,7 +28,7 @@ function l(...$args) {

if ($lines instanceof PHPBuilder) {
if ($lines->needsStack) {
$this->needsStack = true;
$this->needsStack = \true;
}
$lines = $lines->lines;
} else {
Expand All @@ -54,7 +54,7 @@ function b(...$args) {
$block->l(...$args);

if ($block->needsStack) {
$this->needsStack = true;
$this->needsStack = \true;
}

$this->lines[] = [$entry, $block->lines];
Expand Down
4 changes: 1 addition & 3 deletions lib/hafriedlander/Peg/Compiler/PHPWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ public function match_fail_block($code) {
'FBREAK' => '$' . $id . ' = \false; break;'
])
)
->l(
'while(false);'
)
->l('while(\false);')
->b('if( $' . $id . ' === \true )', 'MATCH')
->b('if( $' . $id . ' === \false)', 'FAIL');
}
Expand Down
5 changes: 3 additions & 2 deletions lib/hafriedlander/Peg/Compiler/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function compile($indent) {

// Build an array of additional arguments to add to result node (if any)
if (empty($this->arguments)) {
$arguments = false;
$arguments = \false;
} else {
$arguments = '[';
foreach ($this->arguments as $k => $v) {
Expand All @@ -319,9 +319,10 @@ public function compile($indent) {
? '$newStack = \array_merge($stack, [$result]);'
: '';

$fnArguments = $block->needsStack ? '$stack = []' : '';
$arguments = $arguments ? ", {$arguments}" : '';
$match->b(
"function match_{$function_name} (\$stack = [])",
"function match_{$function_name} ($fnArguments)",
'$matchrule = "' . $function_name . '"; $result = $this->construct($matchrule, $matchrule' . $arguments . '); ' . $newStack ,
$block
);
Expand Down
8 changes: 4 additions & 4 deletions lib/hafriedlander/Peg/Compiler/Token/Expressionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

abstract class Expressionable extends Terminal {

static $expression_rx = '/ \$(\w+) | { \$(\w+) } /x';
const EXPR_REGEX = '/ \$(\w+) | { \$(\w+) } /x';

function contains_expression( $value ){
return preg_match(self::$expression_rx, $value);
return \preg_match(self::EXPR_REGEX, $value);
}

function expression_replace($matches) {
return '\'.$this->expression($result, $stack, \'' . (!empty($matches[1]) ? $matches[1] : $matches[2]) . "').'";
return '\'.$this->expression($result, $stack, \'' . ($matches[1] ?: $matches[2]) . "').'";
}

function match_code( $value ) {
$value = \preg_replace_callback(self::$expression_rx, [$this, 'expression_replace'], $value);
$value = \preg_replace_callback(self::EXPR_REGEX, [$this, 'expression_replace'], $value);
return parent::match_code($value);
}
}
4 changes: 2 additions & 2 deletions lib/hafriedlander/Peg/Compiler/Token/ExpressionedRecurse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

class ExpressionedRecurse extends Recurse {
function match_function( $value ) {
return '$this->expression($result, $stack, \''.$value.'\')';
return '$this->expression($result, $stack, \'' . $value . '\')';
}
}
}
11 changes: 6 additions & 5 deletions lib/hafriedlander/Peg/Compiler/Token/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use hafriedlander\Peg\Compiler\PHPBuilder;

class Literal extends Expressionable {
function __construct( $value ) {
parent::__construct( 'literal', "'" . \substr($value,1,-1) . "'" );

function __construct($value) {
parent::__construct('literal', "'" . \substr($value,1,-1) . "'");
}

function match_code( $value ) {
function match_code($value) {
// We inline single-character matches for speed
if ( !$this->contains_expression($value) && \strlen( eval( 'return '. $value . ';' ) ) === 1 ) {
return $this->match_fail_conditional( '\substr($this->string, $this->pos, 1) === '.$value,
if (!$this->contains_expression($value) && \strlen(eval('return '. $value . ';')) === 1) {
return $this->match_fail_conditional('\substr($this->string, $this->pos, 1) === '.$value,
PHPBuilder::build()->l(
'$this->pos += 1;',
$this->set_text($value)
Expand Down
2 changes: 1 addition & 1 deletion lib/hafriedlander/Peg/Compiler/Token/Recurse.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function match_code($value) {
)
);

$builder->needsStack = true;
$builder->needsStack = \true;
return $builder;

}
Expand Down
6 changes: 3 additions & 3 deletions lib/hafriedlander/Peg/Parser/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function expression($result, $stack, $value) {
foreach ($this->typestack($node['_matchrule']) as $type) {
$callback = [$this, "{$type}_DLR{$value}"];
if (is_callable($callback)) {
$rv = \call_user_func($callback);
$rv = $callback();
if ($rv !== \false) {
break;
}
Expand All @@ -97,7 +97,7 @@ public function expression($result, $stack, $value) {
$rv = @$this->$value();
}

return \is_array($rv) ? $rv['text'] : ($rv ? $rv : '');
return \is_array($rv) ? $rv['text'] : ($rv ?: '');
}

public function packhas($key, $pos) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public function construct($matchrule, $name, $arguments = []) {
];

if ($arguments) {
$result = array_merge($result, $arguments);
$result = \array_merge($result, $arguments);
}

foreach ($this->typestack($matchrule) as $type) {
Expand Down
2 changes: 1 addition & 1 deletion lib/hafriedlander/Peg/Parser/FalseOnlyPackrat.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct($string) {
}

public function packhas($key, $pos) {
return ($this->packstate[$key][$pos] ?? false) == 'F';
return ($this->packstate[$key][$pos] ?? \false) == 'F';
}

public function packread($key, $pos) {
Expand Down

0 comments on commit a54ec29

Please sign in to comment.