koesie10 /
LuaSerializer
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Vlaswinkel\Lua; |
||
| 4 | |||
| 5 | use Vlaswinkel\Lua\AST\ASTNode; |
||
| 6 | use Vlaswinkel\Lua\AST\NilASTNode; |
||
| 7 | use Vlaswinkel\Lua\AST\NumberASTNode; |
||
| 8 | use Vlaswinkel\Lua\AST\StringASTNode; |
||
| 9 | use Vlaswinkel\Lua\AST\TableASTNode; |
||
| 10 | use Vlaswinkel\Lua\AST\TableEntryASTNode; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Class Parser |
||
| 14 | * |
||
| 15 | * @see http://lisperator.net/pltut/parser/the-parser |
||
| 16 | * |
||
| 17 | * @author Koen Vlaswinkel <[email protected]> |
||
| 18 | * @package Vlaswinkel\Lua |
||
| 19 | */ |
||
| 20 | class Parser { |
||
| 21 | /** |
||
| 22 | * @var TokenStream |
||
| 23 | */ |
||
| 24 | private $input; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Parser constructor. |
||
| 28 | * |
||
| 29 | * @param TokenStream $input |
||
| 30 | */ |
||
| 31 | 24 | public function __construct(TokenStream $input) { |
|
| 32 | 24 | $this->input = $input; |
|
| 33 | 24 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @return ASTNode |
||
| 37 | * |
||
| 38 | * @throws ParseException |
||
| 39 | */ |
||
| 40 | 24 | public function parse() { |
|
| 41 | 24 | $result = $this->parseInternal(); |
|
| 42 | |||
| 43 | 22 | if (!$this->input->eof()) { |
|
| 44 | 2 | if ($result instanceof StringASTNode && $this->isPunctuation('=')) { |
|
| 45 | 2 | $this->skipPunctuation('='); |
|
| 46 | 2 | $value = $this->parseInternal(); |
|
| 47 | |||
| 48 | 2 | return new TableASTNode([new TableEntryASTNode($value, $result)]); |
|
| 49 | } |
||
| 50 | |||
| 51 | $this->input->error('Parser has finished parsing, but end of file was not reached. Next character is ' . $this->input->peek()->getValue()); |
||
| 52 | } |
||
| 53 | |||
| 54 | 20 | return $result; |
|
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return ASTNode |
||
| 59 | * |
||
| 60 | * @throws ParseException |
||
| 61 | */ |
||
| 62 | 24 | protected function parseInternal() { |
|
| 63 | 24 | if ($this->isPunctuation('{')) { |
|
| 64 | 13 | return $this->parseTable(); |
|
| 65 | } |
||
| 66 | 23 | if ($this->isPunctuation('[')) { |
|
| 67 | 4 | return $this->parseTableKey(); |
|
| 68 | } |
||
| 69 | 23 | $token = $this->input->next(); |
|
| 70 | 23 | if ($token->getType() == Token::TYPE_NUMBER) { |
|
| 71 | 8 | return new NumberASTNode($token->getValue()); |
|
| 72 | } |
||
| 73 | 21 | if ($token->getType() == Token::TYPE_STRING || $token->getType() == Token::TYPE_IDENTIFIER) { |
|
| 74 | 18 | return new StringASTNode($token->getValue()); |
|
| 75 | } |
||
| 76 | 5 | if ($token->getType() == Token::TYPE_KEYWORD) { |
|
| 77 | 5 | if ($token->getValue() === 'nil') { |
|
| 78 | 4 | return new NilASTNode(); |
|
| 79 | } else { |
||
| 80 | 1 | $this->input->error('Unexpected keyword: ' . $token->getValue()); |
|
| 81 | } |
||
| 82 | } |
||
| 83 | $this->unexpected(); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return TableASTNode |
||
| 88 | */ |
||
| 89 | 13 | protected function parseTable() { |
|
| 90 | 13 | return new TableASTNode( |
|
| 91 | 13 | $this->delimited( |
|
| 92 | 13 | '{', |
|
| 93 | 13 | '}', |
|
| 94 | 13 | ',', |
|
| 95 | 13 | [$this, 'parseTableEntry'] |
|
| 96 | 13 | ) |
|
| 97 | 12 | ); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return TableEntryASTNode |
||
| 102 | */ |
||
| 103 | 12 | protected function parseTableEntry() { |
|
| 104 | 12 | $token = $this->parseInternal(); |
|
| 105 | 12 | if ($this->isPunctuation('=')) { |
|
| 106 | 11 | $this->skipPunctuation('='); |
|
| 107 | 11 | $value = $this->parseInternal(); |
|
| 108 | 11 | return new TableEntryASTNode( |
|
| 109 | 11 | $value, |
|
| 110 | $token |
||
| 111 | 11 | ); |
|
| 112 | } |
||
| 113 | 5 | return new TableEntryASTNode($token); |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return ASTNode |
||
|
0 ignored issues
–
show
|
|||
| 118 | */ |
||
| 119 | 4 | protected function parseTableKey() { |
|
| 120 | 4 | $this->skipPunctuation('['); |
|
| 121 | 4 | $token = $this->parseInternal(); |
|
| 122 | 4 | $this->skipPunctuation(']'); |
|
| 123 | 4 | return $token; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $start |
||
| 128 | * @param string $stop |
||
| 129 | * @param string $separator |
||
| 130 | * @param callable $parser |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | 13 | protected function delimited($start, $stop, $separator, callable $parser) { |
|
| 135 | 13 | $a = []; |
|
| 136 | 13 | $first = true; |
|
| 137 | 13 | $this->skipPunctuation($start); |
|
| 138 | 13 | while (!$this->input->eof()) { |
|
| 139 | 13 | if ($this->isPunctuation($stop)) { |
|
| 140 | 12 | break; |
|
| 141 | } |
||
| 142 | 12 | if ($first) { |
|
| 143 | 12 | $first = false; |
|
| 144 | 12 | } else { |
|
| 145 | 5 | $this->skipPunctuation($separator); |
|
| 146 | } |
||
| 147 | 12 | if ($this->isPunctuation($stop)) { |
|
| 148 | 2 | break; |
|
| 149 | } |
||
| 150 | 12 | $a[] = $parser(); |
|
| 151 | 12 | } |
|
| 152 | 12 | $this->skipPunctuation($stop); |
|
| 153 | 12 | return $a; |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string|null $char |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | 24 | protected function isPunctuation($char = null) { |
|
| 162 | 24 | $token = $this->input->peek(); |
|
| 163 | 24 | return $token && $token->getType() == Token::TYPE_PUNCTUATION && ($char === null || $token->getValue( |
|
| 164 | 24 | ) == $char); |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string|null $char |
||
| 169 | * |
||
| 170 | * @throws ParseException |
||
| 171 | */ |
||
| 172 | 14 | protected function skipPunctuation($char = null) { |
|
| 173 | 14 | if ($this->isPunctuation($char)) { |
|
| 174 | 14 | $this->input->next(); |
|
| 175 | 14 | } else { |
|
| 176 | 1 | $this->input->error('Expecting punctuation: "' . $char . '"'); |
|
| 177 | } |
||
| 178 | 14 | } |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @throws ParseException |
||
| 182 | */ |
||
| 183 | protected function unexpected() { |
||
| 184 | $this->input->error('Unexpected token: ' . json_encode($this->input->peek())); |
||
| 185 | } |
||
| 186 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.