/**
  * @param Input $input
  * @return Result
  */
 public function parse(Input $input)
 {
     $result = $this->parser->parse($input)->addParser($this);
     // Don't call the callback if there was an error.
     if ($result->errorMessage) {
         return $result;
     }
     $result->data = call_user_func($this->function, $result, $input);
     return $result;
 }
Exemple #2
0
 public function __set($name, $parser)
 {
     $parser = Parser::sanitize($parser);
     $this->parsers[$name] = $parser;
     if (!$parser->hasName()) {
         $parser->setName($name);
     }
     return $parser;
 }
 /**
  * @return File
  */
 public function parse($input)
 {
     $result = $this->rootParser->parse(new Input($input));
     if ($result->errorMessage) {
         throw new ParseException($result->errorMessage);
     } else {
         return $result->data;
     }
 }
 public function setOffset($offset)
 {
     $diff = abs($this->getOffset() - $offset);
     if ($offset < $this->getOffset()) {
         $this->getParserData()['rewound'] += $diff;
         $this->log(Parser::getInlineParserStack() . " - rewind({$diff})");
     } elseif ($offset > $this->getOffset()) {
         $this->getParserData()['consumed'] += $diff;
         $this->log(Parser::getInlineParserStack() . " - consume({$diff}, '{$this->get($diff)}')");
     }
     parent::setOffset($offset);
 }
 public function __construct($parsers = null)
 {
     if (!is_array($parsers)) {
         $parsers = func_get_args();
     }
     foreach ($parsers as $id => $parser) {
         $parser = Parser::sanitize($parser);
         $parsers[$id] = $parser;
         if (!$parser instanceof Parser) {
             throw new GrammarException('There is an object that is not a parser in this combinator.');
         }
     }
     $this->parsers = $parsers;
 }
Exemple #6
0
 public function __construct($parser, $separator = ',', $allow_trailing = true)
 {
     $parser = Parser::sanitize($parser);
     $separator = Parser::sanitize($separator);
     $sequence = new Sequence($parser, new Many(new Sequence($separator, $parser)));
     if ($allow_trailing) {
         $sequence->append(new OptionalChoice($separator));
     }
     $this->root = new Closure(new OptionalChoice($sequence), function ($data) {
         $result = [];
         if (is_array($data)) {
             $result[] = $data[0];
             foreach ($data[1] as $datum) {
                 $result[] = $datum[1];
             }
         }
         return $result;
     });
     $this->root->setName('repsep');
 }
 public function __construct($message)
 {
     parent::__construct($message . "\nParser Stack:\n" . Parser::getParserStack());
 }
Exemple #8
0
 public function __construct($parser)
 {
     $this->parser = Parser::sanitize($parser);
 }