Chris@13: expectException(Error::class); Chris@17: $this->expectExceptionMessage('Syntax error, unexpected EOF on line 1'); Chris@0: $parser = $this->getParser(new Lexer()); Chris@0: $parser->parse('expectException(Error::class); Chris@17: $this->expectExceptionMessage('Cannot use foo as self because \'self\' is a special class name on line 1'); Chris@0: $parser = $this->getParser(new Lexer()); Chris@0: $parser->parse('expectException(Error::class); Chris@17: $this->expectExceptionMessage('Unterminated comment on line 1'); Chris@0: $parser = $this->getParser(new Lexer()); Chris@0: $parser->parse(' [ Chris@0: 'comments', 'startLine', 'endLine', Chris@0: 'startTokenPos', 'endTokenPos', Chris@13: ] Chris@13: ]); Chris@0: Chris@0: $code = <<<'EOC' Chris@0: getParser($lexer); Chris@0: $stmts = $parser->parse($code); Chris@0: Chris@13: /** @var Stmt\Function_ $fn */ Chris@0: $fn = $stmts[0]; Chris@13: $this->assertInstanceOf(Stmt\Function_::class, $fn); Chris@13: $this->assertEquals([ Chris@13: 'comments' => [ Chris@13: new Comment\Doc('/** Doc comment */', 2, 6, 1), Chris@13: ], Chris@0: 'startLine' => 3, Chris@0: 'endLine' => 7, Chris@0: 'startTokenPos' => 3, Chris@0: 'endTokenPos' => 21, Chris@13: ], $fn->getAttributes()); Chris@0: Chris@0: $param = $fn->params[0]; Chris@13: $this->assertInstanceOf(Node\Param::class, $param); Chris@13: $this->assertEquals([ Chris@0: 'startLine' => 3, Chris@0: 'endLine' => 3, Chris@0: 'startTokenPos' => 7, Chris@0: 'endTokenPos' => 7, Chris@13: ], $param->getAttributes()); Chris@0: Chris@13: /** @var Stmt\Echo_ $echo */ Chris@0: $echo = $fn->stmts[0]; Chris@13: $this->assertInstanceOf(Stmt\Echo_::class, $echo); Chris@13: $this->assertEquals([ Chris@13: 'comments' => [ Chris@13: new Comment("// Line\n", 4, 49, 12), Chris@13: new Comment("// Comments\n", 5, 61, 14), Chris@13: ], Chris@0: 'startLine' => 6, Chris@0: 'endLine' => 6, Chris@0: 'startTokenPos' => 16, Chris@0: 'endTokenPos' => 19, Chris@13: ], $echo->getAttributes()); Chris@0: Chris@0: /** @var \PhpParser\Node\Expr\Variable $var */ Chris@0: $var = $echo->exprs[0]; Chris@13: $this->assertInstanceOf(Expr\Variable::class, $var); Chris@13: $this->assertEquals([ Chris@0: 'startLine' => 6, Chris@0: 'endLine' => 6, Chris@0: 'startTokenPos' => 18, Chris@0: 'endTokenPos' => 18, Chris@13: ], $var->getAttributes()); Chris@0: } Chris@0: Chris@0: public function testInvalidToken() { Chris@17: $this->expectException(\RangeException::class); Chris@17: $this->expectExceptionMessage('The lexer returned an invalid token (id=999, value=foobar)'); Chris@0: $lexer = new InvalidTokenLexer; Chris@0: $parser = $this->getParser($lexer); Chris@0: $parser->parse('dummy'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider provideTestExtraAttributes Chris@0: */ Chris@0: public function testExtraAttributes($code, $expectedAttributes) { Chris@17: $parser = $this->getParser(new Lexer\Emulative); Chris@0: $stmts = $parser->parse("expr : $stmts[0]; Chris@13: $attributes = $node->getAttributes(); Chris@0: foreach ($expectedAttributes as $name => $value) { Chris@0: $this->assertSame($value, $attributes[$name]); Chris@0: } Chris@0: } Chris@0: Chris@0: public function provideTestExtraAttributes() { Chris@13: return [ Chris@13: ['0', ['kind' => Scalar\LNumber::KIND_DEC]], Chris@13: ['9', ['kind' => Scalar\LNumber::KIND_DEC]], Chris@13: ['07', ['kind' => Scalar\LNumber::KIND_OCT]], Chris@13: ['0xf', ['kind' => Scalar\LNumber::KIND_HEX]], Chris@13: ['0XF', ['kind' => Scalar\LNumber::KIND_HEX]], Chris@13: ['0b1', ['kind' => Scalar\LNumber::KIND_BIN]], Chris@13: ['0B1', ['kind' => Scalar\LNumber::KIND_BIN]], Chris@13: ['[]', ['kind' => Expr\Array_::KIND_SHORT]], Chris@13: ['array()', ['kind' => Expr\Array_::KIND_LONG]], Chris@13: ["'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]], Chris@13: ["b'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]], Chris@13: ["B'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]], Chris@13: ['"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]], Chris@13: ['b"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]], Chris@13: ['B"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]], Chris@13: ['"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], Chris@13: ['b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], Chris@13: ['B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], Chris@17: ["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["<< String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff", 'docIndentation' => '']], Chris@17: ["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], Chris@17: ["<< String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']], Chris@17: ["<< String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => "\t"]], Chris@17: ["<<<'STR'\n Foo\n STR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']], Chris@13: ["die", ['kind' => Expr\Exit_::KIND_DIE]], Chris@13: ["die('done')", ['kind' => Expr\Exit_::KIND_DIE]], Chris@13: ["exit", ['kind' => Expr\Exit_::KIND_EXIT]], Chris@13: ["exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]], Chris@13: ["?>Foo", ['hasLeadingNewline' => false]], Chris@13: ["?>\nFoo", ['hasLeadingNewline' => true]], Chris@13: ["namespace Foo;", ['kind' => Stmt\Namespace_::KIND_SEMICOLON]], Chris@13: ["namespace Foo {}", ['kind' => Stmt\Namespace_::KIND_BRACED]], Chris@13: ["namespace {}", ['kind' => Stmt\Namespace_::KIND_BRACED]], Chris@17: ["(float) 5.0", ['kind' => Expr\Cast\Double::KIND_FLOAT]], Chris@17: ["(double) 5.0", ['kind' => Expr\Cast\Double::KIND_DOUBLE]], Chris@17: ["(real) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]], Chris@17: [" ( REAL ) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]], Chris@13: ]; Chris@0: } Chris@0: } Chris@0: Chris@13: class InvalidTokenLexer extends Lexer Chris@13: { Chris@13: public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) : int { Chris@0: $value = 'foobar'; Chris@0: return 999; Chris@0: } Chris@0: }