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