| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Tokenizer for wikitext, using WikiPEG and a |
| 6 |
* separate PEG grammar file |
| 7 |
* (Grammar.pegphp) |
| 8 |
* |
| 9 |
* Use along with a {@link Wt2Html/TreeBuilder/TreeBuilderStage} and the |
| 10 |
* {@link DOMPostProcessor}(s) for HTML output. |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Wikimedia\Parsoid\Wt2Html; |
| 14 |
|
| 15 |
use Generator; |
| 16 |
use Wikimedia\Assert\Assert; |
| 17 |
use Wikimedia\Parsoid\Config\Env; |
| 18 |
use Wikimedia\Parsoid\Tokens\EOFTk; |
| 19 |
use Wikimedia\Parsoid\Tokens\SourceRange; |
| 20 |
use Wikimedia\WikiPEG\SyntaxError; |
| 21 |
|
| 22 |
class PegTokenizer extends PipelineStage { |
| 23 |
private $options; |
| 24 |
private $offsets; |
| 25 |
private ?SyntaxError $lastError = null; |
| 26 |
private ?Grammar $grammar = null; |
| 27 |
|
| 28 |
public function __construct( |
| 29 |
Env $env, array $options = [], string $stageId = "", |
| 30 |
?PipelineStage $prevStage = null |
| 31 |
) { |
| 32 |
parent::__construct( $env, $prevStage ); |
| 33 |
$this->env = $env; |
| 34 |
$this->options = $options; |
| 35 |
$this->offsets = []; |
| 36 |
} |
| 37 |
|
| 38 |
private function initGrammar() { |
| 39 |
if ( !$this->grammar ) { |
| 40 |
$this->grammar = new Grammar; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the constructor options. |
| 46 |
* |
| 47 |
* @internal |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
public function getOptions(): array { |
| 51 |
return $this->options; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Set start and end offsets of the source that generated this DOM. |
| 56 |
* |
| 57 |
* @param SourceRange $so |
| 58 |
*/ |
| 59 |
public function setSourceOffsets( SourceRange $so ): void { |
| 60 |
$this->offsets['startOffset'] = $so->start; |
| 61 |
$this->offsets['endOffset'] = $so->end; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* See PipelineStage::process docs as well. This doc block refines |
| 66 |
* the generic arg types to be specific to this pipeline stage. |
| 67 |
* |
| 68 |
* @param string $input wikitext to tokenize |
| 69 |
* @param array{sol:bool} $opts |
| 70 |
* - atTopLevel: (bool) Whether we are processing the top-level document |
| 71 |
* - sol: (bool) Whether input should be processed in start-of-line context |
| 72 |
* @return array|false The token array, or false for a syntax error |
| 73 |
*/ |
| 74 |
public function process( $input, array $opts ) { |
| 75 |
Assert::invariant( is_string( $input ), "Input should be a string" ); |
| 76 |
return $this->tokenizeSync( $input, $opts ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* The text is tokenized in chunks (one per top-level block) |
| 81 |
* and registered event listeners are called with the chunk |
| 82 |
* to let it get processed further. |
| 83 |
* |
| 84 |
* The main worker. Sets up event emission ('chunk' and 'end' events). |
| 85 |
* Consumers are supposed to register with PegTokenizer before calling |
| 86 |
* process(). |
| 87 |
* |
| 88 |
* @param string $text |
| 89 |
* @param array{sol:bool} $opts |
| 90 |
* - sol (bool) Whether text should be processed in start-of-line context. |
| 91 |
* @return Generator |
| 92 |
*/ |
| 93 |
public function processChunkily( $text, array $opts ): Generator { |
| 94 |
if ( !$this->grammar ) { |
| 95 |
$this->initGrammar(); |
| 96 |
} |
| 97 |
|
| 98 |
Assert::invariant( is_string( $text ), "Input should be a string" ); |
| 99 |
Assert::invariant( isset( $opts['sol'] ), "Sol should be set" ); |
| 100 |
|
| 101 |
// Kick it off! |
| 102 |
$pipelineOffset = $this->offsets['startOffset'] ?? 0; |
| 103 |
$args = [ |
| 104 |
'env' => $this->env, |
| 105 |
'pipelineId' => $this->getPipelineId(), |
| 106 |
'pegTokenizer' => $this, |
| 107 |
'pipelineOffset' => $pipelineOffset, |
| 108 |
'sol' => $opts['sol'], |
| 109 |
'stream' => true, |
| 110 |
'startRule' => 'start_async', |
| 111 |
]; |
| 112 |
|
| 113 |
try { |
| 114 |
// Wrap wikipeg's generator with our own generator |
| 115 |
// to catch exceptions and track time usage. |
| 116 |
// @phan-suppress-next-line PhanTypeInvalidYieldFrom |
| 117 |
yield from $this->grammar->parse( $text, $args ); |
| 118 |
yield [ new EOFTk() ]; |
| 119 |
} catch ( SyntaxError $e ) { |
| 120 |
$this->lastError = $e; |
| 121 |
throw $e; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Tokenize via a rule passed in as an arg. |
| 127 |
* The text is tokenized synchronously in one shot. |
| 128 |
* |
| 129 |
* @param string $text |
| 130 |
* @param array{sol:bool} $args |
| 131 |
* - sol: (bool) Whether input should be processed in start-of-line context. |
| 132 |
* - startRule: (string) which tokenizer rule to tokenize with |
| 133 |
* @return array|false The token array, or false for a syntax error |
| 134 |
*/ |
| 135 |
public function tokenizeSync( string $text, array $args ) { |
| 136 |
if ( !$this->grammar ) { |
| 137 |
$this->initGrammar(); |
| 138 |
} |
| 139 |
Assert::invariant( isset( $args['sol'] ), "Sol should be set" ); |
| 140 |
$args += [ |
| 141 |
'pegTokenizer' => $this, |
| 142 |
'pipelineId' => $this->getPipelineId(), |
| 143 |
'pipelineOffset' => $this->offsets['startOffset'] ?? 0, |
| 144 |
'startRule' => 'start', |
| 145 |
'env' => $this->env |
| 146 |
]; |
| 147 |
|
| 148 |
$start = null; |
| 149 |
$profile = null; |
| 150 |
if ( $this->env->profiling() ) { |
| 151 |
$profile = $this->env->getCurrentProfile(); |
| 152 |
$start = microtime( true ); |
| 153 |
} |
| 154 |
|
| 155 |
try { |
| 156 |
$toks = $this->grammar->parse( $text, $args ); |
| 157 |
} catch ( SyntaxError $e ) { |
| 158 |
$this->lastError = $e; |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
if ( $profile ) { |
| 163 |
$profile->bumpTimeUse( |
| 164 |
'PEG', 1000 * ( microtime( true ) - $start ), 'PEG' ); |
| 165 |
} |
| 166 |
return $toks; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Tokenizes a string as a rule |
| 171 |
* |
| 172 |
* @param string $text The input text |
| 173 |
* @param string $rule The rule name |
| 174 |
* @param bool $sol Start of line flag |
| 175 |
* @return array|false Array of tokens/strings or false on error |
| 176 |
*/ |
| 177 |
public function tokenizeAs( string $text, string $rule, bool $sol ) { |
| 178 |
$args = [ |
| 179 |
'startRule' => $rule, |
| 180 |
'sol' => $sol, |
| 181 |
'pipelineOffset' => 0 |
| 182 |
]; |
| 183 |
return $this->tokenizeSync( $text, $args ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Tokenize a URL. |
| 188 |
* @param string $text |
| 189 |
* @return array|false Array of tokens/strings or false on error |
| 190 |
*/ |
| 191 |
public function tokenizeURL( string $text ) { |
| 192 |
return $this->tokenizeAs( $text, 'url', /* sol */true ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Tokenize table cell attributes. |
| 197 |
* @param string $text |
| 198 |
* @param bool $sol |
| 199 |
* @return array|false Array of tokens/strings or false on error |
| 200 |
*/ |
| 201 |
public function tokenizeTableCellAttributes( string $text, bool $sol ) { |
| 202 |
return $this->tokenizeAs( $text, 'row_syntax_table_args', $sol ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* If a tokenize method returned false, this will return a string describing the error, |
| 207 |
* suitable for use in a log entry. If there has not been any error, returns false. |
| 208 |
* |
| 209 |
* @return string|false |
| 210 |
*/ |
| 211 |
public function getLastErrorLogMessage() { |
| 212 |
if ( $this->lastError ) { |
| 213 |
return "Tokenizer parse error at input location {$this->lastError->location}: " . |
| 214 |
$this->lastError->getMessage(); |
| 215 |
} else { |
| 216 |
return false; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* @inheritDoc |
| 222 |
*/ |
| 223 |
public function resetState( array $opts ): void { |
| 224 |
TokenizerUtils::resetAnnotationIncludeRegex(); |
| 225 |
if ( $this->grammar ) { |
| 226 |
$this->grammar->resetState(); |
| 227 |
} |
| 228 |
parent::resetState( $opts ); |
| 229 |
} |
| 230 |
} |