Skip to content

Commit

Permalink
Fixed passing new stack, removed colors from debug output for cleaner…
Browse files Browse the repository at this point in the history
… diffs.
  • Loading branch information
smuuf committed Dec 3, 2020
1 parent a54ec29 commit 270db35
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 35 deletions.
Empty file modified bin/tests
100644 → 100755
Empty file.
10 changes: 0 additions & 10 deletions lib/hafriedlander/Peg/Compiler/PHPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

class PHPBuilder {

public $needsStack = \false;

static function build () {
return new PHPBuilder();
}
Expand All @@ -27,9 +25,6 @@ function l(...$args) {
}

if ($lines instanceof PHPBuilder) {
if ($lines->needsStack) {
$this->needsStack = \true;
}
$lines = $lines->lines;
} else {
$lines = \array_map('rtrim', $lines);
Expand All @@ -52,11 +47,6 @@ function b(...$args) {

$block = new PHPBuilder();
$block->l(...$args);

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

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

return $this;
Expand Down
23 changes: 5 additions & 18 deletions lib/hafriedlander/Peg/Compiler/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,6 @@ public function compile($indent) {

$typestack = "['" . \implode("','", $typestack) . "']";

// Build an array of additional arguments to add to result node (if any)
if (empty($this->arguments)) {
$arguments = \false;
} else {
$arguments = '[';
foreach ($this->arguments as $k => $v) {
$arguments .= "'$k' => '$v', ";
}
$arguments .= ']';
}

$match = PHPBuilder::build() ;

$match->l("protected \$match_{$function_name}_typestack = $typestack;");
Expand All @@ -314,16 +303,14 @@ public function compile($indent) {
'FAIL' => 'return \false;'
]);

// This is only needed if '$newStack' variable is actually used.
$newStack = $block->needsStack
? '$newStack = \array_merge($stack, [$result]);'
// Build an array of additional arguments to add to result node (if any).
$arguments = $this->arguments
? (", " . \var_export($this->arguments, \true))
: '';

$fnArguments = $block->needsStack ? '$stack = []' : '';
$arguments = $arguments ? ", {$arguments}" : '';
$match->b(
"function match_{$function_name} ($fnArguments)",
'$matchrule = "' . $function_name . '"; $result = $this->construct($matchrule, $matchrule' . $arguments . '); ' . $newStack ,
"function match_{$function_name}(\$stack = [])",
"\$matchrule = '$function_name'; \$result = \$this->construct(\$matchrule, \$matchrule$arguments); ",
$block
);

Expand Down
8 changes: 7 additions & 1 deletion lib/hafriedlander/Peg/Compiler/Token/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ function __construct($value) {
}

function match_code($value) {
try {
$evald = eval('return '. $value . ';');
} catch (\ParseError $e) {
die("PEG grammar parsing error in >return $value;<': " . $e->getMessage());
}

// We inline single-character matches for speed
if (!$this->contains_expression($value) && \strlen(eval('return '. $value . ';')) === 1) {
if (!$this->contains_expression($value) && \strlen($evald) === 1) {
return $this->match_fail_conditional('\substr($this->string, $this->pos, 1) === '.$value,
PHPBuilder::build()->l(
'$this->pos += 1;',
Expand Down
11 changes: 5 additions & 6 deletions lib/hafriedlander/Peg/Compiler/Token/Recurse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public 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("| ", $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 = ((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";', $function)
sprintf('print $indent . "Matching: <%s> in \'".$sub."\' \n";', $function)
);

$debug_match = PHPBuilder::build()
->l(
'print $indent . "\e[1m\e[42mOK\n\e[0m";',
'print $indent . "OK\n";',
'$this->depth -= 2;'
);

Expand All @@ -49,7 +49,7 @@ public function match_code($value) {
$debug_header,
'$subres = $this->packhas($key, $pos)' . "\n\t"
. '? $this->packread($key, $pos)' . "\n\t"
. ': $this->packwrite($key, $pos, $this->match_' . $function . '($newStack));',
. ': $this->packwrite($key, $pos, $this->match_' . $function . '(\array_merge($stack, [$result])));',
$this->match_fail_conditional(
'$subres !== \false',
PHPBuilder::build()->l(
Expand All @@ -64,7 +64,6 @@ public function match_code($value) {
)
);

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

}
Expand Down

0 comments on commit 270db35

Please sign in to comment.