Skip to content

Commit

Permalink
Write parser conflict error to stderr, not the debug stream
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Oct 25, 2019
1 parent 153f4f6 commit 31ee165
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions bin/phpyacc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ if (!defined('COMPOSER_INSTALL')) {
require COMPOSER_INSTALL;

$grammarFile = end($argv);

$resultFile = substr($grammarFile, 0, -1);

if ($grammarFile === '-') {
Expand All @@ -41,7 +40,8 @@ if ($argc < 2 || !file_exists($grammarFile)) {
exit(4);
}

$context = new Context($grammarFile);
$errorFile = fopen('php://stderr', 'w');
$context = new Context($grammarFile, $errorFile);

$skeleton = '';

Expand Down
3 changes: 2 additions & 1 deletion examples/rebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ function buildFolder(CliOptions $options, Generator $generator, string $dir) {
rename("$dir/grammar.php", "$dir/parser.kmyacc.php");
}

$errorFile = fopen("php://stderr", "w");
$debugFile = DEBUG ? fopen("$dir/y.phpyacc.output", 'w') : null;
$context = new Context($grammar, $debugFile, VERBOSE_DEBUG);
$context = new Context($grammar, $errorFile, $debugFile, VERBOSE_DEBUG);
$context->tflag = true;
$generator->generate(
$context,
Expand Down
13 changes: 11 additions & 2 deletions lib/Grammar/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Context
public $filename = 'YY';
public $pureFlag = false;
public $startSymbol = null;
public $expected = null;
public $expected = 0;
public $unioned = false;
public $eofToken = null;
public $errorToken = null;
Expand All @@ -76,15 +76,24 @@ class Context
public $naux = 0;

public $debugFile;
public $errorFile;

public function __construct(
string $filename = 'YY', $debugFile = null, bool $verboseDebug = false)
string $filename = 'YY', $errorFile = null, $debugFile = null, bool $verboseDebug = false)
{
$this->filename = $filename;
$this->errorFile = $errorFile;
$this->debugFile = $debugFile;
$this->verboseDebug = $verboseDebug;
}

public function error(string $data)
{
if ($this->errorFile) {
fwrite($this->errorFile, $data);
}
}

public function debug(string $data)
{
if ($this->debugFile) {
Expand Down
15 changes: 7 additions & 8 deletions lib/Lalr/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,19 +704,18 @@ protected function printState(State $state)
protected function printDiagnostics()
{
// TODO check expected_srconf
$expected_srconf = 0;
if ($this->nsrerr !== $expected_srconf || $this->nrrerr !== 0) {
$this->context->debug("{$this->context->filename}: there are ");
if ($this->nsrerr !== $expected_srconf) {
$this->context->debug(" $this->nsrerr shift/reduce");
if ($this->nsrerr !== $this->context->expected || $this->nrrerr !== 0) {
$this->context->error("{$this->context->filename}: there are ");
if ($this->nsrerr !== $this->context->expected) {
$this->context->error(" $this->nsrerr shift/reduce");
if ($this->nrrerr !== 0) {
$this->context->debug(" and");
$this->context->error(" and");
}
}
if ($this->nrrerr !== 0) {
$this->context->debug(" $this->nrrerr reduce/reduce");
$this->context->error(" $this->nrrerr reduce/reduce");
}
$this->context->debug(" conflicts\n");
$this->context->error(" conflicts\n");
}
}

Expand Down

0 comments on commit 31ee165

Please sign in to comment.