Skip to content

Commit 964dd6c

Browse files
committed
Add AST dump helper and sample fixtures
1 parent 29c3551 commit 964dd6c

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

TOLERANT_TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Monitor RFCs merged into php-src and mirror the token/grammar changes, for examp
6262

6363
### Verification Strategy
6464

65-
- **AST comparison**: use Phan’s `tools/dump_ast.php` (php-ast) to capture the expected AST for new syntax. Create or extend a tolerant-side helper (instantiate `Microsoft\PhpParser\Parser` and `var_dump` the node tree) so we can diff tolerant output against php-ast.
65+
- **AST comparison**: use Phan’s `tools/dump_ast.php` (php-ast) to capture the expected AST for new syntax. For the tolerant side, run `php tools/PrintTolerantAst.php` (raw tolerant tree) in combination with Phan’s `internal/dump_fallback_ast.php` (uses the converter) to ensure both the parse tree and converted php-ast structures match expectations.
6666
- **PHP runtime selection**: on this dev machine we can run `sudo newphp 83`, `sudo newphp 84`, etc. to switch CLI versions; other environments may require Docker images, phpenv, asdf, etc. Record the PHP version used when regenerating fixtures.
6767
- **Leverage Phan fixtures**: pull feature-specific testcases (e.g. property hooks, asymmetric visibility) from `phan/tests/files/src` into tolerant’s parser tests to validate new constructs.
6868
- **Run tolerant PHPUnit suites**: keep `vendor/bin/phpunit --testsuite invariants,api` (with `zend.assertions=1`) as a fast regression check while iterating.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
class Example {
4+
public const FOO = 'foo';
5+
public const BAR = 'bar';
6+
}
7+
8+
function pickConst(): string {
9+
return 'FOO';
10+
}
11+
12+
$name = 'BAR';
13+
14+
// Dynamic class constant fetch (PHP 8.3)
15+
var_dump(Example::{pickConst()});
16+
var_dump(Example::{ $name });

tests/samples/property_hooks.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class Counter {
4+
private int $value = 0;
5+
6+
public int $count {
7+
get => $this->value;
8+
set (int $newValue) => $this->value = max(0, $newValue);
9+
}
10+
}
11+
12+
$counter = new Counter();
13+
$counter->count = 5;
14+
var_dump($counter->count);

tools/PrintTolerantAst.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
use Microsoft\PhpParser\DiagnosticsProvider;
8+
use Microsoft\PhpParser\Parser;
9+
10+
if ($argc < 2) {
11+
fwrite(STDERR, "Usage: php tools/PrintTolerantAst.php <file.php>\n");
12+
exit(1);
13+
}
14+
15+
$file = $argv[1];
16+
if (!is_file($file)) {
17+
fwrite(STDERR, "File not found: {$file}\n");
18+
exit(1);
19+
}
20+
21+
$code = file_get_contents($file);
22+
$parser = new Parser();
23+
$node = $parser->parseSourceFile($code);
24+
25+
fwrite(STDOUT, "===== AST =====\n");
26+
echo json_encode($node, JSON_PRETTY_PRINT) ?: '';
27+
fwrite(STDOUT, "\n\n===== Diagnostics =====\n");
28+
echo json_encode(DiagnosticsProvider::getDiagnostics($node), JSON_PRETTY_PRINT) ?: '';
29+
fwrite(STDOUT, "\n");

0 commit comments

Comments
 (0)