1: <?php
2:
3: namespace BN\Compiler\Scanner;
4:
5: class LexemeToTokens
6: {
7: private $converter;
8: private $lexeme;
9: private $lexemeLength;
10: private $lastToken;
11:
12: public function __construct(LexemeToToken $toToken)
13: {
14: $this->converter = $toToken;
15: }
16:
17: public function lexemeToTokens($lexeme)
18: {
19: $tokens = array();
20: $this->initLexeme($lexeme);
21: while ($this->hasLexemeAtLeastOneCharacter()) {
22: $this->lexemeToToken();
23: $this->makeLexemeShorter();
24: $tokens[] = $this->lastToken;
25: }
26: return $tokens;
27: }
28:
29: private function initLexeme($lexeme)
30: {
31: $this->lexeme = $lexeme;
32: $this->lexemeLength = mb_strlen($this->lexeme, 'UTF-8');
33: }
34:
35: private function hasLexemeAtLeastOneCharacter()
36: {
37: return $this->lexemeLength > 0;
38: }
39:
40: private function lexemeToToken()
41: {
42: $this->lastToken = $this->converter->lexemeToToken($this->lexeme);
43: }
44:
45: private function makeLexemeShorter()
46: {
47: $tokenLength = mb_strlen($this->lastToken->value, 'UTF-8');
48: $this->lexeme = mb_substr($this->lexeme, $tokenLength, $this->lexemeLength, 'UTF-8');
49: $this->lexemeLength -= $tokenLength;
50: }
51: }
52: