| 1 |
/** |
| 2 |
* PEG.js grammar for reading MediaWiki parser tests files |
| 3 |
* 2011-07-20 Brion Vibber <[email protected]> |
| 4 |
*/ |
| 5 |
{ |
| 6 |
/* File-scope initializer */ |
| 7 |
namespace Wikimedia\Parsoid\ParserTests; |
| 8 |
|
| 9 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 10 |
} |
| 11 |
{ |
| 12 |
/** @var string */ |
| 13 |
private $filename = ''; |
| 14 |
/** @var int */ |
| 15 |
private $lineNum = 1; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param string $filename |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
public static function load( string $filename ) { |
| 22 |
$g = new Grammar(); |
| 23 |
$g->filename = $filename; |
| 24 |
$contents = file_get_contents( $filename ) ?: ''; |
| 25 |
if ( substr( $contents, -1 ) !== "\n" ) { |
| 26 |
# ensure that the file is terminated with a newline |
| 27 |
# to match `end_section` rule (and other uses of `eol`) |
| 28 |
$contents .= "\n"; |
| 29 |
} |
| 30 |
return $g->parse( $contents ); |
| 31 |
} |
| 32 |
|
| 33 |
private function addLines( int $lineStart, array $item ) { |
| 34 |
$item['filename'] = $this->filename; |
| 35 |
$item['lineNumStart'] = $lineStart; |
| 36 |
$item['lineNumEnd'] = $this->lineNum; |
| 37 |
return $item; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
testfile = |
| 42 |
comment_or_blank_line* |
| 43 |
format? |
| 44 |
comment_or_blank_line* |
| 45 |
testfile_options? |
| 46 |
lined_chunk+ |
| 47 |
|
| 48 |
testfile_options = |
| 49 |
l:("" { return $this->lineNum; }) |
| 50 |
sec:option_section end_section { |
| 51 |
return $this->addLines( $l, $sec ); |
| 52 |
} |
| 53 |
|
| 54 |
/* Line number bookkeeping. |
| 55 |
* Be careful about backtracking after you successfully match this production. |
| 56 |
*/ |
| 57 |
eol = nl:"\n" { $this->lineNum++; return $nl; } |
| 58 |
|
| 59 |
lined_chunk = l:("" { return $this->lineNum; }) c:chunk { |
| 60 |
return $this->addLines($l, $c); |
| 61 |
} |
| 62 |
|
| 63 |
whitespace = [ \t]+ |
| 64 |
|
| 65 |
ws = whitespace |
| 66 |
|
| 67 |
rest_of_line = c:([^\n]*) eol |
| 68 |
{ |
| 69 |
return implode($c); |
| 70 |
} |
| 71 |
|
| 72 |
line = (!"!!") line:rest_of_line |
| 73 |
{ |
| 74 |
return $line; |
| 75 |
} |
| 76 |
|
| 77 |
text = lines:line* |
| 78 |
{ |
| 79 |
return implode("\n", $lines); |
| 80 |
} |
| 81 |
|
| 82 |
chunk = |
| 83 |
comment_or_blank_line |
| 84 |
/ article |
| 85 |
/ test |
| 86 |
/ hooks |
| 87 |
/ functionhooks |
| 88 |
|
| 89 |
/* Final fallback production is a catch-all, since some ancient |
| 90 |
* parserTest files have garbage text between tests and in the old |
| 91 |
* hand-coded parser test parser this was just ignored as a comment. |
| 92 |
* We'll go ahead and parse it, then emit a warning in TestFileReader. |
| 93 |
*/ |
| 94 |
/ l: line { return [ 'type' => 'line', 'text' => $l ]; } |
| 95 |
|
| 96 |
format = |
| 97 |
l:("" { return $this->lineNum; }) |
| 98 |
"!!" ws? version_keyword ws+ v:$([0-9]+) rest_of_line { |
| 99 |
return $this->addLines( $l, [ 'type' => 'version', 'text' => $v ] ); |
| 100 |
} |
| 101 |
|
| 102 |
version_keyword = 'version'i |
| 103 |
|
| 104 |
comment = |
| 105 |
"#" text:rest_of_line { return [ 'type' => 'comment', 'text' => $text ]; } |
| 106 |
|
| 107 |
comment_or_blank_line = |
| 108 |
comment |
| 109 |
/ ws? nl:eol { return [ 'type' => 'line', 'text' => $nl ]; } |
| 110 |
|
| 111 |
article = |
| 112 |
start_article title:line start_text text:text ( end_article / end_section ) |
| 113 |
{ |
| 114 |
return [ |
| 115 |
'type' => 'article', |
| 116 |
'title' => $title, |
| 117 |
'text' => $text |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
start_article = |
| 122 |
"!!" ws? "article" ws? eol |
| 123 |
|
| 124 |
start_text = |
| 125 |
"!!" ws? "text" ws? eol |
| 126 |
|
| 127 |
end_article = |
| 128 |
"!!" ws? "endarticle" ws? eol |
| 129 |
|
| 130 |
// function hooks |
| 131 |
|
| 132 |
functionhooks = start_functionhooks text:text ( end_functionhooks / end_section ) |
| 133 |
{ |
| 134 |
return [ 'type' => 'functionhooks', 'text' => $text ]; |
| 135 |
} |
| 136 |
|
| 137 |
start_functionhooks = |
| 138 |
"!!" ws? "functionhooks" ":"? ws? eol |
| 139 |
|
| 140 |
end_functionhooks = |
| 141 |
"!!" ws? "endfunctionhooks" ":"? ws? eol |
| 142 |
|
| 143 |
test = |
| 144 |
start_test |
| 145 |
testName:text |
| 146 |
sections:(section / config_section / option_section)* |
| 147 |
end_section |
| 148 |
{ |
| 149 |
$test = [ |
| 150 |
'type' => 'test', |
| 151 |
'testName' => $testName |
| 152 |
]; |
| 153 |
|
| 154 |
foreach ( $sections as $section ) { |
| 155 |
$test[$section['name']] = $section['text']; |
| 156 |
} |
| 157 |
// pegjs parser handles item options as follows: |
| 158 |
// item option value of item.options.parsoid |
| 159 |
// <none> undefined |
| 160 |
// parsoid "" |
| 161 |
// parsoid=wt2html "wt2html" |
| 162 |
// parsoid=wt2html,wt2wt ["wt2html","wt2wt"] |
| 163 |
// parsoid={"modes":["wt2wt"]} {modes:['wt2wt']} |
| 164 |
|
| 165 |
// treat 'parsoid=xxx,yyy' in options section as shorthand for |
| 166 |
// 'parsoid={modes:["xxx","yyy"]}' |
| 167 |
if ( isset($test['options']['parsoid'] ) ) { |
| 168 |
if ($test['options']['parsoid'] === '') { |
| 169 |
$test['options']['parsoid'] = []; |
| 170 |
} |
| 171 |
if ( is_string( $test['options']['parsoid'] ) ) { |
| 172 |
$test['options']['parsoid'] = [ $test['options']['parsoid'] ]; |
| 173 |
} |
| 174 |
if ( is_array( $test['options']['parsoid'] ) && |
| 175 |
isset( $test['options']['parsoid'][0] ) && |
| 176 |
!isset( $test['options']['parsoid']['modes'] ) |
| 177 |
) { |
| 178 |
$test['options']['parsoid'] = [ 'modes' => $test['options']['parsoid'] ]; |
| 179 |
} |
| 180 |
} |
| 181 |
return $test; |
| 182 |
} |
| 183 |
|
| 184 |
section = |
| 185 |
"!!" ws? (!"test") (!"end") (!"options") (!"config") name:(c:[^ \t\r\n]+ { return implode( $c ); }) rest_of_line |
| 186 |
text:text |
| 187 |
{ |
| 188 |
return [ 'name' => $name, 'text' => $text ]; |
| 189 |
} |
| 190 |
|
| 191 |
config_section = |
| 192 |
"!!" ws? "config" ws? eol |
| 193 |
items:config_list? |
| 194 |
{ |
| 195 |
$c = []; |
| 196 |
if ( $items && count($items) > 0 ) { |
| 197 |
foreach ( $items as $item ) { |
| 198 |
$c[$item['k']] = $item['v']; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return [ 'type' => 'section', 'name' => 'config', 'text' => $c ]; |
| 203 |
} |
| 204 |
|
| 205 |
option_section = |
| 206 |
"!!" ws? "options" ws? eol |
| 207 |
opts:option_list? |
| 208 |
{ |
| 209 |
$o = []; |
| 210 |
if ( $opts && count($opts) > 0 ) { |
| 211 |
foreach ( $opts as $opt ) { |
| 212 |
$o[$opt['k']] = $opt['v']; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return [ 'type' => 'section', 'name' => 'options', 'text' => $o ]; |
| 217 |
} |
| 218 |
|
| 219 |
config_list = c:a_config_line eol+ rest:config_list? |
| 220 |
{ |
| 221 |
$result = [ $c ]; |
| 222 |
if ( $rest && count( $rest ) > 0 ) { |
| 223 |
$result = array_merge( $result, $rest ); |
| 224 |
} |
| 225 |
return $result; |
| 226 |
} |
| 227 |
|
| 228 |
option_list = o:an_option ([ \t] / eol)+ rest:option_list? |
| 229 |
{ |
| 230 |
$result = [ $o ]; |
| 231 |
if ( $rest && count( $rest ) > 0 ) { |
| 232 |
$result = array_merge( $result, $rest ); |
| 233 |
} |
| 234 |
return $result; |
| 235 |
} |
| 236 |
|
| 237 |
a_config_line = k:option_name v:config_value |
| 238 |
{ |
| 239 |
return [ 'k' => $k, 'v' => $v ]; |
| 240 |
} |
| 241 |
|
| 242 |
config_value = ws? "=" ws? v:valid_json_value { return $v; } |
| 243 |
|
| 244 |
valid_json_value = v:$(quoted_value / plain_value / array_value / json_value) &{ |
| 245 |
// validate this as acceptable JSON |
| 246 |
// (this ensures that wikipeg throws a syntax error if |
| 247 |
// the JSON is invalid; note that PHP 7.3 would allow us |
| 248 |
// to use JSON_THROW_ON_ERROR instead of json_last_error()...) |
| 249 |
$ignore = json_decode($v, true, 100); |
| 250 |
return (json_last_error() === JSON_ERROR_NONE); |
| 251 |
} { |
| 252 |
// The value is valid JSON; return the decoded value. |
| 253 |
return json_decode($v, true); |
| 254 |
} |
| 255 |
|
| 256 |
// from PHP parser in tests/parser/parserTest.inc:parseOptions() |
| 257 |
// foo |
| 258 |
// foo=bar |
| 259 |
// foo="bar baz" |
| 260 |
// foo=[[bar baz]] |
| 261 |
// foo={...json...} |
| 262 |
// foo=bar,"baz quux",[[bat]] |
| 263 |
an_option = k:option_name v:option_value? |
| 264 |
{ |
| 265 |
return [ 'k' => strtolower( $k ), 'v' => $v ?? '' ]; |
| 266 |
} |
| 267 |
|
| 268 |
option_name = c:[^ \t\n=!]+ |
| 269 |
{ |
| 270 |
return implode($c); |
| 271 |
} |
| 272 |
|
| 273 |
option_value = ws? "=" ws? ovl:option_value_list |
| 274 |
{ |
| 275 |
return count( $ovl ) === 1 ? $ovl[0] : $ovl; |
| 276 |
} |
| 277 |
|
| 278 |
option_value_list = v:an_option_value |
| 279 |
rest:( ws? "," ws? ovl:option_value_list { return $ovl; })? |
| 280 |
{ |
| 281 |
$result = [ $v ]; |
| 282 |
if ( $rest && count( $rest ) > 0 ) { |
| 283 |
$result = array_merge( $result, $rest ); |
| 284 |
} |
| 285 |
return $result; |
| 286 |
} |
| 287 |
|
| 288 |
an_option_value = v:(link_target_value / quoted_value / plain_value / json_value) |
| 289 |
{ |
| 290 |
if ( $v[0] === '"' || $v[0] === '{' ) { // } is needed to make pegjs happy |
| 291 |
return PHPUtils::jsonDecode( $v ); |
| 292 |
} |
| 293 |
return $v; |
| 294 |
} |
| 295 |
|
| 296 |
link_target_value = "[[" v:[^\]\n]* "]]" |
| 297 |
{ |
| 298 |
// Perhaps we should canonicalize the title? |
| 299 |
// Protect with JSON.stringify just in case the link target starts with |
| 300 |
// double-quote or open-brace. |
| 301 |
return PHPUtils::jsonEncode( implode( $v ) ); |
| 302 |
} |
| 303 |
|
| 304 |
quoted_value = [\"] v:( [^\\\"\n] / ("\\" c:[^\n] { return "\\" . $c; } ) )* [\"] |
| 305 |
{ |
| 306 |
return '"' . implode( $v ) . '"'; |
| 307 |
} |
| 308 |
|
| 309 |
plain_value = v:[^ \t\n\"\'\[\]=,!\{]+ |
| 310 |
{ |
| 311 |
return implode( $v ); |
| 312 |
} |
| 313 |
|
| 314 |
array_value = "[" v:( [^\"\[\]\n] / quoted_value / array_value / eol )* "]" |
| 315 |
{ |
| 316 |
return "[" . implode( $v ) . "]"; |
| 317 |
} |
| 318 |
|
| 319 |
json_value = "{" v:( [^\"\{\}\n] / quoted_value / json_value / eol )* "}" |
| 320 |
{ |
| 321 |
return "{" . implode( $v ) . "}"; |
| 322 |
} |
| 323 |
|
| 324 |
start_test = |
| 325 |
"!!" ws? "test" ws? eol |
| 326 |
|
| 327 |
end_section = |
| 328 |
"!!" ws? "end" ws? eol |
| 329 |
|
| 330 |
hooks = start_hooks text:text ( end_hooks / end_section ) |
| 331 |
{ |
| 332 |
return [ 'type' => 'hooks', 'text' => $text ]; |
| 333 |
} |
| 334 |
|
| 335 |
start_hooks = |
| 336 |
"!!" ws? "hooks" ":"? ws? eol |
| 337 |
|
| 338 |
end_hooks = |
| 339 |
"!!" ws? "endhooks" ws? eol |