Skip to content

Commit

Permalink
Create "next stack" inside rule handlers only if necessary.
Browse files Browse the repository at this point in the history
+ Some code reformatting.
  • Loading branch information
smuuf committed Mar 27, 2020
1 parent 146375d commit 134e20a
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 264 deletions.
46 changes: 34 additions & 12 deletions lib/hafriedlander/Peg/Compiler/PHPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class PHPBuilder {

public $needsStack = false;

static function build () {
return new PHPBuilder();
}
Expand All @@ -12,30 +14,50 @@ function __construct() {
$this->lines = [];
}

function l() {
foreach (\func_get_args() as $lines) {
if (!$lines) continue;
function l(...$args) {

foreach ($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 instanceof PHPBuilder) $lines = $lines->lines;
else $lines = \array_map('ltrim', $lines);
if (!$lines) continue;
if ($lines instanceof PHPBuilder) {
if ($lines->needsStack) {
$this->needsStack = true;
}
$lines = $lines->lines;
} else {
$lines = \array_map('rtrim', $lines);
}

if (!$lines) {
continue;
}

$this->lines = \array_merge($this->lines, $lines);

}

return $this;
}

function b() {
$args = \func_get_args();
function b(...$args) {

$entry = \array_shift($args);

$block = new PHPBuilder();
call_user_func_array(array($block, 'l'), $args);
$block->l(...$args);

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

$this->lines[] = array($entry, $block->lines);
$this->lines[] = [$entry, $block->lines];

return $this;
}
Expand Down
95 changes: 49 additions & 46 deletions lib/hafriedlander/Peg/Compiler/PHPWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,76 @@
*/
class PHPWriter {

static $varid = 0 ;
public static $varid = 0;

function varid() {
return '_' . (self::$varid++) ;
public function varid() {
return '_' . (self::$varid++);
}

function function_name( $str ) {
$str = \preg_replace( '/-/', '_', $str ) ;
$str = \preg_replace( '/\$/', 'DLR', $str ) ;
$str = \preg_replace( '/\*/', 'STR', $str ) ;
$str = \preg_replace( '/[^\w]+/', '', $str ) ;
return $str ;
public function function_name($str) {
$str = \preg_replace('/-/', '_', $str);
$str = \preg_replace('/\$/', 'DLR', $str);
$str = \preg_replace('/\*/', 'STR', $str);
$str = \preg_replace('/[^\w]+/', '', $str);
return $str;
}

function save($id) {
public function save($id) {
return PHPBuilder::build()
->l(
'$res'.$id.' = $result;',
'$pos'.$id.' = $this->pos;'
);
'$res' . $id . ' = $result;',
'$pos' . $id . ' = $this->pos;'
);
}

function restore( $id, $remove = \false ) {
public function restore($id, $remove = \false) {
$code = PHPBuilder::build()
->l(
'$result = $res'.$id.';',
'$this->pos = $pos'.$id.';'
);
'$result = $res' . $id . ';',
'$this->pos = $pos' . $id . ';'
);

if ( $remove ) $code->l(
'unset( $res'.$id.' );',
'unset( $pos'.$id.' );'
);
if ($remove) {
$code->l(
'unset($res' . $id . ', $pos' . $id . ');'
);
}

return $code ;
return $code;
}

function match_fail_conditional( $on, $match = \null, $fail = \null ) {
public function match_fail_conditional($on, $match = \null, $fail = \null) {
return PHPBuilder::build()
->b( 'if (' . $on . ')',
$match,
'MATCH'
)
->b( 'else',
$fail,
'FAIL'
);
->b(
'if (' . $on . ')',
$match,
'MATCH'
)
->b(
'else',
$fail,
'FAIL'
);
}

function match_fail_block( $code ) {
$id = $this->varid() ;
public function match_fail_block($code) {
$id = $this->varid();

return PHPBuilder::build()
->l(
'$'.$id.' = \null;'
)
->b( 'do',
$code->replace([
'MBREAK' => '$'.$id.' = \true; break;',
'FBREAK' => '$'.$id.' = \false; break;'
])
)
'$' . $id . ' = \null;'
)
->b(
'do',
$code->replace([
'MBREAK' => '$' . $id . ' = \true; break;',
'FBREAK' => '$' . $id . ' = \false; break;'
])
)
->l(
'while(0);'
)
->b( 'if( $'.$id.' === \true )', 'MATCH' )
->b( 'if( $'.$id.' === \false)', 'FAIL' )
;
'while(false);'
)
->b('if( $' . $id . ' === \true )', 'MATCH')
->b('if( $' . $id . ' === \false)', 'FAIL');
}
}
Loading

0 comments on commit 134e20a

Please sign in to comment.