@@ -12,18 +12,18 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Any extends Parser { |
| 14 | 14 | |
| 15 | - protected function parse(&$input, $offset, Context $context) |
|
| 16 | - { |
|
| 17 | - if ($offset < mb_strlen($input)) { |
|
| 18 | - return $this->success($input, $offset, 1); |
|
| 19 | - } |
|
| 15 | + protected function parse(&$input, $offset, Context $context) |
|
| 16 | + { |
|
| 17 | + if ($offset < mb_strlen($input)) { |
|
| 18 | + return $this->success($input, $offset, 1); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - return $this->failure($input, $offset); |
|
| 22 | - } |
|
| 21 | + return $this->failure($input, $offset); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function __toString() |
|
| 25 | - { |
|
| 26 | - return '.'; |
|
| 27 | - } |
|
| 24 | + public function __toString() |
|
| 25 | + { |
|
| 26 | + return '.'; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | } |
@@ -12,42 +12,42 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Text extends Parser { |
| 14 | 14 | |
| 15 | - use CaseSensitiveTrait; |
|
| 15 | + use CaseSensitiveTrait; |
|
| 16 | 16 | |
| 17 | - private $text = null; |
|
| 18 | - private $length = null; |
|
| 17 | + private $text = null; |
|
| 18 | + private $length = null; |
|
| 19 | 19 | |
| 20 | - public function __construct($text) |
|
| 21 | - { |
|
| 22 | - $this->text = $text; |
|
| 20 | + public function __construct($text) |
|
| 21 | + { |
|
| 22 | + $this->text = $text; |
|
| 23 | 23 | |
| 24 | - $this->length = mb_strlen($text); |
|
| 25 | - if ($this->length <= 0) { |
|
| 26 | - throw new \InvalidArgumentException('Empty argument'); |
|
| 27 | - } |
|
| 28 | - } |
|
| 24 | + $this->length = mb_strlen($text); |
|
| 25 | + if ($this->length <= 0) { |
|
| 26 | + throw new \InvalidArgumentException('Empty argument'); |
|
| 27 | + } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - protected function parse(&$input, $offset, Context $context) |
|
| 31 | - { |
|
| 32 | - $this->pushCaseSensitivityToContext($context); |
|
| 30 | + protected function parse(&$input, $offset, Context $context) |
|
| 31 | + { |
|
| 32 | + $this->pushCaseSensitivityToContext($context); |
|
| 33 | 33 | |
| 34 | - $text = $context->handleCase($this->text); |
|
| 35 | - for ($c = 0; $c < $this->length; $c++) { |
|
| 36 | - if ($offset + $c >= mb_strlen($input) || $text[$c] != $context->handleCase($input[$offset + $c])) { |
|
| 37 | - $this->popCaseSensitivityFromContext($context); |
|
| 34 | + $text = $context->handleCase($this->text); |
|
| 35 | + for ($c = 0; $c < $this->length; $c++) { |
|
| 36 | + if ($offset + $c >= mb_strlen($input) || $text[$c] != $context->handleCase($input[$offset + $c])) { |
|
| 37 | + $this->popCaseSensitivityFromContext($context); |
|
| 38 | 38 | |
| 39 | - return $this->failure($input, $offset, $c); |
|
| 40 | - } |
|
| 41 | - } |
|
| 39 | + return $this->failure($input, $offset, $c); |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - $this->popCaseSensitivityFromContext($context); |
|
| 43 | + $this->popCaseSensitivityFromContext($context); |
|
| 44 | 44 | |
| 45 | - return $this->success($input, $offset, $this->length); |
|
| 46 | - } |
|
| 45 | + return $this->success($input, $offset, $this->length); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function __toString() |
|
| 49 | - { |
|
| 50 | - return '"' . $this->text . '"'; |
|
| 51 | - } |
|
| 48 | + public function __toString() |
|
| 49 | + { |
|
| 50 | + return '"' . $this->text . '"'; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | } |
@@ -12,48 +12,48 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Range extends Parser { |
| 14 | 14 | |
| 15 | - use CaseSensitiveTrait; |
|
| 15 | + use CaseSensitiveTrait; |
|
| 16 | 16 | |
| 17 | - private $first = null; |
|
| 18 | - private $last = null; |
|
| 19 | - private $in = true; |
|
| 17 | + private $first = null; |
|
| 18 | + private $last = null; |
|
| 19 | + private $in = true; |
|
| 20 | 20 | |
| 21 | - public function __construct($first, $last, $in = true) |
|
| 22 | - { |
|
| 23 | - if ($first === null && $last === null) { |
|
| 24 | - throw new \InvalidArgumentException('Empty arguments'); |
|
| 25 | - } |
|
| 21 | + public function __construct($first, $last, $in = true) |
|
| 22 | + { |
|
| 23 | + if ($first === null && $last === null) { |
|
| 24 | + throw new \InvalidArgumentException('Empty arguments'); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - $this->first = $first === null ? null : self::parseCharacter($first); |
|
| 28 | - $this->last = $last === null ? null : self::parseCharacter($last); |
|
| 29 | - $this->in = (bool) $in; |
|
| 30 | - } |
|
| 27 | + $this->first = $first === null ? null : self::parseCharacter($first); |
|
| 28 | + $this->last = $last === null ? null : self::parseCharacter($last); |
|
| 29 | + $this->in = (bool) $in; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - protected function parse(&$input, $offset, Context $context) |
|
| 33 | - { |
|
| 34 | - if ($offset >= mb_strlen($input)) { |
|
| 35 | - return $this->failure($input, $offset); |
|
| 36 | - } |
|
| 32 | + protected function parse(&$input, $offset, Context $context) |
|
| 33 | + { |
|
| 34 | + if ($offset >= mb_strlen($input)) { |
|
| 35 | + return $this->failure($input, $offset); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - $this->pushCaseSensitivityToContext($context); |
|
| 38 | + $this->pushCaseSensitivityToContext($context); |
|
| 39 | 39 | |
| 40 | - $first = ord($context->handleCase($this->first)); |
|
| 41 | - $last = ord($context->handleCase($this->last)); |
|
| 42 | - $ord = ord($context->handleCase($input[$offset])); |
|
| 43 | - if ($first <= $ord && ($this->last === null || $ord <= $last)) { |
|
| 44 | - $this->popCaseSensitivityFromContext($context); |
|
| 40 | + $first = ord($context->handleCase($this->first)); |
|
| 41 | + $last = ord($context->handleCase($this->last)); |
|
| 42 | + $ord = ord($context->handleCase($input[$offset])); |
|
| 43 | + if ($first <= $ord && ($this->last === null || $ord <= $last)) { |
|
| 44 | + $this->popCaseSensitivityFromContext($context); |
|
| 45 | 45 | |
| 46 | - return $this->in ? $this->success($input, $offset, 1) : $this->failure($input, $offset); |
|
| 47 | - } |
|
| 46 | + return $this->in ? $this->success($input, $offset, 1) : $this->failure($input, $offset); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - $this->popCaseSensitivityFromContext($context); |
|
| 49 | + $this->popCaseSensitivityFromContext($context); |
|
| 50 | 50 | |
| 51 | - return $this->in ? $this->failure($input, $offset) : $this->success($input, $offset, 1); |
|
| 52 | - } |
|
| 51 | + return $this->in ? $this->failure($input, $offset) : $this->success($input, $offset, 1); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function __toString() |
|
| 55 | - { |
|
| 56 | - return sprintf('x%02x-x%02x', ord($this->first), ord($this->last)); |
|
| 57 | - } |
|
| 54 | + public function __toString() |
|
| 55 | + { |
|
| 56 | + return sprintf('x%02x-x%02x', ord($this->first), ord($this->last)); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | 59 | } |
@@ -12,14 +12,14 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class End extends Parser { |
| 14 | 14 | |
| 15 | - protected function parse(&$input, $offset, Context $context) |
|
| 16 | - { |
|
| 17 | - return $offset == mb_strlen($input) ? $this->success($input, $offset) : $this->failure($input, $offset); |
|
| 18 | - } |
|
| 15 | + protected function parse(&$input, $offset, Context $context) |
|
| 16 | + { |
|
| 17 | + return $offset == mb_strlen($input) ? $this->success($input, $offset) : $this->failure($input, $offset); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - public function __toString() |
|
| 21 | - { |
|
| 22 | - return 'end'; |
|
| 23 | - } |
|
| 20 | + public function __toString() |
|
| 21 | + { |
|
| 22 | + return 'end'; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | 25 | } |
@@ -11,42 +11,42 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | trait CaseSensitiveTrait { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var boolean |
|
| 16 | - */ |
|
| 17 | - private $caseSensitivity = null; |
|
| 18 | - |
|
| 19 | - private function pushCaseSensitivityToContext(Context $context) |
|
| 20 | - { |
|
| 21 | - if ($this->caseSensitivity !== null) { |
|
| 22 | - $context->pushCaseSensitivity($this->caseSensitivity); |
|
| 23 | - } |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - private function popCaseSensitivityFromContext(Context $context) |
|
| 27 | - { |
|
| 28 | - if ($this->caseSensitivity !== null) { |
|
| 29 | - $context->popCaseSensitivity(); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - public function setCaseSensitivity(string $preference) |
|
| 34 | - { |
|
| 35 | - $this->caseSensitivity = $preference; |
|
| 14 | + /** |
|
| 15 | + * @var boolean |
|
| 16 | + */ |
|
| 17 | + private $caseSensitivity = null; |
|
| 18 | + |
|
| 19 | + private function pushCaseSensitivityToContext(Context $context) |
|
| 20 | + { |
|
| 21 | + if ($this->caseSensitivity !== null) { |
|
| 22 | + $context->pushCaseSensitivity($this->caseSensitivity); |
|
| 23 | + } |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + private function popCaseSensitivityFromContext(Context $context) |
|
| 27 | + { |
|
| 28 | + if ($this->caseSensitivity !== null) { |
|
| 29 | + $context->popCaseSensitivity(); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + public function setCaseSensitivity(string $preference) |
|
| 34 | + { |
|
| 35 | + $this->caseSensitivity = $preference; |
|
| 36 | 36 | |
| 37 | - return $this; |
|
| 38 | - } |
|
| 37 | + return $this; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public function caseSensitive() { |
|
| 41 | - $this->caseSensitivity = true; |
|
| 40 | + public function caseSensitive() { |
|
| 41 | + $this->caseSensitivity = true; |
|
| 42 | 42 | |
| 43 | - return $this; |
|
| 44 | - } |
|
| 43 | + return $this; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function caseInsensitive() { |
|
| 47 | - $this->caseSensitivity = false; |
|
| 46 | + public function caseInsensitive() { |
|
| 47 | + $this->caseSensitivity = false; |
|
| 48 | 48 | |
| 49 | - return $this; |
|
| 50 | - } |
|
| 49 | + return $this; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | } |
@@ -12,44 +12,44 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Regex extends Parser { |
| 14 | 14 | |
| 15 | - use CaseSensitiveTrait; |
|
| 15 | + use CaseSensitiveTrait; |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * @var string|null |
| 19 | 19 | */ |
| 20 | - private $pattern = null; |
|
| 20 | + private $pattern = null; |
|
| 21 | 21 | |
| 22 | - public function __construct($pattern) |
|
| 23 | - { |
|
| 24 | - if (empty($pattern)) { |
|
| 25 | - throw new \InvalidArgumentException('Empty pattern'); |
|
| 26 | - } |
|
| 22 | + public function __construct($pattern) |
|
| 23 | + { |
|
| 24 | + if (empty($pattern)) { |
|
| 25 | + throw new \InvalidArgumentException('Empty pattern'); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - if (@preg_match($pattern, null) === false) { |
|
| 29 | - throw new \InvalidArgumentException('Invalid pattern'); |
|
| 30 | - } |
|
| 28 | + if (@preg_match($pattern, null) === false) { |
|
| 29 | + throw new \InvalidArgumentException('Invalid pattern'); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - $this->pattern = $pattern; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - protected function parse(&$input, $offset, Context $context) |
|
| 36 | - { |
|
| 37 | - $this->pushCaseSensitivityToContext($context); |
|
| 38 | - $pattern = $this->pattern . ($context->isCaseSensitive() ? '' : 'i'); |
|
| 39 | - $this->popCaseSensitivityFromContext($context); |
|
| 40 | - |
|
| 41 | - if (preg_match($pattern, $input, $match, 0, $offset) !== FALSE) { |
|
| 42 | - if (count($match) > 0 && mb_strlen($match[0]) > 0 && strpos($input, $match[0], $offset) == $offset) { |
|
| 43 | - return $this->success($input, $offset, mb_strlen($match[0])); |
|
| 44 | - } |
|
| 45 | - } |
|
| 32 | + $this->pattern = $pattern; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + protected function parse(&$input, $offset, Context $context) |
|
| 36 | + { |
|
| 37 | + $this->pushCaseSensitivityToContext($context); |
|
| 38 | + $pattern = $this->pattern . ($context->isCaseSensitive() ? '' : 'i'); |
|
| 39 | + $this->popCaseSensitivityFromContext($context); |
|
| 40 | + |
|
| 41 | + if (preg_match($pattern, $input, $match, 0, $offset) !== FALSE) { |
|
| 42 | + if (count($match) > 0 && mb_strlen($match[0]) > 0 && strpos($input, $match[0], $offset) == $offset) { |
|
| 43 | + return $this->success($input, $offset, mb_strlen($match[0])); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - return $this->failure($input, $offset); |
|
| 48 | - } |
|
| 47 | + return $this->failure($input, $offset); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function __toString() |
|
| 51 | - { |
|
| 52 | - return (string) $this->pattern; |
|
| 53 | - } |
|
| 50 | + public function __toString() |
|
| 51 | + { |
|
| 52 | + return (string) $this->pattern; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | 55 | } |
@@ -14,14 +14,14 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Nothing extends Parser { |
| 16 | 16 | |
| 17 | - protected function parse(&$input, $offset, Context $context) |
|
| 18 | - { |
|
| 19 | - return $this->makeMatch($offset <= mb_strlen($input), $input, 0); |
|
| 20 | - } |
|
| 17 | + protected function parse(&$input, $offset, Context $context) |
|
| 18 | + { |
|
| 19 | + return $this->makeMatch($offset <= mb_strlen($input), $input, 0); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function __toString() |
|
| 23 | - { |
|
| 24 | - return '0.'; |
|
| 25 | - } |
|
| 22 | + public function __toString() |
|
| 23 | + { |
|
| 24 | + return '0.'; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -38,7 +38,7 @@ |
||
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | $this->set = count_chars($set, 3); |
| 41 | - $this->include = (bool)$include; |
|
| 41 | + $this->include = (bool) $include; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | protected function parse(&$input, $offset, Context $context) |
@@ -8,38 +8,38 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | trait AssignTrait { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * List of result names to assign the matched text to. |
|
| 13 | - * @var callable[] |
|
| 14 | - */ |
|
| 15 | - private $assignCallbacks = []; |
|
| 11 | + /** |
|
| 12 | + * List of result names to assign the matched text to. |
|
| 13 | + * @var callable[] |
|
| 14 | + */ |
|
| 15 | + private $assignCallbacks = []; |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Resolve all callbacks registered to this trait |
| 19 | 19 | * |
| 20 | 20 | * @param $text |
| 21 | 21 | */ |
| 22 | - private function resolveAssignCallbacks($text) |
|
| 23 | - { |
|
| 24 | - foreach ($this->assignCallbacks as $callback) { |
|
| 25 | - $callback($text); |
|
| 26 | - } |
|
| 27 | - } |
|
| 22 | + private function resolveAssignCallbacks($text) |
|
| 23 | + { |
|
| 24 | + foreach ($this->assignCallbacks as $callback) { |
|
| 25 | + $callback($text); |
|
| 26 | + } |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * After parsing, assign the matched input to the specified local variable. |
|
| 31 | - * Only assign if successfully matched entire parent up to root. |
|
| 32 | - * |
|
| 33 | - * @param mixed $variable |
|
| 34 | - * @return $this |
|
| 35 | - */ |
|
| 36 | - public function assignTo(&$variable) |
|
| 37 | - { |
|
| 38 | - $this->assignCallbacks[] = function($text) use (&$variable) { |
|
| 39 | - $variable = $text; |
|
| 40 | - }; |
|
| 29 | + /** |
|
| 30 | + * After parsing, assign the matched input to the specified local variable. |
|
| 31 | + * Only assign if successfully matched entire parent up to root. |
|
| 32 | + * |
|
| 33 | + * @param mixed $variable |
|
| 34 | + * @return $this |
|
| 35 | + */ |
|
| 36 | + public function assignTo(&$variable) |
|
| 37 | + { |
|
| 38 | + $this->assignCallbacks[] = function($text) use (&$variable) { |
|
| 39 | + $variable = $text; |
|
| 40 | + }; |
|
| 41 | 41 | |
| 42 | - return $this; |
|
| 43 | - } |
|
| 42 | + return $this; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | } |