Completed
Push — master ( 8a12d1...a9e060 )
by Tom
01:46
created

BasicChars::simpleTokens()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
 namespace Transphporm\Parser\Tokenizer;
8
use \Transphporm\Parser\Tokenizer;
9
use \Transphporm\Parser\Tokens;
10
11
class BasicChars implements \Transphporm\Parser\Tokenize {
12
13
	public function tokenize(TokenizedString $str, Tokens $tokens) {
14
		$this->newLine($str, $tokens);
15
		$this->whitespace($str, $tokens);
16
		$this->simpleTokens($str, $tokens);
17
	}
18
19
	public function whitespace(TokenizedString $str, Tokens $tokens) {
20
		//Combine whitespace, this increases performance across the board: Anywhere tokens are iterated over, whitespace is only looped once 8 spaces of indentation = 1 iteration
21
		$char = $str->identifyChar();
22
		if ($char === Tokenizer::WHITESPACE) {
23
			$last = $tokens->end();
24
			if ($last['type'] !== Tokenizer::WHITESPACE) {
25
				$tokens->add(['type' => $char]);
26
			}
27
		}
28
	}
29
30
	private function newLine(TokenizedString $str, Tokens $tokens) {
31
		if ($str->identifyChar() == Tokenizer::NEW_LINE) {
32
			$tokens->add(['type' => Tokenizer::WHITESPACE, 'line' => $str->newLine()]);
33
		}
34
	}
35
36
37
	private function simpleTokens($str, $tokens) {
38
		$char = $str->identifyChar();
39
		if (in_array($char, [Tokenizer::ARG, Tokenizer::CONCAT, Tokenizer::DOT, Tokenizer::NOT, Tokenizer::EQUALS,
40
			Tokenizer::COLON, Tokenizer::SEMI_COLON, Tokenizer::NUM_SIGN,
41
			Tokenizer::GREATER_THAN, Tokenizer::LOWER_THAN, Tokenizer::AT_SIGN, Tokenizer::SUBTRACT, Tokenizer::MULTIPLY, Tokenizer::DIVIDE])) {
42
			$tokens->add(['type' => $char, 'line' => $str->lineNo()]);
43
		}
44
	}
45
46
}