Completed
Push — master ( 18f2d2...864802 )
by Tom
02:51
created

Tokenizer::isLiteral()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.2
cc 4
eloc 3
nc 4
nop 1
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 = 'LITERAL';
11
	const STRING = 'STRING';
12
	const OPEN_BRACKET = 'OPEN_BRACKET';
13
	const CLOSE_BRACKET = 'CLOSE_BRACKET';
14
	const OPEN_SQUARE_BRACKET = 'SQUARE_BRACKET';
15
	const CLOSE_SQUARE_BRACKET = 'CLOSE_SQUARE_BRACKET';
16
	const CONCAT = 'CONCAT';
17
	const ARG = 'ARG';
18
	const WHITESPACE = 'WHITESPACE';
19
	const NEW_LINE = 'NEW_LINE';
20
	const DOT = 'DOT';
21
	const NUMERIC = 'NUMERIC';
22
	const EQUALS = 'EQUALS';
23
	const NOT = 'NOT';
24
	const OPEN_BRACE = 'OPEN_BRACE';
25
	const CLOSE_BRACE = 'CLOSE_BRACE';
26
	const BOOL = 'BOOL';
27
	const COLON = 'COLON';
28
	const SEMI_COLON = 'SEMI_COLON';
29
	const NUM_SIGN = 'NUM_SIGN';
30
	const GREATER_THAN = 'GREATER_THAN';
31
	const AT_SIGN = 'AT_SIGN';
32
	const SUBTRACT = 'SUBTRACT';
33
	const MULTIPLY = 'MULTIPLY';
34
	const DIVIDE = 'DIVIDE';
35
36
	private $chars = [
37
		'"' => self::STRING,
38
		'\'' => self::STRING,
39
		'(' => self::OPEN_BRACKET,
40
		')' => self::CLOSE_BRACKET,
41
		'[' => self::OPEN_SQUARE_BRACKET,
42
		']' => self::CLOSE_SQUARE_BRACKET,
43
		'+' => self::CONCAT,
44
		',' => self::ARG,
45
		'.' => self::DOT,
46
		'!' => self::NOT,
47
		'=' => self::EQUALS,
48
		'{' => self::OPEN_BRACE,
49
		'}' => self::CLOSE_BRACE,
50
		':' => self::COLON,
51
		';' => self::SEMI_COLON,
52
		'#' => self::NUM_SIGN,
53
		'>' => self::GREATER_THAN,
54
		'@' => self::AT_SIGN,
55
		'-' => self::SUBTRACT,
56
		'*' => self::MULTIPLY,
57
		'/' => self::DIVIDE,
58
		' ' => self::WHITESPACE,
59
		"\n" => self::NEW_LINE,
60
		"\r" => self::WHITESPACE,
61
		"\t" => self::WHITESPACE
62
	];
63
64
	public function __construct($str) {
65
		$this->str = $str;
66
	}
67
68
	public function getTokens($returnObj = true) {
69
		$tokens = [];
70
71
		for ($i = 0; $i < strlen($this->str); $i++) {
72
			$char = $this->identifyChar($this->str[$i]);
73
74
			$this->doSimpleTokens($tokens, $char);
75
			$this->doLiterals($tokens, $char, $i);
76
			$i += $this->doStrings($tokens, $char, $i);
77
			$i += $this->doBrackets($tokens, $char, $i);
78
		}
79
		if ($returnObj) return new Tokens($tokens);
80
		else return $tokens;
81
	}
82
83
	private function doSimpleTokens(&$tokens, $char) {
84
		if (in_array($char, [Tokenizer::ARG, Tokenizer::CONCAT, Tokenizer::DOT, Tokenizer::NOT, Tokenizer::EQUALS,
85
			Tokenizer::COLON, Tokenizer::SEMI_COLON, Tokenizer::WHITESPACE, Tokenizer::NEW_LINE, Tokenizer::NUM_SIGN,
86
			Tokenizer::GREATER_THAN, Tokenizer::AT_SIGN, Tokenizer::SUBTRACT, Tokenizer::MULTIPLY, Tokenizer::DIVIDE])) {
87
			$tokens[] = ['type' => $char];
88
		}
89
	}
90
91
	private function isLiteral($n) {
92
		//Is it a normal literal character
93
		return isset($this->str[$n]) && ($this->identifyChar($this->str[$n]) == self::NAME
94
		//but a subtract can be part of a class name or a mathematical operation
95
				|| ($this->identifyChar($this->str[$n]) == self::SUBTRACT && !is_numeric($this->str[$n-1])));
96
	}
97
	
98
	private function doLiterals(&$tokens, $char, &$i) {
99
		if ($char === self::NAME) {
100
			$name = $this->str[$i];
101
			while ($this->isLiteral($i+1)) {
102
				$name .= $this->str[$i+1];
103
				$i++;
104
			}
105
			$this->processLiterals($tokens, $name);
106
		}
107
	}
108
109
	private function processLiterals(&$tokens, $name) {
110
		if (is_numeric($name)) $tokens[] = ['type' => self::NUMERIC, 'value' => $name];
111
		else if ($name == 'true') $tokens[] = ['type' => self::BOOL, 'value' => true];
112
		else if ($name == 'false') $tokens[] = ['type' => self::BOOL, 'value' => false];
113
		else $tokens[] = ['type' => self::NAME, 'value' => $name];
114
	}
115
116
	private function doBrackets(&$tokens, $char, $i) {
117
		$types = [
118
			self::OPEN_BRACKET => ['(', ')'],
119
			self::OPEN_BRACE => ['{', '}'],
120
			self::OPEN_SQUARE_BRACKET => ['[', ']']
121
		];
122
123
		foreach ($types as $type => $brackets) {
124
			if ($char === $type) {
125
				$contents = $this->extractBrackets($i, $brackets[0], $brackets[1]);
126
				$tokenizer = new Tokenizer($contents);
127
				$tokens[] = ['type' => $type, 'value' => $tokenizer->getTokens(), 'string' => $contents];
128
				return strlen($contents);
129
			}
130
		}
131
	}
132
133
	private function doStrings(&$tokens, $char, $i) {
134
		if ($char === self::STRING) {
135
			$string = $this->extractString($i);
136
			$length = strlen($string)+1;
137
			$char = $this->getChar($char);
138
			$string = str_replace('\\' . $char, $char, $string);
139
			$tokens[] = ['type' => self::STRING, 'value' => $string];
140
			return $length;
141
		}
142
	}
143
144
	private function extractString($pos) {
145
		$char = $this->str[$pos];
146
		$end = strpos($this->str, $char, $pos+1);
147
		while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1);
148
149
		return substr($this->str, $pos+1, $end-$pos-1);
150
	}
151
152
	private function extractBrackets($open, $startBracket = '(', $closeBracket = ')') {
153
		$close = strpos($this->str, $closeBracket, $open);
154
155
		$cPos = $open+1;
156
		while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1);
157
		return substr($this->str, $open+1, $close-$open-1);
158
	}
159
160
	private function identifyChar($chr) {
161
		if (isset($this->chars[$chr])) return $this->chars[$chr];
162
		else return self::NAME;
163
	}
164
165
	private function getChar($num) {
166
		$chars = array_reverse($this->chars);
167
		if (isset($chars[$num])) return $chars[$num];
168
		else return false;
169
	}
170
}
171