Skip to content

Commit

Permalink
Some more explicit top-level namespacing and some formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
smuuf committed Feb 23, 2020
1 parent d4422d0 commit 146375d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 107 deletions.
145 changes: 73 additions & 72 deletions lib/hafriedlander/Peg/Compiler/PHPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

}
2 changes: 1 addition & 1 deletion lib/hafriedlander/Peg/Compiler/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;'
Expand Down
16 changes: 8 additions & 8 deletions lib/hafriedlander/Peg/Compiler/Token/Recurse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;'
);
}
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/hafriedlander/Peg/Compiler/Token/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ;
Expand Down
18 changes: 11 additions & 7 deletions lib/hafriedlander/Peg/Compiler/Token/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
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,
'FAIL' => 'FBREAK'
])
);
}
$code->l( 'MBREAK' );

return $this->match_fail_block( $code ) ;
$code->l('MBREAK');
return $this->match_fail_block($code);

}

}
9 changes: 6 additions & 3 deletions lib/hafriedlander/Peg/Compiler/Token/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

}
12 changes: 7 additions & 5 deletions lib/hafriedlander/Peg/Compiler/Token/Whitespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
14 changes: 7 additions & 7 deletions lib/hafriedlander/Peg/Parser/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
Loading

0 comments on commit 146375d

Please sign in to comment.