|
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 Literals implements \Transphporm\Parser\Tokenize { |
|
12
|
|
|
|
|
13
|
|
|
public function tokenize(TokenizedString $str, Tokens $tokens) { |
|
14
|
|
|
$char = $str->identifyChar(); |
|
15
|
|
|
if ($char === Tokenizer::NAME) { |
|
16
|
|
|
|
|
17
|
|
|
$name = $str->read(); |
|
18
|
|
|
$j = 0; |
|
19
|
|
|
|
|
20
|
|
|
while ($this->isLiteral($j+1, $str)) { |
|
21
|
|
|
$name .= $str->read($j+1); |
|
22
|
|
|
$j++; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$str->move($j); |
|
26
|
|
|
|
|
27
|
|
|
$this->processLiterals($tokens, $name, $str); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function isLiteral($n, $str) { |
|
32
|
|
|
//Is it a normal literal character |
|
33
|
|
|
return ($str->has($n) && ($str->identifyChar($n, $str) === Tokenizer::NAME |
|
34
|
|
|
//but a subtract can be part of a class name or a mathematical operation |
|
35
|
|
|
|| $str->identifyChar($n) == Tokenizer::SUBTRACT && !is_numeric($str->read($n-1))) |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function processLiterals($tokens, $name, $str) { |
|
40
|
|
|
if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
41
|
|
|
else if ($name == 'true') $tokens->add(['type' => Tokenizer::BOOL, 'value' => true]); |
|
42
|
|
|
else if ($name == 'false') $tokens->add(['type' => Tokenizer::BOOL, 'value' => false]); |
|
43
|
|
|
else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
44
|
|
|
} |
|
45
|
|
|
} |