Completed
Push — master ( e6458b...c9bd8d )
by Tom
03:29
created

Tokenizer::doStrings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Parser;
8
class Tokenizer {
9
	private $str;
10
	const NAME = 1;
11
	const STRING = 2;
12
	const STRING2 = 3;
13
	const OPEN_BRACKET = 4;
14
	const CLOSE_BRACKET = 5;
15
	const OPEN_SQUARE_BRACKET = 6;
16
	const CLOSE_SQUARE_BRACKET = 7;
17
	const CONCAT = 8;
18
	const ARG = 9;
19
	const WHITESPACE = 10;
20
	const DOT = 11;
21
	const NUMERIC = 12;
22
	const EQUALS = 13;
23
	const NOT = 14;
24
	const OPEN_BRACE = 15;
25
	const CLOSE_BRACE = 16;
26
	const BOOL = 17;
27
28
	public $chars = [
29
		'"' => self::STRING,
30
		'\'' => self::STRING2,
31
		'(' => self::OPEN_BRACKET,
32
		')' => self::CLOSE_BRACKET,
33
		'[' => self::OPEN_SQUARE_BRACKET,
34
		']' => self::CLOSE_SQUARE_BRACKET,
35
		'+' => self::CONCAT,
36
		',' => self::ARG,
37
		'.' => self::DOT,
38
		'!' => self::NOT,
39
		'=' => self::EQUALS,
40
		'{' => self::OPEN_BRACE,
41
		'}' => self::CLOSE_BRACE,
42
		' ' => self::WHITESPACE,
43
		"\n" => self::WHITESPACE,
44
		"\r" => self::WHITESPACE,
45
		"\t" => self::WHITESPACE
46
	];
47
48
	public function __construct($str) {
49
		$this->str = $str;
50
	}
51
52
	public function getTokens() {
53
		$tokens = [];
54
55
		for ($i = 0; $i < strlen($this->str); $i++) {
56
			$char = $this->identifyChar($this->str[$i]);
57
58
			$this->doSimpleTokens($tokens, $char);
59
			$this->doLiterals($tokens, $char, $i);
60
			$i += $this->doStrings($tokens, $char, $i);
61
			$i += $this->doBrackets($tokens, $char, $i);
62
		}
63
		return $tokens;
64
	}
65
66
	private function doSimpleTokens(&$tokens, $char) {
67
		if (in_array($char, [Tokenizer::ARG, Tokenizer::CONCAT, Tokenizer::DOT, Tokenizer::NOT, Tokenizer::EQUALS])) {
68
			$tokens[] = ['type' => $char];
69
		}
70
	}
71
72
	private function doLiterals(&$tokens, $char, &$i) {
73
		if ($char === self::NAME) {
74
			$name = $this->str[$i];
75
			while (isset($this->str[$i+1]) && $this->identifyChar($this->str[$i+1]) == self::NAME) {
76
				$name .= $this->str[$i+1];
77
				$i++;
78
			}
79
			if (is_numeric($name)) $tokens[] = ['type' => self::NUMERIC, 'value' => $name];
80
			else if ($name == 'true') $tokens[] = ['type' => self::BOOL, 'value' => true];
81
			else if ($name == 'false') $tokens[] = ['type' => self::BOOL, 'value' => false];
82
			else $tokens[] = ['type' => self::NAME, 'value' => $name];
83
		}
84
	}
85
86
	private function doBrackets(&$tokens, $char, $i) {
87
		$types = [
88
			self::OPEN_BRACKET => ['(', ')'],
89
			self::OPEN_BRACE => ['{', '}'],
90
			self::OPEN_SQUARE_BRACKET => ['[', ']']
91
		];
92
93
		foreach ($types as $type => $brackets) {
94
			if ($char === $type) {
95
				$contents = $this->extractBrackets($i, $brackets[0], $brackets[1]);
96
				$tokenizer = new Tokenizer($contents);
97
				$tokens[] = ['type' => $type, 'value' => $tokenizer->getTokens()];
98
				return strlen($contents);
99
			}
100
		}
101
	}
102
103
	private function doStrings(&$tokens, $char, $i) {
104
		if (in_array($char, [self::STRING, self::STRING2])) {
105
			$string = $this->extractString($i);
106
			$length = strlen($string)+1;
107
			$char = $this->getChar($char);
108
			$string = str_replace('\\' . $char, $char, $string);
109
			$tokens[] = ['type' => self::STRING, 'value' => $string];
110
			return $length;
111
		}
112
	}
113
114
	private function extractString($pos) {
115
		$char = $this->str[$pos];
116
		$end = strpos($this->str, $char, $pos+1);
117
		while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1);
118
119
		return substr($this->str, $pos+1, $end-$pos-1);
120
	}
121
122
	private function extractBrackets($open, $startBracket = '(', $closeBracket = ')') {
123
		$close = strpos($this->str, $closeBracket, $open);
124
125
		$cPos = $open+1;
126
		while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1);
127
		return substr($this->str, $open+1, $close-$open-1);
128
	}
129
130
	private function identifyChar($chr) {
131
		if (isset($this->chars[$chr])) return $this->chars[$chr];
132
		else return self::NAME;
133
	}
134
135
	private function getChar($num) {
136
		$chars = array_reverse($this->chars);
137
		if (isset($chars[$num])) return $chars[$num];
138
		else return false;
139
	}
140
}
141