1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Latte;
9:
10:
11: 12: 13: 14:
15: class Tokenizer
16: {
17: use Strict;
18:
19: const VALUE = 0,
20: OFFSET = 1,
21: TYPE = 2;
22:
23:
24: private $re;
25:
26:
27: private $types;
28:
29:
30: 31: 32: 33:
34: public function __construct(array $patterns, $flags = '')
35: {
36: $this->re = '~(' . implode(')|(', $patterns) . ')~A' . $flags;
37: $this->types = array_keys($patterns);
38: }
39:
40:
41: 42: 43: 44: 45:
46: public function tokenize($input)
47: {
48: preg_match_all($this->re, $input, $tokens, PREG_SET_ORDER);
49: if (preg_last_error()) {
50: throw new RegexpException(null, preg_last_error());
51: }
52: $len = 0;
53: $count = count($this->types);
54: foreach ($tokens as &$match) {
55: $type = null;
56: for ($i = 1; $i <= $count; $i++) {
57: if (!isset($match[$i])) {
58: break;
59: } elseif ($match[$i] != null) {
60: $type = $this->types[$i - 1];
61: break;
62: }
63: }
64: $match = [self::VALUE => $match[0], self::OFFSET => $len, self::TYPE => $type];
65: $len += strlen($match[self::VALUE]);
66: }
67: if ($len !== strlen($input)) {
68: list($line, $col) = $this->getCoordinates($input, $len);
69: $token = str_replace("\n", '\n', substr($input, $len, 10));
70: throw new CompileException("Unexpected '$token' on line $line, column $col.");
71: }
72: return $tokens;
73: }
74:
75:
76: 77: 78: 79: 80: 81:
82: public static function getCoordinates($text, $offset)
83: {
84: $text = substr($text, 0, $offset);
85: return [substr_count($text, "\n") + 1, $offset - strrpos("\n" . $text, "\n") + 1];
86: }
87: }
88: