@@ -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,75 +38,75 @@ 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 | - return $this->sliceTokens($tokenType, "from", !$inclusive); |
|
55 | - } |
|
56 | - |
|
57 | - public function to($tokenType, $inclusive = false) { |
|
58 | - return $this->sliceTokens($tokenType, "to", $inclusive); |
|
59 | - } |
|
60 | - |
|
61 | - private function sliceTokens($tokenType, $type, $increment = false) { |
|
62 | - $key = $this->getKeyToSlice($tokenType); |
|
63 | - if ($key === false) return new Tokens([]); |
|
64 | - if ($increment) $key++; |
|
65 | - if ($type === "from") return new Tokens(array_slice($this->tokens, $key)); |
|
66 | - else return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
67 | - } |
|
68 | - |
|
69 | - public function skip($count) { |
|
70 | - $this->iterator += $count; |
|
71 | - } |
|
72 | - |
|
73 | - public function splitOnToken($tokenType) { |
|
74 | - $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 | + return $this->sliceTokens($tokenType, "from", !$inclusive); |
|
55 | + } |
|
56 | + |
|
57 | + public function to($tokenType, $inclusive = false) { |
|
58 | + return $this->sliceTokens($tokenType, "to", $inclusive); |
|
59 | + } |
|
60 | + |
|
61 | + private function sliceTokens($tokenType, $type, $increment = false) { |
|
62 | + $key = $this->getKeyToSlice($tokenType); |
|
63 | + if ($key === false) return new Tokens([]); |
|
64 | + if ($increment) $key++; |
|
65 | + if ($type === "from") return new Tokens(array_slice($this->tokens, $key)); |
|
66 | + else return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
67 | + } |
|
68 | + |
|
69 | + public function skip($count) { |
|
70 | + $this->iterator += $count; |
|
71 | + } |
|
72 | + |
|
73 | + public function splitOnToken($tokenType) { |
|
74 | + $splitTokens = []; |
|
75 | 75 | $i = 0; |
76 | 76 | foreach ($this->tokens as $token) { |
77 | 77 | if ($token['type'] === $tokenType) $i++; |
78 | 78 | else $splitTokens[$i][] = $token; |
79 | 79 | } |
80 | - return array_map(function ($tokens) { |
|
81 | - return new Tokens($tokens); |
|
82 | - }, $splitTokens); |
|
80 | + return array_map(function ($tokens) { |
|
81 | + return new Tokens($tokens); |
|
82 | + }, $splitTokens); |
|
83 | 83 | //return $splitTokens; |
84 | - } |
|
85 | - |
|
86 | - public function trim() { |
|
87 | - $tokens = $this->tokens; |
|
88 | - // Remove end whitespace |
|
89 | - while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
90 | - array_pop($tokens); |
|
91 | - } |
|
92 | - // Remove begining whitespace |
|
93 | - while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
94 | - array_shift($tokens); |
|
95 | - } |
|
96 | - return new Tokens($tokens); |
|
97 | - } |
|
98 | - |
|
99 | - public function removeLine() { |
|
100 | - $tokens = $this->tokens; |
|
101 | - foreach ($tokens as &$token) unset($token['line']); |
|
102 | - return new Tokens($tokens); |
|
103 | - } |
|
104 | - |
|
105 | - public function read($offset = 0) { |
|
106 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
107 | - } |
|
108 | - |
|
109 | - public function type($offset = 0) { |
|
110 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
111 | - } |
|
84 | + } |
|
85 | + |
|
86 | + public function trim() { |
|
87 | + $tokens = $this->tokens; |
|
88 | + // Remove end whitespace |
|
89 | + while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
90 | + array_pop($tokens); |
|
91 | + } |
|
92 | + // Remove begining whitespace |
|
93 | + while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
94 | + array_shift($tokens); |
|
95 | + } |
|
96 | + return new Tokens($tokens); |
|
97 | + } |
|
98 | + |
|
99 | + public function removeLine() { |
|
100 | + $tokens = $this->tokens; |
|
101 | + foreach ($tokens as &$token) unset($token['line']); |
|
102 | + return new Tokens($tokens); |
|
103 | + } |
|
104 | + |
|
105 | + public function read($offset = 0) { |
|
106 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
107 | + } |
|
108 | + |
|
109 | + public function type($offset = 0) { |
|
110 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
111 | + } |
|
112 | 112 | } |
@@ -44,9 +44,13 @@ 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 | |
@@ -60,10 +64,17 @@ discard block |
||
60 | 64 | |
61 | 65 | private function sliceTokens($tokenType, $type, $increment = false) { |
62 | 66 | $key = $this->getKeyToSlice($tokenType); |
63 | - if ($key === false) return new Tokens([]); |
|
64 | - if ($increment) $key++; |
|
65 | - if ($type === "from") return new Tokens(array_slice($this->tokens, $key)); |
|
66 | - else return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
67 | + if ($key === false) { |
|
68 | + return new Tokens([]); |
|
69 | + } |
|
70 | + if ($increment) { |
|
71 | + $key++; |
|
72 | + } |
|
73 | + if ($type === "from") { |
|
74 | + return new Tokens(array_slice($this->tokens, $key)); |
|
75 | + } else { |
|
76 | + return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
77 | + } |
|
67 | 78 | } |
68 | 79 | |
69 | 80 | public function skip($count) { |
@@ -74,8 +85,11 @@ discard block |
||
74 | 85 | $splitTokens = []; |
75 | 86 | $i = 0; |
76 | 87 | foreach ($this->tokens as $token) { |
77 | - if ($token['type'] === $tokenType) $i++; |
|
78 | - else $splitTokens[$i][] = $token; |
|
88 | + if ($token['type'] === $tokenType) { |
|
89 | + $i++; |
|
90 | + } else { |
|
91 | + $splitTokens[$i][] = $token; |
|
92 | + } |
|
79 | 93 | } |
80 | 94 | return array_map(function ($tokens) { |
81 | 95 | return new Tokens($tokens); |
@@ -98,7 +112,9 @@ discard block |
||
98 | 112 | |
99 | 113 | public function removeLine() { |
100 | 114 | $tokens = $this->tokens; |
101 | - foreach ($tokens as &$token) unset($token['line']); |
|
115 | + foreach ($tokens as &$token) { |
|
116 | + unset($token['line']); |
|
117 | + } |
|
102 | 118 | return new Tokens($tokens); |
103 | 119 | } |
104 | 120 |