@@ -13,41 +13,41 @@ |
||
13 | 13 | */ |
14 | 14 | class CaseSensitive extends Parser { |
15 | 15 | |
16 | - use ArgumentsTrait; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var Parser |
|
20 | - */ |
|
21 | - private $parser = null; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var bool |
|
25 | - */ |
|
26 | - private $sensitivity = null; |
|
27 | - |
|
28 | - /** |
|
29 | - * |
|
30 | - * @param Parser|string|integer $parser |
|
31 | - * @param bool $sensitivity |
|
32 | - */ |
|
33 | - public function __construct($sensitivity, $parser) |
|
34 | - { |
|
35 | - $this->parser = self::getArgument($parser); |
|
36 | - $this->sensitivity = (bool) $sensitivity; |
|
37 | - } |
|
38 | - |
|
39 | - protected function parse(&$input, $offset, Context $context) |
|
40 | - { |
|
41 | - $context->pushCaseSensitivity($this->sensitivity); |
|
42 | - $match = $this->parser->parse($input, $offset, $context); |
|
43 | - $context->popCaseSensitivity(); |
|
44 | - |
|
45 | - return $match; |
|
46 | - } |
|
47 | - |
|
48 | - public function __toString() |
|
49 | - { |
|
50 | - return ($this->sensitivity ? 'case' : 'no-case') . '( ' . $this->parser . ' )'; |
|
51 | - } |
|
16 | + use ArgumentsTrait; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var Parser |
|
20 | + */ |
|
21 | + private $parser = null; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var bool |
|
25 | + */ |
|
26 | + private $sensitivity = null; |
|
27 | + |
|
28 | + /** |
|
29 | + * |
|
30 | + * @param Parser|string|integer $parser |
|
31 | + * @param bool $sensitivity |
|
32 | + */ |
|
33 | + public function __construct($sensitivity, $parser) |
|
34 | + { |
|
35 | + $this->parser = self::getArgument($parser); |
|
36 | + $this->sensitivity = (bool) $sensitivity; |
|
37 | + } |
|
38 | + |
|
39 | + protected function parse(&$input, $offset, Context $context) |
|
40 | + { |
|
41 | + $context->pushCaseSensitivity($this->sensitivity); |
|
42 | + $match = $this->parser->parse($input, $offset, $context); |
|
43 | + $context->popCaseSensitivity(); |
|
44 | + |
|
45 | + return $match; |
|
46 | + } |
|
47 | + |
|
48 | + public function __toString() |
|
49 | + { |
|
50 | + return ($this->sensitivity ? 'case' : 'no-case') . '( ' . $this->parser . ' )'; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | } |
@@ -13,42 +13,42 @@ |
||
13 | 13 | */ |
14 | 14 | class Space extends Parser { |
15 | 15 | |
16 | - use ArgumentsTrait; |
|
16 | + use ArgumentsTrait; |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * @var null|Parser |
20 | 20 | */ |
21 | - private $spacer = null; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var null|Parser |
|
25 | - */ |
|
26 | - private $parser = null; |
|
27 | - |
|
28 | - /** |
|
29 | - * Set (or disable) a spacer for the parser |
|
30 | - * |
|
31 | - * @param Parser|string|int|bool|null $spacer |
|
32 | - * @param Parser|string|int $parser |
|
33 | - */ |
|
34 | - public function __construct($spacer, $parser) |
|
35 | - { |
|
36 | - $this->spacer = $spacer === null ? null : self::getArgument($spacer); |
|
37 | - $this->parser = self::getArgument($parser); |
|
38 | - } |
|
39 | - |
|
40 | - protected function parse(&$input, $offset, Context $context) |
|
41 | - { |
|
42 | - $context->pushSpacer($this->spacer); |
|
43 | - $match = $this->parser->parse($input, $offset, $context); |
|
44 | - $context->popSpacer(); |
|
45 | - |
|
46 | - return $match; |
|
47 | - } |
|
21 | + private $spacer = null; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var null|Parser |
|
25 | + */ |
|
26 | + private $parser = null; |
|
27 | + |
|
28 | + /** |
|
29 | + * Set (or disable) a spacer for the parser |
|
30 | + * |
|
31 | + * @param Parser|string|int|bool|null $spacer |
|
32 | + * @param Parser|string|int $parser |
|
33 | + */ |
|
34 | + public function __construct($spacer, $parser) |
|
35 | + { |
|
36 | + $this->spacer = $spacer === null ? null : self::getArgument($spacer); |
|
37 | + $this->parser = self::getArgument($parser); |
|
38 | + } |
|
39 | + |
|
40 | + protected function parse(&$input, $offset, Context $context) |
|
41 | + { |
|
42 | + $context->pushSpacer($this->spacer); |
|
43 | + $match = $this->parser->parse($input, $offset, $context); |
|
44 | + $context->popSpacer(); |
|
45 | + |
|
46 | + return $match; |
|
47 | + } |
|
48 | 48 | |
49 | - public function __toString() |
|
50 | - { |
|
51 | - return (string) $this->parser; |
|
52 | - } |
|
49 | + public function __toString() |
|
50 | + { |
|
51 | + return (string) $this->parser; |
|
52 | + } |
|
53 | 53 | |
54 | 54 | } |
@@ -9,14 +9,14 @@ |
||
9 | 9 | */ |
10 | 10 | class Failure extends Match { |
11 | 11 | |
12 | - public function __get($name) |
|
13 | - { |
|
14 | - return $name === 'match' ? false : parent::__get($name); |
|
15 | - } |
|
12 | + public function __get($name) |
|
13 | + { |
|
14 | + return $name === 'match' ? false : parent::__get($name); |
|
15 | + } |
|
16 | 16 | |
17 | - public function __toString() |
|
18 | - { |
|
19 | - return 'Failed match at ' . $this->length . ' characters'; |
|
20 | - } |
|
17 | + public function __toString() |
|
18 | + { |
|
19 | + return 'Failed match at ' . $this->length . ' characters'; |
|
20 | + } |
|
21 | 21 | |
22 | 22 | } |
@@ -14,48 +14,48 @@ |
||
14 | 14 | */ |
15 | 15 | class Stub extends Parser { |
16 | 16 | |
17 | - use ArgumentsTrait; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var Parser|null |
|
21 | - */ |
|
22 | - private $parser = null; |
|
23 | - |
|
24 | - public function __set($name, $parser) |
|
25 | - { |
|
26 | - if ($name == 'parser') { |
|
27 | - return $this->parser = self::getArgument($parser); |
|
28 | - } |
|
17 | + use ArgumentsTrait; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var Parser|null |
|
21 | + */ |
|
22 | + private $parser = null; |
|
23 | + |
|
24 | + public function __set($name, $parser) |
|
25 | + { |
|
26 | + if ($name == 'parser') { |
|
27 | + return $this->parser = self::getArgument($parser); |
|
28 | + } |
|
29 | 29 | |
30 | - throw new \InvalidArgumentException("Property `{$name}` does not exist"); |
|
31 | - } |
|
32 | - |
|
33 | - public function __get($name) |
|
34 | - { |
|
35 | - if ($name == 'parser') { |
|
36 | - return $this->parser; |
|
37 | - } |
|
30 | + throw new \InvalidArgumentException("Property `{$name}` does not exist"); |
|
31 | + } |
|
32 | + |
|
33 | + public function __get($name) |
|
34 | + { |
|
35 | + if ($name == 'parser') { |
|
36 | + return $this->parser; |
|
37 | + } |
|
38 | 38 | |
39 | - throw new \InvalidArgumentException("Property `{$name}` does not exist"); |
|
40 | - } |
|
41 | - |
|
42 | - protected function parse(&$input, $offset, Context $context) |
|
43 | - { |
|
44 | - if ($this->parser === null) { |
|
45 | - throw new \UnexpectedValueException('Missing parser'); |
|
46 | - } |
|
47 | - |
|
48 | - $match = $this->parser->parse($input, $offset, $context); |
|
49 | - if ($match->match) { |
|
50 | - return $this->success($input, $offset, $match->length, $match); |
|
51 | - } |
|
52 | - |
|
53 | - return $this->failure($input, $offset, $match->length); |
|
54 | - } |
|
55 | - |
|
56 | - public function __toString() |
|
57 | - { |
|
58 | - return $this->parser ? (string) $this->parser : '<undefined>'; |
|
59 | - } |
|
39 | + throw new \InvalidArgumentException("Property `{$name}` does not exist"); |
|
40 | + } |
|
41 | + |
|
42 | + protected function parse(&$input, $offset, Context $context) |
|
43 | + { |
|
44 | + if ($this->parser === null) { |
|
45 | + throw new \UnexpectedValueException('Missing parser'); |
|
46 | + } |
|
47 | + |
|
48 | + $match = $this->parser->parse($input, $offset, $context); |
|
49 | + if ($match->match) { |
|
50 | + return $this->success($input, $offset, $match->length, $match); |
|
51 | + } |
|
52 | + |
|
53 | + return $this->failure($input, $offset, $match->length); |
|
54 | + } |
|
55 | + |
|
56 | + public function __toString() |
|
57 | + { |
|
58 | + return $this->parser ? (string) $this->parser : '<undefined>'; |
|
59 | + } |
|
60 | 60 | |
61 | 61 | } |
@@ -12,14 +12,14 @@ |
||
12 | 12 | */ |
13 | 13 | class Start extends Parser { |
14 | 14 | |
15 | - protected function parse(&$input, $offset, Context $context) |
|
16 | - { |
|
17 | - return $offset == 0 ? $this->success($input, $offset) : $this->failure($input, $offset); |
|
18 | - } |
|
15 | + protected function parse(&$input, $offset, Context $context) |
|
16 | + { |
|
17 | + return $offset == 0 ? $this->success($input, $offset) : $this->failure($input, $offset); |
|
18 | + } |
|
19 | 19 | |
20 | - public function __toString() |
|
21 | - { |
|
22 | - return 'start'; |
|
23 | - } |
|
20 | + public function __toString() |
|
21 | + { |
|
22 | + return 'start'; |
|
23 | + } |
|
24 | 24 | |
25 | 25 | } |
@@ -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 | } |