@@ -13,37 +13,37 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class All extends Parser { |
| 15 | 15 | |
| 16 | - use ArgumentsTrait; |
|
| 16 | + use ArgumentsTrait; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * @var Parser[] |
| 20 | 20 | */ |
| 21 | - private $parsers = []; |
|
| 22 | - |
|
| 23 | - public function __construct(...$arguments) |
|
| 24 | - { |
|
| 25 | - if (count($arguments) < 2) { |
|
| 26 | - throw new \InvalidArgumentException('Less than 2 arguments provided'); |
|
| 27 | - } |
|
| 28 | - $this->parsers = self::getArguments($arguments); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - protected function parse(&$input, $offset, Context $context) |
|
| 32 | - { |
|
| 33 | - $length = PHP_INT_MAX; |
|
| 34 | - foreach ($this->parsers as $parser) { |
|
| 35 | - $match = $parser->parse($input, $offset, $context); |
|
| 36 | - $length = min($length, $match->length); |
|
| 37 | - if (!$match->match) { |
|
| 38 | - return $this->failure($input, $offset, $length); |
|
| 39 | - } |
|
| 40 | - } |
|
| 41 | - return $this->success($input, $offset, $length); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function __toString() |
|
| 45 | - { |
|
| 46 | - return '( ' . join(' + ', $this->parsers) . ' )'; |
|
| 47 | - } |
|
| 21 | + private $parsers = []; |
|
| 22 | + |
|
| 23 | + public function __construct(...$arguments) |
|
| 24 | + { |
|
| 25 | + if (count($arguments) < 2) { |
|
| 26 | + throw new \InvalidArgumentException('Less than 2 arguments provided'); |
|
| 27 | + } |
|
| 28 | + $this->parsers = self::getArguments($arguments); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + protected function parse(&$input, $offset, Context $context) |
|
| 32 | + { |
|
| 33 | + $length = PHP_INT_MAX; |
|
| 34 | + foreach ($this->parsers as $parser) { |
|
| 35 | + $match = $parser->parse($input, $offset, $context); |
|
| 36 | + $length = min($length, $match->length); |
|
| 37 | + if (!$match->match) { |
|
| 38 | + return $this->failure($input, $offset, $length); |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | + return $this->success($input, $offset, $length); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function __toString() |
|
| 45 | + { |
|
| 46 | + return '( ' . join(' + ', $this->parsers) . ' )'; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | } |
@@ -61,7 +61,7 @@ |
||
| 61 | 61 | if ($skip !== false) { |
| 62 | 62 | $match = $this->parser->parse($input, $offset + $length + $skip, $context); |
| 63 | 63 | if ($match->match) { |
| 64 | - $length += $skip + $match->length; |
|
| 64 | + $length += $skip + $match->length; |
|
| 65 | 65 | $child_matches[] = $match; |
| 66 | 66 | } |
| 67 | 67 | } |
@@ -47,7 +47,7 @@ |
||
| 47 | 47 | { |
| 48 | 48 | if ($spacer === true) { |
| 49 | 49 | $this->spacer = true; |
| 50 | - } elseif ($spacer === null || $spacer === false ) { |
|
| 50 | + } elseif ($spacer === null || $spacer === false) { |
|
| 51 | 51 | $this->spacer = null; |
| 52 | 52 | } else { |
| 53 | 53 | $this->spacer = self::getArgument($spacer); |
@@ -14,37 +14,37 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Except extends Parser { |
| 16 | 16 | |
| 17 | - use ArgumentsTrait; |
|
| 18 | - |
|
| 19 | - private $parser_match = null; |
|
| 20 | - private $parser_not = null; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * |
|
| 24 | - * @param Parser|string $match |
|
| 25 | - * @param Parser|string $not |
|
| 26 | - */ |
|
| 27 | - public function __construct($match, $not) |
|
| 28 | - { |
|
| 29 | - $this->parser_match = self::getArgument($match); |
|
| 30 | - $this->parser_not = self::getArgument($not); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - protected function parse(&$input, $offset, Context $context) |
|
| 34 | - { |
|
| 35 | - $match = $this->parser_match->parse($input, $offset, $context); |
|
| 36 | - $not = $this->parser_not->parse($input, $offset, $context); |
|
| 37 | - |
|
| 38 | - if ($match->match && !$not->match) { |
|
| 39 | - return $this->success($input, $offset, $match->length, $match); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - return $this->failure($input, $offset, min($match->length, $not->length)); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function __toString() |
|
| 46 | - { |
|
| 47 | - return '( ' . $this->parser_match . ' - ' . $this->parser_not . ' )'; |
|
| 48 | - } |
|
| 17 | + use ArgumentsTrait; |
|
| 18 | + |
|
| 19 | + private $parser_match = null; |
|
| 20 | + private $parser_not = null; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * |
|
| 24 | + * @param Parser|string $match |
|
| 25 | + * @param Parser|string $not |
|
| 26 | + */ |
|
| 27 | + public function __construct($match, $not) |
|
| 28 | + { |
|
| 29 | + $this->parser_match = self::getArgument($match); |
|
| 30 | + $this->parser_not = self::getArgument($not); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + protected function parse(&$input, $offset, Context $context) |
|
| 34 | + { |
|
| 35 | + $match = $this->parser_match->parse($input, $offset, $context); |
|
| 36 | + $not = $this->parser_not->parse($input, $offset, $context); |
|
| 37 | + |
|
| 38 | + if ($match->match && !$not->match) { |
|
| 39 | + return $this->success($input, $offset, $match->length, $match); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + return $this->failure($input, $offset, min($match->length, $not->length)); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function __toString() |
|
| 46 | + { |
|
| 47 | + return '( ' . $this->parser_match . ' - ' . $this->parser_not . ' )'; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | } |
@@ -13,52 +13,52 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | trait PreferTrait { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Parser used for scanning the text |
|
| 18 | - * @var Parser |
|
| 19 | - */ |
|
| 20 | - private $preference = null; |
|
| 16 | + /** |
|
| 17 | + * Parser used for scanning the text |
|
| 18 | + * @var Parser |
|
| 19 | + */ |
|
| 20 | + private $preference = null; |
|
| 21 | 21 | |
| 22 | - private function pushPreferenceToContext(Context $context) |
|
| 23 | - { |
|
| 24 | - if ($this->preference) { |
|
| 25 | - $context->pushPreference($this->preference); |
|
| 26 | - } |
|
| 27 | - } |
|
| 22 | + private function pushPreferenceToContext(Context $context) |
|
| 23 | + { |
|
| 24 | + if ($this->preference) { |
|
| 25 | + $context->pushPreference($this->preference); |
|
| 26 | + } |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - private function popPreferenceFromContext(Context $context) |
|
| 30 | - { |
|
| 31 | - if ($this->preference) { |
|
| 32 | - $context->popPreference(); |
|
| 33 | - } |
|
| 34 | - } |
|
| 29 | + private function popPreferenceFromContext(Context $context) |
|
| 30 | + { |
|
| 31 | + if ($this->preference) { |
|
| 32 | + $context->popPreference(); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public function setPreference(string $preference) |
|
| 37 | - { |
|
| 38 | - $this->preference = $preference; |
|
| 36 | + public function setPreference(string $preference) |
|
| 37 | + { |
|
| 38 | + $this->preference = $preference; |
|
| 39 | 39 | |
| 40 | - return $this; |
|
| 41 | - } |
|
| 40 | + return $this; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function preferShortest() |
|
| 44 | - { |
|
| 45 | - $this->preference = Prefer::SHORTEST; |
|
| 43 | + public function preferShortest() |
|
| 44 | + { |
|
| 45 | + $this->preference = Prefer::SHORTEST; |
|
| 46 | 46 | |
| 47 | - return $this; |
|
| 48 | - } |
|
| 47 | + return $this; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function preferLongest() |
|
| 51 | - { |
|
| 52 | - $this->preference = Prefer::LONGEST; |
|
| 50 | + public function preferLongest() |
|
| 51 | + { |
|
| 52 | + $this->preference = Prefer::LONGEST; |
|
| 53 | 53 | |
| 54 | - return $this; |
|
| 55 | - } |
|
| 54 | + return $this; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function preferFirst() |
|
| 58 | - { |
|
| 59 | - $this->preference = Prefer::FIRST; |
|
| 57 | + public function preferFirst() |
|
| 58 | + { |
|
| 59 | + $this->preference = Prefer::FIRST; |
|
| 60 | 60 | |
| 61 | - return $this; |
|
| 62 | - } |
|
| 61 | + return $this; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | } |
@@ -13,24 +13,24 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Not extends Parser { |
| 15 | 15 | |
| 16 | - use ArgumentsTrait; |
|
| 16 | + use ArgumentsTrait; |
|
| 17 | 17 | |
| 18 | - private $parser = null; |
|
| 18 | + private $parser = null; |
|
| 19 | 19 | |
| 20 | - public function __construct($parser) |
|
| 21 | - { |
|
| 22 | - $this->parser = self::getArgument($parser); |
|
| 23 | - } |
|
| 20 | + public function __construct($parser) |
|
| 21 | + { |
|
| 22 | + $this->parser = self::getArgument($parser); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - protected function parse(&$input, $offset, Context $context) |
|
| 26 | - { |
|
| 27 | - $match = $this->parser->parse($input, $offset, $context); |
|
| 28 | - return $match->match ? $this->failure($input, $offset, $match->length) : $this->success($input, $offset, 0); |
|
| 29 | - } |
|
| 25 | + protected function parse(&$input, $offset, Context $context) |
|
| 26 | + { |
|
| 27 | + $match = $this->parser->parse($input, $offset, $context); |
|
| 28 | + return $match->match ? $this->failure($input, $offset, $match->length) : $this->success($input, $offset, 0); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - public function __toString() |
|
| 32 | - { |
|
| 33 | - return '!' . $this->parser; |
|
| 34 | - } |
|
| 31 | + public function __toString() |
|
| 32 | + { |
|
| 33 | + return '!' . $this->parser; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | |
| 95 | 95 | if ($match->match && !empty($this->definition->processors)) { |
| 96 | 96 | foreach ($this->definition->processors as $key => $processor) { |
| 97 | - $match->addResultCallback(function (&$results) use ($key, $processor, $localResults) { |
|
| 97 | + $match->addResultCallback(function(&$results) use ($key, $processor, $localResults) { |
|
| 98 | 98 | $results[$key] = $processor($localResults, $results); |
| 99 | 99 | }); |
| 100 | 100 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | // ignore |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - return (string)$this->parser; |
|
| 115 | + return (string) $this->parser; |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | } |
@@ -11,35 +11,35 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class Definition { |
| 13 | 13 | |
| 14 | - public $generator = null; |
|
| 15 | - public $validators = []; |
|
| 16 | - public $processors = []; |
|
| 14 | + public $generator = null; |
|
| 15 | + public $validators = []; |
|
| 16 | + public $processors = []; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @param Parser|callable $generator Either a parser or a function returning a parser ('generator') |
|
| 20 | - * @param callable[] $validator |
|
| 21 | - */ |
|
| 22 | - public function __construct($generator = null, $validator = null) |
|
| 23 | - { |
|
| 24 | - //@todo validate parser and validator |
|
| 18 | + /** |
|
| 19 | + * @param Parser|callable $generator Either a parser or a function returning a parser ('generator') |
|
| 20 | + * @param callable[] $validator |
|
| 21 | + */ |
|
| 22 | + public function __construct($generator = null, $validator = null) |
|
| 23 | + { |
|
| 24 | + //@todo validate parser and validator |
|
| 25 | 25 | |
| 26 | - $this->generator = $generator; |
|
| 27 | - if (is_callable($validator)) { |
|
| 26 | + $this->generator = $generator; |
|
| 27 | + if (is_callable($validator)) { |
|
| 28 | 28 | $this->validators[] = $validator; |
| 29 | 29 | } |
| 30 | - } |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - public function setGenerator($parser) |
|
| 33 | - { |
|
| 34 | - $this->generator = $parser; |
|
| 32 | + public function setGenerator($parser) |
|
| 33 | + { |
|
| 34 | + $this->generator = $parser; |
|
| 35 | 35 | |
| 36 | - return $this; |
|
| 37 | - } |
|
| 36 | + return $this; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function clearValidators() { |
|
| 40 | - $this->validators = []; |
|
| 39 | + public function clearValidators() { |
|
| 40 | + $this->validators = []; |
|
| 41 | 41 | |
| 42 | - return $this; |
|
| 42 | + return $this; |
|
| 43 | 43 | } |
| 44 | 44 | public function addValidator($validator) |
| 45 | 45 | { |
@@ -55,27 +55,27 @@ discard block |
||
| 55 | 55 | return $this; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Build an instance of this parser definition. |
|
| 60 | - * |
|
| 61 | - * @param Mixed[] $arguments |
|
| 62 | - * @return Implementation |
|
| 63 | - */ |
|
| 64 | - public function build(...$arguments) |
|
| 65 | - { |
|
| 66 | - return new Implementation($this, $arguments); |
|
| 67 | - } |
|
| 58 | + /** |
|
| 59 | + * Build an instance of this parser definition. |
|
| 60 | + * |
|
| 61 | + * @param Mixed[] $arguments |
|
| 62 | + * @return Implementation |
|
| 63 | + */ |
|
| 64 | + public function build(...$arguments) |
|
| 65 | + { |
|
| 66 | + return new Implementation($this, $arguments); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Build an instance of this parser definition. |
|
| 71 | - * Alias of `build()` method. |
|
| 72 | - * |
|
| 73 | - * @param Mixed[] $arguments |
|
| 74 | - * @return Implementation |
|
| 75 | - */ |
|
| 76 | - public function __invoke(...$arguments) |
|
| 77 | - { |
|
| 78 | - return $this->build(...$arguments); |
|
| 79 | - } |
|
| 69 | + /** |
|
| 70 | + * Build an instance of this parser definition. |
|
| 71 | + * Alias of `build()` method. |
|
| 72 | + * |
|
| 73 | + * @param Mixed[] $arguments |
|
| 74 | + * @return Implementation |
|
| 75 | + */ |
|
| 76 | + public function __invoke(...$arguments) |
|
| 77 | + { |
|
| 78 | + return $this->build(...$arguments); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | } |
@@ -23,7 +23,7 @@ |
||
| 23 | 23 | { |
| 24 | 24 | //@todo validate parser and validator |
| 25 | 25 | |
| 26 | - $this->generator = $generator; |
|
| 26 | + $this->generator = $generator; |
|
| 27 | 27 | if (is_callable($validator)) { |
| 28 | 28 | $this->validators[] = $validator; |
| 29 | 29 | } |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | protected static function isRuleDefined(&$rules, $names) |
| 139 | 139 | { |
| 140 | - foreach ((array)$names as $key) { |
|
| 140 | + foreach ((array) $names as $key) { |
|
| 141 | 141 | if (!isset($rules[$key])) { |
| 142 | 142 | return false; |
| 143 | 143 | } |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | |
| 149 | 149 | protected static function undefineRule(&$rules, $names) |
| 150 | 150 | { |
| 151 | - foreach ((array)$names as $key) { |
|
| 151 | + foreach ((array) $names as $key) { |
|
| 152 | 152 | unset($rules[$key]); |
| 153 | 153 | } |
| 154 | 154 | } |
@@ -280,6 +280,6 @@ discard block |
||
| 280 | 280 | // ignore |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - return (string)$this->parser; |
|
| 283 | + return (string) $this->parser; |
|
| 284 | 284 | } |
| 285 | 285 | } |