@@ -48,7 +48,7 @@ |
||
48 | 48 | |
49 | 49 | public function extract($last, $autoLookup, $traversing) { |
50 | 50 | $value = $this->read($last); |
51 | - if ($value && ($autoLookup || $traversing) ) { |
|
51 | + if ($value && ($autoLookup || $traversing)) { |
|
52 | 52 | return $value; |
53 | 53 | } |
54 | 54 | throw new \UnexpectedValueException('Not found'); |
@@ -6,28 +6,28 @@ discard block |
||
6 | 6 | * @version 1.0 */ |
7 | 7 | namespace Transphporm\Parser; |
8 | 8 | class Tokens implements \Iterator, \Countable { |
9 | - private $tokens; |
|
10 | - private $iterator = 0; |
|
9 | + private $tokens; |
|
10 | + private $iterator = 0; |
|
11 | 11 | |
12 | - public function __construct(array $tokens) { |
|
13 | - $this->tokens = $tokens; |
|
14 | - } |
|
12 | + public function __construct(array $tokens) { |
|
13 | + $this->tokens = $tokens; |
|
14 | + } |
|
15 | 15 | |
16 | - public function count() { |
|
17 | - return count($this->tokens); |
|
18 | - } |
|
16 | + public function count() { |
|
17 | + return count($this->tokens); |
|
18 | + } |
|
19 | 19 | |
20 | - // Iterator Functions |
|
21 | - public function current() { |
|
22 | - return $this->tokens[$this->iterator]; |
|
23 | - } |
|
20 | + // Iterator Functions |
|
21 | + public function current() { |
|
22 | + return $this->tokens[$this->iterator]; |
|
23 | + } |
|
24 | 24 | |
25 | - public function key() { |
|
26 | - return $this->iterator; |
|
27 | - } |
|
25 | + public function key() { |
|
26 | + return $this->iterator; |
|
27 | + } |
|
28 | 28 | |
29 | - public function next() { |
|
30 | - ++$this->iterator; |
|
29 | + public function next() { |
|
30 | + ++$this->iterator; |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | public function valid() { |
@@ -38,67 +38,67 @@ discard block |
||
38 | 38 | $this->iterator = 0; |
39 | 39 | } |
40 | 40 | |
41 | - private function getKeysOfTokenType($tokenType) { |
|
42 | - return array_keys(array_column($this->tokens, 'type'), $tokenType); |
|
43 | - } |
|
44 | - |
|
45 | - private function getKeyToSlice($tokenType) { |
|
46 | - $keys = $this->getKeysOfTokenType($tokenType); |
|
47 | - if (empty($keys)) return false; |
|
48 | - $key = $keys[0]; |
|
49 | - for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) $key = $keys[$i]; |
|
50 | - return $key; |
|
51 | - } |
|
52 | - |
|
53 | - public function from($tokenType, $inclusive = false) { |
|
54 | - $key = $this->getKeyToSlice($tokenType); |
|
55 | - if ($key === false) return new Tokens([]); |
|
56 | - if (!$inclusive) $key++; |
|
57 | - return new Tokens(array_slice($this->tokens, $key)); |
|
58 | - } |
|
59 | - |
|
60 | - public function to($tokenType, $inclusive = false) { |
|
61 | - $key = $this->getKeyToSlice($tokenType); |
|
62 | - if ($key === false) return new Tokens([]); |
|
63 | - if ($inclusive) $key++; |
|
64 | - return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
65 | - } |
|
66 | - |
|
67 | - public function skip($count) { |
|
68 | - $this->iterator += $count; |
|
69 | - } |
|
70 | - |
|
71 | - public function splitOnToken($tokenType) { |
|
72 | - $splitTokens = []; |
|
41 | + private function getKeysOfTokenType($tokenType) { |
|
42 | + return array_keys(array_column($this->tokens, 'type'), $tokenType); |
|
43 | + } |
|
44 | + |
|
45 | + private function getKeyToSlice($tokenType) { |
|
46 | + $keys = $this->getKeysOfTokenType($tokenType); |
|
47 | + if (empty($keys)) return false; |
|
48 | + $key = $keys[0]; |
|
49 | + for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) $key = $keys[$i]; |
|
50 | + return $key; |
|
51 | + } |
|
52 | + |
|
53 | + public function from($tokenType, $inclusive = false) { |
|
54 | + $key = $this->getKeyToSlice($tokenType); |
|
55 | + if ($key === false) return new Tokens([]); |
|
56 | + if (!$inclusive) $key++; |
|
57 | + return new Tokens(array_slice($this->tokens, $key)); |
|
58 | + } |
|
59 | + |
|
60 | + public function to($tokenType, $inclusive = false) { |
|
61 | + $key = $this->getKeyToSlice($tokenType); |
|
62 | + if ($key === false) return new Tokens([]); |
|
63 | + if ($inclusive) $key++; |
|
64 | + return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
65 | + } |
|
66 | + |
|
67 | + public function skip($count) { |
|
68 | + $this->iterator += $count; |
|
69 | + } |
|
70 | + |
|
71 | + public function splitOnToken($tokenType) { |
|
72 | + $splitTokens = []; |
|
73 | 73 | $i = 0; |
74 | 74 | foreach ($this->tokens as $token) { |
75 | 75 | if ($token['type'] === $tokenType) $i++; |
76 | 76 | else $splitTokens[$i][] = $token; |
77 | 77 | } |
78 | - return array_map(function ($tokens) { |
|
79 | - return new Tokens($tokens); |
|
80 | - }, $splitTokens); |
|
78 | + return array_map(function ($tokens) { |
|
79 | + return new Tokens($tokens); |
|
80 | + }, $splitTokens); |
|
81 | 81 | //return $splitTokens; |
82 | - } |
|
83 | - |
|
84 | - public function trim() { |
|
85 | - $tokens = $this->tokens; |
|
86 | - // Remove end whitespace |
|
87 | - while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
88 | - array_pop($tokens); |
|
89 | - } |
|
90 | - // Remove begining whitespace |
|
91 | - while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
92 | - array_shift($tokens); |
|
93 | - } |
|
94 | - return new Tokens($tokens); |
|
95 | - } |
|
96 | - |
|
97 | - public function read($offset = 0) { |
|
98 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
99 | - } |
|
100 | - |
|
101 | - public function type($offset = 0) { |
|
102 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
103 | - } |
|
82 | + } |
|
83 | + |
|
84 | + public function trim() { |
|
85 | + $tokens = $this->tokens; |
|
86 | + // Remove end whitespace |
|
87 | + while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
88 | + array_pop($tokens); |
|
89 | + } |
|
90 | + // Remove begining whitespace |
|
91 | + while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
92 | + array_shift($tokens); |
|
93 | + } |
|
94 | + return new Tokens($tokens); |
|
95 | + } |
|
96 | + |
|
97 | + public function read($offset = 0) { |
|
98 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
99 | + } |
|
100 | + |
|
101 | + public function type($offset = 0) { |
|
102 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
103 | + } |
|
104 | 104 | } |
@@ -44,23 +44,35 @@ discard block |
||
44 | 44 | |
45 | 45 | private function getKeyToSlice($tokenType) { |
46 | 46 | $keys = $this->getKeysOfTokenType($tokenType); |
47 | - if (empty($keys)) return false; |
|
47 | + if (empty($keys)) { |
|
48 | + return false; |
|
49 | + } |
|
48 | 50 | $key = $keys[0]; |
49 | - for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) $key = $keys[$i]; |
|
51 | + for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) { |
|
52 | + $key = $keys[$i]; |
|
53 | + } |
|
50 | 54 | return $key; |
51 | 55 | } |
52 | 56 | |
53 | 57 | public function from($tokenType, $inclusive = false) { |
54 | 58 | $key = $this->getKeyToSlice($tokenType); |
55 | - if ($key === false) return new Tokens([]); |
|
56 | - if (!$inclusive) $key++; |
|
59 | + if ($key === false) { |
|
60 | + return new Tokens([]); |
|
61 | + } |
|
62 | + if (!$inclusive) { |
|
63 | + $key++; |
|
64 | + } |
|
57 | 65 | return new Tokens(array_slice($this->tokens, $key)); |
58 | 66 | } |
59 | 67 | |
60 | 68 | public function to($tokenType, $inclusive = false) { |
61 | 69 | $key = $this->getKeyToSlice($tokenType); |
62 | - if ($key === false) return new Tokens([]); |
|
63 | - if ($inclusive) $key++; |
|
70 | + if ($key === false) { |
|
71 | + return new Tokens([]); |
|
72 | + } |
|
73 | + if ($inclusive) { |
|
74 | + $key++; |
|
75 | + } |
|
64 | 76 | return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
65 | 77 | } |
66 | 78 | |
@@ -72,8 +84,11 @@ discard block |
||
72 | 84 | $splitTokens = []; |
73 | 85 | $i = 0; |
74 | 86 | foreach ($this->tokens as $token) { |
75 | - if ($token['type'] === $tokenType) $i++; |
|
76 | - else $splitTokens[$i][] = $token; |
|
87 | + if ($token['type'] === $tokenType) { |
|
88 | + $i++; |
|
89 | + } else { |
|
90 | + $splitTokens[$i][] = $token; |
|
91 | + } |
|
77 | 92 | } |
78 | 93 | return array_map(function ($tokens) { |
79 | 94 | return new Tokens($tokens); |