protected function parse($code, $prefix = '<?php ')
 {
     $code = $prefix . $code;
     try {
         return $this->getParser()->parse($code);
     } catch (\PHPParser_Error $e) {
         if (!$this->parseErrorIsEOF($e)) {
             throw ParseErrorException::fromParseError($e);
         }
         try {
             // Unexpected EOF, try again with an implicit semicolon
             return $this->getParser()->parse($code . ';');
         } catch (\PHPParser_Error $e) {
             return false;
         }
     }
 }
 public function testConstructFromParseError()
 {
     $e = ParseErrorException::fromParseError(new \PhpParser\Error('{msg}'));
     $this->assertContains('{msg}', $e->getRawMessage());
     $this->assertContains('PHP Parse error:', $e->getMessage());
 }
Ejemplo n.º 3
0
 /**
  * Lex and parse a block of code.
  *
  * @see Parser::parse
  *
  * @param string $code
  * @param bool   $requireSemicolons
  *
  * @return array A set of statements
  */
 protected function parse($code, $requireSemicolons = false)
 {
     try {
         return $this->parser->parse($code);
     } catch (\PhpParser\Error $e) {
         if ($this->parseErrorIsUnclosedString($e, $code)) {
             return false;
         }
         if (!$this->parseErrorIsEOF($e)) {
             throw ParseErrorException::fromParseError($e);
         }
         if ($requireSemicolons) {
             return false;
         }
         try {
             // Unexpected EOF, try again with an implicit semicolon
             return $this->parser->parse($code . ';');
         } catch (\PhpParser\Error $e) {
             return false;
         }
     }
 }