1: <?php
2:
3: namespace BN\Compiler\Scanner;
4:
5: class Scanner
6: {
7: private $converter;
8: private $whitespace;
9: private $statementsSeparator;
10:
11: public function __construct(LexemeToTokens $converter, array $whitespace, $statementsSeparator)
12: {
13: $this->converter = $converter;
14: $this->whitespace = $whitespace;
15: $this->statementsSeparator = $statementsSeparator;
16: }
17:
18: public function tokenize($text)
19: {
20: $statements = array();
21: foreach (explode($this->statementsSeparator, $text) as $statement) {
22: $tokens = $this->statementToTokens($statement);
23: if ($tokens) {
24: $statements[] = new Statement($statement, $tokens);
25: }
26: }
27: return $statements;
28: }
29:
30: private function statementToTokens($statement)
31: {
32: $tokens = array();
33: $lexemes = $this->inputToLexemes($statement);
34: foreach ($lexemes as $lexeme) {
35: $tokens = array_merge(
36: $tokens,
37: $this->converter->lexemeToTokens($lexeme)
38: );
39: }
40: return $tokens;
41: }
42:
43: private function inputToLexemes($input)
44: {
45: return explode(
46: ' ',
47: $this->replaceWhitespaceWithSpace($input)
48: );
49: }
50:
51: private function replaceWhitespaceWithSpace($input)
52: {
53: return str_replace($this->whitespace, ' ', $input);
54: }
55: }
56: