@@ -39,6 +39,9 @@ discard block |
||
39 | 39 | return $this->cache->write($this->file, $rules, $this->import); |
40 | 40 | } |
41 | 41 | |
42 | + /** |
|
43 | + * @param integer $indexStart |
|
44 | + */ |
|
42 | 45 | private function parseTokens($indexStart) { |
43 | 46 | $this->rules = []; |
44 | 47 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
@@ -63,6 +66,9 @@ discard block |
||
63 | 66 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
64 | 67 | } |
65 | 68 | |
69 | + /** |
|
70 | + * @param integer $index |
|
71 | + */ |
|
66 | 72 | private function CssToRules($selector, $index, $properties, $line) { |
67 | 73 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
68 | 74 | $rules = []; |
@@ -86,6 +92,9 @@ discard block |
||
86 | 92 | return $rules; |
87 | 93 | } |
88 | 94 | |
95 | + /** |
|
96 | + * @param integer $indexStart |
|
97 | + */ |
|
89 | 98 | private function processingInstructions($token, $indexStart) { |
90 | 99 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
91 | 100 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -25,14 +25,19 @@ discard block |
||
25 | 25 | $this->file = $tss; |
26 | 26 | $this->rules = $this->cache->load($tss); |
27 | 27 | $this->filePath->addPath(dirname(realpath($tss))); |
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 = (new SheetTokenizer($tss))->getTokens(); |
32 | 35 | } |
33 | 36 | |
34 | 37 | public function parse($indexStart = 0) { |
35 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
38 | + if (!empty($this->rules)) { |
|
39 | + return $this->rules['rules']; |
|
40 | + } |
|
36 | 41 | $rules = $this->parseTokens($indexStart); |
37 | 42 | usort($rules, [$this, 'sortRules']); |
38 | 43 | $this->checkError($rules); |
@@ -44,8 +49,9 @@ discard block |
||
44 | 49 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
45 | 50 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
46 | 51 | $this->rules = array_merge($this->rules, $processing); |
52 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
53 | + $this->addRules($token, $indexStart); |
|
47 | 54 | } |
48 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart); |
|
49 | 55 | } |
50 | 56 | return $this->rules; |
51 | 57 | } |
@@ -53,14 +59,18 @@ discard block |
||
53 | 59 | private function addRules($token, $indexStart) { |
54 | 60 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
55 | 61 | $this->tss->skip(count($selector)); |
56 | - if (count($selector) === 0) return; |
|
62 | + if (count($selector) === 0) { |
|
63 | + return; |
|
64 | + } |
|
57 | 65 | |
58 | 66 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
59 | 67 | $this->rules = $this->writeRule($this->rules, $newRules); |
60 | 68 | } |
61 | 69 | |
62 | 70 | private function checkError($rules) { |
63 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
71 | + if (empty($rules) && count($this->tss) > 0) { |
|
72 | + throw new \Exception('No TSS rules parsed'); |
|
73 | + } |
|
64 | 74 | } |
65 | 75 | |
66 | 76 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -87,7 +97,9 @@ discard block |
||
87 | 97 | } |
88 | 98 | |
89 | 99 | private function processingInstructions($token, $indexStart) { |
90 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
100 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
101 | + return false; |
|
102 | + } |
|
91 | 103 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
92 | 104 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
93 | 105 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -107,7 +119,9 @@ discard block |
||
107 | 119 | |
108 | 120 | private function sortRules($a, $b) { |
109 | 121 | //If they have the same depth, compare on index |
110 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
122 | + if ($a->depth === $b->depth) { |
|
123 | + return $a->index < $b->index ? -1 : 1; |
|
124 | + } |
|
111 | 125 | |
112 | 126 | return ($a->depth < $b->depth) ? -1 : 1; |
113 | 127 | } |
@@ -2,48 +2,48 @@ |
||
2 | 2 | namespace Transphporm; |
3 | 3 | use Transphporm\Parser\Tokenizer; |
4 | 4 | class TSSValidator { |
5 | - private $error; |
|
5 | + private $error; |
|
6 | 6 | |
7 | - public function validate($tss) { |
|
8 | - $this->error = null; |
|
9 | - $tokens = $this->tokenize($tss); |
|
7 | + public function validate($tss) { |
|
8 | + $this->error = null; |
|
9 | + $tokens = $this->tokenize($tss); |
|
10 | 10 | |
11 | - foreach ($tokens as $token) |
|
12 | - if (!$this->validateRule($token)) return false; |
|
11 | + foreach ($tokens as $token) |
|
12 | + if (!$this->validateRule($token)) return false; |
|
13 | 13 | |
14 | - return true; |
|
15 | - } |
|
14 | + return true; |
|
15 | + } |
|
16 | 16 | |
17 | - public function getLastError() { |
|
18 | - return $this->error; |
|
19 | - } |
|
17 | + public function getLastError() { |
|
18 | + return $this->error; |
|
19 | + } |
|
20 | 20 | |
21 | - private function validateRule($token) { |
|
22 | - if ($token['type'] !== Tokenizer::OPEN_BRACE) return true; |
|
21 | + private function validateRule($token) { |
|
22 | + if ($token['type'] !== Tokenizer::OPEN_BRACE) return true; |
|
23 | 23 | |
24 | - return $this->checkBraces($token) && $this->checkSemicolons($token) |
|
25 | - && $this->checkParenthesis($token); |
|
26 | - } |
|
24 | + return $this->checkBraces($token) && $this->checkSemicolons($token) |
|
25 | + && $this->checkParenthesis($token); |
|
26 | + } |
|
27 | 27 | |
28 | - private function checkBraces($token) { |
|
29 | - return strpos($token['string'], '{') === false; |
|
30 | - } |
|
28 | + private function checkBraces($token) { |
|
29 | + return strpos($token['string'], '{') === false; |
|
30 | + } |
|
31 | 31 | |
32 | - private function checkSemicolons($braceToken) { |
|
33 | - $splitTokens = $braceToken['value']->splitOnToken(Tokenizer::COLON); |
|
34 | - array_shift($splitTokens); array_pop($splitTokens); |
|
35 | - foreach ($splitTokens as $tokens) |
|
36 | - if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
32 | + private function checkSemicolons($braceToken) { |
|
33 | + $splitTokens = $braceToken['value']->splitOnToken(Tokenizer::COLON); |
|
34 | + array_shift($splitTokens); array_pop($splitTokens); |
|
35 | + foreach ($splitTokens as $tokens) |
|
36 | + if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
37 | 37 | |
38 | - return true; |
|
39 | - } |
|
38 | + return true; |
|
39 | + } |
|
40 | 40 | |
41 | - private function checkParenthesis($token) { |
|
42 | - return substr_count($token['string'], '(') === substr_count($token['string'], ')'); |
|
43 | - } |
|
41 | + private function checkParenthesis($token) { |
|
42 | + return substr_count($token['string'], '(') === substr_count($token['string'], ')'); |
|
43 | + } |
|
44 | 44 | |
45 | - private function tokenize($tss) { |
|
46 | - if (is_file($tss)) $tss = file_get_contents($tss); |
|
47 | - return (new Parser\SheetTokenizer($tss))->getTokens(); |
|
48 | - } |
|
45 | + private function tokenize($tss) { |
|
46 | + if (is_file($tss)) $tss = file_get_contents($tss); |
|
47 | + return (new Parser\SheetTokenizer($tss))->getTokens(); |
|
48 | + } |
|
49 | 49 | } |
@@ -8,8 +8,9 @@ discard block |
||
8 | 8 | $this->error = null; |
9 | 9 | $tokens = $this->tokenize($tss); |
10 | 10 | |
11 | - foreach ($tokens as $token) |
|
12 | - if (!$this->validateRule($token)) return false; |
|
11 | + foreach ($tokens as $token) { |
|
12 | + if (!$this->validateRule($token)) return false; |
|
13 | + } |
|
13 | 14 | |
14 | 15 | return true; |
15 | 16 | } |
@@ -19,7 +20,9 @@ discard block |
||
19 | 20 | } |
20 | 21 | |
21 | 22 | private function validateRule($token) { |
22 | - if ($token['type'] !== Tokenizer::OPEN_BRACE) return true; |
|
23 | + if ($token['type'] !== Tokenizer::OPEN_BRACE) { |
|
24 | + return true; |
|
25 | + } |
|
23 | 26 | |
24 | 27 | return $this->checkBraces($token) && $this->checkSemicolons($token) |
25 | 28 | && $this->checkParenthesis($token); |
@@ -32,8 +35,9 @@ discard block |
||
32 | 35 | private function checkSemicolons($braceToken) { |
33 | 36 | $splitTokens = $braceToken['value']->splitOnToken(Tokenizer::COLON); |
34 | 37 | array_shift($splitTokens); array_pop($splitTokens); |
35 | - foreach ($splitTokens as $tokens) |
|
36 | - if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
38 | + foreach ($splitTokens as $tokens) { |
|
39 | + if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
40 | + } |
|
37 | 41 | |
38 | 42 | return true; |
39 | 43 | } |
@@ -43,7 +47,9 @@ discard block |
||
43 | 47 | } |
44 | 48 | |
45 | 49 | private function tokenize($tss) { |
46 | - if (is_file($tss)) $tss = file_get_contents($tss); |
|
50 | + if (is_file($tss)) { |
|
51 | + $tss = file_get_contents($tss); |
|
52 | + } |
|
47 | 53 | return (new Parser\SheetTokenizer($tss))->getTokens(); |
48 | 54 | } |
49 | 55 | } |
@@ -1,19 +1,19 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Transphporm\Parser; |
3 | 3 | class SheetTokenizer { |
4 | - private $tokenizer; |
|
4 | + private $tokenizer; |
|
5 | 5 | |
6 | - public function __construct($tss) { |
|
7 | - $tss = $this->stripComments($tss, '//', "\n"); |
|
6 | + public function __construct($tss) { |
|
7 | + $tss = $this->stripComments($tss, '//', "\n"); |
|
8 | 8 | $tss = $this->stripComments($tss, '/*', '*/'); |
9 | 9 | $this->tokenizer = new Tokenizer($tss); |
10 | - } |
|
10 | + } |
|
11 | 11 | |
12 | - public function getTokens() { |
|
12 | + public function getTokens() { |
|
13 | 13 | return $this->tokenizer->getTokens(); |
14 | - } |
|
14 | + } |
|
15 | 15 | |
16 | - private function stripComments($str, $open, $close) { |
|
16 | + private function stripComments($str, $open, $close) { |
|
17 | 17 | $pos = 0; |
18 | 18 | while (($pos = strpos($str, $open, $pos)) !== false) { |
19 | 19 | $end = strpos($str, $close, $pos); |
@@ -17,7 +17,9 @@ |
||
17 | 17 | $pos = 0; |
18 | 18 | while (($pos = strpos($str, $open, $pos)) !== false) { |
19 | 19 | $end = strpos($str, $close, $pos); |
20 | - if ($end === false) break; |
|
20 | + if ($end === false) { |
|
21 | + break; |
|
22 | + } |
|
21 | 23 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
22 | 24 | } |
23 | 25 |