| 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 |  |  |     public static function fromStream($stream) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         if (!\is_resource($stream) || get_resource_type($stream) !== 'stream') { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |             throw new \InvalidArgumentException('Must give me a file stream'); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         return new static($stream); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public static function fromFilepath($filepath) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         if (!file_exists($filepath)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             throw new \InvalidArgumentException("{$filepath} does not exist or is not readable."); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         return new static(fopen($filepath, 'r+b')); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     private function __construct($file) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->quotes = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             '\'' => Symbol::make('quote'), | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             '``' => Symbol::make('quasiquote'), | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             ',' => Symbol::make('unquote'), | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             ',@' => Symbol::make('unquote-splicing'), | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $this->file = $file; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param $token | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @return array|mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     private function readAhead($token) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->guardAgainstUnexpectedEof($token); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->guardAgainstInvalidExpression($token); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         if ($token === '(') { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $l = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             while (true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 $token = $this->nextToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 if ($token === ')') { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     return $l; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 $l[] = $this->readAhead($token); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         if (isset($this->quotes[$token])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             return [$this->quotes[$token], $this->read()]; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         return atom($token); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function read() | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $first = $this->nextToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         return $first !== false | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             ? $this->readAhead($first) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             : false; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @return bool|string | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function nextToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         while (true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             if ($this->line === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 $this->line = fgets($this->file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             if ($this->line === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 return $this->line; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             preg_match($this->tokenizeRegexp, $this->line, $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             [$_, $token, $this->line] = $matches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             if ($token !== ';' || $token !== '') { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |                 return $token; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * @param $token | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     private function guardAgainstUnexpectedEof($token): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if ($token === false) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |             throw new \Exception('unexpected eof!'); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 118 |  |  |      * @param $token | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     private function guardAgainstInvalidExpression($token): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if ($token === ')') { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |             throw new \Exception('unexpected end of expression'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                        
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.