Update formatting
[lhc/web/wiklou.git] / includes / utils / 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 * The path stack is a stack of associative arrays with the following elements:
71 * name The name of top level of the path
72 * level The level (number of elements) of the path
73 * startByte The byte offset of the start of the path
74 * startToken The token offset of the start
75 * endByte The byte offset of thee
76 * endToken The token offset of the end, plus one
77 * valueStartToken The start token offset of the value part
78 * valueStartByte The start byte offset of the value part
79 * valueEndToken The end token offset of the value part, plus one
80 * valueEndByte The end byte offset of the value part, plus one
81 * nextArrayIndex The next numeric array index at this level
82 * hasComma True if the array element ends with a comma
83 * arrowByte The byte offset of the "=>", or false if there isn't one
84 */
85 var $pathStack;
86
87 /**
88 * The elements of the top of the pathStack for every path encountered, indexed
89 * by slash-separated path.
90 */
91 var $pathInfo;
92
93 /**
94 * Next serial number for whitespace placeholder paths (\@extra-N)
95 */
96 var $serial;
97
98 /**
99 * Editor state. This consists of the internal copy/insert operations which
100 * are applied to the source string to obtain the destination string.
101 */
102 var $edits;
103
104 /**
105 * Simple entry point for command-line testing
106 *
107 * @param $text string
108 *
109 * @return string
110 */
111 static function test( $text ) {
112 try {
113 $ce = new self( $text );
114 $ce->parse();
115 } catch ( ConfEditorParseError $e ) {
116 return $e->getMessage() . "\n" . $e->highlight( $text );
117 }
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 array $ops 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
273 return $out;
274 }
275
276 /**
277 * Get the variables defined in the text
278 * @return array( varname => value )
279 */
280 function getVars() {
281 $vars = array();
282 $this->parse();
283 foreach ( $this->pathInfo as $path => $data ) {
284 if ( $path[0] != '$' ) {
285 continue;
286 }
287 $trimmedPath = substr( $path, 1 );
288 $name = $data['name'];
289 if ( $name[0] == '@' ) {
290 continue;
291 }
292 if ( $name[0] == '$' ) {
293 $name = substr( $name, 1 );
294 }
295 $parentPath = substr( $trimmedPath, 0,
296 strlen( $trimmedPath ) - strlen( $name ) );
297 if ( substr( $parentPath, -1 ) == '/' ) {
298 $parentPath = substr( $parentPath, 0, -1 );
299 }
300
301 $value = substr( $this->text, $data['valueStartByte'],
302 $data['valueEndByte'] - $data['valueStartByte']
303 );
304 $this->setVar( $vars, $parentPath, $name,
305 $this->parseScalar( $value ) );
306 }
307
308 return $vars;
309 }
310
311 /**
312 * Set a value in an array, unless it's set already. For instance,
313 * setVar( $arr, 'foo/bar', 'baz', 3 ); will set
314 * $arr['foo']['bar']['baz'] = 3;
315 * @param $array array
316 * @param string $path slash-delimited path
317 * @param $key mixed Key
318 * @param $value mixed Value
319 */
320 function setVar( &$array, $path, $key, $value ) {
321 $pathArr = explode( '/', $path );
322 $target =& $array;
323 if ( $path !== '' ) {
324 foreach ( $pathArr as $p ) {
325 if ( !isset( $target[$p] ) ) {
326 $target[$p] = array();
327 }
328 $target =& $target[$p];
329 }
330 }
331 if ( !isset( $target[$key] ) ) {
332 $target[$key] = $value;
333 }
334 }
335
336 /**
337 * Parse a scalar value in PHP
338 * @return mixed Parsed value
339 */
340 function parseScalar( $str ) {
341 if ( $str !== '' && $str[0] == '\'' ) {
342 // Single-quoted string
343 // @todo FIXME: trim() call is due to mystery bug where whitespace gets
344 // appended to the token; without it we ended up reading in the
345 // extra quote on the end!
346 return strtr( substr( trim( $str ), 1, -1 ),
347 array( '\\\'' => '\'', '\\\\' => '\\' ) );
348 }
349 if ( $str !== '' && $str[0] == '"' ) {
350 // Double-quoted string
351 // @todo FIXME: trim() call is due to mystery bug where whitespace gets
352 // appended to the token; without it we ended up reading in the
353 // extra quote on the end!
354 return stripcslashes( substr( trim( $str ), 1, -1 ) );
355 }
356 if ( substr( $str, 0, 4 ) == 'true' ) {
357 return true;
358 }
359 if ( substr( $str, 0, 5 ) == 'false' ) {
360 return false;
361 }
362 if ( substr( $str, 0, 4 ) == 'null' ) {
363 return null;
364 }
365
366 // Must be some kind of numeric value, so let PHP's weak typing
367 // be useful for a change
368 return $str;
369 }
370
371 /**
372 * Replace the byte offset region of the source with $newText.
373 * Works by adding elements to the $this->edits array.
374 */
375 function replaceSourceRegion( $start, $end, $newText = false ) {
376 // Split all copy operations with a source corresponding to the region
377 // in question.
378 $newEdits = array();
379 foreach ( $this->edits as $edit ) {
380 if ( $edit[0] !== 'copy' ) {
381 $newEdits[] = $edit;
382 continue;
383 }
384 $copyStart = $edit[1];
385 $copyEnd = $edit[2];
386 if ( $start >= $copyEnd || $end <= $copyStart ) {
387 // Outside this region
388 $newEdits[] = $edit;
389 continue;
390 }
391 if ( ( $start < $copyStart && $end > $copyStart )
392 || ( $start < $copyEnd && $end > $copyEnd )
393 ) {
394 throw new MWException( "Overlapping regions found, can't do the edit" );
395 }
396 // Split the copy
397 $newEdits[] = array( 'copy', $copyStart, $start );
398 if ( $newText !== false ) {
399 $newEdits[] = array( 'insert', $newText );
400 }
401 $newEdits[] = array( 'copy', $end, $copyEnd );
402 }
403 $this->edits = $newEdits;
404 }
405
406 /**
407 * Finds the source byte region which you would want to delete, if $pathName
408 * was to be deleted. Includes the leading spaces and tabs, the trailing line
409 * break, and any comments in between.
410 * @param $pathName
411 * @throws MWException
412 * @return array
413 */
414 function findDeletionRegion( $pathName ) {
415 if ( !isset( $this->pathInfo[$pathName] ) ) {
416 throw new MWException( "Can't find path \"$pathName\"" );
417 }
418 $path = $this->pathInfo[$pathName];
419 // Find the start
420 $this->firstToken();
421 while ( $this->pos != $path['startToken'] ) {
422 $this->nextToken();
423 }
424 $regionStart = $path['startByte'];
425 for ( $offset = -1; $offset >= -$this->pos; $offset-- ) {
426 $token = $this->getTokenAhead( $offset );
427 if ( !$token->isSkip() ) {
428 // If there is other content on the same line, don't move the start point
429 // back, because that will cause the regions to overlap.
430 $regionStart = $path['startByte'];
431 break;
432 }
433 $lfPos = strrpos( $token->text, "\n" );
434 if ( $lfPos === false ) {
435 $regionStart -= strlen( $token->text );
436 } else {
437 // The line start does not include the LF
438 $regionStart -= strlen( $token->text ) - $lfPos - 1;
439 break;
440 }
441 }
442 // Find the end
443 while ( $this->pos != $path['endToken'] ) {
444 $this->nextToken();
445 }
446 $regionEnd = $path['endByte']; // past the end
447 for ( $offset = 0; $offset < count( $this->tokens ) - $this->pos; $offset++ ) {
448 $token = $this->getTokenAhead( $offset );
449 if ( !$token->isSkip() ) {
450 break;
451 }
452 $lfPos = strpos( $token->text, "\n" );
453 if ( $lfPos === false ) {
454 $regionEnd += strlen( $token->text );
455 } else {
456 // This should point past the LF
457 $regionEnd += $lfPos + 1;
458 break;
459 }
460 }
461
462 return array( $regionStart, $regionEnd );
463 }
464
465 /**
466 * Find the byte region in the source corresponding to the value part.
467 * This includes the quotes, but does not include the trailing comma
468 * or semicolon.
469 *
470 * The end position is the past-the-end (end + 1) value as per convention.
471 * @param $pathName
472 * @throws MWException
473 * @return array
474 */
475 function findValueRegion( $pathName ) {
476 if ( !isset( $this->pathInfo[$pathName] ) ) {
477 throw new MWException( "Can't find path \"$pathName\"" );
478 }
479 $path = $this->pathInfo[$pathName];
480 if ( $path['valueStartByte'] === false || $path['valueEndByte'] === false ) {
481 throw new MWException( "Can't find value region for path \"$pathName\"" );
482 }
483
484 return array( $path['valueStartByte'], $path['valueEndByte'] );
485 }
486
487 /**
488 * Find the path name of the last element in the array.
489 * If the array is empty, this will return the \@extra interstitial element.
490 * If the specified path is not found or is not an array, it will return false.
491 * @return bool|int|string
492 */
493 function findLastArrayElement( $path ) {
494 // Try for a real element
495 $lastEltPath = false;
496 foreach ( $this->pathInfo as $candidatePath => $info ) {
497 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
498 $part2 = substr( $candidatePath, strlen( $path ) + 1, 1 );
499 if ( $part2 == '@' ) {
500 // Do nothing
501 } elseif ( $part1 == "$path/" ) {
502 $lastEltPath = $candidatePath;
503 } elseif ( $lastEltPath !== false ) {
504 break;
505 }
506 }
507 if ( $lastEltPath !== false ) {
508 return $lastEltPath;
509 }
510
511 // Try for an interstitial element
512 $extraPath = false;
513 foreach ( $this->pathInfo as $candidatePath => $info ) {
514 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
515 if ( $part1 == "$path/" ) {
516 $extraPath = $candidatePath;
517 } elseif ( $extraPath !== false ) {
518 break;
519 }
520 }
521
522 return $extraPath;
523 }
524
525 /**
526 * Find the path name of first element in the array.
527 * If the array is empty, this will return the \@extra interstitial element.
528 * If the specified path is not found or is not an array, it will return false.
529 * @return bool|int|string
530 */
531 function findFirstArrayElement( $path ) {
532 // Try for an ordinary element
533 foreach ( $this->pathInfo as $candidatePath => $info ) {
534 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
535 $part2 = substr( $candidatePath, strlen( $path ) + 1, 1 );
536 if ( $part1 == "$path/" && $part2 != '@' ) {
537 return $candidatePath;
538 }
539 }
540
541 // Try for an interstitial element
542 foreach ( $this->pathInfo as $candidatePath => $info ) {
543 $part1 = substr( $candidatePath, 0, strlen( $path ) + 1 );
544 if ( $part1 == "$path/" ) {
545 return $candidatePath;
546 }
547 }
548
549 return false;
550 }
551
552 /**
553 * Get the indent string which sits after a given start position.
554 * Returns false if the position is not at the start of the line.
555 * @return array
556 */
557 function getIndent( $pos, $key = false, $arrowPos = false ) {
558 $arrowIndent = ' ';
559 if ( $pos == 0 || $this->text[$pos - 1] == "\n" ) {
560 $indentLength = strspn( $this->text, " \t", $pos );
561 $indent = substr( $this->text, $pos, $indentLength );
562 } else {
563 $indent = false;
564 }
565 if ( $indent !== false && $arrowPos !== false ) {
566 $arrowIndentLength = $arrowPos - $pos - $indentLength - strlen( $key );
567 if ( $arrowIndentLength > 0 ) {
568 $arrowIndent = str_repeat( ' ', $arrowIndentLength );
569 }
570 }
571
572 return array( $indent, $arrowIndent );
573 }
574
575 /**
576 * Run the parser on the text. Throws an exception if the string does not
577 * match our defined subset of PHP syntax.
578 */
579 public function parse() {
580 $this->initParse();
581 $this->pushState( 'file' );
582 $this->pushPath( '@extra-' . ( $this->serial++ ) );
583 $token = $this->firstToken();
584
585 while ( !$token->isEnd() ) {
586 $state = $this->popState();
587 if ( !$state ) {
588 $this->error( 'internal error: empty state stack' );
589 }
590
591 switch ( $state ) {
592 case 'file':
593 $this->expect( T_OPEN_TAG );
594 $token = $this->skipSpace();
595 if ( $token->isEnd() ) {
596 break 2;
597 }
598 $this->pushState( 'statement', 'file 2' );
599 break;
600 case 'file 2':
601 $token = $this->skipSpace();
602 if ( $token->isEnd() ) {
603 break 2;
604 }
605 $this->pushState( 'statement', 'file 2' );
606 break;
607 case 'statement':
608 $token = $this->skipSpace();
609 if ( !$this->validatePath( $token->text ) ) {
610 $this->error( "Invalid variable name \"{$token->text}\"" );
611 }
612 $this->nextPath( $token->text );
613 $this->expect( T_VARIABLE );
614 $this->skipSpace();
615 $arrayAssign = false;
616 if ( $this->currentToken()->type == '[' ) {
617 $this->nextToken();
618 $token = $this->skipSpace();
619 if ( !$token->isScalar() ) {
620 $this->error( "expected a string or number for the array key" );
621 }
622 if ( $token->type == T_CONSTANT_ENCAPSED_STRING ) {
623 $text = $this->parseScalar( $token->text );
624 } else {
625 $text = $token->text;
626 }
627 if ( !$this->validatePath( $text ) ) {
628 $this->error( "Invalid associative array name \"$text\"" );
629 }
630 $this->pushPath( $text );
631 $this->nextToken();
632 $this->skipSpace();
633 $this->expect( ']' );
634 $this->skipSpace();
635 $arrayAssign = true;
636 }
637 $this->expect( '=' );
638 $this->skipSpace();
639 $this->startPathValue();
640 if ( $arrayAssign ) {
641 $this->pushState( 'expression', 'array assign end' );
642 } else {
643 $this->pushState( 'expression', 'statement end' );
644 }
645 break;
646 case 'array assign end':
647 case 'statement end':
648 $this->endPathValue();
649 if ( $state == 'array assign end' ) {
650 $this->popPath();
651 }
652 $this->skipSpace();
653 $this->expect( ';' );
654 $this->nextPath( '@extra-' . ( $this->serial++ ) );
655 break;
656 case 'expression':
657 $token = $this->skipSpace();
658 if ( $token->type == T_ARRAY ) {
659 $this->pushState( 'array' );
660 } elseif ( $token->isScalar() ) {
661 $this->nextToken();
662 } elseif ( $token->type == T_VARIABLE ) {
663 $this->nextToken();
664 } else {
665 $this->error( "expected simple expression" );
666 }
667 break;
668 case 'array':
669 $this->skipSpace();
670 $this->expect( T_ARRAY );
671 $this->skipSpace();
672 $this->expect( '(' );
673 $this->skipSpace();
674 $this->pushPath( '@extra-' . ( $this->serial++ ) );
675 if ( $this->isAhead( ')' ) ) {
676 // Empty array
677 $this->pushState( 'array end' );
678 } else {
679 $this->pushState( 'element', 'array end' );
680 }
681 break;
682 case 'array end':
683 $this->skipSpace();
684 $this->popPath();
685 $this->expect( ')' );
686 break;
687 case 'element':
688 $token = $this->skipSpace();
689 // Look ahead to find the double arrow
690 if ( $token->isScalar() && $this->isAhead( T_DOUBLE_ARROW, 1 ) ) {
691 // Found associative element
692 $this->pushState( 'assoc-element', 'element end' );
693 } else {
694 // Not associative
695 $this->nextPath( '@next' );
696 $this->startPathValue();
697 $this->pushState( 'expression', 'element end' );
698 }
699 break;
700 case 'element end':
701 $token = $this->skipSpace();
702 if ( $token->type == ',' ) {
703 $this->endPathValue();
704 $this->markComma();
705 $this->nextToken();
706 $this->nextPath( '@extra-' . ( $this->serial++ ) );
707 // Look ahead to find ending bracket
708 if ( $this->isAhead( ")" ) ) {
709 // Found ending bracket, no continuation
710 $this->skipSpace();
711 } else {
712 // No ending bracket, continue to next element
713 $this->pushState( 'element' );
714 }
715 } elseif ( $token->type == ')' ) {
716 // End array
717 $this->endPathValue();
718 } else {
719 $this->error( "expected the next array element or the end of the array" );
720 }
721 break;
722 case 'assoc-element':
723 $token = $this->skipSpace();
724 if ( !$token->isScalar() ) {
725 $this->error( "expected a string or number for the array key" );
726 }
727 if ( $token->type == T_CONSTANT_ENCAPSED_STRING ) {
728 $text = $this->parseScalar( $token->text );
729 } else {
730 $text = $token->text;
731 }
732 if ( !$this->validatePath( $text ) ) {
733 $this->error( "Invalid associative array name \"$text\"" );
734 }
735 $this->nextPath( $text );
736 $this->nextToken();
737 $this->skipSpace();
738 $this->markArrow();
739 $this->expect( T_DOUBLE_ARROW );
740 $this->skipSpace();
741 $this->startPathValue();
742 $this->pushState( 'expression' );
743 break;
744 }
745 }
746 if ( count( $this->stateStack ) ) {
747 $this->error( 'unexpected end of file' );
748 }
749 $this->popPath();
750 }
751
752 /**
753 * Initialise a parse.
754 */
755 protected function initParse() {
756 $this->tokens = token_get_all( $this->text );
757 $this->stateStack = array();
758 $this->pathStack = array();
759 $this->firstToken();
760 $this->pathInfo = array();
761 $this->serial = 1;
762 }
763
764 /**
765 * Set the parse position. Do not call this except from firstToken() and
766 * nextToken(), there is more to update than just the position.
767 */
768 protected function setPos( $pos ) {
769 $this->pos = $pos;
770 if ( $this->pos >= count( $this->tokens ) ) {
771 $this->currentToken = ConfEditorToken::newEnd();
772 } else {
773 $this->currentToken = $this->newTokenObj( $this->tokens[$this->pos] );
774 }
775
776 return $this->currentToken;
777 }
778
779 /**
780 * Create a ConfEditorToken from an element of token_get_all()
781 * @return ConfEditorToken
782 */
783 function newTokenObj( $internalToken ) {
784 if ( is_array( $internalToken ) ) {
785 return new ConfEditorToken( $internalToken[0], $internalToken[1] );
786 } else {
787 return new ConfEditorToken( $internalToken, $internalToken );
788 }
789 }
790
791 /**
792 * Reset the parse position
793 */
794 function firstToken() {
795 $this->setPos( 0 );
796 $this->prevToken = ConfEditorToken::newEnd();
797 $this->lineNum = 1;
798 $this->colNum = 1;
799 $this->byteNum = 0;
800
801 return $this->currentToken;
802 }
803
804 /**
805 * Get the current token
806 */
807 function currentToken() {
808 return $this->currentToken;
809 }
810
811 /**
812 * Advance the current position and return the resulting next token
813 */
814 function nextToken() {
815 if ( $this->currentToken ) {
816 $text = $this->currentToken->text;
817 $lfCount = substr_count( $text, "\n" );
818 if ( $lfCount ) {
819 $this->lineNum += $lfCount;
820 $this->colNum = strlen( $text ) - strrpos( $text, "\n" );
821 } else {
822 $this->colNum += strlen( $text );
823 }
824 $this->byteNum += strlen( $text );
825 }
826 $this->prevToken = $this->currentToken;
827 $this->setPos( $this->pos + 1 );
828
829 return $this->currentToken;
830 }
831
832 /**
833 * Get the token $offset steps ahead of the current position.
834 * $offset may be negative, to get tokens behind the current position.
835 * @return ConfEditorToken
836 */
837 function getTokenAhead( $offset ) {
838 $pos = $this->pos + $offset;
839 if ( $pos >= count( $this->tokens ) || $pos < 0 ) {
840 return ConfEditorToken::newEnd();
841 } else {
842 return $this->newTokenObj( $this->tokens[$pos] );
843 }
844 }
845
846 /**
847 * Advances the current position past any whitespace or comments
848 */
849 function skipSpace() {
850 while ( $this->currentToken && $this->currentToken->isSkip() ) {
851 $this->nextToken();
852 }
853
854 return $this->currentToken;
855 }
856
857 /**
858 * Throws an error if the current token is not of the given type, and
859 * then advances to the next position.
860 */
861 function expect( $type ) {
862 if ( $this->currentToken && $this->currentToken->type == $type ) {
863 return $this->nextToken();
864 } else {
865 $this->error( "expected " . $this->getTypeName( $type ) .
866 ", got " . $this->getTypeName( $this->currentToken->type ) );
867 }
868 }
869
870 /**
871 * Push a state or two on to the state stack.
872 */
873 function pushState( $nextState, $stateAfterThat = null ) {
874 if ( $stateAfterThat !== null ) {
875 $this->stateStack[] = $stateAfterThat;
876 }
877 $this->stateStack[] = $nextState;
878 }
879
880 /**
881 * Pop a state from the state stack.
882 * @return mixed
883 */
884 function popState() {
885 return array_pop( $this->stateStack );
886 }
887
888 /**
889 * Returns true if the user input path is valid.
890 * This exists to allow "/" and "@" to be reserved for string path keys
891 * @return bool
892 */
893 function validatePath( $path ) {
894 return strpos( $path, '/' ) === false && substr( $path, 0, 1 ) != '@';
895 }
896
897 /**
898 * Internal function to update some things at the end of a path region. Do
899 * not call except from popPath() or nextPath().
900 */
901 function endPath() {
902 $key = '';
903 foreach ( $this->pathStack as $pathInfo ) {
904 if ( $key !== '' ) {
905 $key .= '/';
906 }
907 $key .= $pathInfo['name'];
908 }
909 $pathInfo['endByte'] = $this->byteNum;
910 $pathInfo['endToken'] = $this->pos;
911 $this->pathInfo[$key] = $pathInfo;
912 }
913
914 /**
915 * Go up to a new path level, for example at the start of an array.
916 */
917 function pushPath( $path ) {
918 $this->pathStack[] = array(
919 'name' => $path,
920 'level' => count( $this->pathStack ) + 1,
921 'startByte' => $this->byteNum,
922 'startToken' => $this->pos,
923 'valueStartToken' => false,
924 'valueStartByte' => false,
925 'valueEndToken' => false,
926 'valueEndByte' => false,
927 'nextArrayIndex' => 0,
928 'hasComma' => false,
929 'arrowByte' => false
930 );
931 }
932
933 /**
934 * Go down a path level, for example at the end of an array.
935 */
936 function popPath() {
937 $this->endPath();
938 array_pop( $this->pathStack );
939 }
940
941 /**
942 * Go to the next path on the same level. This ends the current path and
943 * starts a new one. If $path is \@next, the new path is set to the next
944 * numeric array element.
945 */
946 function nextPath( $path ) {
947 $this->endPath();
948 $i = count( $this->pathStack ) - 1;
949 if ( $path == '@next' ) {
950 $nextArrayIndex =& $this->pathStack[$i]['nextArrayIndex'];
951 $this->pathStack[$i]['name'] = $nextArrayIndex;
952 $nextArrayIndex++;
953 } else {
954 $this->pathStack[$i]['name'] = $path;
955 }
956 $this->pathStack[$i] =
957 array(
958 'startByte' => $this->byteNum,
959 'startToken' => $this->pos,
960 'valueStartToken' => false,
961 'valueStartByte' => false,
962 'valueEndToken' => false,
963 'valueEndByte' => false,
964 'hasComma' => false,
965 'arrowByte' => false,
966 ) + $this->pathStack[$i];
967 }
968
969 /**
970 * Mark the start of the value part of a path.
971 */
972 function startPathValue() {
973 $path =& $this->pathStack[count( $this->pathStack ) - 1];
974 $path['valueStartToken'] = $this->pos;
975 $path['valueStartByte'] = $this->byteNum;
976 }
977
978 /**
979 * Mark the end of the value part of a path.
980 */
981 function endPathValue() {
982 $path =& $this->pathStack[count( $this->pathStack ) - 1];
983 $path['valueEndToken'] = $this->pos;
984 $path['valueEndByte'] = $this->byteNum;
985 }
986
987 /**
988 * Mark the comma separator in an array element
989 */
990 function markComma() {
991 $path =& $this->pathStack[count( $this->pathStack ) - 1];
992 $path['hasComma'] = true;
993 }
994
995 /**
996 * Mark the arrow separator in an associative array element
997 */
998 function markArrow() {
999 $path =& $this->pathStack[count( $this->pathStack ) - 1];
1000 $path['arrowByte'] = $this->byteNum;
1001 }
1002
1003 /**
1004 * Generate a parse error
1005 */
1006 function error( $msg ) {
1007 throw new ConfEditorParseError( $this, $msg );
1008 }
1009
1010 /**
1011 * Get a readable name for the given token type.
1012 * @return string
1013 */
1014 function getTypeName( $type ) {
1015 if ( is_int( $type ) ) {
1016 return token_name( $type );
1017 } else {
1018 return "\"$type\"";
1019 }
1020 }
1021
1022 /**
1023 * Looks ahead to see if the given type is the next token type, starting
1024 * from the current position plus the given offset. Skips any intervening
1025 * whitespace.
1026 * @return bool
1027 */
1028 function isAhead( $type, $offset = 0 ) {
1029 $ahead = $offset;
1030 $token = $this->getTokenAhead( $offset );
1031 while ( !$token->isEnd() ) {
1032 if ( $token->isSkip() ) {
1033 $ahead++;
1034 $token = $this->getTokenAhead( $ahead );
1035 continue;
1036 } elseif ( $token->type == $type ) {
1037 // Found the type
1038 return true;
1039 } else {
1040 // Not found
1041 return false;
1042 }
1043 }
1044
1045 return false;
1046 }
1047
1048 /**
1049 * Get the previous token object
1050 */
1051 function prevToken() {
1052 return $this->prevToken;
1053 }
1054
1055 /**
1056 * Echo a reasonably readable representation of the tokenizer array.
1057 */
1058 function dumpTokens() {
1059 $out = '';
1060 foreach ( $this->tokens as $token ) {
1061 $obj = $this->newTokenObj( $token );
1062 $out .= sprintf( "%-28s %s\n",
1063 $this->getTypeName( $obj->type ),
1064 addcslashes( $obj->text, "\0..\37" ) );
1065 }
1066 echo "<pre>" . htmlspecialchars( $out ) . "</pre>";
1067 }
1068 }
1069
1070 /**
1071 * Exception class for parse errors
1072 */
1073 class ConfEditorParseError extends MWException {
1074 var $lineNum, $colNum;
1075
1076 function __construct( $editor, $msg ) {
1077 $this->lineNum = $editor->lineNum;
1078 $this->colNum = $editor->colNum;
1079 parent::__construct( "Parse error on line {$editor->lineNum} " .
1080 "col {$editor->colNum}: $msg" );
1081 }
1082
1083 function highlight( $text ) {
1084 $lines = StringUtils::explode( "\n", $text );
1085 foreach ( $lines as $lineNum => $line ) {
1086 if ( $lineNum == $this->lineNum - 1 ) {
1087 return "$line\n" . str_repeat( ' ', $this->colNum - 1 ) . "^\n";
1088 }
1089 }
1090
1091 return '';
1092 }
1093 }
1094
1095 /**
1096 * Class to wrap a token from the tokenizer.
1097 */
1098 class ConfEditorToken {
1099 var $type, $text;
1100
1101 static $scalarTypes = array( T_LNUMBER, T_DNUMBER, T_STRING, T_CONSTANT_ENCAPSED_STRING );
1102 static $skipTypes = array( T_WHITESPACE, T_COMMENT, T_DOC_COMMENT );
1103
1104 static function newEnd() {
1105 return new self( 'END', '' );
1106 }
1107
1108 function __construct( $type, $text ) {
1109 $this->type = $type;
1110 $this->text = $text;
1111 }
1112
1113 function isSkip() {
1114 return in_array( $this->type, self::$skipTypes );
1115 }
1116
1117 function isScalar() {
1118 return in_array( $this->type, self::$scalarTypes );
1119 }
1120
1121 function isEnd() {
1122 return $this->type == 'END';
1123 }
1124 }