diff --git a/lib/hafriedlander/Peg/Compiler/PHPBuilder.php b/lib/hafriedlander/Peg/Compiler/PHPBuilder.php index f0ac334..743e195 100644 --- a/lib/hafriedlander/Peg/Compiler/PHPBuilder.php +++ b/lib/hafriedlander/Peg/Compiler/PHPBuilder.php @@ -5,125 +5,126 @@ class PHPBuilder { static function build () { - return new PHPBuilder() ; + return new PHPBuilder(); } function __construct() { - $this->lines = [] ; + $this->lines = []; } function l() { - foreach ( \func_get_args() as $lines ) { - if ( !$lines ) continue ; + foreach (\func_get_args() as $lines) { + if (!$lines) continue; - if ( is_string( $lines ) ) $lines = preg_split( '/\r\n|\r|\n/', $lines ) ; - if ( !$lines ) continue ; + if (is_string($lines)) $lines = preg_split('/\r\n|\r|\n/', $lines); + if (!$lines) continue; - if ( $lines instanceof PHPBuilder ) $lines = $lines->lines ; - else $lines = \array_map( 'ltrim', $lines ) ; - if ( !$lines ) continue ; + if ($lines instanceof PHPBuilder) $lines = $lines->lines; + else $lines = \array_map('ltrim', $lines); + if (!$lines) continue; - $this->lines = \array_merge( $this->lines, $lines ) ; + $this->lines = \array_merge($this->lines, $lines); } - return $this ; + return $this; } function b() { - $args = \func_get_args() ; - $entry = \array_shift( $args ) ; + $args = \func_get_args(); + $entry = \array_shift($args); - $block = new PHPBuilder() ; - call_user_func_array( array( $block, 'l' ), $args ) ; + $block = new PHPBuilder(); + call_user_func_array(array($block, 'l'), $args); - $this->lines[] = array( $entry, $block->lines ) ; + $this->lines[] = array($entry, $block->lines); - return $this ; + return $this; } - function replace( $replacements, &$array = \null ) { - if ( $array === \null ) { - unset( $array ) ; - $array =& $this->lines ; + function replace($replacements, &$array = \null) { + if ($array === \null) { + unset($array); + $array =& $this->lines; } - $i = 0 ; - while ( $i < \count( $array ) ) { + $i = 0; + while ($i < \count($array)) { /* Recurse into blocks */ - if ( \is_array( $array[$i] ) ) { - $this->replace( $replacements, $array[$i][1] ) ; + if (\is_array($array[$i])) { + $this->replace($replacements, $array[$i][1]); - if ( \count( $array[$i][1] ) == 0 ) { - $nextelse = isset( $array[$i+1] ) && \is_array( $array[$i+1] ) && \preg_match( '/^\s*else\s*$/i', $array[$i+1][0] ) ; + if (\count($array[$i][1]) == 0) { + $nextelse = isset($array[$i+1]) && \is_array($array[$i+1]) && \preg_match('/^\s*else\s*$/i', $array[$i+1][0]); - $delete = \preg_match( '/^\s*else\s*$/i', $array[$i][0] ) ; - $delete = $delete || ( \preg_match( '/^\s*if\s*\(/i', $array[$i][0] ) && !$nextelse ) ; + $delete = \preg_match('/^\s*else\s*$/i', $array[$i][0]); + $delete = $delete || (\preg_match('/^\s*if\s*\(/i', $array[$i][0]) && !$nextelse); - if ( $delete ) { + if ($delete) { // Is this always safe? Not if the expression has side-effects. - // print "/* REMOVING EMPTY BLOCK: " . $array[$i][0] . "*/\n" ; - \array_splice( $array, $i, 1 ) ; - continue ; + // print "/* REMOVING EMPTY BLOCK: " . $array[$i][0] . "*/\n"; + \array_splice($array, $i, 1); + continue; } } - } - - /* Handle replacing lines with \null to remove, or string, array of strings or PHPBuilder to replace */ - else { - if ( \array_key_exists( $array[$i], $replacements ) ) { - $rep = $replacements[$array[$i]] ; - - if ( $rep === \null ) { - \array_splice( $array, $i, 1 ) ; - continue ; + } else { + /* Handle replacing lines with \null to remove, or string, array of strings or PHPBuilder to replace */ + if (\array_key_exists($array[$i], $replacements)) { + $rep = $replacements[$array[$i]]; + + if ($rep === \null) { + \array_splice($array, $i, 1); + continue; } - if ( is_string( $rep ) ) { - $array[$i] = $rep ; - $i++ ; - continue ; + if (is_string($rep)) { + $array[$i] = $rep; + $i++ ; + continue; } - if ( $rep instanceof PHPBuilder ) $rep = $rep->lines ; + if ($rep instanceof PHPBuilder) $rep = $rep->lines; - if ( \is_array( $rep ) ) { - \array_splice( $array, $i, 1, $rep ) ; $i += \count( $rep ) + 1 ; - continue ; + if (\is_array($rep)) { + \array_splice($array, $i, 1, $rep); $i += \count($rep) + 1; + continue; } - throw new \Exception('Unknown type passed to PHPBuilder#replace') ; + throw new \Exception('Unknown type passed to PHPBuilder#replace'); } } - $i++ ; + $i++; } - return $this ; + return $this; } - function render( $array = \null, $indent = "" ) { - if ( $array === \null ) $array = $this->lines ; + function render($array = \null, $indent = "") { - $out = [] ; - foreach( $array as $line ) { - if ( \is_array( $line ) ) { - [$entry, $block] = $line ; - $str = $this->render( $block, $indent . "\t" ) ; + if ($array === \null) { + $array = $this->lines; + } - if ( \strlen( $str ) < 40 ) { - $out[] = $indent . $entry . ' { ' . \ltrim( $str ) . ' }' ; + $out = []; + foreach($array as $line) { + if (\is_array($line)) { + [$entry, $block] = $line; + $str = $this->render($block, $indent . "\t"); + + if (\strlen($str) < 40) { + $out[] = $indent . $entry . ' { ' . \ltrim($str) . ' }'; + } else { + $out[] = $indent . $entry . ' {'; + $out[] = $str; + $out[] = $indent . '}'; } - else { - $out[] = $indent . $entry . ' {' ; - $out[] = $str ; - $out[] = $indent . '}' ; - } - } - else { - $out[] = $indent . $line ; + } else { + $out[] = $indent . $line; } } - return \implode(\PHP_EOL, $out ) ; + return \implode(\PHP_EOL, $out); + } + } diff --git a/lib/hafriedlander/Peg/Compiler/Rule.php b/lib/hafriedlander/Peg/Compiler/Rule.php index 75156d3..00edeb9 100644 --- a/lib/hafriedlander/Peg/Compiler/Rule.php +++ b/lib/hafriedlander/Peg/Compiler/Rule.php @@ -298,7 +298,7 @@ function compile($indent) { $match->l("protected \$match_{$function_name}_typestack = $typestack;"); $match->b( "function match_{$function_name} (\$stack = [])", - '$matchrule = "'.$function_name.'"; $result = $this->construct($matchrule, $matchrule, '.$arguments.');$newStack = \array_merge($stack, [$result]);', + '$matchrule = "'.$function_name.'"; $result = $this->construct($matchrule, $matchrule, '.$arguments.'); $newStack = \array_merge($stack, [$result]);', $this->parsed->compile()->replace(array( 'MATCH' => 'return $this->finalise($result);', 'FAIL' => 'return \false;' diff --git a/lib/hafriedlander/Peg/Compiler/Token/Recurse.php b/lib/hafriedlander/Peg/Compiler/Token/Recurse.php index 956e066..e65aaf5 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Recurse.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Recurse.php @@ -21,22 +21,22 @@ function match_code( $value ) { if ( \hafriedlander\Peg\Compiler::$debug ) { $debug_header = PHPBuilder::build() ->l( - '$indent = str_repeat( "\e[90m| \e[0m", $this->depth / 2);', + '$indent = str_repeat("\e[90m| \e[0m", $this->depth / 2);', '$this->depth += 2;', - '$sub = ( strlen( $this->string ) - $this->pos > 40 ) ? ( substr( $this->string, $this->pos, 40 ) . "..." ) : substr( $this->string, $this->pos );', - '$sub = preg_replace( \'/(\r|\n)+/\', " {NL} ", $sub );', - sprintf('print( $indent."Matching \e[32m%s\e[0m \"\e[36m".$sub."\e[0m\" \n" );', trim($function, "'")) + '$sub = (strlen( $this->string ) - $this->pos > 40) ? substr($this->string, $this->pos, 40) . "...") : substr($this->string, $this->pos);', + '$sub = preg_replace(\'/(\r|\n)+/\', " {NL} ", $sub);', + sprintf('print $indent . "Matching \e[32m%s\e[0m \"\e[36m".$sub."\e[0m\" \n";', trim($function, "'")) ); $debug_match = PHPBuilder::build() ->l( - 'print( $indent."\e[1m\e[42mOK\n\e[0m" );', + 'print $indent . "\e[1m\e[42mOK\n\e[0m";', '$this->depth -= 2;' ); $debug_fail = PHPBuilder::build() ->l( - 'print( $indent."-\n" );', + 'print $indent . "-\n";', '$this->depth -= 2;' ); } @@ -52,8 +52,8 @@ function match_code( $value ) { PHPBuilder::build()->l( $debug_match, $this->tag === \false ? - '$this->store( $result, $subres );' : - '$this->store( $result, $subres, "'.$storetag.'" );' + '$this->store($result, $subres);' : + '$this->store($result, $subres, "'.$storetag.'");' ), PHPBuilder::build()->l( $debug_fail diff --git a/lib/hafriedlander/Peg/Compiler/Token/Regex.php b/lib/hafriedlander/Peg/Compiler/Token/Regex.php index c7c5386..227ba78 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Regex.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Regex.php @@ -2,8 +2,6 @@ namespace hafriedlander\Peg\Compiler\Token; -use hafriedlander\Peg\Compiler\PHPBuilder; - class Regex extends Expressionable { static function escape( $rx ) { $rx = \str_replace( "'", "\\'", $rx ) ; diff --git a/lib/hafriedlander/Peg/Compiler/Token/Sequence.php b/lib/hafriedlander/Peg/Compiler/Token/Sequence.php index 70d10b9..b36b86a 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Sequence.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Sequence.php @@ -6,13 +6,15 @@ use hafriedlander\Peg\Compiler\PHPBuilder; class Sequence extends Token { - function __construct( $value ) { - parent::__construct( 'sequence', $value ) ; + + function __construct($value) { + parent::__construct('sequence', $value); } - function match_code( $value ) { - $code = PHPBuilder::build() ; - foreach( $value as $token ) { + function match_code($value) { + $code = PHPBuilder::build(); + + foreach ($value as $token) { $code->l( $token->compile()->replace([ 'MATCH' => \null, @@ -20,8 +22,10 @@ function match_code( $value ) { ]) ); } - $code->l( 'MBREAK' ); - return $this->match_fail_block( $code ) ; + $code->l('MBREAK'); + return $this->match_fail_block($code); + } + } diff --git a/lib/hafriedlander/Peg/Compiler/Token/Terminal.php b/lib/hafriedlander/Peg/Compiler/Token/Terminal.php index aa75bdb..22ee351 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Terminal.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Terminal.php @@ -5,13 +5,16 @@ use hafriedlander\Peg\Compiler\Token; abstract class Terminal extends Token { - function set_text( $text ) { + + function set_text($text) { return $this->silent ? \null : '$result["text"] .= ' . $text . ';'; } - protected function match_code( $value ) { - return $this->match_fail_conditional( '( $subres = $this->'.$this->type.'( '.$value.' ) ) !== \false', + protected function match_code($value) { + return $this->match_fail_conditional( + '($subres = $this->' . $this->type . '(' . $value . ')) !== \false', $this->set_text('$subres') ); } + } diff --git a/lib/hafriedlander/Peg/Compiler/Token/Whitespace.php b/lib/hafriedlander/Peg/Compiler/Token/Whitespace.php index a2fb84d..8972eb9 100644 --- a/lib/hafriedlander/Peg/Compiler/Token/Whitespace.php +++ b/lib/hafriedlander/Peg/Compiler/Token/Whitespace.php @@ -3,13 +3,15 @@ namespace hafriedlander\Peg\Compiler\Token; class Whitespace extends Terminal { - function __construct( $optional ) { - parent::__construct( 'whitespace', $optional ) ; + + function __construct($optional) { + parent::__construct('whitespace', $optional); } /* Call recursion indirectly */ - function match_code( $value ) { - $code = parent::match_code( '' ) ; - return $value ? $code->replace( ['FAIL' => \null] ) : $code ; + function match_code($value) { + $code = parent::match_code(''); + return $value ? $code->replace(['FAIL' => \null]) : $code; } + } diff --git a/lib/hafriedlander/Peg/Parser/Basic.php b/lib/hafriedlander/Peg/Parser/Basic.php index b3a9b81..c6b9608 100644 --- a/lib/hafriedlander/Peg/Parser/Basic.php +++ b/lib/hafriedlander/Peg/Parser/Basic.php @@ -48,21 +48,21 @@ function expression( $result, $stack, $value ) { $stack[] = $result; $rv = \false; /* Search backwards through the sub-expression stacks */ - for ( $i = count($stack) - 1 ; $i >= 0 ; $i-- ) { + for ( $i = \count($stack) - 1 ; $i >= 0 ; $i-- ) { $node = $stack[$i]; if ( isset($node[$value]) ) { $rv = $node[$value]; break; } foreach ($this->typestack($node['_matchrule']) as $type) { $callback = array($this, "{$type}_DLR{$value}"); - if ( is_callable( $callback ) ) { $rv = call_user_func( $callback ) ; if ($rv !== \false) break; } + if ( is_callable( $callback ) ) { $rv = \call_user_func( $callback ) ; if ($rv !== \false) break; } } } if ($rv === \false) $rv = @$this->$value; if ($rv === \false) $rv = @$this->$value(); - return is_array($rv) ? $rv['text'] : ($rv ? $rv : ''); + return \is_array($rv) ? $rv['text'] : ($rv ? $rv : ''); } function packhas( $key, $pos ) { @@ -92,7 +92,7 @@ function construct( $matchrule, $name, $arguments = \null ) { foreach ($this->typestack($matchrule) as $type) { $callback = array( $this, "{$type}__construct" ) ; if ( \is_callable( $callback ) ) { - call_user_func_array( $callback, array( &$result ) ) ; + \call_user_func_array( $callback, array( &$result ) ) ; break; } } @@ -105,7 +105,7 @@ function finalise( &$result ) { foreach ($this->typestack($result['_matchrule']) as $type) { $callback = array( $this, "{$type}__finalise" ) ; if ( \is_callable( $callback ) ) { - call_user_func_array( $callback, array( &$result ) ) ; + \call_user_func_array( $callback, array( &$result ) ) ; break; } } @@ -121,13 +121,13 @@ function store ( &$result, $subres, $storetag = \null ) { foreach ($this->typestack($result['_matchrule']) as $type) { $callback = array( $this, $storetag ? "{$type}_{$storetag}" : "{$type}_{$subres['name']}" ) ; if ( \is_callable( $callback ) ) { - call_user_func_array( $callback, array( &$result, $subres ) ) ; + \call_user_func_array( $callback, array( &$result, $subres ) ) ; $storecalled = \true; break; } $globalcb = array( $this, "{$type}_STR" ) ; if ( \is_callable( $globalcb ) ) { - call_user_func_array( $globalcb, array( &$result, $subres ) ) ; + \call_user_func_array( $globalcb, array( &$result, $subres ) ) ; $storecalled = \true; break; } } diff --git a/lib/hafriedlander/Peg/Parser/CachedRegexp.php b/lib/hafriedlander/Peg/Parser/CachedRegexp.php index 1110f2f..f63e871 100644 --- a/lib/hafriedlander/Peg/Parser/CachedRegexp.php +++ b/lib/hafriedlander/Peg/Parser/CachedRegexp.php @@ -24,7 +24,7 @@ function __construct($parser, $rx) { $this->parser = $parser; $modifiers = \str_split(\substr($rx, \strrpos($rx, '/') + 1)); - $this->modifiers = array_unique(array_merge(self::DEFAULT_MODIFIERS, $modifiers)); + $this->modifiers = \array_unique(\array_merge(self::DEFAULT_MODIFIERS, $modifiers)); $this->rx = $rx . \implode('', $this->modifiers); $this->matches = \null; @@ -51,7 +51,7 @@ function match() { } if ($this->match_pos === $current_pos) { - $this->parser->pos += strlen($this->matches[0][0]); + $this->parser->pos += \strlen($this->matches[0][0]); return $this->matches[0][0]; }