Пример #1
0
 /**
  * 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 = "")
 {
     $this->offset = 0;
     $this->string = func_num_args() > 0 ? $string : $this->string;
     $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;
 }