1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sphpeme; |
4
|
|
|
|
5
|
|
|
class Reader |
6
|
|
|
{ |
7
|
|
|
private $tokenizeRegexp = '/\s*(,@|[(\'`,)]|"(?:[\\].|[^])+"|;.+|[^\s(\'`,;)]+)(.*)/'; |
8
|
|
|
private $file; |
9
|
|
|
private $line = ''; |
10
|
|
|
|
11
|
|
|
private $quotes; |
12
|
|
|
|
13
|
6 |
|
public static function fromStream($stream) |
14
|
|
|
{ |
15
|
6 |
|
if (!\is_resource($stream) || get_resource_type($stream) !== 'stream') { |
16
|
1 |
|
throw new \InvalidArgumentException('Must give me a file stream'); |
17
|
|
|
} |
18
|
|
|
|
19
|
5 |
|
return new static($stream); |
20
|
|
|
} |
21
|
|
|
|
22
|
2 |
|
public static function fromFilepath($filepath) |
23
|
|
|
{ |
24
|
2 |
|
if (!file_exists($filepath)) { |
25
|
1 |
|
throw new \InvalidArgumentException("{$filepath} does not exist or is not readable."); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
return new static(fopen($filepath, 'r+b')); |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
private function __construct($file) |
32
|
|
|
{ |
33
|
6 |
|
$this->quotes = [ |
34
|
6 |
|
'\'' => Symbol::make('quote'), |
35
|
6 |
|
'`' => Symbol::make('quasiquote'), |
36
|
6 |
|
',' => Symbol::make('unquote'), |
37
|
6 |
|
',@' => Symbol::make('unquote-splicing'), |
38
|
|
|
]; |
39
|
|
|
|
40
|
6 |
|
$this->file = $file; |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $token |
45
|
|
|
* @return array|mixed |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
5 |
|
private function readAhead($token) |
49
|
|
|
{ |
50
|
5 |
|
$this->guardAgainstUnexpectedEof($token); |
51
|
5 |
|
$this->guardAgainstInvalidExpression($token); |
52
|
|
|
|
53
|
4 |
|
if ($token === '(') { |
54
|
4 |
|
$list = []; |
55
|
4 |
|
while (true) { |
56
|
4 |
|
$token = $this->nextToken(); |
57
|
4 |
|
if ($token === ')') { |
58
|
3 |
|
return $list; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
$list[] = $this->readAhead($token); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
4 |
|
if (isset($this->quotes[$token])) { |
67
|
2 |
|
return [$this->quotes[$token], $this->read()]; |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
return atom($token); |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
public function read() |
74
|
|
|
{ |
75
|
5 |
|
$first = $this->nextToken(); |
76
|
|
|
|
77
|
5 |
|
return $first !== false |
78
|
5 |
|
? $this->readAhead($first) |
79
|
3 |
|
: false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool|string |
84
|
|
|
*/ |
85
|
5 |
|
public function nextToken() |
86
|
|
|
{ |
87
|
5 |
|
while (true) { |
88
|
5 |
|
if (trim($this->line) === '') { |
89
|
5 |
|
$this->line = fgets($this->file); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
if ($this->line === false) { |
93
|
1 |
|
return $this->line; |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
preg_match($this->tokenizeRegexp, $this->line, $matches); |
97
|
|
|
|
98
|
5 |
|
list($_, $token, $this->line) = $matches; |
|
|
|
|
99
|
|
|
|
100
|
5 |
|
if ($token !== ';' || $token !== '') { |
101
|
5 |
|
return $token; |
102
|
|
|
} |
103
|
|
|
// @codeCoverageIgnoreStart |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
// @codeCoverageIgnoreEnd |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $token |
110
|
|
|
* @return null |
111
|
|
|
* @throws \Exception |
112
|
|
|
*/ |
113
|
5 |
|
private function guardAgainstUnexpectedEof($token) |
114
|
|
|
{ |
115
|
5 |
|
if ($token === false) { |
116
|
1 |
|
throw new \Exception('unexpected eof!'); |
117
|
|
|
} |
118
|
5 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $token |
122
|
|
|
* @return null |
123
|
|
|
* @throws \Exception |
124
|
|
|
*/ |
125
|
5 |
|
private function guardAgainstInvalidExpression($token) |
126
|
|
|
{ |
127
|
5 |
|
if ($token === ')') { |
128
|
1 |
|
throw new \Exception('unexpected end of expression'); |
129
|
|
|
} |
130
|
4 |
|
} |
131
|
|
|
} |
132
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.