1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sphpeme; |
4
|
|
|
|
5
|
|
|
class Reader |
6
|
|
|
{ |
7
|
|
|
private $tokenizeRegexp = '/\s*(,@|[(\'`,)]|"(?:[\\].|[^\\"])*"|;.*|[^\s(\'"`,;)]*)(.*)/'; |
8
|
|
|
private $file; |
9
|
|
|
private $line = ''; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
private $quotes; |
13
|
|
|
|
14
|
|
|
public function __construct($file) |
15
|
|
|
{ |
16
|
|
|
if (!\is_resource($file) || get_resource_type($file) !== 'stream') { |
17
|
|
|
throw new \InvalidArgumentException('Must give me a file stream'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$this->quotes = [ |
21
|
|
|
'\'' => Symbol::make('quote'), |
22
|
|
|
'``' => Symbol::make('quasiquote'), |
23
|
|
|
',' => Symbol::make('unquote'), |
24
|
|
|
',@' => Symbol::make('unquote-splicing'), |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
$this->file = $file; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $token |
32
|
|
|
* @return array|mixed |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
private function readAhead($token) |
36
|
|
|
{ |
37
|
|
|
$this->guardAgainstUnexpectedEof($token); |
38
|
|
|
$this->guardAgainstInvalidExpression($token); |
39
|
|
|
|
40
|
|
|
if ($token === '(') { |
41
|
|
|
$l = []; |
42
|
|
|
while (true) { |
43
|
|
|
$token = $this->nextToken(); |
44
|
|
|
if ($token === ')') { |
45
|
|
|
return $l; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$l[] = $this->readAhead($token); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
if (isset($this->quotes[$token])) { |
54
|
|
|
return [$this->quotes[$token], $this->read()]; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return atom($token); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function read() |
61
|
|
|
{ |
62
|
|
|
$first = $this->nextToken(); |
63
|
|
|
|
64
|
|
|
return $first !== false |
65
|
|
|
? $this->readAhead($first) |
66
|
|
|
: false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool|string |
71
|
|
|
*/ |
72
|
|
|
public function nextToken() |
73
|
|
|
{ |
74
|
|
|
while (true) { |
75
|
|
|
if ($this->line === '') { |
76
|
|
|
$this->line = fgets($this->file); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($this->line === false) { |
80
|
|
|
return $this->line; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
preg_match($this->tokenizeRegexp, $this->line, $matches); |
84
|
|
|
|
85
|
|
|
[$_, $token, $this->line] = $matches; |
86
|
|
|
|
87
|
|
|
if ($token !== ';' || $token !== '') { |
88
|
|
|
return $token; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $token |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
private function guardAgainstUnexpectedEof($token): void |
98
|
|
|
{ |
99
|
|
|
if ($token === false) { |
100
|
|
|
throw new \Exception('unexpected eof!'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $token |
106
|
|
|
* @throws \Exception |
107
|
|
|
*/ |
108
|
|
|
private function guardAgainstInvalidExpression($token): void |
109
|
|
|
{ |
110
|
|
|
if ($token === ')') { |
111
|
|
|
throw new \Exception('unexpected end of expression'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.