@@ -16,6 +16,9 @@ discard block |
||
16 | 16 | private $filePath; |
17 | 17 | private $import = []; |
18 | 18 | |
19 | + /** |
|
20 | + * @param string $tss |
|
21 | + */ |
|
19 | 22 | public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\TSSCache $cache, \Transphporm\FilePath $filePath) { |
20 | 23 | $this->cache = $cache; |
21 | 24 | $this->xPath = $xPath; |
@@ -42,6 +45,9 @@ discard block |
||
42 | 45 | return $this->cache->write($this->file, $rules, $this->import); |
43 | 46 | } |
44 | 47 | |
48 | + /** |
|
49 | + * @param integer $indexStart |
|
50 | + */ |
|
45 | 51 | private function parseTokens($indexStart) { |
46 | 52 | $this->rules = []; |
47 | 53 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
@@ -66,6 +72,9 @@ discard block |
||
66 | 72 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
67 | 73 | } |
68 | 74 | |
75 | + /** |
|
76 | + * @param integer $index |
|
77 | + */ |
|
69 | 78 | private function CssToRules($selector, $index, $properties, $line) { |
70 | 79 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
71 | 80 | $rules = []; |
@@ -89,6 +98,9 @@ discard block |
||
89 | 98 | return $rules; |
90 | 99 | } |
91 | 100 | |
101 | + /** |
|
102 | + * @param integer $indexStart |
|
103 | + */ |
|
92 | 104 | private function processingInstructions($token, $indexStart) { |
93 | 105 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
94 | 106 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -117,6 +129,10 @@ discard block |
||
117 | 129 | return ($a->depth < $b->depth) ? -1 : 1; |
118 | 130 | } |
119 | 131 | |
132 | + /** |
|
133 | + * @param string $open |
|
134 | + * @param string $close |
|
135 | + */ |
|
120 | 136 | private function stripComments($str, $open, $close) { |
121 | 137 | $pos = 0; |
122 | 138 | while (($pos = strpos($str, $open, $pos)) !== false) { |
@@ -24,7 +24,7 @@ |
||
24 | 24 | if (is_file($tss)) { |
25 | 25 | $this->file = $tss; |
26 | 26 | $this->rules = $this->cache->load($tss); |
27 | - $this->filePath->setBaseDir(dirname(realpath($tss)) . DIRECTORY_SEPARATOR); |
|
27 | + $this->filePath->setBaseDir(dirname(realpath($tss)).DIRECTORY_SEPARATOR); |
|
28 | 28 | if (empty($this->rules)) $tss = file_get_contents($tss); |
29 | 29 | else return; |
30 | 30 | } |
@@ -25,8 +25,11 @@ discard block |
||
25 | 25 | $this->file = $tss; |
26 | 26 | $this->rules = $this->cache->load($tss); |
27 | 27 | $this->filePath->setBaseDir(dirname(realpath($tss)) . DIRECTORY_SEPARATOR); |
28 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
29 | - else return; |
|
28 | + if (empty($this->rules)) { |
|
29 | + $tss = file_get_contents($tss); |
|
30 | + } else { |
|
31 | + return; |
|
32 | + } |
|
30 | 33 | } |
31 | 34 | $this->tss = $this->stripComments($tss, '//', "\n"); |
32 | 35 | $this->tss = $this->stripComments($this->tss, '/*', '*/'); |
@@ -35,7 +38,9 @@ discard block |
||
35 | 38 | } |
36 | 39 | |
37 | 40 | public function parse($indexStart = 0) { |
38 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
41 | + if (!empty($this->rules)) { |
|
42 | + return $this->rules['rules']; |
|
43 | + } |
|
39 | 44 | $rules = $this->parseTokens($indexStart); |
40 | 45 | usort($rules, [$this, 'sortRules']); |
41 | 46 | $this->checkError($rules); |
@@ -47,8 +52,9 @@ discard block |
||
47 | 52 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
48 | 53 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
49 | 54 | $this->rules = array_merge($this->rules, $processing); |
55 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
56 | + $this->addRules($token, $indexStart); |
|
50 | 57 | } |
51 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart); |
|
52 | 58 | } |
53 | 59 | return $this->rules; |
54 | 60 | } |
@@ -56,14 +62,18 @@ discard block |
||
56 | 62 | private function addRules($token, $indexStart) { |
57 | 63 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
58 | 64 | $this->tss->skip(count($selector)); |
59 | - if (count($selector) === 0) return; |
|
65 | + if (count($selector) === 0) { |
|
66 | + return; |
|
67 | + } |
|
60 | 68 | |
61 | 69 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
62 | 70 | $this->rules = $this->writeRule($this->rules, $newRules); |
63 | 71 | } |
64 | 72 | |
65 | 73 | private function checkError($rules) { |
66 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
74 | + if (empty($rules) && count($this->tss) > 0) { |
|
75 | + throw new \Exception('No TSS rules parsed'); |
|
76 | + } |
|
67 | 77 | } |
68 | 78 | |
69 | 79 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -90,7 +100,9 @@ discard block |
||
90 | 100 | } |
91 | 101 | |
92 | 102 | private function processingInstructions($token, $indexStart) { |
93 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
103 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
104 | + return false; |
|
105 | + } |
|
94 | 106 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
95 | 107 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
96 | 108 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -112,7 +124,9 @@ discard block |
||
112 | 124 | |
113 | 125 | private function sortRules($a, $b) { |
114 | 126 | //If they have the same depth, compare on index |
115 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
127 | + if ($a->depth === $b->depth) { |
|
128 | + return $a->index < $b->index ? -1 : 1; |
|
129 | + } |
|
116 | 130 | |
117 | 131 | return ($a->depth < $b->depth) ? -1 : 1; |
118 | 132 | } |
@@ -121,7 +135,9 @@ discard block |
||
121 | 135 | $pos = 0; |
122 | 136 | while (($pos = strpos($str, $open, $pos)) !== false) { |
123 | 137 | $end = strpos($str, $close, $pos); |
124 | - if ($end === false) break; |
|
138 | + if ($end === false) { |
|
139 | + break; |
|
140 | + } |
|
125 | 141 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
126 | 142 | } |
127 | 143 |
@@ -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,73 +38,73 @@ 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 removeLine() { |
|
98 | - $tokens = $this->tokens; |
|
99 | - foreach ($tokens as &$token) unset($token['line']); |
|
100 | - return new Tokens($tokens); |
|
101 | - } |
|
102 | - |
|
103 | - public function read($offset = 0) { |
|
104 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
105 | - } |
|
106 | - |
|
107 | - public function type($offset = 0) { |
|
108 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
109 | - } |
|
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 removeLine() { |
|
98 | + $tokens = $this->tokens; |
|
99 | + foreach ($tokens as &$token) unset($token['line']); |
|
100 | + return new Tokens($tokens); |
|
101 | + } |
|
102 | + |
|
103 | + public function read($offset = 0) { |
|
104 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
105 | + } |
|
106 | + |
|
107 | + public function type($offset = 0) { |
|
108 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
109 | + } |
|
110 | 110 | } |
@@ -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); |
@@ -96,7 +111,9 @@ discard block |
||
96 | 111 | |
97 | 112 | public function removeLine() { |
98 | 113 | $tokens = $this->tokens; |
99 | - foreach ($tokens as &$token) unset($token['line']); |
|
114 | + foreach ($tokens as &$token) { |
|
115 | + unset($token['line']); |
|
116 | + } |
|
100 | 117 | return new Tokens($tokens); |
101 | 118 | } |
102 | 119 |
@@ -145,7 +145,7 @@ |
||
145 | 145 | $string = $this->extractString($i); |
146 | 146 | $length = strlen($string)+1; |
147 | 147 | $char = $this->getChar($char); |
148 | - $string = str_replace('\\' . $char, $char, $string); |
|
148 | + $string = str_replace('\\'.$char, $char, $string); |
|
149 | 149 | $tokens[] = ['type' => self::STRING, 'value' => $string, 'line' => $this->lineNo]; |
150 | 150 | return $length; |
151 | 151 | } |
@@ -79,8 +79,11 @@ discard block |
||
79 | 79 | $i += $this->doStrings($tokens, $char, $i); |
80 | 80 | $i += $this->doBrackets($tokens, $char, $i); |
81 | 81 | } |
82 | - if ($returnObj) return new Tokens($tokens); |
|
83 | - else return $tokens; |
|
82 | + if ($returnObj) { |
|
83 | + return new Tokens($tokens); |
|
84 | + } else { |
|
85 | + return $tokens; |
|
86 | + } |
|
84 | 87 | } |
85 | 88 | |
86 | 89 | private function doSimpleTokens(&$tokens, $char) { |
@@ -117,10 +120,15 @@ discard block |
||
117 | 120 | } |
118 | 121 | |
119 | 122 | private function processLiterals(&$tokens, $name) { |
120 | - if (is_numeric($name)) $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
121 | - else if ($name == 'true') $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
122 | - else if ($name == 'false') $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
123 | - else $tokens[] = ['type' => self::NAME, 'value' => $name, 'line' => $this->lineNo]; |
|
123 | + if (is_numeric($name)) { |
|
124 | + $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
125 | + } else if ($name == 'true') { |
|
126 | + $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
127 | + } else if ($name == 'false') { |
|
128 | + $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
129 | + } else { |
|
130 | + $tokens[] = ['type' => self::NAME, 'value' => $name, 'line' => $this->lineNo]; |
|
131 | + } |
|
124 | 132 | } |
125 | 133 | |
126 | 134 | private function doBrackets(&$tokens, $char, $i) { |
@@ -154,7 +162,9 @@ discard block |
||
154 | 162 | private function extractString($pos) { |
155 | 163 | $char = $this->str[$pos]; |
156 | 164 | $end = strpos($this->str, $char, $pos+1); |
157 | - while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1); |
|
165 | + while ($end !== false && $this->str[$end-1] == '\\') { |
|
166 | + $end = strpos($this->str, $char, $end+1); |
|
167 | + } |
|
158 | 168 | |
159 | 169 | return substr($this->str, $pos+1, $end-$pos-1); |
160 | 170 | } |
@@ -163,18 +173,26 @@ discard block |
||
163 | 173 | $close = strpos($this->str, $closeBracket, $open); |
164 | 174 | |
165 | 175 | $cPos = $open+1; |
166 | - while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1); |
|
176 | + while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) { |
|
177 | + $close = strpos($this->str, $closeBracket, $close+1); |
|
178 | + } |
|
167 | 179 | return substr($this->str, $open+1, $close-$open-1); |
168 | 180 | } |
169 | 181 | |
170 | 182 | private function identifyChar($chr) { |
171 | - if (isset($this->chars[$chr])) return $this->chars[$chr]; |
|
172 | - else return self::NAME; |
|
183 | + if (isset($this->chars[$chr])) { |
|
184 | + return $this->chars[$chr]; |
|
185 | + } else { |
|
186 | + return self::NAME; |
|
187 | + } |
|
173 | 188 | } |
174 | 189 | |
175 | 190 | private function getChar($num) { |
176 | 191 | $chars = array_reverse($this->chars); |
177 | - if (isset($chars[$num])) return $chars[$num]; |
|
178 | - else return false; |
|
192 | + if (isset($chars[$num])) { |
|
193 | + return $chars[$num]; |
|
194 | + } else { |
|
195 | + return false; |
|
196 | + } |
|
179 | 197 | } |
180 | 198 | } |