@@ -12,145 +12,145 @@ |
||
| 12 | 12 | |
| 13 | 13 | abstract class ANode |
| 14 | 14 | { |
| 15 | - use TGetter; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Htsl main object. |
|
| 19 | - * |
|
| 20 | - * @var \Htsl\Htsl |
|
| 21 | - */ |
|
| 22 | - protected $htsl; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The document. |
|
| 26 | - * |
|
| 27 | - * @var \Htsl\Parser\Document |
|
| 28 | - */ |
|
| 29 | - protected $document; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The document. |
|
| 33 | - * |
|
| 34 | - * @var \Htsl\ReadingBuffer\Line |
|
| 35 | - */ |
|
| 36 | - protected $line; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The config. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $config; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Fake contructor of Node. |
|
| 47 | - * |
|
| 48 | - * @param \Htsl\Parser\Document $document |
|
| 49 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 50 | - */ |
|
| 51 | - final public function __construct(Document $document, Line $line) |
|
| 52 | - { |
|
| 53 | - $this->htsl = $document->htsl; |
|
| 54 | - $this->document = $document; |
|
| 55 | - $this->line = $line; |
|
| 56 | - |
|
| 57 | - $this->construct(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Real contructor to be rewrite. |
|
| 62 | - * |
|
| 63 | - * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 64 | - */ |
|
| 65 | - abstract protected function construct():self; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Opening this node, and returning node opener. |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - abstract public function open():string; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Close this node, and returning node closer. |
|
| 76 | - * |
|
| 77 | - * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 78 | - * |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - abstract public function close(Line $closerLine):string; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Getting whether this node contains a scope and scope name. |
|
| 85 | - * |
|
| 86 | - * @return string | null |
|
| 87 | - */ |
|
| 88 | - public function getScope() |
|
| 89 | - { |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Getting the type of this node. |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function getNodeType() |
|
| 98 | - { |
|
| 99 | - static $nodeType; |
|
| 100 | - |
|
| 101 | - return $nodeType ?? $nodeType = $this->nodeType ?? (static function ($className) { |
|
| 102 | - return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_$1', preg_replace('/^(?:\\w+\\\\)*(\\w+)Node$/', '$1', $className))); |
|
| 103 | - })(get_class($this)); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Loading node configuration. |
|
| 108 | - * |
|
| 109 | - * @param string $name |
|
| 110 | - * @param \Htsl\Helper\IConfigProvider $configProvider |
|
| 111 | - * |
|
| 112 | - * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 113 | - */ |
|
| 114 | - protected function loadConfig(string $name, IConfigProvider $configProvider) |
|
| 115 | - { |
|
| 116 | - $config = $configProvider->getConfig($this->nodeType.'_nodes', $name) ?: $configProvider->getConfig($this->nodeType.'_nodes', '*'); |
|
| 117 | - |
|
| 118 | - if (isset($config['multiple'])) { |
|
| 119 | - foreach ($config['multiple'] as $value) { |
|
| 120 | - if ($this->line->pregGet($value['pattern'])) { |
|
| 121 | - $config = $value; |
|
| 122 | - break; |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if (isset($config['in'])) { |
|
| 128 | - $config = (function (array $config) use ($name) { |
|
| 129 | - foreach ($config['in'] as $key => $value) { |
|
| 130 | - if ($this->document->scope && $this->document->scope->scope === $key) { |
|
| 131 | - $value['in_scope'] = $key; |
|
| 132 | - |
|
| 133 | - return $value; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - if (!isset($config['out'])) { |
|
| 137 | - $this->document->throw("The $this->nodeType node $name only use in scope ".implode(',', array_keys($config['in']))); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $config['out']; |
|
| 141 | - })($config); |
|
| 142 | - } elseif (isset($config['only_in']) && (!$this->document->scope || !in_array($this->document->scope->scope, $config['only_in']))) { |
|
| 143 | - $this->document->throw("The $this->nodeType node $name only use in scope ".implode(',', $config['only_in'])); |
|
| 144 | - } elseif (isset($config['not_in']) && (!$this->document->scope || !in_array($this->document->scope->scope, $config['not_in']))) { |
|
| 145 | - $this->document->throw("The $this->nodeType node $name not use in scope ".implode(',', $config['not_in'])); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - if (!is_array($config)) { |
|
| 149 | - $this->document->throw("The $this->nodeType node $name is not supported."); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $this->config = $config; |
|
| 153 | - |
|
| 154 | - return $this; |
|
| 155 | - } |
|
| 15 | + use TGetter; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Htsl main object. |
|
| 19 | + * |
|
| 20 | + * @var \Htsl\Htsl |
|
| 21 | + */ |
|
| 22 | + protected $htsl; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * The document. |
|
| 26 | + * |
|
| 27 | + * @var \Htsl\Parser\Document |
|
| 28 | + */ |
|
| 29 | + protected $document; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The document. |
|
| 33 | + * |
|
| 34 | + * @var \Htsl\ReadingBuffer\Line |
|
| 35 | + */ |
|
| 36 | + protected $line; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The config. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $config; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Fake contructor of Node. |
|
| 47 | + * |
|
| 48 | + * @param \Htsl\Parser\Document $document |
|
| 49 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 50 | + */ |
|
| 51 | + final public function __construct(Document $document, Line $line) |
|
| 52 | + { |
|
| 53 | + $this->htsl = $document->htsl; |
|
| 54 | + $this->document = $document; |
|
| 55 | + $this->line = $line; |
|
| 56 | + |
|
| 57 | + $this->construct(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Real contructor to be rewrite. |
|
| 62 | + * |
|
| 63 | + * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 64 | + */ |
|
| 65 | + abstract protected function construct():self; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Opening this node, and returning node opener. |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + abstract public function open():string; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Close this node, and returning node closer. |
|
| 76 | + * |
|
| 77 | + * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 78 | + * |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + abstract public function close(Line $closerLine):string; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Getting whether this node contains a scope and scope name. |
|
| 85 | + * |
|
| 86 | + * @return string | null |
|
| 87 | + */ |
|
| 88 | + public function getScope() |
|
| 89 | + { |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Getting the type of this node. |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function getNodeType() |
|
| 98 | + { |
|
| 99 | + static $nodeType; |
|
| 100 | + |
|
| 101 | + return $nodeType ?? $nodeType = $this->nodeType ?? (static function ($className) { |
|
| 102 | + return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_$1', preg_replace('/^(?:\\w+\\\\)*(\\w+)Node$/', '$1', $className))); |
|
| 103 | + })(get_class($this)); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Loading node configuration. |
|
| 108 | + * |
|
| 109 | + * @param string $name |
|
| 110 | + * @param \Htsl\Helper\IConfigProvider $configProvider |
|
| 111 | + * |
|
| 112 | + * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 113 | + */ |
|
| 114 | + protected function loadConfig(string $name, IConfigProvider $configProvider) |
|
| 115 | + { |
|
| 116 | + $config = $configProvider->getConfig($this->nodeType.'_nodes', $name) ?: $configProvider->getConfig($this->nodeType.'_nodes', '*'); |
|
| 117 | + |
|
| 118 | + if (isset($config['multiple'])) { |
|
| 119 | + foreach ($config['multiple'] as $value) { |
|
| 120 | + if ($this->line->pregGet($value['pattern'])) { |
|
| 121 | + $config = $value; |
|
| 122 | + break; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if (isset($config['in'])) { |
|
| 128 | + $config = (function (array $config) use ($name) { |
|
| 129 | + foreach ($config['in'] as $key => $value) { |
|
| 130 | + if ($this->document->scope && $this->document->scope->scope === $key) { |
|
| 131 | + $value['in_scope'] = $key; |
|
| 132 | + |
|
| 133 | + return $value; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + if (!isset($config['out'])) { |
|
| 137 | + $this->document->throw("The $this->nodeType node $name only use in scope ".implode(',', array_keys($config['in']))); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $config['out']; |
|
| 141 | + })($config); |
|
| 142 | + } elseif (isset($config['only_in']) && (!$this->document->scope || !in_array($this->document->scope->scope, $config['only_in']))) { |
|
| 143 | + $this->document->throw("The $this->nodeType node $name only use in scope ".implode(',', $config['only_in'])); |
|
| 144 | + } elseif (isset($config['not_in']) && (!$this->document->scope || !in_array($this->document->scope->scope, $config['not_in']))) { |
|
| 145 | + $this->document->throw("The $this->nodeType node $name not use in scope ".implode(',', $config['not_in'])); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + if (!is_array($config)) { |
|
| 149 | + $this->document->throw("The $this->nodeType node $name is not supported."); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $this->config = $config; |
|
| 153 | + |
|
| 154 | + return $this; |
|
| 155 | + } |
|
| 156 | 156 | } |
@@ -9,46 +9,46 @@ |
||
| 9 | 9 | |
| 10 | 10 | class CommentNode extends ANode |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Whether the comment is html comment. |
|
| 14 | - * |
|
| 15 | - * @var bool |
|
| 16 | - */ |
|
| 17 | - private $htmlComment; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Real contructor. |
|
| 21 | - * |
|
| 22 | - * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 23 | - */ |
|
| 24 | - protected function construct():parent |
|
| 25 | - { |
|
| 26 | - $this->htmlComment = '!' !== $this->line->getChar(1); |
|
| 27 | - |
|
| 28 | - return $this; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Opening this node, and returning node opener. |
|
| 33 | - * |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - public function open():string |
|
| 37 | - { |
|
| 38 | - return $this->htmlComment ? |
|
| 39 | - '<!--'.str_replace('-->', '--'.chr(0xC).'>', substr($this->line->getContent(), 1)) : |
|
| 40 | - '<?php /* '.substr($this->line->getContent(), 2); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Close this node, and returning node closer. |
|
| 45 | - * |
|
| 46 | - * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 47 | - * |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function close(Line $closerLine):string |
|
| 51 | - { |
|
| 52 | - return $this->htmlComment ? '-->' : ' */ ?>'; |
|
| 53 | - } |
|
| 12 | + /** |
|
| 13 | + * Whether the comment is html comment. |
|
| 14 | + * |
|
| 15 | + * @var bool |
|
| 16 | + */ |
|
| 17 | + private $htmlComment; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Real contructor. |
|
| 21 | + * |
|
| 22 | + * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 23 | + */ |
|
| 24 | + protected function construct():parent |
|
| 25 | + { |
|
| 26 | + $this->htmlComment = '!' !== $this->line->getChar(1); |
|
| 27 | + |
|
| 28 | + return $this; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Opening this node, and returning node opener. |
|
| 33 | + * |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + public function open():string |
|
| 37 | + { |
|
| 38 | + return $this->htmlComment ? |
|
| 39 | + '<!--'.str_replace('-->', '--'.chr(0xC).'>', substr($this->line->getContent(), 1)) : |
|
| 40 | + '<?php /* '.substr($this->line->getContent(), 2); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Close this node, and returning node closer. |
|
| 45 | + * |
|
| 46 | + * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 47 | + * |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function close(Line $closerLine):string |
|
| 51 | + { |
|
| 52 | + return $this->htmlComment ? '-->' : ' */ ?>'; |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -10,48 +10,48 @@ |
||
| 10 | 10 | |
| 11 | 11 | class SectionNode extends ANode |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The name of the section. |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - private $name; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Real constructor. |
|
| 22 | - * |
|
| 23 | - * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 24 | - */ |
|
| 25 | - protected function construct():parent |
|
| 26 | - { |
|
| 27 | - $this->name = $this->line->pregGet('/(?<=\( ).*(?= \))/'); |
|
| 28 | - |
|
| 29 | - return $this; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Opening this node, and returning node opener. |
|
| 34 | - * |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function open():string |
|
| 38 | - { |
|
| 39 | - $this->document->setSection(new Section($this->name)); |
|
| 40 | - |
|
| 41 | - return ''; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Close this node, and returning node closer. |
|
| 46 | - * |
|
| 47 | - * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 48 | - * |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - public function close(Line $closerLine):string |
|
| 52 | - { |
|
| 53 | - $this->document->setSection(null); |
|
| 54 | - |
|
| 55 | - return ''; |
|
| 56 | - } |
|
| 13 | + /** |
|
| 14 | + * The name of the section. |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + private $name; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Real constructor. |
|
| 22 | + * |
|
| 23 | + * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 24 | + */ |
|
| 25 | + protected function construct():parent |
|
| 26 | + { |
|
| 27 | + $this->name = $this->line->pregGet('/(?<=\( ).*(?= \))/'); |
|
| 28 | + |
|
| 29 | + return $this; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Opening this node, and returning node opener. |
|
| 34 | + * |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function open():string |
|
| 38 | + { |
|
| 39 | + $this->document->setSection(new Section($this->name)); |
|
| 40 | + |
|
| 41 | + return ''; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Close this node, and returning node closer. |
|
| 46 | + * |
|
| 47 | + * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 48 | + * |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + public function close(Line $closerLine):string |
|
| 52 | + { |
|
| 53 | + $this->document->setSection(null); |
|
| 54 | + |
|
| 55 | + return ''; |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -9,35 +9,35 @@ |
||
| 9 | 9 | |
| 10 | 10 | class StringNode extends ANode |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Real constructor. |
|
| 14 | - * |
|
| 15 | - * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 16 | - */ |
|
| 17 | - protected function construct():parent |
|
| 18 | - { |
|
| 19 | - return $this; |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * Real constructor. |
|
| 14 | + * |
|
| 15 | + * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 16 | + */ |
|
| 17 | + protected function construct():parent |
|
| 18 | + { |
|
| 19 | + return $this; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Opening this node, and returning node opener. |
|
| 24 | - * |
|
| 25 | - * @return string |
|
| 26 | - */ |
|
| 27 | - public function open():string |
|
| 28 | - { |
|
| 29 | - return ''; |
|
| 30 | - } |
|
| 22 | + /** |
|
| 23 | + * Opening this node, and returning node opener. |
|
| 24 | + * |
|
| 25 | + * @return string |
|
| 26 | + */ |
|
| 27 | + public function open():string |
|
| 28 | + { |
|
| 29 | + return ''; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Close this node, and returning node closer. |
|
| 34 | - * |
|
| 35 | - * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 36 | - * |
|
| 37 | - * @return string |
|
| 38 | - */ |
|
| 39 | - public function close(Line $closerLine):string |
|
| 40 | - { |
|
| 41 | - return ''; |
|
| 42 | - } |
|
| 32 | + /** |
|
| 33 | + * Close this node, and returning node closer. |
|
| 34 | + * |
|
| 35 | + * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 36 | + * |
|
| 37 | + * @return string |
|
| 38 | + */ |
|
| 39 | + public function close(Line $closerLine):string |
|
| 40 | + { |
|
| 41 | + return ''; |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -10,122 +10,122 @@ |
||
| 10 | 10 | |
| 11 | 11 | class ControlNode extends ANode |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The name of the Htsl.php control structure. |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - private $name; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The name of the complied(PHP) control structure. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - private $structureName; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Parameters. |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - private $param; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Real constructor. |
|
| 36 | - * |
|
| 37 | - * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 38 | - */ |
|
| 39 | - protected function construct():parent |
|
| 40 | - { |
|
| 41 | - $name = $this->line->pregGet('/(?<=^~)[\w-]*/'); |
|
| 42 | - $this->name = $name; |
|
| 43 | - |
|
| 44 | - $this->loadConfig($name, $this->htsl); |
|
| 45 | - |
|
| 46 | - $this->param = $this->line->pregGet('/^~[\w-]*\( (.*) \)/', 1); |
|
| 47 | - |
|
| 48 | - $this->structureName = $this->config['name'] ?? $name; |
|
| 49 | - |
|
| 50 | - $this->id = strtoupper(uniqid()); |
|
| 51 | - |
|
| 52 | - return $this; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Opening this control node, and returning node opener. |
|
| 57 | - * |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function open():string |
|
| 61 | - { |
|
| 62 | - return $this->withParam($this->config['opener']); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Getting whether this node contains a scope and scope name. |
|
| 67 | - * |
|
| 68 | - * @return string | null |
|
| 69 | - */ |
|
| 70 | - public function getScope() |
|
| 71 | - { |
|
| 72 | - return $this->config['scope'] ?? null; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Close this control node, and returning node closer. |
|
| 77 | - * |
|
| 78 | - * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 79 | - * |
|
| 80 | - * @return string |
|
| 81 | - */ |
|
| 82 | - public function close(Line $closerLine):string |
|
| 83 | - { |
|
| 84 | - if (isset($this->config['close_by']) && $closerLine->indentLevel == $this->line->indentLevel) { |
|
| 85 | - foreach ($this->config['close_by'] as $key => $value) { |
|
| 86 | - if ($closerLine->pregMatch($key)) { |
|
| 87 | - return $this->withParam($value); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - if (isset($this->config['closer'])) { |
|
| 93 | - return $this->withParam($this->config['closer']); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return ''; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Parse opener or closer with parameters. |
|
| 101 | - * |
|
| 102 | - * @param string $input Opener or Closer |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - private function withParam(string $input) |
|
| 107 | - { |
|
| 108 | - return str_replace('$_FLAG_$', "__HTSL_CTRL_FLAG_{$this->id}__", preg_replace_callback('/(?<!%)%s((?:\\/.+?(?<!\\\\)\\/.+?(?<!\\\\)\\/)+)?/', function (array $matches) { |
|
| 109 | - $param = $this->param; |
|
| 110 | - |
|
| 111 | - if (isset($matches[1])) { |
|
| 112 | - array_map(...[ |
|
| 113 | - function ($replacer) use (&$param) { |
|
| 114 | - list($pattern, $replacement) = preg_split('/(?<!\\\\)\\//', $replacer); |
|
| 115 | - $param = preg_replace(...[ |
|
| 116 | - "/$pattern/", |
|
| 117 | - preg_replace('/^\\\\_$/', '', $replacement), |
|
| 118 | - $param, |
|
| 119 | - ]); |
|
| 120 | - }, |
|
| 121 | - preg_split( |
|
| 122 | - '/(?<!\\\\)\\/\\//', |
|
| 123 | - trim($matches[1], '/') |
|
| 124 | - ), |
|
| 125 | - ]); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - return $param; |
|
| 129 | - }, $input)); |
|
| 130 | - } |
|
| 13 | + /** |
|
| 14 | + * The name of the Htsl.php control structure. |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + private $name; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The name of the complied(PHP) control structure. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + private $structureName; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Parameters. |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + private $param; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Real constructor. |
|
| 36 | + * |
|
| 37 | + * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 38 | + */ |
|
| 39 | + protected function construct():parent |
|
| 40 | + { |
|
| 41 | + $name = $this->line->pregGet('/(?<=^~)[\w-]*/'); |
|
| 42 | + $this->name = $name; |
|
| 43 | + |
|
| 44 | + $this->loadConfig($name, $this->htsl); |
|
| 45 | + |
|
| 46 | + $this->param = $this->line->pregGet('/^~[\w-]*\( (.*) \)/', 1); |
|
| 47 | + |
|
| 48 | + $this->structureName = $this->config['name'] ?? $name; |
|
| 49 | + |
|
| 50 | + $this->id = strtoupper(uniqid()); |
|
| 51 | + |
|
| 52 | + return $this; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Opening this control node, and returning node opener. |
|
| 57 | + * |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function open():string |
|
| 61 | + { |
|
| 62 | + return $this->withParam($this->config['opener']); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Getting whether this node contains a scope and scope name. |
|
| 67 | + * |
|
| 68 | + * @return string | null |
|
| 69 | + */ |
|
| 70 | + public function getScope() |
|
| 71 | + { |
|
| 72 | + return $this->config['scope'] ?? null; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Close this control node, and returning node closer. |
|
| 77 | + * |
|
| 78 | + * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 79 | + * |
|
| 80 | + * @return string |
|
| 81 | + */ |
|
| 82 | + public function close(Line $closerLine):string |
|
| 83 | + { |
|
| 84 | + if (isset($this->config['close_by']) && $closerLine->indentLevel == $this->line->indentLevel) { |
|
| 85 | + foreach ($this->config['close_by'] as $key => $value) { |
|
| 86 | + if ($closerLine->pregMatch($key)) { |
|
| 87 | + return $this->withParam($value); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + if (isset($this->config['closer'])) { |
|
| 93 | + return $this->withParam($this->config['closer']); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return ''; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Parse opener or closer with parameters. |
|
| 101 | + * |
|
| 102 | + * @param string $input Opener or Closer |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + private function withParam(string $input) |
|
| 107 | + { |
|
| 108 | + return str_replace('$_FLAG_$', "__HTSL_CTRL_FLAG_{$this->id}__", preg_replace_callback('/(?<!%)%s((?:\\/.+?(?<!\\\\)\\/.+?(?<!\\\\)\\/)+)?/', function (array $matches) { |
|
| 109 | + $param = $this->param; |
|
| 110 | + |
|
| 111 | + if (isset($matches[1])) { |
|
| 112 | + array_map(...[ |
|
| 113 | + function ($replacer) use (&$param) { |
|
| 114 | + list($pattern, $replacement) = preg_split('/(?<!\\\\)\\//', $replacer); |
|
| 115 | + $param = preg_replace(...[ |
|
| 116 | + "/$pattern/", |
|
| 117 | + preg_replace('/^\\\\_$/', '', $replacement), |
|
| 118 | + $param, |
|
| 119 | + ]); |
|
| 120 | + }, |
|
| 121 | + preg_split( |
|
| 122 | + '/(?<!\\\\)\\/\\//', |
|
| 123 | + trim($matches[1], '/') |
|
| 124 | + ), |
|
| 125 | + ]); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + return $param; |
|
| 129 | + }, $input)); |
|
| 130 | + } |
|
| 131 | 131 | } |
@@ -8,63 +8,63 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Section |
| 10 | 10 | { |
| 11 | - use TGetter; |
|
| 11 | + use TGetter; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Section name. |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - private $name; |
|
| 13 | + /** |
|
| 14 | + * Section name. |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + private $name; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Content. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - private $content = ''; |
|
| 20 | + /** |
|
| 21 | + * Content. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + private $content = ''; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Setting name and constructing instance of Section. |
|
| 29 | - * |
|
| 30 | - * @param string | null $name |
|
| 31 | - */ |
|
| 32 | - public function __construct(string $name = null) |
|
| 33 | - { |
|
| 34 | - $this->name = $name; |
|
| 35 | - } |
|
| 27 | + /** |
|
| 28 | + * Setting name and constructing instance of Section. |
|
| 29 | + * |
|
| 30 | + * @param string | null $name |
|
| 31 | + */ |
|
| 32 | + public function __construct(string $name = null) |
|
| 33 | + { |
|
| 34 | + $this->name = $name; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Appending content to this section. |
|
| 39 | - * |
|
| 40 | - * @param string $content |
|
| 41 | - * |
|
| 42 | - * @return self |
|
| 43 | - */ |
|
| 44 | - public function append(string $content):self |
|
| 45 | - { |
|
| 46 | - $this->content .= $content; |
|
| 37 | + /** |
|
| 38 | + * Appending content to this section. |
|
| 39 | + * |
|
| 40 | + * @param string $content |
|
| 41 | + * |
|
| 42 | + * @return self |
|
| 43 | + */ |
|
| 44 | + public function append(string $content):self |
|
| 45 | + { |
|
| 46 | + $this->content .= $content; |
|
| 47 | 47 | |
| 48 | - return $this; |
|
| 49 | - } |
|
| 48 | + return $this; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Getting content of this section. |
|
| 53 | - * |
|
| 54 | - * @return string |
|
| 55 | - */ |
|
| 56 | - public function getContent():string |
|
| 57 | - { |
|
| 58 | - return $this->content; |
|
| 59 | - } |
|
| 51 | + /** |
|
| 52 | + * Getting content of this section. |
|
| 53 | + * |
|
| 54 | + * @return string |
|
| 55 | + */ |
|
| 56 | + public function getContent():string |
|
| 57 | + { |
|
| 58 | + return $this->content; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Getting name of this section. |
|
| 63 | - * |
|
| 64 | - * @return string | null |
|
| 65 | - */ |
|
| 66 | - public function getName()//:string|null |
|
| 67 | - { |
|
| 68 | - return $this->name; |
|
| 69 | - } |
|
| 61 | + /** |
|
| 62 | + * Getting name of this section. |
|
| 63 | + * |
|
| 64 | + * @return string | null |
|
| 65 | + */ |
|
| 66 | + public function getName()//:string|null |
|
| 67 | + { |
|
| 68 | + return $this->name; |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -8,65 +8,65 @@ |
||
| 8 | 8 | |
| 9 | 9 | class FileBuffer extends Contracts\ABuffer |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * File handle. |
|
| 13 | - * |
|
| 14 | - * @var resource |
|
| 15 | - */ |
|
| 16 | - private $handle; |
|
| 11 | + /** |
|
| 12 | + * File handle. |
|
| 13 | + * |
|
| 14 | + * @var resource |
|
| 15 | + */ |
|
| 16 | + private $handle; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructing a file buffer reading HTSL content from file system. |
|
| 20 | - * |
|
| 21 | - * @param Htsl $htsl Main Htsl object |
|
| 22 | - * @param string $filePath |
|
| 23 | - */ |
|
| 24 | - public function __construct(Htsl $htsl, string $filePath) |
|
| 25 | - { |
|
| 26 | - substr($filePath, -5) === '.htsl' or $filePath .= '.htsl'; |
|
| 18 | + /** |
|
| 19 | + * Constructing a file buffer reading HTSL content from file system. |
|
| 20 | + * |
|
| 21 | + * @param Htsl $htsl Main Htsl object |
|
| 22 | + * @param string $filePath |
|
| 23 | + */ |
|
| 24 | + public function __construct(Htsl $htsl, string $filePath) |
|
| 25 | + { |
|
| 26 | + substr($filePath, -5) === '.htsl' or $filePath .= '.htsl'; |
|
| 27 | 27 | |
| 28 | - if (!file_exists($filePath) || !is_file($filePath)) { |
|
| 29 | - throw new \Exception("File $filePath not exists.", 1); |
|
| 30 | - } |
|
| 28 | + if (!file_exists($filePath) || !is_file($filePath)) { |
|
| 29 | + throw new \Exception("File $filePath not exists.", 1); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - $this->filePath = $filePath; |
|
| 32 | + $this->filePath = $filePath; |
|
| 33 | 33 | |
| 34 | - $this->handle = fopen($filePath, 'r'); |
|
| 34 | + $this->handle = fopen($filePath, 'r'); |
|
| 35 | 35 | |
| 36 | - parent::__construct($htsl); |
|
| 37 | - } |
|
| 36 | + parent::__construct($htsl); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Destructor. |
|
| 41 | - */ |
|
| 42 | - public function __destruct() |
|
| 43 | - { |
|
| 44 | - fclose($this->handle); |
|
| 45 | - } |
|
| 39 | + /** |
|
| 40 | + * Destructor. |
|
| 41 | + */ |
|
| 42 | + public function __destruct() |
|
| 43 | + { |
|
| 44 | + fclose($this->handle); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Getting first line or next line. |
|
| 49 | - * |
|
| 50 | - * @return \Htsl\ReadingBuffer\Line |
|
| 51 | - */ |
|
| 52 | - public function getLine():Line |
|
| 53 | - { |
|
| 54 | - while ("\n" === $content = fgets($this->handle)); |
|
| 47 | + /** |
|
| 48 | + * Getting first line or next line. |
|
| 49 | + * |
|
| 50 | + * @return \Htsl\ReadingBuffer\Line |
|
| 51 | + */ |
|
| 52 | + public function getLine():Line |
|
| 53 | + { |
|
| 54 | + while ("\n" === $content = fgets($this->handle)); |
|
| 55 | 55 | |
| 56 | - return new Line($content); |
|
| 57 | - } |
|
| 56 | + return new Line($content); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Getting another file reference file of this buffer. |
|
| 61 | - * |
|
| 62 | - * @param string $filePath |
|
| 63 | - * |
|
| 64 | - * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 65 | - */ |
|
| 66 | - public function goSide($filePath):parent |
|
| 67 | - { |
|
| 68 | - $filePath = $this->htsl->getFilePath($filePath, dirname($this->filePath)); |
|
| 59 | + /** |
|
| 60 | + * Getting another file reference file of this buffer. |
|
| 61 | + * |
|
| 62 | + * @param string $filePath |
|
| 63 | + * |
|
| 64 | + * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 65 | + */ |
|
| 66 | + public function goSide($filePath):parent |
|
| 67 | + { |
|
| 68 | + $filePath = $this->htsl->getFilePath($filePath, dirname($this->filePath)); |
|
| 69 | 69 | |
| 70 | - return new static($this->htsl, $filePath); |
|
| 71 | - } |
|
| 70 | + return new static($this->htsl, $filePath); |
|
| 71 | + } |
|
| 72 | 72 | } |
@@ -9,50 +9,50 @@ |
||
| 9 | 9 | |
| 10 | 10 | abstract class ABuffer |
| 11 | 11 | { |
| 12 | - use TGetter; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * The htsl server. |
|
| 16 | - * |
|
| 17 | - * @var \Htsl\Htsl |
|
| 18 | - */ |
|
| 19 | - protected $htsl; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The file name. |
|
| 23 | - * |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $fileName = ''; |
|
| 27 | - |
|
| 28 | - public function __construct(Htsl $htsl) |
|
| 29 | - { |
|
| 30 | - $this->htsl = $htsl; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Get a line of the document. |
|
| 35 | - * |
|
| 36 | - * @return \Htsl\ReadingBuffer\Line |
|
| 37 | - */ |
|
| 38 | - abstract public function getLine(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Get a line of the document. |
|
| 42 | - * |
|
| 43 | - * @return \Htsl\ReadingBuffer\Line |
|
| 44 | - */ |
|
| 45 | - public function getFileName():string |
|
| 46 | - { |
|
| 47 | - return $this->fileName; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Getting another file reference file of this buffer. |
|
| 52 | - * |
|
| 53 | - * @param string $filePath |
|
| 54 | - * |
|
| 55 | - * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 56 | - */ |
|
| 57 | - abstract public function goSide($fileName):self; |
|
| 12 | + use TGetter; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * The htsl server. |
|
| 16 | + * |
|
| 17 | + * @var \Htsl\Htsl |
|
| 18 | + */ |
|
| 19 | + protected $htsl; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The file name. |
|
| 23 | + * |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $fileName = ''; |
|
| 27 | + |
|
| 28 | + public function __construct(Htsl $htsl) |
|
| 29 | + { |
|
| 30 | + $this->htsl = $htsl; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Get a line of the document. |
|
| 35 | + * |
|
| 36 | + * @return \Htsl\ReadingBuffer\Line |
|
| 37 | + */ |
|
| 38 | + abstract public function getLine(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Get a line of the document. |
|
| 42 | + * |
|
| 43 | + * @return \Htsl\ReadingBuffer\Line |
|
| 44 | + */ |
|
| 45 | + public function getFileName():string |
|
| 46 | + { |
|
| 47 | + return $this->fileName; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Getting another file reference file of this buffer. |
|
| 52 | + * |
|
| 53 | + * @param string $filePath |
|
| 54 | + * |
|
| 55 | + * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 56 | + */ |
|
| 57 | + abstract public function goSide($fileName):self; |
|
| 58 | 58 | } |
@@ -8,190 +8,190 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Line |
| 10 | 10 | { |
| 11 | - use TGetter; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * Line content. |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - private $content; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Whether this line is last line. |
|
| 22 | - * |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - private $isLast = false; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param string | bool $content String content for normal line and false for last line. |
|
| 31 | - */ |
|
| 32 | - public function __construct(/*string|bool*/$content) |
|
| 33 | - { |
|
| 34 | - false === $content and $this->isLast = true; |
|
| 35 | - |
|
| 36 | - $this->content = rtrim($content, "\n"); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Getting content without indentation. |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function getContent():string |
|
| 45 | - { |
|
| 46 | - return ltrim($this->content, "\t"); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Getting full content. |
|
| 51 | - * |
|
| 52 | - * @return string |
|
| 53 | - */ |
|
| 54 | - public function getFullContent():string |
|
| 55 | - { |
|
| 56 | - return $this->content; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Getting a section of content, like call substr(). |
|
| 61 | - * |
|
| 62 | - * @param int $start |
|
| 63 | - * @param int ...$lengths |
|
| 64 | - * |
|
| 65 | - * @return string |
|
| 66 | - */ |
|
| 67 | - public function slice(int $start = 0, int ...$lengths):string |
|
| 68 | - { |
|
| 69 | - return substr($this->getContent(), $start, ...array_slice($lengths, 0, 1)); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Getting single character from content. |
|
| 74 | - * |
|
| 75 | - * @param int $offset |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function getChar(int $offset):string |
|
| 80 | - { |
|
| 81 | - return substr($this->getcontent(), $offset, 1); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Matching a preg pattern and return whether matches. |
|
| 86 | - * |
|
| 87 | - * @param string $pattern |
|
| 88 | - * |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - public function pregMatch(string $pattern):bool |
|
| 92 | - { |
|
| 93 | - return (bool) preg_match($pattern, ltrim($this->content, "\t")); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Matching a preg pattern and return the all or one of groups of matchment. |
|
| 98 | - * |
|
| 99 | - * @param string $pattern |
|
| 100 | - * @param int | string $match Group index or name |
|
| 101 | - * |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - public function pregGet(string $pattern, /*int|string*/$match = 0):string |
|
| 105 | - { |
|
| 106 | - preg_match($pattern, ltrim($this->content, "\t"), $matches); |
|
| 107 | - |
|
| 108 | - return $matches[$match] ?? ''; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Multiple matching a preg pattern and map result with a callback. |
|
| 113 | - * |
|
| 114 | - * @param string $pattern |
|
| 115 | - * @param callable $callback |
|
| 116 | - * |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - public function pregMap(string $pattern, callable $callback):array |
|
| 120 | - { |
|
| 121 | - preg_match_all($pattern, ltrim($this->content, "\t"), $matches); |
|
| 122 | - |
|
| 123 | - return array_map($callback, ...$matches); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Getting the indent level of this line, the number of starting tab characters. |
|
| 128 | - * |
|
| 129 | - * @return int |
|
| 130 | - */ |
|
| 131 | - public function getIndentLevel():int |
|
| 132 | - { |
|
| 133 | - // return (static function( $a ):int{$i=0;while($a{$i}==="\t")++$i;return $i;})($this->content); |
|
| 134 | - |
|
| 135 | - return strlen($this->content) - strlen(ltrim($this->content, "\t")); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Converting this object to string, returning content without indentation. |
|
| 140 | - * |
|
| 141 | - * @return string |
|
| 142 | - */ |
|
| 143 | - public function __toString():string |
|
| 144 | - { |
|
| 145 | - return $this->getContent(); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Wether this line is empty. |
|
| 150 | - * |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - public function isEmpty():bool |
|
| 154 | - { |
|
| 155 | - return !strlen($this->content); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Whether this line is last line. |
|
| 160 | - * |
|
| 161 | - * @return bool |
|
| 162 | - */ |
|
| 163 | - public function isLast():bool |
|
| 164 | - { |
|
| 165 | - return $this->isLast; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Whether this line is last line. |
|
| 170 | - * |
|
| 171 | - * @return bool |
|
| 172 | - */ |
|
| 173 | - public function noMore():bool |
|
| 174 | - { |
|
| 175 | - return $this->isLast; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Whether next line exists. |
|
| 180 | - * |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - public function hasMore():bool |
|
| 184 | - { |
|
| 185 | - return !$this->isLast; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Getting line with sub level indent( tab space tab ). |
|
| 190 | - * |
|
| 191 | - * @return \Htsl\ReadingBuffer\Line |
|
| 192 | - */ |
|
| 193 | - public function getSubIndentLine():self |
|
| 194 | - { |
|
| 195 | - return new static(ltrim($this->getContent(), ' ')); |
|
| 196 | - } |
|
| 11 | + use TGetter; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * Line content. |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + private $content; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Whether this line is last line. |
|
| 22 | + * |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + private $isLast = false; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param string | bool $content String content for normal line and false for last line. |
|
| 31 | + */ |
|
| 32 | + public function __construct(/*string|bool*/$content) |
|
| 33 | + { |
|
| 34 | + false === $content and $this->isLast = true; |
|
| 35 | + |
|
| 36 | + $this->content = rtrim($content, "\n"); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Getting content without indentation. |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function getContent():string |
|
| 45 | + { |
|
| 46 | + return ltrim($this->content, "\t"); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Getting full content. |
|
| 51 | + * |
|
| 52 | + * @return string |
|
| 53 | + */ |
|
| 54 | + public function getFullContent():string |
|
| 55 | + { |
|
| 56 | + return $this->content; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Getting a section of content, like call substr(). |
|
| 61 | + * |
|
| 62 | + * @param int $start |
|
| 63 | + * @param int ...$lengths |
|
| 64 | + * |
|
| 65 | + * @return string |
|
| 66 | + */ |
|
| 67 | + public function slice(int $start = 0, int ...$lengths):string |
|
| 68 | + { |
|
| 69 | + return substr($this->getContent(), $start, ...array_slice($lengths, 0, 1)); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Getting single character from content. |
|
| 74 | + * |
|
| 75 | + * @param int $offset |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function getChar(int $offset):string |
|
| 80 | + { |
|
| 81 | + return substr($this->getcontent(), $offset, 1); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Matching a preg pattern and return whether matches. |
|
| 86 | + * |
|
| 87 | + * @param string $pattern |
|
| 88 | + * |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + public function pregMatch(string $pattern):bool |
|
| 92 | + { |
|
| 93 | + return (bool) preg_match($pattern, ltrim($this->content, "\t")); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Matching a preg pattern and return the all or one of groups of matchment. |
|
| 98 | + * |
|
| 99 | + * @param string $pattern |
|
| 100 | + * @param int | string $match Group index or name |
|
| 101 | + * |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + public function pregGet(string $pattern, /*int|string*/$match = 0):string |
|
| 105 | + { |
|
| 106 | + preg_match($pattern, ltrim($this->content, "\t"), $matches); |
|
| 107 | + |
|
| 108 | + return $matches[$match] ?? ''; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Multiple matching a preg pattern and map result with a callback. |
|
| 113 | + * |
|
| 114 | + * @param string $pattern |
|
| 115 | + * @param callable $callback |
|
| 116 | + * |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + public function pregMap(string $pattern, callable $callback):array |
|
| 120 | + { |
|
| 121 | + preg_match_all($pattern, ltrim($this->content, "\t"), $matches); |
|
| 122 | + |
|
| 123 | + return array_map($callback, ...$matches); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Getting the indent level of this line, the number of starting tab characters. |
|
| 128 | + * |
|
| 129 | + * @return int |
|
| 130 | + */ |
|
| 131 | + public function getIndentLevel():int |
|
| 132 | + { |
|
| 133 | + // return (static function( $a ):int{$i=0;while($a{$i}==="\t")++$i;return $i;})($this->content); |
|
| 134 | + |
|
| 135 | + return strlen($this->content) - strlen(ltrim($this->content, "\t")); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Converting this object to string, returning content without indentation. |
|
| 140 | + * |
|
| 141 | + * @return string |
|
| 142 | + */ |
|
| 143 | + public function __toString():string |
|
| 144 | + { |
|
| 145 | + return $this->getContent(); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Wether this line is empty. |
|
| 150 | + * |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + public function isEmpty():bool |
|
| 154 | + { |
|
| 155 | + return !strlen($this->content); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Whether this line is last line. |
|
| 160 | + * |
|
| 161 | + * @return bool |
|
| 162 | + */ |
|
| 163 | + public function isLast():bool |
|
| 164 | + { |
|
| 165 | + return $this->isLast; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Whether this line is last line. |
|
| 170 | + * |
|
| 171 | + * @return bool |
|
| 172 | + */ |
|
| 173 | + public function noMore():bool |
|
| 174 | + { |
|
| 175 | + return $this->isLast; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Whether next line exists. |
|
| 180 | + * |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + public function hasMore():bool |
|
| 184 | + { |
|
| 185 | + return !$this->isLast; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Getting line with sub level indent( tab space tab ). |
|
| 190 | + * |
|
| 191 | + * @return \Htsl\ReadingBuffer\Line |
|
| 192 | + */ |
|
| 193 | + public function getSubIndentLine():self |
|
| 194 | + { |
|
| 195 | + return new static(ltrim($this->getContent(), ' ')); |
|
| 196 | + } |
|
| 197 | 197 | } |