1: <?php
2: namespace Pharborist;
3:
4: use Pharborist\Constants\ClassMagicConstantNode;
5: use Pharborist\Constants\DirMagicConstantNode;
6: use Pharborist\Constants\FileMagicConstantNode;
7: use Pharborist\Constants\FunctionMagicConstantNode;
8: use Pharborist\Constants\LineMagicConstantNode;
9: use Pharborist\Constants\MethodMagicConstantNode;
10: use Pharborist\Constants\NamespaceMagicConstantNode;
11: use Pharborist\Constants\TraitMagicConstantNode;
12: use Pharborist\Types\FloatNode;
13: use Pharborist\Types\IntegerNode;
14: use Pharborist\Types\StringNode;
15: use Pharborist\Variables\VariableNode;
16:
17: 18: 19:
20: class Tokenizer {
21: private $lineNo;
22: private $colNo;
23:
24: private function parseToken($token) {
25: if (is_array($token)) {
26: $type = $token[0];
27: $text = $token[1];
28: } else {
29: $type = $token;
30: $text = $token;
31: }
32: $lineNo = $this->lineNo;
33: $colNo = $this->colNo;
34: $newline_count = substr_count($text, "\n");
35: if ($newline_count > 0) {
36: $this->lineNo += $newline_count;
37: $lines = explode("\n", $text);
38: $last_line = end($lines);
39: $this->colNo = strlen($last_line) + 1;
40: } else {
41: $this->colNo += strlen($text);
42: }
43: return $this->createToken($type, $text, new SourcePosition($lineNo, $colNo));
44: }
45:
46: private function createToken($type, $text, $position) {
47: switch ($type) {
48: case T_VARIABLE:
49: return new VariableNode($type, $text, $position);
50: case T_LNUMBER:
51: return new IntegerNode($type, $text, $position);
52: case T_DNUMBER:
53: return new FloatNode($type, $text, $position);
54: case T_CONSTANT_ENCAPSED_STRING:
55: return new StringNode($type, $text, $position);
56: case T_LINE:
57: return new LineMagicConstantNode($type, $text, $position);
58: case T_FILE:
59: return new FileMagicConstantNode($type, $text, $position);
60: case T_DIR:
61: return new DirMagicConstantNode($type, $text, $position);
62: case T_FUNC_C:
63: return new FunctionMagicConstantNode($type, $text, $position);
64: case T_CLASS_C:
65: return new ClassMagicConstantNode($type, $text, $position);
66: case T_TRAIT_C:
67: return new TraitMagicConstantNode($type, $text, $position);
68: case T_METHOD_C:
69: return new MethodMagicConstantNode($type, $text, $position);
70: case T_NS_C:
71: return new NamespaceMagicConstantNode($type, $text, $position);
72: case T_COMMENT:
73: return new CommentNode($type, $text, $position);
74: case T_DOC_COMMENT:
75: return new DocCommentNode($type, $text, $position);
76: case T_WHITESPACE:
77: return new WhitespaceNode($type, $text, $position);
78: default:
79: return new TokenNode($type, $text, $position);
80: }
81: }
82:
83: public function getAll($source) {
84: $this->colNo = 1;
85: $this->lineNo = 1;
86: $tokens = [];
87: foreach (token_get_all($source) as $rawToken) {
88: $tokens[] = $this->parseToken($rawToken);
89: }
90: return $tokens;
91: }
92: }
93: