Ejemplo n.º 1
0
 function test3()
 {
     $input = "\n            =====\n            ";
     $ran = false;
     $regexes = new Regexes();
     $regexes->add('/=====[=]*/', function (array $capture) use(&$ran) {
         $ran = true;
     });
     $regexes->add("/\n\r|\r\n|\n|\r/", function (array $capture) {
     });
     $regexes->match($input)->all();
     $this->assertTrue($ran);
 }
Ejemplo n.º 2
0
 function __construct()
 {
     $regexes = new Regexes();
     $regexes->add('/<<<<<[<]*/', function (array $capture) {
         $this->log('start', $capture);
         switch ($this->state) {
             case State::No:
                 $this->state = State::Name;
                 break;
             case State::Name:
                 throw new \Exception('Is waiting for name. Encountered <<<<<<...');
                 break;
         }
     });
     $regexes->add('/=====[=]*/', function (array $capture) {
         $this->log('middle', $capture);
         switch ($this->state) {
             case State::Name:
                 throw new \Exception('Expected name but got =====');
                 break;
             case State::Middle:
                 $this->entry->str_start = substr($this->str, $this->str_start, $capture[1] - $this->str_start);
                 $this->str_start = $capture[1] + strlen($capture[0]);
                 $this->state = State::End;
         }
     });
     $regexes->add('/>>>>>[>]*/', function (array $capture) {
         $this->log('end', $capture);
         switch ($this->state) {
             case State::Name:
                 throw new \Exception('Is waiting for name Encountered <<<<<...');
                 break;
             case State::End:
                 $this->entry->str_end = substr($this->str, $this->str_start, $capture[1] - $this->str_start);
                 $this->state = State::EndName;
                 break;
         }
     });
     $regexes->add('/[a-zA-Z0-9_.]+/', function (array $capture) {
         $this->log('name', $capture);
         $name = $this->name;
         switch ($this->state) {
             case State::Name:
                 $this->name = $capture[0];
                 $this->entry = new Entry($capture[0]);
                 $this->state = State::NewRow;
                 $this->nextState = State::Middle;
                 break;
             case State::EndName:
                 if ($this->name != $capture[0]) {
                     throw new \Exception("Expected end name {${$name}}. Got {${$capture[0]}}");
                 }
                 $this->state = State::No;
                 $this->doc->addEntry($this->entry);
         }
     });
     $regexes->add("/\n\r|\r\n|\n|\r/", function (array $capture) {
         $this->log('new_row', $capture);
         switch ($this->state) {
             case State::Name:
                 throw new \Exception('Expecting a name before line break');
                 break;
             case State::EndName:
                 throw new \Exception('Expecting a end name before line break');
                 break;
             case State::NewRow:
                 $this->str_start = $capture[1] + strlen($capture[0]);
                 $this->state = $this->nextState;
         }
     });
     $this->parser = $regexes;
 }