Skip to content

Commit

Permalink
PHP7.4 - PHP8.0 consistency, not entirely possible because for usort:…
Browse files Browse the repository at this point in the history
… prior to PHP 8.0.0, the relative order in the sorted array was undefined.
  • Loading branch information
wisskid committed Jan 29, 2023
1 parent 8f4a204 commit 9fa2b83
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/SmartyGenerator/ParserGenerator/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function display($processed = false)
self::REDUCE => 'REDUCE',
self::SHIFT => 'SHIFT'
);
$sep = isset($_SERVER['_']) ? "\n" : "<br>";
$sep = "\n";
echo $map[$this->type] . ' for ' . $this->sp->name;
if ($this->type == self::REDUCE) {
echo ' - rule ' . $this->x->lhs->name . $sep;
Expand All @@ -189,12 +189,12 @@ public static function Action_add(&$app, $type, Symbol $sp, $arg)
{
$new = new Action;
$new->next = $app;
$app = $new;
$new->type = $type;
$new->sp = $sp;
$new->x = $arg;
echo ' Adding ';
$new->display();
return $new;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/SmartyGenerator/ParserGenerator/ActionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function acttab_action($lookahead, $action)
public function acttab_insert()
{
if ($this->nLookahead <= 0) {
throw new Exception('nLookahead is not set up?');
throw new \Exception('nLookahead is not set up?');
}

/* Scan the existing action table looking for an offset where we can
Expand Down
17 changes: 11 additions & 6 deletions src/SmartyGenerator/ParserGenerator/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,10 @@ private function buildshifts(State $stp)
** on the symbol "sp" */
if ($sp->type == Symbol::MULTITERMINAL) {
for ($i = 0; $i < $sp->nsubsym; $i++) {
Action::Action_add($stp->ap, Action::SHIFT, $sp->subsym[$i],
$newstp);
$stp->ap = Action::Action_add($stp->ap, Action::SHIFT, $sp->subsym[$i], $newstp);
}
} else {
Action::Action_add($stp->ap, Action::SHIFT, $sp, $newstp);
$stp->ap = Action::Action_add($stp->ap, Action::SHIFT, $sp, $newstp);
}
}
}
Expand Down Expand Up @@ -641,7 +640,7 @@ public function FindActions()
if (isset($cfp->fws[$j])) {
/* Add a reduce action to the state "stp" which will reduce by the
** rule "cfp->rp" if the lookahead symbol is "$this->symbols[j]" */
Action::Action_add($stp->ap, Action::REDUCE,
$stp->ap = Action::Action_add($stp->ap, Action::REDUCE,
$this->symbols[$j], $cfp->rp);
}
}
Expand All @@ -661,7 +660,7 @@ public function FindActions()
/* Add to the first state (which is always the starting state of the
** finite state machine) an action to ACCEPT if the lookahead is the
** start nonterminal. */
Action::Action_add($this->sorted[0]->data->ap, Action::ACCEPT, $sp, 0);
$this->sorted[0]->data->ap = Action::Action_add($this->sorted[0]->data->ap, Action::ACCEPT, $sp, 0);

/* Resolve conflicts */
for ($i = 0; $i < $this->nstate; $i++) {
Expand Down Expand Up @@ -879,6 +878,12 @@ public function ResortStates()
}
$save = $this->sorted[0];
unset($this->sorted[0]);

/*
* The usort may cause the order of sorted to differ when compiled with PHP >=8.0, as compared to PHP <8.0.
* If two members compare as equal, they retain their original order. Prior to PHP 8.0.0, their relative
* order in the sorted array was undefined.
*/
usort($this->sorted, array(State::class, 'stateResortCompare'));
array_unshift($this->sorted, $save);
for ($i = 0; $i < $this->nstate; $i++) {
Expand Down Expand Up @@ -1044,7 +1049,7 @@ private function tplt_linedir($out, $lineno, $filename)
*/
private function tplt_print($out, $str, $strln, &$lineno)
{
if ($str == '' || $str == 0) {
if ($str === '' || $str === 0) {
return;
}
$this->tplt_linedir($out, $strln, $this->filename);
Expand Down
8 changes: 4 additions & 4 deletions src/SmartyGenerator/ParserGenerator/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,25 @@ class State
*
* @var int
*/
public $nTknAct,
public $nTknAct;
/**
* Number of non-terminal actions
*
* @var int
*/
$nNtAct;
public $nNtAct;
/**
* The offset into the $yy_action table for terminal tokens.
*
* @var int
*/
public $iTknOfst,
public $iTknOfst;
/**
* The offset into the $yy_action table for non-terminals.
*
* @var int
*/
$iNtOfst;
public $iNtOfst;
/**
* Default action
*
Expand Down

0 comments on commit 9fa2b83

Please sign in to comment.