mark ApiEditPageTest as being slow tests
[lhc/web/wiklou.git] / includes / ConfEditor.php
1 <?php
2 /**
3 * Configuration file editor.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * This is a state machine style parser with two internal stacks:
25 * * A next state stack, which determines the state the machine will progress to next
26 * * A path stack, which keeps track of the logical location in the file.
27 *
28 * Reference grammar:
29 *
30 * file = T_OPEN_TAG *statement
31 * statement = T_VARIABLE "=" expression ";"
32 * expression = array / scalar / T_VARIABLE
33 * array = T_ARRAY "(" [ element *( "," element ) [ "," ] ] ")"
34 * element = assoc-element / expression
35 * assoc-element = scalar T_DOUBLE_ARROW expression
36 * scalar = T_LNUMBER / T_DNUMBER / T_STRING / T_CONSTANT_ENCAPSED_STRING
37 */
38 class ConfEditor {
39 /** The text to parse */
40 var $text;
41
42 /** The token array from token_get_all() */
43 var $tokens;
44
45 /** The current position in the token array */
46 var $pos;
47
48 /** The current 1-based line number */
49 var $lineNum;
50
51 /** The current 1-based column number */
52 var $colNum;
53
54 /** The current 0-based byte number */
55 var $byteNum;
56
57 /** The current ConfEditorToken object */
58 var $currentToken;
59
60 /** The previous ConfEditorToken object */
61 var $prevToken;
62
63 /**
64 * The state machine stack. This is an array of strings where the topmost
65 * element will be popped off and become the next parser state.
66 */
67 var $stateStack;
68
69
70 /**
71 * The path stack is a stack of associative arrays with the following elements:
72 * name The name of top level of the path
73 * level The level (number of elements) of the path
74 * startByte The byte offset of the start of the path
75 * startToken The token offset of the start
76 * endByte The byte offset of thee
77 * endToken The token offset of the end, plus one
78 * valueStartToken The start token offset of the value part
79 * valueStartByte The start byte offset of the value part
80 * valueEndToken The end token offset of the value part, plus one
81 * valueEndByte The end byte offset of the value part, plus one
82 * nextArrayIndex The next numeric array index at this level
83 * hasComma True if the array element ends with a comma
84 * arrowByte The byte offset of the "=>", or false if there isn't one
85 */
86 var $pathStack;
87
88 /**
89 * The elements of the top of the pathStack for every path encountered, indexed
90 * by slash-separated path.
91 */
92 var $pathInfo;
93
94 /**
95 * Next serial number for whitespace placeholder paths (\@extra-N)
96 */
97 var $serial;
98
99 /**
100 * Editor state. This consists of the internal copy/insert operations which
101 * are applied to the source string to obtain the destination string.
102 */
103 var $edits;
104
105 /**
106 * Simple entry point for command-line testing
107 *
108 * @param $text string
109 *
110 * @return string
111 */
112 static function test( $text ) {
113 try {
114 $ce = new self( $text );
115 $ce->parse();
116 } catch ( ConfEditorParseError $e ) {
117 return $e->getMessage() . "\n" . $e->highlight( $text );
118 }
119 return "OK";
120 }
121
122 /**
123 * Construct a new parser
124 */
125 public function __construct( $text ) {
126 $this->text = $text;
127 }
128
129 /**
130 * Edit the text. Returns the edited text.
131 * @param $ops Array of operations.
132 *
133 * Operations are given as an associative array, with members:
134 * type: One of delete, set, append or insert (required)
135 * path: The path to operate on (required)
136 * key: The array key to insert/append, with PHP quotes
137 * value: The value, with PHP quotes
138 *
139 * delete
140 * Deletes an array element or statement with the specified path.
141 * e.g.
142 * array('type' => 'delete', 'path' => '$foo/bar/baz' )
143 * is equivalent to the runtime PHP code:
144 * unset( $foo['bar']['baz'] );
145 *
146 * set
147 * Sets the value of an array element. If the element doesn't exist, it
148 * is appended to the array. If it does exist, the value is set, with
149 * comments and indenting preserved.
150 *
151 * append
152 * Appends a new element to the end of the array. Adds a trailing comma.
153 * e.g.
154 * array( 'type' => 'append', 'path', '$foo/bar',
155 * 'key' => 'baz', 'value' => "'x'" )
156 * is like the PHP code:
157 * $foo['bar']['baz'] = 'x';
158 *
159 * insert
160 * Insert a new element at the start of the array.
161 *
162 * @throws MWException
163 * @return string
164 */
165 public function edit( $ops ) {
166 $this->parse();
167
168 $this->edits = array(
169 array( 'copy', 0, strlen( $this->text ) )
170 );
171 foreach ( $ops as $op ) {
172 $type = $op['type'];
173 $path = $op['path'];
174 $value = isset( $op['value'] ) ? $op['value'] : null;
175 $key = isset( $op['key'] ) ? $op['key'] : null;
176
177 switch ( $type ) {
178 case 'delete':
179 list( $start, $end ) = $this->findDeletionRegion( $path );
180 $this->replaceSourceRegion( $start, $end, false );
181 break;
182 case 'set':
183 if ( isset( $this->pathInfo[$path] ) ) {
184 list( $start, $end ) = $this->findValueRegion( $path );
185 $encValue = $value; // var_export( $value, true );
186 $this->replaceSourceRegion( $start, $end, $encValue );
187 break;
188 }
189 // No existing path, fall through to append
190 $slashPos = strrpos( $path, '/' );
191 $key = var_export( substr( $path, $slashPos + 1 ), true );
192 $path = substr( $path, 0, $slashPos );
193 // Fall through
194 case 'append':
195 // Find the last array element
196 $lastEltPath = $this->findLastArrayElement( $path );
197 if ( $lastEltPath === false ) {
198 throw new MWException( "Can't find any element of array \"$path\"" );
199 }
200 $lastEltInfo = $this->pathInfo[$lastEltPath];
201
202 // Has it got a comma already?
203 if ( strpos( $lastEltPath, '@extra' ) === false && !$lastEltInfo['hasComma'] ) {
204 // No comma, insert one after the value region
205 list( , $end ) = $this->findValueRegion( $lastEltPath );
206 $this->replaceSourceRegion( $end - 1, $end - 1, ',' );
207 }
208
209 // Make the text to insert
210 list( $start, $end ) = $this->findDeletionRegion( $lastEltPath );
211
212 if ( $key === null ) {
213 list( $indent, ) = $this->getIndent( $start );
214 $textToInsert = "$indent$value,";
215 } else {
216 list( $indent, $arrowIndent ) =
217 $this->getIndent( $start, $key, $lastEltInfo['arrowByte'] );
218 $textToInsert = "$indent$key$arrowIndent=> $value,";
219 }
220 $textToInsert .= ( $indent === false ? ' ' : "\n" );
221
222 // Insert the item
223 $this->replaceSourceRegion( $end, $end, $textToInsert );
224 break;
225 case 'insert':
226 // Find first array element
227 $firstEltPath = $this->findFirstArrayElement( $path );
228 if ( $firstEltPath === false ) {
229 throw new MWException( "Can't find array element of \"$path\"" );
230 }
231 list( $start, ) = $this->findDeletionRegion( $firstEltPath );
232 $info = $this->pathInfo[$firstEltPath];
233
234 // Make the text to insert
235 if ( $key === null ) {
236 list( $indent, ) = $this->getIndent( $start );
237 $textToInsert = "$indent$value,";
238 } else {
239 list( $indent, $arrowIndent ) =
240 $this->getIndent( $start, $key, $info['arrowByte'] );
241 $textToInsert = "$indent$key$arrowIndent=> $value,";
242 }
243 $textToInsert .= ( $indent === false ? ' ' : "\n" );
244
245 // Insert the item
246 $this->replaceSourceRegion( $start, $start, $textToInsert );
247 break;
248 default:
249 throw new MWException( "Unrecognised operation: \"$type\"" );
250 }
251 }
252
253 // Do the edits
254 $out = '';
255 foreach ( $this->edits as $edit ) {
256 if ( $edit[0] == 'copy' ) {
257 $out .= substr( $this->text, $edit[1], $edit[2] - $edit[1] );
258 } else { // if ( $edit[0] == 'insert' )
259 $out .= $edit[1];
260 }
261 }
262
263 // Do a second parse as a sanity check
264 $this->text = $out;
265 try {
266 $this->parse();
267 } catch ( ConfEditorParseError $e ) {
268 throw new MWException(
269 "Sorry, ConfEditor broke the file during editing and it won't parse anymore: " .
270 $e->getMessage() );
271 }
272 return $out;
273 }
274
275 /**
276 * Get the variables defined in the text
277 * @return array( varname => value )
278 */
279 function getVars() {
280 $vars = array();
281 $this->parse();
282 foreach( $this->pathInfo as $path => $data ) {
283 if ( $path[0] != '$' )
284 continue;
285 $trimmedPath = substr( $path, 1 );
286 $name = $data['name'];
287 if ( $name[0] == '@' )
288 continue;
289 if ( $name[0] == '$' )
290 $name = substr( $name, 1 );
291 $parentPath = substr( $trimmedPath, 0,
292 strlen( $trimmedPath ) - strlen( $name ) );
293 if( substr( $parentPath, -1 ) == '/' )
294 $parentPath = substr( $parentPath, 0, -1 );
295
296 $value = substr( $this->text, $data['valueStartByte'],
297 $data['valueEndByte'] - $data['valueStartByte']
298 );
299 $this->setVar( $vars, $parentPath, $name,
300 $this->parseScalar( $value ) );
301 }
302 return $vars;
303 }
304
305 /**
306 * Set a value in an array, unless it's set already. For instance,
307 * setVar( $arr, 'foo/bar', 'baz', 3 ); will set
308 * $arr['foo']['bar']['baz'] = 3;
309 * @param $array array
310 * @param $path string slash-delimited path
311 * @param $key mixed Key
312 * @param $value mixed Value
313 */
314 function setVar( &$array, $path, $key, $value ) {
315 $pathArr = explode( '/', $path );
316 $target =& $array;
317 if ( $path !== '' ) {
318 foreach ( $pathArr as $p ) {
319 if( !isset( $target[$p] ) )
320 $target[$p] = array();
321 $target =& $target[$p];
322 }
323 }
324 if ( !isset( $target[$key] ) )
325 $target[$key] = $value;
326 }
327
328 /**
329 * Parse a scalar value in PHP
330 * @return mixed Parsed value
331 */
332 function parseScalar( $str ) {
333 if ( $str !== '' && $str[0] == '\'' )
334 // Single-quoted string
335 // @todo FIXME: trim() call is due to mystery bug where whitespace gets
336 // appended to the token; without it we ended up reading in the
337 // extra quote on the end!
338 return strtr( substr( trim( $str ), 1, -1 ),
339 array( '\\\'' => '\'', '\\\\' => '\\' ) );
340 if ( $str !== '' && $str[0] == '"' )
341 // Double-quoted string
342 // @todo FIXME: trim() call is due to mystery bug where whitespace gets
343 // appended to the token; without it we ended up reading in the
344 // extra quote on the end!
345 return stripcslashes( substr( trim( $str ), 1, -1 ) );
346 if ( substr( $str, 0, 4 ) == 'true' )
347 return true;
348 if ( substr( $str, 0, 5 ) == 'false' )
349 return false;
350 if ( substr( $str, 0, 4 ) == 'null' )
351 return null;
352 // Must be some kind of numeric value, so let PHP's weak typing
353 // be useful for a change
354 return $str;
355 }
356
357 /**
358 * Replace the byte offset region of the source with $newText.
359 * Works by adding elements to the $this->edits array.
360 */
361 function replaceSourceRegion( $start, $end, $newText = false ) {
362 // Split all copy operations with a source corresponding to the region
363 // in question.
364 $newEdits = array();
365 foreach ( $this->edits as $edit ) {
366 if ( $edit[0] !== 'copy' ) {
367 $newEdits[] = $edit;
368 continue;
369 }
370 $copyStart = $edit[1];
371 $copyEnd = $edit[2];
372 if ( $start >= $copyEnd || $end <= $copyStart ) {
373 // Outside this region
374 $newEdits[] = $edit;
375 continue;
376 }
377 if ( ( $start < $copyStart && $end > $copyStart )
378 || ( $start < $copyEnd && $end > $copyEnd )
379 ) {
380 throw new MWException( "Overlapping regions found, can't do the edit" );
381 }
382 // Split the copy
383 $newEdits[] = array( 'copy', $copyStart, $start );
384 if ( $newText !== false ) {
385 $newEdits[] = array( 'insert', $newText );
386 }
387 $newEdits[] = array( 'copy', $end, $copyEnd );
388 }
389 $this->edits = $newEdits;
390 }
391
392 /**
393 * Finds the source byte region which you would want to delete, if $pathName
394 * was to be deleted. Includes the leading spaces and tabs, the trailing line
395 * break, and any comments in between.
396 * @param $pathName
397 * @throws MWException
398 * @return array
399 */
400 function findDeletionRegion( $pathName ) {
401 if ( !isset( $this->pathInfo[$pathName] ) ) {
402 throw new MWException( "Can't find path \"$pathName\"" );
403 }
404 $path = $this->pathInfo[$pathName];
405 // Find the start
406 $this->firstToken();
407 while ( $this->pos != $path['startToken'] ) {
408 $this->nextToken();
409 }
410 $regionStart = $path['startByte'];
411 for ( $offset = -1; $offset >= -$this->pos; $offset-- ) {
412 $token = $this->getTokenAhead( $offset );
413 if ( !$token->isSkip() ) {
414 // If there is other content on the same line, don't move the start point
415 // back, because that will cause the regions to overlap.
416 $regionStart = $path['startByte'];
417 break;
418 }
419 $lfPos = strrpos( $token->text, "\n" );
420 if ( $lfPos === false ) {
421 $regionStart -= strlen( $token->text );
422 } else {
423 // The line start does not include the LF
424 $regionStart -= strlen( $token->text ) - $lfPos - 1;
425 break;
426 }
427 }
428 // Find the end
429 while ( $this->pos != $path['endToken'] ) {
430 $this->nextToken();
431 }
432 $regionEnd = $path['endByte']; // past the end
433 for ( $offset = 0; $offset < count( $this->tokens ) - $this->pos; $offset++ ) {
434 $token = $this->getTokenAhead( $offset );
435 if ( !$token->isSkip() ) {
436 break;
437 }
438 $lfPos = strpos( $token->text, "\n" );
439 if ( $lfPos === false ) {
440 $regionEnd += strlen( $token->text );
441 } else {
442 // This should point past the LF
443 $regionEnd += $lfPos + 1;
444 break;
445 }
446 }
447 return array( $regionStart, $regionEnd );
448 }
449
450 /**
451 * Find the byte region in the source corresponding to the value part.
452 * This includes the quotes, but does not include the trailing comma
453 * or semicolon.
454 *
455 * The end position is the past-the-end (end + 1) value as per convention.
456 * @param $pathName
457 * @throws MWException
458 * @return array
459 */
460 function findValueRegion( $pathName ) {
461 if ( !isset( $this->pathInfo[$pathName] ) ) {
462 throw new MWException( "Can't find path \"$pathName\"" );
463 }
464 $path = $this->pathInfo[$pathName];
465 if ( $path['valueStartByte'] === false || $path['valueEndByte'] === false ) {
466 throw new MWException( "Can't find value region for path \"$pathName\"" );
467 }
468 return array( $path['valueStartByte'], $path['valueEndByte'] );
469 }
470
471 /**
472 * Find the path name of the last element in the array.
473 * If the array is empty, this will return the \@extra interstitial element.
474 * If the specified path is not found or is not an array, it will return false.
475 * @return bool|int|string
476 */
477 function findLastArrayElement( $path ) {
478 // Try for a real element
479 $lastEltPath = false;
480 foreach ( $this->pathInfo as $candidatePath => $info ) {
481 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
482 $part2 = substr( $candidatePath, strlen( $path ) + 1, 1 );
483 if ( $part2 == '@' ) {
484 // Do nothing
485 } elseif ( $part1 == "$path/" ) {
486 $lastEltPath = $candidatePath;
487 } elseif ( $lastEltPath !== false ) {
488 break;
489 }
490 }
491 if ( $lastEltPath !== false ) {
492 return $lastEltPath;
493 }
494
495 // Try for an interstitial element
496 $extraPath = false;
497 foreach ( $this->pathInfo as $candidatePath => $info ) {
498 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
499 if ( $part1 == "$path/" ) {
500 $extraPath = $candidatePath;
501 } elseif ( $extraPath !== false ) {
502 break;
503 }
504 }
505 return $extraPath;
506 }
507
508 /**
509 * Find the path name of first element in the array.
510 * If the array is empty, this will return the \@extra interstitial element.
511 * If the specified path is not found or is not an array, it will return false.
512 * @return bool|int|string
513 */
514 function findFirstArrayElement( $path ) {
515 // Try for an ordinary element
516 foreach ( $this->pathInfo as $candidatePath => $info ) {
517 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
518 $part2 = substr( $candidatePath, strlen( $path ) + 1, 1 );
519 if ( $part1 == "$path/" && $part2 != '@' ) {
520 return $candidatePath;
521 }
522 }
523
524 // Try for an interstitial element
525 foreach ( $this->pathInfo as $candidatePath => $info ) {
526 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
527 if ( $part1 == "$path/" ) {
528 return $candidatePath;
529 }
530 }
531 return false;
532 }
533
534 /**
535 * Get the indent string which sits after a given start position.
536 * Returns false if the position is not at the start of the line.
537 * @return array
538 */
539 function getIndent( $pos, $key = false, $arrowPos = false ) {
540 $arrowIndent = ' ';
541 if ( $pos == 0 || $this->text[$pos-1] == "\n" ) {
542 $indentLength = strspn( $this->text, " \t", $pos );
543 $indent = substr( $this->text, $pos, $indentLength );
544 } else {
545 $indent = false;
546 }
547 if ( $indent !== false && $arrowPos !== false ) {
548 $arrowIndentLength = $arrowPos - $pos - $indentLength - strlen( $key );
549 if ( $arrowIndentLength > 0 ) {
550 $arrowIndent = str_repeat( ' ', $arrowIndentLength );
551 }
552 }
553 return array( $indent, $arrowIndent );
554 }
555
556 /**
557 * Run the parser on the text. Throws an exception if the string does not
558 * match our defined subset of PHP syntax.
559 */
560 public function parse() {
561 $this->initParse();
562 $this->pushState( 'file' );
563 $this->pushPath( '@extra-' . ($this->serial++) );
564 $token = $this->firstToken();
565
566 while ( !$token->isEnd() ) {
567 $state = $this->popState();
568 if ( !$state ) {
569 $this->error( 'internal error: empty state stack' );
570 }
571
572 switch ( $state ) {
573 case 'file':
574 $this->expect( T_OPEN_TAG );
575 $token = $this->skipSpace();
576 if ( $token->isEnd() ) {
577 break 2;
578 }
579 $this->pushState( 'statement', 'file 2' );
580 break;
581 case 'file 2':
582 $token = $this->skipSpace();
583 if ( $token->isEnd() ) {
584 break 2;
585 }
586 $this->pushState( 'statement', 'file 2' );
587 break;
588 case 'statement':
589 $token = $this->skipSpace();
590 if ( !$this->validatePath( $token->text ) ) {
591 $this->error( "Invalid variable name \"{$token->text}\"" );
592 }
593 $this->nextPath( $token->text );
594 $this->expect( T_VARIABLE );
595 $this->skipSpace();
596 $arrayAssign = false;
597 if ( $this->currentToken()->type == '[' ) {
598 $this->nextToken();
599 $token = $this->skipSpace();
600 if ( !$token->isScalar() ) {
601 $this->error( "expected a string or number for the array key" );
602 }
603 if ( $token->type == T_CONSTANT_ENCAPSED_STRING ) {
604 $text = $this->parseScalar( $token->text );
605 } else {
606 $text = $token->text;
607 }
608 if ( !$this->validatePath( $text ) ) {
609 $this->error( "Invalid associative array name \"$text\"" );
610 }
611 $this->pushPath( $text );
612 $this->nextToken();
613 $this->skipSpace();
614 $this->expect( ']' );
615 $this->skipSpace();
616 $arrayAssign = true;
617 }
618 $this->expect( '=' );
619 $this->skipSpace();
620 $this->startPathValue();
621 if ( $arrayAssign )
622 $this->pushState( 'expression', 'array assign end' );
623 else
624 $this->pushState( 'expression', 'statement end' );
625 break;
626 case 'array assign end':
627 case 'statement end':
628 $this->endPathValue();
629 if ( $state == 'array assign end' )
630 $this->popPath();
631 $this->skipSpace();
632 $this->expect( ';' );
633 $this->nextPath( '@extra-' . ($this->serial++) );
634 break;
635 case 'expression':
636 $token = $this->skipSpace();
637 if ( $token->type == T_ARRAY ) {
638 $this->pushState( 'array' );
639 } elseif ( $token->isScalar() ) {
640 $this->nextToken();
641 } elseif ( $token->type == T_VARIABLE ) {
642 $this->nextToken();
643 } else {
644 $this->error( "expected simple expression" );
645 }
646 break;
647 case 'array':
648 $this->skipSpace();
649 $this->expect( T_ARRAY );
650 $this->skipSpace();
651 $this->expect( '(' );
652 $this->skipSpace();
653 $this->pushPath( '@extra-' . ($this->serial++) );
654 if ( $this->isAhead( ')' ) ) {
655 // Empty array
656 $this->pushState( 'array end' );
657 } else {
658 $this->pushState( 'element', 'array end' );
659 }
660 break;
661 case 'array end':
662 $this->skipSpace();
663 $this->popPath();
664 $this->expect( ')' );
665 break;
666 case 'element':
667 $token = $this->skipSpace();
668 // Look ahead to find the double arrow
669 if ( $token->isScalar() && $this->isAhead( T_DOUBLE_ARROW, 1 ) ) {
670 // Found associative element
671 $this->pushState( 'assoc-element', 'element end' );
672 } else {
673 // Not associative
674 $this->nextPath( '@next' );
675 $this->startPathValue();
676 $this->pushState( 'expression', 'element end' );
677 }
678 break;
679 case 'element end':
680 $token = $this->skipSpace();
681 if ( $token->type == ',' ) {
682 $this->endPathValue();
683 $this->markComma();
684 $this->nextToken();
685 $this->nextPath( '@extra-' . ($this->serial++) );
686 // Look ahead to find ending bracket
687 if ( $this->isAhead( ")" ) ) {
688 // Found ending bracket, no continuation
689 $this->skipSpace();
690 } else {
691 // No ending bracket, continue to next element
692 $this->pushState( 'element' );
693 }
694 } elseif ( $token->type == ')' ) {
695 // End array
696 $this->endPathValue();
697 } else {
698 $this->error( "expected the next array element or the end of the array" );
699 }
700 break;
701 case 'assoc-element':
702 $token = $this->skipSpace();
703 if ( !$token->isScalar() ) {
704 $this->error( "expected a string or number for the array key" );
705 }
706 if ( $token->type == T_CONSTANT_ENCAPSED_STRING ) {
707 $text = $this->parseScalar( $token->text );
708 } else {
709 $text = $token->text;
710 }
711 if ( !$this->validatePath( $text ) ) {
712 $this->error( "Invalid associative array name \"$text\"" );
713 }
714 $this->nextPath( $text );
715 $this->nextToken();
716 $this->skipSpace();
717 $this->markArrow();
718 $this->expect( T_DOUBLE_ARROW );
719 $this->skipSpace();
720 $this->startPathValue();
721 $this->pushState( 'expression' );
722 break;
723 }
724 }
725 if ( count( $this->stateStack ) ) {
726 $this->error( 'unexpected end of file' );
727 }
728 $this->popPath();
729 }
730
731 /**
732 * Initialise a parse.
733 */
734 protected function initParse() {
735 $this->tokens = token_get_all( $this->text );
736 $this->stateStack = array();
737 $this->pathStack = array();
738 $this->firstToken();
739 $this->pathInfo = array();
740 $this->serial = 1;
741 }
742
743 /**
744 * Set the parse position. Do not call this except from firstToken() and
745 * nextToken(), there is more to update than just the position.
746 */
747 protected function setPos( $pos ) {
748 $this->pos = $pos;
749 if ( $this->pos >= count( $this->tokens ) ) {
750 $this->currentToken = ConfEditorToken::newEnd();
751 } else {
752 $this->currentToken = $this->newTokenObj( $this->tokens[$this->pos] );
753 }
754 return $this->currentToken;
755 }
756
757 /**
758 * Create a ConfEditorToken from an element of token_get_all()
759 * @return ConfEditorToken
760 */
761 function newTokenObj( $internalToken ) {
762 if ( is_array( $internalToken ) ) {
763 return new ConfEditorToken( $internalToken[0], $internalToken[1] );
764 } else {
765 return new ConfEditorToken( $internalToken, $internalToken );
766 }
767 }
768
769 /**
770 * Reset the parse position
771 */
772 function firstToken() {
773 $this->setPos( 0 );
774 $this->prevToken = ConfEditorToken::newEnd();
775 $this->lineNum = 1;
776 $this->colNum = 1;
777 $this->byteNum = 0;
778 return $this->currentToken;
779 }
780
781 /**
782 * Get the current token
783 */
784 function currentToken() {
785 return $this->currentToken;
786 }
787
788 /**
789 * Advance the current position and return the resulting next token
790 */
791 function nextToken() {
792 if ( $this->currentToken ) {
793 $text = $this->currentToken->text;
794 $lfCount = substr_count( $text, "\n" );
795 if ( $lfCount ) {
796 $this->lineNum += $lfCount;
797 $this->colNum = strlen( $text ) - strrpos( $text, "\n" );
798 } else {
799 $this->colNum += strlen( $text );
800 }
801 $this->byteNum += strlen( $text );
802 }
803 $this->prevToken = $this->currentToken;
804 $this->setPos( $this->pos + 1 );
805 return $this->currentToken;
806 }
807
808 /**
809 * Get the token $offset steps ahead of the current position.
810 * $offset may be negative, to get tokens behind the current position.
811 * @return ConfEditorToken
812 */
813 function getTokenAhead( $offset ) {
814 $pos = $this->pos + $offset;
815 if ( $pos >= count( $this->tokens ) || $pos < 0 ) {
816 return ConfEditorToken::newEnd();
817 } else {
818 return $this->newTokenObj( $this->tokens[$pos] );
819 }
820 }
821
822 /**
823 * Advances the current position past any whitespace or comments
824 */
825 function skipSpace() {
826 while ( $this->currentToken && $this->currentToken->isSkip() ) {
827 $this->nextToken();
828 }
829 return $this->currentToken;
830 }
831
832 /**
833 * Throws an error if the current token is not of the given type, and
834 * then advances to the next position.
835 */
836 function expect( $type ) {
837 if ( $this->currentToken && $this->currentToken->type == $type ) {
838 return $this->nextToken();
839 } else {
840 $this->error( "expected " . $this->getTypeName( $type ) .
841 ", got " . $this->getTypeName( $this->currentToken->type ) );
842 }
843 }
844
845 /**
846 * Push a state or two on to the state stack.
847 */
848 function pushState( $nextState, $stateAfterThat = null ) {
849 if ( $stateAfterThat !== null ) {
850 $this->stateStack[] = $stateAfterThat;
851 }
852 $this->stateStack[] = $nextState;
853 }
854
855 /**
856 * Pop a state from the state stack.
857 * @return mixed
858 */
859 function popState() {
860 return array_pop( $this->stateStack );
861 }
862
863 /**
864 * Returns true if the user input path is valid.
865 * This exists to allow "/" and "@" to be reserved for string path keys
866 * @return bool
867 */
868 function validatePath( $path ) {
869 return strpos( $path, '/' ) === false && substr( $path, 0, 1 ) != '@';
870 }
871
872 /**
873 * Internal function to update some things at the end of a path region. Do
874 * not call except from popPath() or nextPath().
875 */
876 function endPath() {
877 $key = '';
878 foreach ( $this->pathStack as $pathInfo ) {
879 if ( $key !== '' ) {
880 $key .= '/';
881 }
882 $key .= $pathInfo['name'];
883 }
884 $pathInfo['endByte'] = $this->byteNum;
885 $pathInfo['endToken'] = $this->pos;
886 $this->pathInfo[$key] = $pathInfo;
887 }
888
889 /**
890 * Go up to a new path level, for example at the start of an array.
891 */
892 function pushPath( $path ) {
893 $this->pathStack[] = array(
894 'name' => $path,
895 'level' => count( $this->pathStack ) + 1,
896 'startByte' => $this->byteNum,
897 'startToken' => $this->pos,
898 'valueStartToken' => false,
899 'valueStartByte' => false,
900 'valueEndToken' => false,
901 'valueEndByte' => false,
902 'nextArrayIndex' => 0,
903 'hasComma' => false,
904 'arrowByte' => false
905 );
906 }
907
908 /**
909 * Go down a path level, for example at the end of an array.
910 */
911 function popPath() {
912 $this->endPath();
913 array_pop( $this->pathStack );
914 }
915
916 /**
917 * Go to the next path on the same level. This ends the current path and
918 * starts a new one. If $path is \@next, the new path is set to the next
919 * numeric array element.
920 */
921 function nextPath( $path ) {
922 $this->endPath();
923 $i = count( $this->pathStack ) - 1;
924 if ( $path == '@next' ) {
925 $nextArrayIndex =& $this->pathStack[$i]['nextArrayIndex'];
926 $this->pathStack[$i]['name'] = $nextArrayIndex;
927 $nextArrayIndex++;
928 } else {
929 $this->pathStack[$i]['name'] = $path;
930 }
931 $this->pathStack[$i] =
932 array(
933 'startByte' => $this->byteNum,
934 'startToken' => $this->pos,
935 'valueStartToken' => false,
936 'valueStartByte' => false,
937 'valueEndToken' => false,
938 'valueEndByte' => false,
939 'hasComma' => false,
940 'arrowByte' => false,
941 ) + $this->pathStack[$i];
942 }
943
944 /**
945 * Mark the start of the value part of a path.
946 */
947 function startPathValue() {
948 $path =& $this->pathStack[count( $this->pathStack ) - 1];
949 $path['valueStartToken'] = $this->pos;
950 $path['valueStartByte'] = $this->byteNum;
951 }
952
953 /**
954 * Mark the end of the value part of a path.
955 */
956 function endPathValue() {
957 $path =& $this->pathStack[count( $this->pathStack ) - 1];
958 $path['valueEndToken'] = $this->pos;
959 $path['valueEndByte'] = $this->byteNum;
960 }
961
962 /**
963 * Mark the comma separator in an array element
964 */
965 function markComma() {
966 $path =& $this->pathStack[count( $this->pathStack ) - 1];
967 $path['hasComma'] = true;
968 }
969
970 /**
971 * Mark the arrow separator in an associative array element
972 */
973 function markArrow() {
974 $path =& $this->pathStack[count( $this->pathStack ) - 1];
975 $path['arrowByte'] = $this->byteNum;
976 }
977
978 /**
979 * Generate a parse error
980 */
981 function error( $msg ) {
982 throw new ConfEditorParseError( $this, $msg );
983 }
984
985 /**
986 * Get a readable name for the given token type.
987 * @return string
988 */
989 function getTypeName( $type ) {
990 if ( is_int( $type ) ) {
991 return token_name( $type );
992 } else {
993 return "\"$type\"";
994 }
995 }
996
997 /**
998 * Looks ahead to see if the given type is the next token type, starting
999 * from the current position plus the given offset. Skips any intervening
1000 * whitespace.
1001 * @return bool
1002 */
1003 function isAhead( $type, $offset = 0 ) {
1004 $ahead = $offset;
1005 $token = $this->getTokenAhead( $offset );
1006 while ( !$token->isEnd() ) {
1007 if ( $token->isSkip() ) {
1008 $ahead++;
1009 $token = $this->getTokenAhead( $ahead );
1010 continue;
1011 } elseif ( $token->type == $type ) {
1012 // Found the type
1013 return true;
1014 } else {
1015 // Not found
1016 return false;
1017 }
1018 }
1019 return false;
1020 }
1021
1022 /**
1023 * Get the previous token object
1024 */
1025 function prevToken() {
1026 return $this->prevToken;
1027 }
1028
1029 /**
1030 * Echo a reasonably readable representation of the tokenizer array.
1031 */
1032 function dumpTokens() {
1033 $out = '';
1034 foreach ( $this->tokens as $token ) {
1035 $obj = $this->newTokenObj( $token );
1036 $out .= sprintf( "%-28s %s\n",
1037 $this->getTypeName( $obj->type ),
1038 addcslashes( $obj->text, "\0..\37" ) );
1039 }
1040 echo "<pre>" . htmlspecialchars( $out ) . "</pre>";
1041 }
1042 }
1043
1044 /**
1045 * Exception class for parse errors
1046 */
1047 class ConfEditorParseError extends MWException {
1048 var $lineNum, $colNum;
1049 function __construct( $editor, $msg ) {
1050 $this->lineNum = $editor->lineNum;
1051 $this->colNum = $editor->colNum;
1052 parent::__construct( "Parse error on line {$editor->lineNum} " .
1053 "col {$editor->colNum}: $msg" );
1054 }
1055
1056 function highlight( $text ) {
1057 $lines = StringUtils::explode( "\n", $text );
1058 foreach ( $lines as $lineNum => $line ) {
1059 if ( $lineNum == $this->lineNum - 1 ) {
1060 return "$line\n" .str_repeat( ' ', $this->colNum - 1 ) . "^\n";
1061 }
1062 }
1063 }
1064
1065 }
1066
1067 /**
1068 * Class to wrap a token from the tokenizer.
1069 */
1070 class ConfEditorToken {
1071 var $type, $text;
1072
1073 static $scalarTypes = array( T_LNUMBER, T_DNUMBER, T_STRING, T_CONSTANT_ENCAPSED_STRING );
1074 static $skipTypes = array( T_WHITESPACE, T_COMMENT, T_DOC_COMMENT );
1075
1076 static function newEnd() {
1077 return new self( 'END', '' );
1078 }
1079
1080 function __construct( $type, $text ) {
1081 $this->type = $type;
1082 $this->text = $text;
1083 }
1084
1085 function isSkip() {
1086 return in_array( $this->type, self::$skipTypes );
1087 }
1088
1089 function isScalar() {
1090 return in_array( $this->type, self::$scalarTypes );
1091 }
1092
1093 function isEnd() {
1094 return $this->type == 'END';
1095 }
1096 }
1097