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

TokenizedString::move()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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
10
class TokenizedString {
11
	private $str;
12
	private $pos = -1;
13
	private $lineNo = 1;
14
15
	private $chars = [
16
		'"' => Tokenizer::STRING,
17
		'\'' => Tokenizer::STRING,
18
		'(' => Tokenizer::OPEN_BRACKET,
19
		')' => Tokenizer::CLOSE_BRACKET,
20
		'[' => Tokenizer::OPEN_SQUARE_BRACKET,
21
		']' => Tokenizer::CLOSE_SQUARE_BRACKET,
22
		'+' => Tokenizer::CONCAT,
23
		',' => Tokenizer::ARG,
24
		'.' => Tokenizer::DOT,
25
		'!' => Tokenizer::NOT,
26
		'=' => Tokenizer::EQUALS,
27
		'{' => Tokenizer::OPEN_BRACE,
28
		'}' => Tokenizer::CLOSE_BRACE,
29
		':' => Tokenizer::COLON,
30
		';' => Tokenizer::SEMI_COLON,
31
		'#' => Tokenizer::NUM_SIGN,
32
		'>' => Tokenizer::GREATER_THAN,
33
		'@' => Tokenizer::AT_SIGN,
34
		'-' => Tokenizer::SUBTRACT,
35
		'*' => Tokenizer::MULTIPLY,
36
		'/' => Tokenizer::DIVIDE,
37
		' ' => Tokenizer::WHITESPACE,
38
		"\n" => Tokenizer::NEW_LINE,
39
		"\r" => Tokenizer::WHITESPACE,
40
		"\t" => Tokenizer::WHITESPACE
41
	];
42
43
	public function __construct($str) {
44
		$this->str = $str;
45
	}
46
47
	public function move($n) {
48
		if ($n === false) $this->pos = strlen($this->str)-1;
49
		else $this->pos += $n;
50
	}
51
52
	public function next() {
53
		$this->pos++;
54
		return $this->pos < strlen($this->str);
55
	}
56
57
	public function reset() {
58
		$this->lineNo = 1;
59
		$this->pos = -1;
60
	}
61
62
	public function read($offset = 0) {
63
		return $this->str[$this->pos + $offset];
64
	}
65
66
	public function identifyChar($offset = 0) {
67
		$chr = $this->str[$this->pos + $offset];
68
		if (!empty($this->chars[$chr])) return $this->chars[$chr];
69
		else return Tokenizer::NAME;
70
	}
71
72
	public function has($offset = 0) {
73
		return isset($this->str[$this->pos + $offset]);
74
	}
75
76
	public function pos($str) {
77
		$pos = strpos($this->str,  $str, $this->pos);
78
		return $pos ? $pos-$this->pos : false;
79
	}
80
81
	public function newLine() {
82
		return $this->lineNo++;
83
	}
84
85
	public function lineNo() {
86
		return $this->lineNo;
87
	}
88
89
	public function extractString($offset = 0) {
90
		$pos = $this->pos + $offset;
91
		$char = $this->str[$pos];
92
		$end = strpos($this->str, $char, $pos+1);
93
		while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1);
94
95
		return substr($this->str, $pos+1, $end-$pos-1);
96
	}
97
98
	public function extractBrackets($startBracket = '(', $closeBracket = ')', $offset = 0) {
99
		$open = $this->pos+$offset;
100
		$close = strpos($this->str, $closeBracket, $open);
101
102
		$cPos = $open+1;
103
		while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1);
104
		return substr($this->str, $open+1, $close-$open-1);
105
	}
106
107
}