|
| 1 | +<?php |
| 2 | + |
| 3 | +/* MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2018 Eridan Domoratskiy |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. */ |
| 24 | + |
| 25 | +namespace PHPDataGen\Parsing; |
| 26 | + |
| 27 | +/** |
| 28 | + * Parsing conveyor |
| 29 | + */ |
| 30 | +class Conveyor { |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string Next string |
| 34 | + */ |
| 35 | + protected $next = null; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var int Current cursor line |
| 39 | + */ |
| 40 | + protected $line = 0; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var int Current cursor row |
| 44 | + */ |
| 45 | + protected $row = 0; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param string $source Source of conveyor |
| 49 | + */ |
| 50 | + public function __construct(string $source) { |
| 51 | + $this->next = $source; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Moves conveyor on specified offset. |
| 56 | + * Offset must be pozitive (or null) |
| 57 | + * |
| 58 | + * @param int $offset Pozitive offset |
| 59 | + */ |
| 60 | + public function move(int $offset) { |
| 61 | + if ($offset < 0) { |
| 62 | + throw new \OutOfRangeException("Offset must be pozitive"); |
| 63 | + } |
| 64 | + |
| 65 | + for ($i = 0; $i < $offset; ++$i) { |
| 66 | + if ($this->next[$i] == "\n") { |
| 67 | + $this->row = 0; |
| 68 | + ++$this->line; |
| 69 | + } else { |
| 70 | + ++$this->row; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + $this->next = substr($this->next, $offset); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Tries to read operator. |
| 79 | + * If required is true throws Exception |
| 80 | + * |
| 81 | + * @param string $operator Operator |
| 82 | + * @param bool $required Is operator required? Default is false |
| 83 | + * |
| 84 | + * @return Operator finded |
| 85 | + */ |
| 86 | + public function readOperator(string $operator, bool $required = false): bool { |
| 87 | + if (strpos($this->next, $operator) === 0) { |
| 88 | + $this->move(strlen($operator)); |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + if ($required) { |
| 93 | + throw new \Exception("$operator not found at {$this->line} line {$this->row} row"); |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Moves conveyor to next not-space symbol |
| 101 | + */ |
| 102 | + public function skipSpaces() { |
| 103 | + if (preg_match('/^\s*/', $this->next, $matches) === 1) { |
| 104 | + $this->move(strlen($matches[0])); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Reads namespace (Foo\Bar\Baz sequence) from parser conveyor |
| 110 | + * Shifts parser coveyor on namespace length |
| 111 | + * |
| 112 | + * @return string Namespace |
| 113 | + */ |
| 114 | + public function readNamespace(): string { |
| 115 | + if (preg_match('/^[a-z_][\w\\]*\w/i', $this->next, $matches) === 1) { |
| 116 | + $this->move(strlen($matches[0])); |
| 117 | + |
| 118 | + return $matches[0]; |
| 119 | + } |
| 120 | + |
| 121 | + throw new \Exception('Namespace not found'); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Reads class name from parser conveyor |
| 126 | + * Shifts parser coveyor on class name length |
| 127 | + * |
| 128 | + * @return string Class name |
| 129 | + */ |
| 130 | + public function readClassname(): string { |
| 131 | + if (preg_match('/^[a-z_]\w*/i', $this->next, $matches) === 1) { |
| 132 | + $this->move(strlen($matches[0])); |
| 133 | + |
| 134 | + return $matches[0]; |
| 135 | + } |
| 136 | + |
| 137 | + throw new \Exception('Class name not found'); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Reads extended class name (\Foo\Bar\Baz sequence or regular) from parser conveyor |
| 142 | + * Shifts parser coveyor on extended class name length |
| 143 | + * |
| 144 | + * @return string Extended class name |
| 145 | + */ |
| 146 | + public function readExtendedClassname(): string { |
| 147 | + if (preg_match('/^(\\[a-z_]\w*)+/i', $this->next, $matches) === 1) { |
| 148 | + $this->move(strlen($matches[0])); |
| 149 | + |
| 150 | + return $matches[0]; |
| 151 | + } |
| 152 | + |
| 153 | + return static::readClassname($next); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Reads semicolon from parser conveyor |
| 158 | + * Shifts parser coveyor on one symbol |
| 159 | + */ |
| 160 | + public function readSemicolon() { |
| 161 | + if ($this->readOperator(';')) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + throw new \Exception('Semicolon not found'); |
| 166 | + } |
| 167 | + |
| 168 | + public function __toString() { |
| 169 | + return $this->next; |
| 170 | + } |
| 171 | +} |
0 commit comments