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