3 * Configuration file editor.
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.
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.
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
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.
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
39 /** The text to parse */
42 /** The token array from token_get_all() */
45 /** The current position in the token array */
48 /** The current 1-based line number */
51 /** The current 1-based column number */
54 /** The current 0-based byte number */
57 /** The current ConfEditorToken object */
60 /** The previous ConfEditorToken object */
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.
71 * The path stack is a stack of associative arrays with the following elements:
72 * name The name of top level of the path
73 * level The level (number of elements) of the path
74 * startByte The byte offset of the start of the path
75 * startToken The token offset of the start
76 * endByte The byte offset of thee
77 * endToken The token offset of the end, plus one
78 * valueStartToken The start token offset of the value part
79 * valueStartByte The start byte offset of the value part
80 * valueEndToken The end token offset of the value part, plus one
81 * valueEndByte The end byte offset of the value part, plus one
82 * nextArrayIndex The next numeric array index at this level
83 * hasComma True if the array element ends with a comma
84 * arrowByte The byte offset of the "=>", or false if there isn't one
89 * The elements of the top of the pathStack for every path encountered, indexed
90 * by slash-separated path.
95 * Next serial number for whitespace placeholder paths (\@extra-N)
100 * Editor state. This consists of the internal copy/insert operations which
101 * are applied to the source string to obtain the destination string.
106 * Simple entry point for command-line testing
108 * @param $text string
112 static function test( $text ) {
114 $ce = new self( $text );
116 } catch ( ConfEditorParseError
$e ) {
117 return $e->getMessage() . "\n" . $e->highlight( $text );
123 * Construct a new parser
125 public function __construct( $text ) {
130 * Edit the text. Returns the edited text.
131 * @param $ops Array of operations.
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
140 * Deletes an array element or statement with the specified path.
142 * array('type' => 'delete', 'path' => '$foo/bar/baz' )
143 * is equivalent to the runtime PHP code:
144 * unset( $foo['bar']['baz'] );
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.
152 * Appends a new element to the end of the array. Adds a trailing comma.
154 * array( 'type' => 'append', 'path', '$foo/bar',
155 * 'key' => 'baz', 'value' => "'x'" )
156 * is like the PHP code:
157 * $foo['bar']['baz'] = 'x';
160 * Insert a new element at the start of the array.
162 * @throws MWException
165 public function edit( $ops ) {
168 $this->edits
= array(
169 array( 'copy', 0, strlen( $this->text
) )
171 foreach ( $ops as $op ) {
174 $value = isset( $op['value'] ) ?
$op['value'] : null;
175 $key = isset( $op['key'] ) ?
$op['key'] : null;
179 list( $start, $end ) = $this->findDeletionRegion( $path );
180 $this->replaceSourceRegion( $start, $end, false );
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 );
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 );
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\"" );
200 $lastEltInfo = $this->pathInfo
[$lastEltPath];
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, ',' );
209 // Make the text to insert
210 list( $start, $end ) = $this->findDeletionRegion( $lastEltPath );
212 if ( $key === null ) {
213 list( $indent, ) = $this->getIndent( $start );
214 $textToInsert = "$indent$value,";
216 list( $indent, $arrowIndent ) =
217 $this->getIndent( $start, $key, $lastEltInfo['arrowByte'] );
218 $textToInsert = "$indent$key$arrowIndent=> $value,";
220 $textToInsert .= ( $indent === false ?
' ' : "\n" );
223 $this->replaceSourceRegion( $end, $end, $textToInsert );
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\"" );
231 list( $start, ) = $this->findDeletionRegion( $firstEltPath );
232 $info = $this->pathInfo
[$firstEltPath];
234 // Make the text to insert
235 if ( $key === null ) {
236 list( $indent, ) = $this->getIndent( $start );
237 $textToInsert = "$indent$value,";
239 list( $indent, $arrowIndent ) =
240 $this->getIndent( $start, $key, $info['arrowByte'] );
241 $textToInsert = "$indent$key$arrowIndent=> $value,";
243 $textToInsert .= ( $indent === false ?
' ' : "\n" );
246 $this->replaceSourceRegion( $start, $start, $textToInsert );
249 throw new MWException( "Unrecognised operation: \"$type\"" );
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' )
263 // Do a second parse as a sanity check
267 } catch ( ConfEditorParseError
$e ) {
268 throw new MWException(
269 "Sorry, ConfEditor broke the file during editing and it won't parse anymore: " .
276 * Get the variables defined in the text
277 * @return array( varname => value )
282 foreach( $this->pathInfo
as $path => $data ) {
283 if ( $path[0] != '$' )
285 $trimmedPath = substr( $path, 1 );
286 $name = $data['name'];
287 if ( $name[0] == '@' )
289 if ( $name[0] == '$' )
290 $name = substr( $name, 1 );
291 $parentPath = substr( $trimmedPath, 0,
292 strlen( $trimmedPath ) - strlen( $name ) );
293 if( substr( $parentPath, -1 ) == '/' )
294 $parentPath = substr( $parentPath, 0, -1 );
296 $value = substr( $this->text
, $data['valueStartByte'],
297 $data['valueEndByte'] - $data['valueStartByte']
299 $this->setVar( $vars, $parentPath, $name,
300 $this->parseScalar( $value ) );
306 * Set a value in an array, unless it's set already. For instance,
307 * setVar( $arr, 'foo/bar', 'baz', 3 ); will set
308 * $arr['foo']['bar']['baz'] = 3;
309 * @param $array array
310 * @param $path string slash-delimited path
311 * @param $key mixed Key
312 * @param $value mixed Value
314 function setVar( &$array, $path, $key, $value ) {
315 $pathArr = explode( '/', $path );
317 if ( $path !== '' ) {
318 foreach ( $pathArr as $p ) {
319 if( !isset( $target[$p] ) )
320 $target[$p] = array();
321 $target =& $target[$p];
324 if ( !isset( $target[$key] ) )
325 $target[$key] = $value;
329 * Parse a scalar value in PHP
330 * @return mixed Parsed value
332 function parseScalar( $str ) {
333 if ( $str !== '' && $str[0] == '\'' )
334 // Single-quoted string
335 // @todo FIXME: trim() call is due to mystery bug where whitespace gets
336 // appended to the token; without it we ended up reading in the
337 // extra quote on the end!
338 return strtr( substr( trim( $str ), 1, -1 ),
339 array( '\\\'' => '\'', '\\\\' => '\\' ) );
340 if ( $str !== '' && $str[0] == '"' )
341 // Double-quoted string
342 // @todo FIXME: trim() call is due to mystery bug where whitespace gets
343 // appended to the token; without it we ended up reading in the
344 // extra quote on the end!
345 return stripcslashes( substr( trim( $str ), 1, -1 ) );
346 if ( substr( $str, 0, 4 ) == 'true' )
348 if ( substr( $str, 0, 5 ) == 'false' )
350 if ( substr( $str, 0, 4 ) == 'null' )
352 // Must be some kind of numeric value, so let PHP's weak typing
353 // be useful for a change
358 * Replace the byte offset region of the source with $newText.
359 * Works by adding elements to the $this->edits array.
361 function replaceSourceRegion( $start, $end, $newText = false ) {
362 // Split all copy operations with a source corresponding to the region
365 foreach ( $this->edits
as $edit ) {
366 if ( $edit[0] !== 'copy' ) {
370 $copyStart = $edit[1];
372 if ( $start >= $copyEnd ||
$end <= $copyStart ) {
373 // Outside this region
377 if ( ( $start < $copyStart && $end > $copyStart )
378 ||
( $start < $copyEnd && $end > $copyEnd )
380 throw new MWException( "Overlapping regions found, can't do the edit" );
383 $newEdits[] = array( 'copy', $copyStart, $start );
384 if ( $newText !== false ) {
385 $newEdits[] = array( 'insert', $newText );
387 $newEdits[] = array( 'copy', $end, $copyEnd );
389 $this->edits
= $newEdits;
393 * Finds the source byte region which you would want to delete, if $pathName
394 * was to be deleted. Includes the leading spaces and tabs, the trailing line
395 * break, and any comments in between.
397 * @throws MWException
400 function findDeletionRegion( $pathName ) {
401 if ( !isset( $this->pathInfo
[$pathName] ) ) {
402 throw new MWException( "Can't find path \"$pathName\"" );
404 $path = $this->pathInfo
[$pathName];
407 while ( $this->pos
!= $path['startToken'] ) {
410 $regionStart = $path['startByte'];
411 for ( $offset = -1; $offset >= -$this->pos
; $offset-- ) {
412 $token = $this->getTokenAhead( $offset );
413 if ( !$token->isSkip() ) {
414 // If there is other content on the same line, don't move the start point
415 // back, because that will cause the regions to overlap.
416 $regionStart = $path['startByte'];
419 $lfPos = strrpos( $token->text
, "\n" );
420 if ( $lfPos === false ) {
421 $regionStart -= strlen( $token->text
);
423 // The line start does not include the LF
424 $regionStart -= strlen( $token->text
) - $lfPos - 1;
429 while ( $this->pos
!= $path['endToken'] ) {
432 $regionEnd = $path['endByte']; // past the end
433 for ( $offset = 0; $offset < count( $this->tokens
) - $this->pos
; $offset++
) {
434 $token = $this->getTokenAhead( $offset );
435 if ( !$token->isSkip() ) {
438 $lfPos = strpos( $token->text
, "\n" );
439 if ( $lfPos === false ) {
440 $regionEnd +
= strlen( $token->text
);
442 // This should point past the LF
443 $regionEnd +
= $lfPos +
1;
447 return array( $regionStart, $regionEnd );
451 * Find the byte region in the source corresponding to the value part.
452 * This includes the quotes, but does not include the trailing comma
455 * The end position is the past-the-end (end + 1) value as per convention.
457 * @throws MWException
460 function findValueRegion( $pathName ) {
461 if ( !isset( $this->pathInfo
[$pathName] ) ) {
462 throw new MWException( "Can't find path \"$pathName\"" );
464 $path = $this->pathInfo
[$pathName];
465 if ( $path['valueStartByte'] === false ||
$path['valueEndByte'] === false ) {
466 throw new MWException( "Can't find value region for path \"$pathName\"" );
468 return array( $path['valueStartByte'], $path['valueEndByte'] );
472 * Find the path name of the last element in the array.
473 * If the array is empty, this will return the \@extra interstitial element.
474 * If the specified path is not found or is not an array, it will return false.
475 * @return bool|int|string
477 function findLastArrayElement( $path ) {
478 // Try for a real element
479 $lastEltPath = false;
480 foreach ( $this->pathInfo
as $candidatePath => $info ) {
481 $part1 = substr( $candidatePath, 0, strlen( $path ) +
1 );
482 $part2 = substr( $candidatePath, strlen( $path ) +
1, 1 );
483 if ( $part2 == '@' ) {
485 } elseif ( $part1 == "$path/" ) {
486 $lastEltPath = $candidatePath;
487 } elseif ( $lastEltPath !== false ) {
491 if ( $lastEltPath !== false ) {
495 // Try for an interstitial element
497 foreach ( $this->pathInfo
as $candidatePath => $info ) {
498 $part1 = substr( $candidatePath, 0, strlen( $path ) +
1 );
499 if ( $part1 == "$path/" ) {
500 $extraPath = $candidatePath;
501 } elseif ( $extraPath !== false ) {
509 * Find the path name of first element in the array.
510 * If the array is empty, this will return the \@extra interstitial element.
511 * If the specified path is not found or is not an array, it will return false.
512 * @return bool|int|string
514 function findFirstArrayElement( $path ) {
515 // Try for an ordinary element
516 foreach ( $this->pathInfo
as $candidatePath => $info ) {
517 $part1 = substr( $candidatePath, 0, strlen( $path ) +
1 );
518 $part2 = substr( $candidatePath, strlen( $path ) +
1, 1 );
519 if ( $part1 == "$path/" && $part2 != '@' ) {
520 return $candidatePath;
524 // Try for an interstitial element
525 foreach ( $this->pathInfo
as $candidatePath => $info ) {
526 $part1 = substr( $candidatePath, 0, strlen( $path ) +
1 );
527 if ( $part1 == "$path/" ) {
528 return $candidatePath;
535 * Get the indent string which sits after a given start position.
536 * Returns false if the position is not at the start of the line.
539 function getIndent( $pos, $key = false, $arrowPos = false ) {
541 if ( $pos == 0 ||
$this->text
[$pos-1] == "\n" ) {
542 $indentLength = strspn( $this->text
, " \t", $pos );
543 $indent = substr( $this->text
, $pos, $indentLength );
547 if ( $indent !== false && $arrowPos !== false ) {
548 $arrowIndentLength = $arrowPos - $pos - $indentLength - strlen( $key );
549 if ( $arrowIndentLength > 0 ) {
550 $arrowIndent = str_repeat( ' ', $arrowIndentLength );
553 return array( $indent, $arrowIndent );
557 * Run the parser on the text. Throws an exception if the string does not
558 * match our defined subset of PHP syntax.
560 public function parse() {
562 $this->pushState( 'file' );
563 $this->pushPath( '@extra-' . ($this->serial++
) );
564 $token = $this->firstToken();
566 while ( !$token->isEnd() ) {
567 $state = $this->popState();
569 $this->error( 'internal error: empty state stack' );
574 $this->expect( T_OPEN_TAG
);
575 $token = $this->skipSpace();
576 if ( $token->isEnd() ) {
579 $this->pushState( 'statement', 'file 2' );
582 $token = $this->skipSpace();
583 if ( $token->isEnd() ) {
586 $this->pushState( 'statement', 'file 2' );
589 $token = $this->skipSpace();
590 if ( !$this->validatePath( $token->text
) ) {
591 $this->error( "Invalid variable name \"{$token->text}\"" );
593 $this->nextPath( $token->text
);
594 $this->expect( T_VARIABLE
);
596 $arrayAssign = false;
597 if ( $this->currentToken()->type
== '[' ) {
599 $token = $this->skipSpace();
600 if ( !$token->isScalar() ) {
601 $this->error( "expected a string or number for the array key" );
603 if ( $token->type
== T_CONSTANT_ENCAPSED_STRING
) {
604 $text = $this->parseScalar( $token->text
);
606 $text = $token->text
;
608 if ( !$this->validatePath( $text ) ) {
609 $this->error( "Invalid associative array name \"$text\"" );
611 $this->pushPath( $text );
614 $this->expect( ']' );
618 $this->expect( '=' );
620 $this->startPathValue();
622 $this->pushState( 'expression', 'array assign end' );
624 $this->pushState( 'expression', 'statement end' );
626 case 'array assign end':
627 case 'statement end':
628 $this->endPathValue();
629 if ( $state == 'array assign end' )
632 $this->expect( ';' );
633 $this->nextPath( '@extra-' . ($this->serial++
) );
636 $token = $this->skipSpace();
637 if ( $token->type
== T_ARRAY
) {
638 $this->pushState( 'array' );
639 } elseif ( $token->isScalar() ) {
641 } elseif ( $token->type
== T_VARIABLE
) {
644 $this->error( "expected simple expression" );
649 $this->expect( T_ARRAY
);
651 $this->expect( '(' );
653 $this->pushPath( '@extra-' . ($this->serial++
) );
654 if ( $this->isAhead( ')' ) ) {
656 $this->pushState( 'array end' );
658 $this->pushState( 'element', 'array end' );
664 $this->expect( ')' );
667 $token = $this->skipSpace();
668 // Look ahead to find the double arrow
669 if ( $token->isScalar() && $this->isAhead( T_DOUBLE_ARROW
, 1 ) ) {
670 // Found associative element
671 $this->pushState( 'assoc-element', 'element end' );
674 $this->nextPath( '@next' );
675 $this->startPathValue();
676 $this->pushState( 'expression', 'element end' );
680 $token = $this->skipSpace();
681 if ( $token->type
== ',' ) {
682 $this->endPathValue();
685 $this->nextPath( '@extra-' . ($this->serial++
) );
686 // Look ahead to find ending bracket
687 if ( $this->isAhead( ")" ) ) {
688 // Found ending bracket, no continuation
691 // No ending bracket, continue to next element
692 $this->pushState( 'element' );
694 } elseif ( $token->type
== ')' ) {
696 $this->endPathValue();
698 $this->error( "expected the next array element or the end of the array" );
701 case 'assoc-element':
702 $token = $this->skipSpace();
703 if ( !$token->isScalar() ) {
704 $this->error( "expected a string or number for the array key" );
706 if ( $token->type
== T_CONSTANT_ENCAPSED_STRING
) {
707 $text = $this->parseScalar( $token->text
);
709 $text = $token->text
;
711 if ( !$this->validatePath( $text ) ) {
712 $this->error( "Invalid associative array name \"$text\"" );
714 $this->nextPath( $text );
718 $this->expect( T_DOUBLE_ARROW
);
720 $this->startPathValue();
721 $this->pushState( 'expression' );
725 if ( count( $this->stateStack
) ) {
726 $this->error( 'unexpected end of file' );
732 * Initialise a parse.
734 protected function initParse() {
735 $this->tokens
= token_get_all( $this->text
);
736 $this->stateStack
= array();
737 $this->pathStack
= array();
739 $this->pathInfo
= array();
744 * Set the parse position. Do not call this except from firstToken() and
745 * nextToken(), there is more to update than just the position.
747 protected function setPos( $pos ) {
749 if ( $this->pos
>= count( $this->tokens
) ) {
750 $this->currentToken
= ConfEditorToken
::newEnd();
752 $this->currentToken
= $this->newTokenObj( $this->tokens
[$this->pos
] );
754 return $this->currentToken
;
758 * Create a ConfEditorToken from an element of token_get_all()
759 * @return ConfEditorToken
761 function newTokenObj( $internalToken ) {
762 if ( is_array( $internalToken ) ) {
763 return new ConfEditorToken( $internalToken[0], $internalToken[1] );
765 return new ConfEditorToken( $internalToken, $internalToken );
770 * Reset the parse position
772 function firstToken() {
774 $this->prevToken
= ConfEditorToken
::newEnd();
778 return $this->currentToken
;
782 * Get the current token
784 function currentToken() {
785 return $this->currentToken
;
789 * Advance the current position and return the resulting next token
791 function nextToken() {
792 if ( $this->currentToken
) {
793 $text = $this->currentToken
->text
;
794 $lfCount = substr_count( $text, "\n" );
796 $this->lineNum +
= $lfCount;
797 $this->colNum
= strlen( $text ) - strrpos( $text, "\n" );
799 $this->colNum +
= strlen( $text );
801 $this->byteNum +
= strlen( $text );
803 $this->prevToken
= $this->currentToken
;
804 $this->setPos( $this->pos +
1 );
805 return $this->currentToken
;
809 * Get the token $offset steps ahead of the current position.
810 * $offset may be negative, to get tokens behind the current position.
811 * @return ConfEditorToken
813 function getTokenAhead( $offset ) {
814 $pos = $this->pos +
$offset;
815 if ( $pos >= count( $this->tokens
) ||
$pos < 0 ) {
816 return ConfEditorToken
::newEnd();
818 return $this->newTokenObj( $this->tokens
[$pos] );
823 * Advances the current position past any whitespace or comments
825 function skipSpace() {
826 while ( $this->currentToken
&& $this->currentToken
->isSkip() ) {
829 return $this->currentToken
;
833 * Throws an error if the current token is not of the given type, and
834 * then advances to the next position.
836 function expect( $type ) {
837 if ( $this->currentToken
&& $this->currentToken
->type
== $type ) {
838 return $this->nextToken();
840 $this->error( "expected " . $this->getTypeName( $type ) .
841 ", got " . $this->getTypeName( $this->currentToken
->type
) );
846 * Push a state or two on to the state stack.
848 function pushState( $nextState, $stateAfterThat = null ) {
849 if ( $stateAfterThat !== null ) {
850 $this->stateStack
[] = $stateAfterThat;
852 $this->stateStack
[] = $nextState;
856 * Pop a state from the state stack.
859 function popState() {
860 return array_pop( $this->stateStack
);
864 * Returns true if the user input path is valid.
865 * This exists to allow "/" and "@" to be reserved for string path keys
868 function validatePath( $path ) {
869 return strpos( $path, '/' ) === false && substr( $path, 0, 1 ) != '@';
873 * Internal function to update some things at the end of a path region. Do
874 * not call except from popPath() or nextPath().
878 foreach ( $this->pathStack
as $pathInfo ) {
882 $key .= $pathInfo['name'];
884 $pathInfo['endByte'] = $this->byteNum
;
885 $pathInfo['endToken'] = $this->pos
;
886 $this->pathInfo
[$key] = $pathInfo;
890 * Go up to a new path level, for example at the start of an array.
892 function pushPath( $path ) {
893 $this->pathStack
[] = array(
895 'level' => count( $this->pathStack
) +
1,
896 'startByte' => $this->byteNum
,
897 'startToken' => $this->pos
,
898 'valueStartToken' => false,
899 'valueStartByte' => false,
900 'valueEndToken' => false,
901 'valueEndByte' => false,
902 'nextArrayIndex' => 0,
909 * Go down a path level, for example at the end of an array.
913 array_pop( $this->pathStack
);
917 * Go to the next path on the same level. This ends the current path and
918 * starts a new one. If $path is \@next, the new path is set to the next
919 * numeric array element.
921 function nextPath( $path ) {
923 $i = count( $this->pathStack
) - 1;
924 if ( $path == '@next' ) {
925 $nextArrayIndex =& $this->pathStack
[$i]['nextArrayIndex'];
926 $this->pathStack
[$i]['name'] = $nextArrayIndex;
929 $this->pathStack
[$i]['name'] = $path;
931 $this->pathStack
[$i] =
933 'startByte' => $this->byteNum
,
934 'startToken' => $this->pos
,
935 'valueStartToken' => false,
936 'valueStartByte' => false,
937 'valueEndToken' => false,
938 'valueEndByte' => false,
940 'arrowByte' => false,
941 ) +
$this->pathStack
[$i];
945 * Mark the start of the value part of a path.
947 function startPathValue() {
948 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
949 $path['valueStartToken'] = $this->pos
;
950 $path['valueStartByte'] = $this->byteNum
;
954 * Mark the end of the value part of a path.
956 function endPathValue() {
957 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
958 $path['valueEndToken'] = $this->pos
;
959 $path['valueEndByte'] = $this->byteNum
;
963 * Mark the comma separator in an array element
965 function markComma() {
966 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
967 $path['hasComma'] = true;
971 * Mark the arrow separator in an associative array element
973 function markArrow() {
974 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
975 $path['arrowByte'] = $this->byteNum
;
979 * Generate a parse error
981 function error( $msg ) {
982 throw new ConfEditorParseError( $this, $msg );
986 * Get a readable name for the given token type.
989 function getTypeName( $type ) {
990 if ( is_int( $type ) ) {
991 return token_name( $type );
998 * Looks ahead to see if the given type is the next token type, starting
999 * from the current position plus the given offset. Skips any intervening
1003 function isAhead( $type, $offset = 0 ) {
1005 $token = $this->getTokenAhead( $offset );
1006 while ( !$token->isEnd() ) {
1007 if ( $token->isSkip() ) {
1009 $token = $this->getTokenAhead( $ahead );
1011 } elseif ( $token->type
== $type ) {
1023 * Get the previous token object
1025 function prevToken() {
1026 return $this->prevToken
;
1030 * Echo a reasonably readable representation of the tokenizer array.
1032 function dumpTokens() {
1034 foreach ( $this->tokens
as $token ) {
1035 $obj = $this->newTokenObj( $token );
1036 $out .= sprintf( "%-28s %s\n",
1037 $this->getTypeName( $obj->type
),
1038 addcslashes( $obj->text
, "\0..\37" ) );
1040 echo "<pre>" . htmlspecialchars( $out ) . "</pre>";
1045 * Exception class for parse errors
1047 class ConfEditorParseError
extends MWException
{
1048 var $lineNum, $colNum;
1049 function __construct( $editor, $msg ) {
1050 $this->lineNum
= $editor->lineNum
;
1051 $this->colNum
= $editor->colNum
;
1052 parent
::__construct( "Parse error on line {$editor->lineNum} " .
1053 "col {$editor->colNum}: $msg" );
1056 function highlight( $text ) {
1057 $lines = StringUtils
::explode( "\n", $text );
1058 foreach ( $lines as $lineNum => $line ) {
1059 if ( $lineNum == $this->lineNum
- 1 ) {
1060 return "$line\n" .str_repeat( ' ', $this->colNum
- 1 ) . "^\n";
1068 * Class to wrap a token from the tokenizer.
1070 class ConfEditorToken
{
1073 static $scalarTypes = array( T_LNUMBER
, T_DNUMBER
, T_STRING
, T_CONSTANT_ENCAPSED_STRING
);
1074 static $skipTypes = array( T_WHITESPACE
, T_COMMENT
, T_DOC_COMMENT
);
1076 static function newEnd() {
1077 return new self( 'END', '' );
1080 function __construct( $type, $text ) {
1081 $this->type
= $type;
1082 $this->text
= $text;
1086 return in_array( $this->type
, self
::$skipTypes );
1089 function isScalar() {
1090 return in_array( $this->type
, self
::$scalarTypes );
1094 return $this->type
== 'END';