Skip to content

Commit e453389

Browse files
committed
Add forward-compatibility ParserFactory methods
Add ParserFactory::createForNewestSupportedVersion() and ParserFactory::createForHostVersion() for forward-compatibility with PHP-Parser 5. These methods do not accept an externally constructed lexer and always enable all attributes.
1 parent 402b6cf commit e453389

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

lib/PhpParser/ParserFactory.php

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace PhpParser;
44

5+
use PhpParser\Lexer\Emulative;
6+
use PhpParser\Parser\Php7;
7+
58
class ParserFactory
69
{
710
const PREFER_PHP7 = 1;
@@ -41,4 +44,33 @@ public function create(int $kind, Lexer $lexer = null, array $parserOptions = []
4144
);
4245
}
4346
}
47+
48+
/**
49+
* Create a parser targeting the newest version supported by this library. Code for older
50+
* versions will be accepted if there have been no relevant backwards-compatibility breaks in
51+
* PHP.
52+
*
53+
* All supported lexer attributes (comments, startLine, endLine, startTokenPos, endTokenPos,
54+
* startFilePos, endFilePos) will be enabled.
55+
*/
56+
public function createForNewestSupportedVersion(): Parser {
57+
return new Php7(new Emulative($this->getLexerOptions()));
58+
}
59+
60+
/**
61+
* Create a parser targeting the host PHP version, that is the PHP version we're currently
62+
* running on. This parser will not use any token emulation.
63+
*
64+
* All supported lexer attributes (comments, startLine, endLine, startTokenPos, endTokenPos,
65+
* startFilePos, endFilePos) will be enabled.
66+
*/
67+
public function createForHostVersion(): Parser {
68+
return new Php7(new Lexer($this->getLexerOptions()));
69+
}
70+
71+
private function getLexerOptions(): array {
72+
return ['usedAttributes' => [
73+
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos', 'startFilePos', 'endFilePos',
74+
]];
75+
}
4476
}

test/PhpParser/ParserFactoryTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/* This test is very weak, because PHPUnit's assertEquals assertion is way too slow dealing with the
66
* large objects involved here. So we just do some basic instanceof tests instead. */
77

8+
use PhpParser\Node\Stmt\Echo_;
9+
810
class ParserFactoryTest extends \PHPUnit\Framework\TestCase
911
{
1012
/** @dataProvider provideTestCreate */
@@ -33,4 +35,26 @@ public function provideTestCreate() {
3335
]
3436
];
3537
}
38+
39+
/** @dataProvider provideTestLexerAttributes */
40+
public function testLexerAttributes(Parser $parser) {
41+
$stmts = $parser->parse("<?php /* Bar */ echo 'Foo';");
42+
$stmt = $stmts[0];
43+
$this->assertInstanceOf(Echo_::class, $stmt);
44+
$this->assertCount(1, $stmt->getComments());
45+
$this->assertSame(1, $stmt->getStartLine());
46+
$this->assertSame(1, $stmt->getEndLine());
47+
$this->assertSame(3, $stmt->getStartTokenPos());
48+
$this->assertSame(6, $stmt->getEndTokenPos());
49+
$this->assertSame(16, $stmt->getStartFilePos());
50+
$this->assertSame(26, $stmt->getEndFilePos());
51+
}
52+
53+
public function provideTestLexerAttributes() {
54+
$factory = new ParserFactory();
55+
return [
56+
[$factory->createForHostVersion()],
57+
[$factory->createForNewestSupportedVersion()],
58+
];
59+
}
3660
}

0 commit comments

Comments
 (0)