From a54ec295e9a45fb266ec664a216530cd9bc36398 Mon Sep 17 00:00:00 2001 From: Smuuf Date: Sun, 23 Aug 2020 11:41:43 +0200 Subject: [PATCH] More fixes and micro-optimizations. --- lib/hafriedlander/Peg/Compiler/PHPBuilder.php | 6 +++--- lib/hafriedlander/Peg/Compiler/PHPWriter.php | 4 +--- lib/hafriedlander/Peg/Compiler/Rule.php | 5 +++-- .../Peg/Compiler/Token/Expressionable.php | 8 ++++---- .../Peg/Compiler/Token/ExpressionedRecurse.php | 4 ++-- lib/hafriedlander/Peg/Compiler/Token/Literal.php | 11 ++++++----- lib/hafriedlander/Peg/Compiler/Token/Recurse.php | 2 +- lib/hafriedlander/Peg/Parser/Basic.php | 6 +++--- lib/hafriedlander/Peg/Parser/FalseOnlyPackrat.php | 2 +- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/hafriedlander/Peg/Compiler/PHPBuilder.php b/lib/hafriedlander/Peg/Compiler/PHPBuilder.php index 0a9b792..33c1ab0 100644 --- a/lib/hafriedlander/Peg/Compiler/PHPBuilder.php +++ b/lib/hafriedlander/Peg/Compiler/PHPBuilder.php @@ -4,7 +4,7 @@ class PHPBuilder { - public $needsStack = false; + public $needsStack = \false; static function build () { return new PHPBuilder(); @@ -28,7 +28,7 @@ function l(...$args) { if ($lines instanceof PHPBuilder) { if ($lines->needsStack) { - $this->needsStack = true; + $this->needsStack = \true; } $lines = $lines->lines; } else { @@ -54,7 +54,7 @@ function b(...$args) { $block->l(...$args); if ($block->needsStack) { - $this->needsStack = true; + $this->needsStack = \true; } $this->lines[] = [$entry, $block->lines]; diff --git a/lib/hafriedlander/Peg/Compiler/PHPWriter.php b/lib/hafriedlander/Peg/Compiler/PHPWriter.php index cf93c03..f0a5557 100644 --- a/lib/hafriedlander/Peg/Compiler/PHPWriter.php +++ b/lib/hafriedlander/Peg/Compiler/PHPWriter.php @@ -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'); } diff --git a/lib/hafriedlander/Peg/Compiler/Rule.php b/lib/hafriedlander/Peg/Compiler/Rule.php index 8e26e36..2ff0bd6 100644 --- a/lib/hafriedlander/Peg/Compiler/Rule.php +++ b/lib/hafriedlander/Peg/Compiler/Rule.php @@ -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) { @@ -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 ); diff --git a/lib/hafriedlander/Peg/Compiler/Token/Expressionable.php b/lib/hafriedlander/Peg/Compiler/Token/Expressionable.php index 48a7b5e..9c42f52 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Expressionable.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Expressionable.php @@ -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); } } diff --git a/lib/hafriedlander/Peg/Compiler/Token/ExpressionedRecurse.php b/lib/hafriedlander/Peg/Compiler/Token/ExpressionedRecurse.php index 558c50c..8c4d70c 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/ExpressionedRecurse.php +++ b/lib/hafriedlander/Peg/Compiler/Token/ExpressionedRecurse.php @@ -4,6 +4,6 @@ class ExpressionedRecurse extends Recurse { function match_function( $value ) { - return '$this->expression($result, $stack, \''.$value.'\')'; + return '$this->expression($result, $stack, \'' . $value . '\')'; } -} \ No newline at end of file +} diff --git a/lib/hafriedlander/Peg/Compiler/Token/Literal.php b/lib/hafriedlander/Peg/Compiler/Token/Literal.php index cb897af..d956fcf 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Literal.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Literal.php @@ -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) diff --git a/lib/hafriedlander/Peg/Compiler/Token/Recurse.php b/lib/hafriedlander/Peg/Compiler/Token/Recurse.php index d55c908..1ec66a7 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Recurse.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Recurse.php @@ -64,7 +64,7 @@ public function match_code($value) { ) ); - $builder->needsStack = true; + $builder->needsStack = \true; return $builder; } diff --git a/lib/hafriedlander/Peg/Parser/Basic.php b/lib/hafriedlander/Peg/Parser/Basic.php index 5f47603..df97076 100644 --- a/lib/hafriedlander/Peg/Parser/Basic.php +++ b/lib/hafriedlander/Peg/Parser/Basic.php @@ -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; } @@ -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) { @@ -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) { diff --git a/lib/hafriedlander/Peg/Parser/FalseOnlyPackrat.php b/lib/hafriedlander/Peg/Parser/FalseOnlyPackrat.php index e5c57e3..076817b 100644 --- a/lib/hafriedlander/Peg/Parser/FalseOnlyPackrat.php +++ b/lib/hafriedlander/Peg/Parser/FalseOnlyPackrat.php @@ -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) {