Example #1
0
 /**
  * @param string $syntax
  * @param array  $data
  * @throws NoMatcherFoundException
  * @return array
  */
 public function getMatcherForSyntax($syntax, array $data = array())
 {
     ArgumentChecker::check($syntax, 'string');
     $rawSyntax = $this->getRawSyntax($syntax);
     $endsWith = ' on error ?';
     if ($this->endsWith($rawSyntax, $endsWith)) {
         $rawSyntax = substr($rawSyntax, 0, strlen($rawSyntax) - strlen($endsWith));
     }
     foreach ($this->modules as $module) {
         $reflectionClass = new ReflectionClass($module);
         foreach ($reflectionClass->getMethods() as $method) {
             $doc = $method->getDocComment();
             foreach (explode("\n", $doc) as $line) {
                 $pos = strpos($line, '@syntax');
                 if ($pos !== false) {
                     $s = new Syntax(trim(substr($line, $pos + 7)), $method->getDeclaringClass()->getName() . '::' . $method->getName());
                     if ($s->getRawSyntax() == $rawSyntax) {
                         $class = $s->getClass();
                         $r = array('matcher' => new $class(), 'originalSyntax' => $s->getSyntax());
                         if ($this->endsWith($this->getRawSyntax($syntax), $endsWith)) {
                             $r['on_error'] = $data[count($data) - 1];
                         }
                         return $r;
                     }
                 }
             }
         }
     }
     throw new NoMatcherFoundException(array($syntax));
 }
Example #2
0
 protected function getSyntaxesFromMethod(ReflectionMethod $method)
 {
     $doc = $method->getDocComment();
     $m = $method->getName();
     $isNested = strpos($doc, '@nested') !== false;
     $syntaxes = array();
     $description = '';
     foreach (explode("\n", $doc) as $line) {
         if (preg_match('/@[a-zA-Z]+/', $line)) {
             continue;
         }
         $description .= ltrim($line, ' */') . "\n";
     }
     foreach (explode("\n", $doc) as $line) {
         $pos = strpos($line, '@syntax');
         // Ignore lines that are not a syntax.
         if ($pos === false) {
             continue;
         }
         $syntax = new Syntax(trim(substr($line, $pos + 7)), $method->getDeclaringClass()->getName() . '::' . $m, $isNested);
         $syntax->setDescription($description);
         $syntaxes[] = $syntax;
     }
     return $syntaxes;
 }
Example #3
0
 public function add(Syntax $syntax)
 {
     if (array_key_exists($syntax->getRawSyntax(), $this->syntaxes)) {
         throw new Exception("Syntax '" . $syntax->getSyntax() . "' already registered.");
     }
     foreach ($this->syntaxes as $s) {
         $s1 = $s->getRawSyntax();
         $s2 = $syntax->getRawSyntax();
         if ($this->isSubSyntax($s1, $s2) || $this->isSubSyntax($s2, $s1)) {
             throw new Exception("Syntax '{$s1}' conflicts with '{$s2}'.");
         }
     }
     $this->syntaxes[$syntax->getRawSyntax()] = $syntax;
 }