z7zmey/php-parser

stop parsing after __halt_compiler();

imuli opened this issue · 2 comments

imuli commented

Anything after __halt_compiler(); is not parsed (or compiled) by PHP, and attempting to parse beyond it only invites syntax errors from trying to parse non-PHP.

Thus parsing something like

<?php
__halt_compiler();
"nothing to see here";

shouldn't produce

==> halt_compiler.php
  | [*node.Root]
  |   "Position": Pos{Line: 2-3 Pos: 7-47};
  |   "Stmts":
  |     [*stmt.HaltCompiler]
  |       "Position": Pos{Line: 2-2 Pos: 7-24};
  |     [*stmt.Expression]
  |       "Position": Pos{Line: 3-3 Pos: 26-47};
  |       "Expr":
  |         [*scalar.String]
  |           "Position": Pos{Line: 3-3 Pos: 26-46};
  |           "Value": "nothing to see here";

but rather, something more like

==> halt_compiler.php
  | [*node.Root]
  |   "Position": Pos{Line: 2-3 Pos: 7-24};
  |   "Stmts":
  |     [*stmt.HaltCompiler]
  |       "Position": Pos{Line: 2-2 Pos: 7-24};

or maybe including the stuff afterward either in a simple wrapper or within the HaltCompiler statement?

imuli commented

This was one of the motivating examples:

<?php

/*
 * This file is part of Mustache.php.
 *
 * (c) 2010-2017 Justin Hileman
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * @group unit
 */
class Mustache_Test_Loader_InlineLoaderTest extends PHPUnit_Framework_TestCase
{
    public function testLoadTemplates()
    {
        $loader = new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__);
        $this->assertEquals('{{ foo }}', $loader->load('foo'));
        $this->assertEquals('{{#bar}}BAR{{/bar}}', $loader->load('bar'));
    }

    /**
     * @expectedException Mustache_Exception_UnknownTemplateException
     */
    public function testMissingTemplatesThrowExceptions()
    {
        $loader = new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__);
        $loader->load('not_a_real_template');
    }

    /**
     * @expectedException Mustache_Exception_InvalidArgumentException
     */
    public function testInvalidOffsetThrowsException()
    {
        new Mustache_Loader_InlineLoader(__FILE__, 'notanumber');
    }

    /**
     * @expectedException Mustache_Exception_InvalidArgumentException
     */
    public function testInvalidFileThrowsException()
    {
        new Mustache_Loader_InlineLoader('notarealfile', __COMPILER_HALT_OFFSET__);
    }
}

__halt_compiler();

@@ foo
{{ foo }}

@@ bar
{{#bar}}BAR{{/bar}}

Now, __halt_compiler(); terminates parsing process.
Saving skipped part would be implemented in #62