/** * Parses a string. * * This function parses a string and throws an exception if unsuccessful. * * For example: * ```php * // parses a string * $p = new MyCustomParser($string); * try { * $info = $p->parse(); * } catch(TextParserException $e) { * echo $e->getPrintableMessage(); * } * * if (!$info) { * echo "This is not a valid expressión"; * } else { * print_r($info); * } * ``` * * @param string $string String target (default is "") * * @throws Exception * @return mixed */ public function parse($string = "") { if (func_num_args() > 0) { $this->string = $string; $this->offset = 0; } $ungreedy = TextParser::UNGREEDY & $this->_flags; $ret = $this->evaluate(); if ($ret) { if ($this->_target instanceof TextParser) { $this->_target->setOffset($this->offset); } elseif (!$ungreedy && !$this->end()) { throw new TextParserException("Unrecognized expression", $this); } } return $ret; }