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.
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
88 * The elements of the top of the pathStack for every path encountered, indexed
89 * by slash-separated path.
94 * Next serial number for whitespace placeholder paths (\@extra-N)
99 * Editor state. This consists of the internal copy/insert operations which
100 * are applied to the source string to obtain the destination string.
105 * Simple entry point for command-line testing
107 * @param $text string
111 static function test( $text ) {
113 $ce = new self( $text );
115 } catch ( ConfEditorParseError
$e ) {
116 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 array $ops 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: " .
277 * Get the variables defined in the text
278 * @return array( varname => value )
283 foreach ( $this->pathInfo
as $path => $data ) {
284 if ( $path[0] != '$' ) {
287 $trimmedPath = substr( $path, 1 );
288 $name = $data['name'];
289 if ( $name[0] == '@' ) {
292 if ( $name[0] == '$' ) {
293 $name = substr( $name, 1 );
295 $parentPath = substr( $trimmedPath, 0,
296 strlen( $trimmedPath ) - strlen( $name ) );
297 if ( substr( $parentPath, -1 ) == '/' ) {
298 $parentPath = substr( $parentPath, 0, -1 );
301 $value = substr( $this->text
, $data['valueStartByte'],
302 $data['valueEndByte'] - $data['valueStartByte']
304 $this->setVar( $vars, $parentPath, $name,
305 $this->parseScalar( $value ) );
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
320 function setVar( &$array, $path, $key, $value ) {
321 $pathArr = explode( '/', $path );
323 if ( $path !== '' ) {
324 foreach ( $pathArr as $p ) {
325 if ( !isset( $target[$p] ) ) {
326 $target[$p] = array();
328 $target =& $target[$p];
331 if ( !isset( $target[$key] ) ) {
332 $target[$key] = $value;
337 * Parse a scalar value in PHP
338 * @return mixed Parsed value
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( '\\\'' => '\'', '\\\\' => '\\' ) );
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 ) );
356 if ( substr( $str, 0, 4 ) == 'true' ) {
359 if ( substr( $str, 0, 5 ) == 'false' ) {
362 if ( substr( $str, 0, 4 ) == 'null' ) {
366 // Must be some kind of numeric value, so let PHP's weak typing
367 // be useful for a change
372 * Replace the byte offset region of the source with $newText.
373 * Works by adding elements to the $this->edits array.
375 function replaceSourceRegion( $start, $end, $newText = false ) {
376 // Split all copy operations with a source corresponding to the region
379 foreach ( $this->edits
as $edit ) {
380 if ( $edit[0] !== 'copy' ) {
384 $copyStart = $edit[1];
386 if ( $start >= $copyEnd ||
$end <= $copyStart ) {
387 // Outside this region
391 if ( ( $start < $copyStart && $end > $copyStart )
392 ||
( $start < $copyEnd && $end > $copyEnd )
394 throw new MWException( "Overlapping regions found, can't do the edit" );
397 $newEdits[] = array( 'copy', $copyStart, $start );
398 if ( $newText !== false ) {
399 $newEdits[] = array( 'insert', $newText );
401 $newEdits[] = array( 'copy', $end, $copyEnd );
403 $this->edits
= $newEdits;
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.
411 * @throws MWException
414 function findDeletionRegion( $pathName ) {
415 if ( !isset( $this->pathInfo
[$pathName] ) ) {
416 throw new MWException( "Can't find path \"$pathName\"" );
418 $path = $this->pathInfo
[$pathName];
421 while ( $this->pos
!= $path['startToken'] ) {
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'];
433 $lfPos = strrpos( $token->text
, "\n" );
434 if ( $lfPos === false ) {
435 $regionStart -= strlen( $token->text
);
437 // The line start does not include the LF
438 $regionStart -= strlen( $token->text
) - $lfPos - 1;
443 while ( $this->pos
!= $path['endToken'] ) {
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() ) {
452 $lfPos = strpos( $token->text
, "\n" );
453 if ( $lfPos === false ) {
454 $regionEnd +
= strlen( $token->text
);
456 // This should point past the LF
457 $regionEnd +
= $lfPos +
1;
462 return array( $regionStart, $regionEnd );
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
470 * The end position is the past-the-end (end + 1) value as per convention.
472 * @throws MWException
475 function findValueRegion( $pathName ) {
476 if ( !isset( $this->pathInfo
[$pathName] ) ) {
477 throw new MWException( "Can't find path \"$pathName\"" );
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\"" );
484 return array( $path['valueStartByte'], $path['valueEndByte'] );
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
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 == '@' ) {
501 } elseif ( $part1 == "$path/" ) {
502 $lastEltPath = $candidatePath;
503 } elseif ( $lastEltPath !== false ) {
507 if ( $lastEltPath !== false ) {
511 // Try for an interstitial element
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 ) {
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
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;
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;
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.
557 function getIndent( $pos, $key = false, $arrowPos = false ) {
559 if ( $pos == 0 ||
$this->text
[$pos - 1] == "\n" ) {
560 $indentLength = strspn( $this->text
, " \t", $pos );
561 $indent = substr( $this->text
, $pos, $indentLength );
565 if ( $indent !== false && $arrowPos !== false ) {
566 $arrowIndentLength = $arrowPos - $pos - $indentLength - strlen( $key );
567 if ( $arrowIndentLength > 0 ) {
568 $arrowIndent = str_repeat( ' ', $arrowIndentLength );
572 return array( $indent, $arrowIndent );
576 * Run the parser on the text. Throws an exception if the string does not
577 * match our defined subset of PHP syntax.
579 public function parse() {
581 $this->pushState( 'file' );
582 $this->pushPath( '@extra-' . ( $this->serial++
) );
583 $token = $this->firstToken();
585 while ( !$token->isEnd() ) {
586 $state = $this->popState();
588 $this->error( 'internal error: empty state stack' );
593 $this->expect( T_OPEN_TAG
);
594 $token = $this->skipSpace();
595 if ( $token->isEnd() ) {
598 $this->pushState( 'statement', 'file 2' );
601 $token = $this->skipSpace();
602 if ( $token->isEnd() ) {
605 $this->pushState( 'statement', 'file 2' );
608 $token = $this->skipSpace();
609 if ( !$this->validatePath( $token->text
) ) {
610 $this->error( "Invalid variable name \"{$token->text}\"" );
612 $this->nextPath( $token->text
);
613 $this->expect( T_VARIABLE
);
615 $arrayAssign = false;
616 if ( $this->currentToken()->type
== '[' ) {
618 $token = $this->skipSpace();
619 if ( !$token->isScalar() ) {
620 $this->error( "expected a string or number for the array key" );
622 if ( $token->type
== T_CONSTANT_ENCAPSED_STRING
) {
623 $text = $this->parseScalar( $token->text
);
625 $text = $token->text
;
627 if ( !$this->validatePath( $text ) ) {
628 $this->error( "Invalid associative array name \"$text\"" );
630 $this->pushPath( $text );
633 $this->expect( ']' );
637 $this->expect( '=' );
639 $this->startPathValue();
640 if ( $arrayAssign ) {
641 $this->pushState( 'expression', 'array assign end' );
643 $this->pushState( 'expression', 'statement end' );
646 case 'array assign end':
647 case 'statement end':
648 $this->endPathValue();
649 if ( $state == 'array assign end' ) {
653 $this->expect( ';' );
654 $this->nextPath( '@extra-' . ( $this->serial++
) );
657 $token = $this->skipSpace();
658 if ( $token->type
== T_ARRAY
) {
659 $this->pushState( 'array' );
660 } elseif ( $token->isScalar() ) {
662 } elseif ( $token->type
== T_VARIABLE
) {
665 $this->error( "expected simple expression" );
670 $this->expect( T_ARRAY
);
672 $this->expect( '(' );
674 $this->pushPath( '@extra-' . ( $this->serial++
) );
675 if ( $this->isAhead( ')' ) ) {
677 $this->pushState( 'array end' );
679 $this->pushState( 'element', 'array end' );
685 $this->expect( ')' );
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' );
695 $this->nextPath( '@next' );
696 $this->startPathValue();
697 $this->pushState( 'expression', 'element end' );
701 $token = $this->skipSpace();
702 if ( $token->type
== ',' ) {
703 $this->endPathValue();
706 $this->nextPath( '@extra-' . ( $this->serial++
) );
707 // Look ahead to find ending bracket
708 if ( $this->isAhead( ")" ) ) {
709 // Found ending bracket, no continuation
712 // No ending bracket, continue to next element
713 $this->pushState( 'element' );
715 } elseif ( $token->type
== ')' ) {
717 $this->endPathValue();
719 $this->error( "expected the next array element or the end of the array" );
722 case 'assoc-element':
723 $token = $this->skipSpace();
724 if ( !$token->isScalar() ) {
725 $this->error( "expected a string or number for the array key" );
727 if ( $token->type
== T_CONSTANT_ENCAPSED_STRING
) {
728 $text = $this->parseScalar( $token->text
);
730 $text = $token->text
;
732 if ( !$this->validatePath( $text ) ) {
733 $this->error( "Invalid associative array name \"$text\"" );
735 $this->nextPath( $text );
739 $this->expect( T_DOUBLE_ARROW
);
741 $this->startPathValue();
742 $this->pushState( 'expression' );
746 if ( count( $this->stateStack
) ) {
747 $this->error( 'unexpected end of file' );
753 * Initialise a parse.
755 protected function initParse() {
756 $this->tokens
= token_get_all( $this->text
);
757 $this->stateStack
= array();
758 $this->pathStack
= array();
760 $this->pathInfo
= array();
765 * Set the parse position. Do not call this except from firstToken() and
766 * nextToken(), there is more to update than just the position.
768 protected function setPos( $pos ) {
770 if ( $this->pos
>= count( $this->tokens
) ) {
771 $this->currentToken
= ConfEditorToken
::newEnd();
773 $this->currentToken
= $this->newTokenObj( $this->tokens
[$this->pos
] );
776 return $this->currentToken
;
780 * Create a ConfEditorToken from an element of token_get_all()
781 * @return ConfEditorToken
783 function newTokenObj( $internalToken ) {
784 if ( is_array( $internalToken ) ) {
785 return new ConfEditorToken( $internalToken[0], $internalToken[1] );
787 return new ConfEditorToken( $internalToken, $internalToken );
792 * Reset the parse position
794 function firstToken() {
796 $this->prevToken
= ConfEditorToken
::newEnd();
801 return $this->currentToken
;
805 * Get the current token
807 function currentToken() {
808 return $this->currentToken
;
812 * Advance the current position and return the resulting next token
814 function nextToken() {
815 if ( $this->currentToken
) {
816 $text = $this->currentToken
->text
;
817 $lfCount = substr_count( $text, "\n" );
819 $this->lineNum +
= $lfCount;
820 $this->colNum
= strlen( $text ) - strrpos( $text, "\n" );
822 $this->colNum +
= strlen( $text );
824 $this->byteNum +
= strlen( $text );
826 $this->prevToken
= $this->currentToken
;
827 $this->setPos( $this->pos +
1 );
829 return $this->currentToken
;
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
837 function getTokenAhead( $offset ) {
838 $pos = $this->pos +
$offset;
839 if ( $pos >= count( $this->tokens
) ||
$pos < 0 ) {
840 return ConfEditorToken
::newEnd();
842 return $this->newTokenObj( $this->tokens
[$pos] );
847 * Advances the current position past any whitespace or comments
849 function skipSpace() {
850 while ( $this->currentToken
&& $this->currentToken
->isSkip() ) {
854 return $this->currentToken
;
858 * Throws an error if the current token is not of the given type, and
859 * then advances to the next position.
861 function expect( $type ) {
862 if ( $this->currentToken
&& $this->currentToken
->type
== $type ) {
863 return $this->nextToken();
865 $this->error( "expected " . $this->getTypeName( $type ) .
866 ", got " . $this->getTypeName( $this->currentToken
->type
) );
871 * Push a state or two on to the state stack.
873 function pushState( $nextState, $stateAfterThat = null ) {
874 if ( $stateAfterThat !== null ) {
875 $this->stateStack
[] = $stateAfterThat;
877 $this->stateStack
[] = $nextState;
881 * Pop a state from the state stack.
884 function popState() {
885 return array_pop( $this->stateStack
);
889 * Returns true if the user input path is valid.
890 * This exists to allow "/" and "@" to be reserved for string path keys
893 function validatePath( $path ) {
894 return strpos( $path, '/' ) === false && substr( $path, 0, 1 ) != '@';
898 * Internal function to update some things at the end of a path region. Do
899 * not call except from popPath() or nextPath().
903 foreach ( $this->pathStack
as $pathInfo ) {
907 $key .= $pathInfo['name'];
909 $pathInfo['endByte'] = $this->byteNum
;
910 $pathInfo['endToken'] = $this->pos
;
911 $this->pathInfo
[$key] = $pathInfo;
915 * Go up to a new path level, for example at the start of an array.
917 function pushPath( $path ) {
918 $this->pathStack
[] = array(
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,
934 * Go down a path level, for example at the end of an array.
938 array_pop( $this->pathStack
);
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.
946 function nextPath( $path ) {
948 $i = count( $this->pathStack
) - 1;
949 if ( $path == '@next' ) {
950 $nextArrayIndex =& $this->pathStack
[$i]['nextArrayIndex'];
951 $this->pathStack
[$i]['name'] = $nextArrayIndex;
954 $this->pathStack
[$i]['name'] = $path;
956 $this->pathStack
[$i] =
958 'startByte' => $this->byteNum
,
959 'startToken' => $this->pos
,
960 'valueStartToken' => false,
961 'valueStartByte' => false,
962 'valueEndToken' => false,
963 'valueEndByte' => false,
965 'arrowByte' => false,
966 ) +
$this->pathStack
[$i];
970 * Mark the start of the value part of a path.
972 function startPathValue() {
973 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
974 $path['valueStartToken'] = $this->pos
;
975 $path['valueStartByte'] = $this->byteNum
;
979 * Mark the end of the value part of a path.
981 function endPathValue() {
982 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
983 $path['valueEndToken'] = $this->pos
;
984 $path['valueEndByte'] = $this->byteNum
;
988 * Mark the comma separator in an array element
990 function markComma() {
991 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
992 $path['hasComma'] = true;
996 * Mark the arrow separator in an associative array element
998 function markArrow() {
999 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
1000 $path['arrowByte'] = $this->byteNum
;
1004 * Generate a parse error
1006 function error( $msg ) {
1007 throw new ConfEditorParseError( $this, $msg );
1011 * Get a readable name for the given token type.
1014 function getTypeName( $type ) {
1015 if ( is_int( $type ) ) {
1016 return token_name( $type );
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
1028 function isAhead( $type, $offset = 0 ) {
1030 $token = $this->getTokenAhead( $offset );
1031 while ( !$token->isEnd() ) {
1032 if ( $token->isSkip() ) {
1034 $token = $this->getTokenAhead( $ahead );
1036 } elseif ( $token->type
== $type ) {
1049 * Get the previous token object
1051 function prevToken() {
1052 return $this->prevToken
;
1056 * Echo a reasonably readable representation of the tokenizer array.
1058 function dumpTokens() {
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" ) );
1066 echo "<pre>" . htmlspecialchars( $out ) . "</pre>";
1071 * Exception class for parse errors
1073 class ConfEditorParseError
extends MWException
{
1074 var $lineNum, $colNum;
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" );
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";
1096 * Class to wrap a token from the tokenizer.
1098 class ConfEditorToken
{
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
);
1104 static function newEnd() {
1105 return new self( 'END', '' );
1108 function __construct( $type, $text ) {
1109 $this->type
= $type;
1110 $this->text
= $text;
1114 return in_array( $this->type
, self
::$skipTypes );
1117 function isScalar() {
1118 return in_array( $this->type
, self
::$scalarTypes );
1122 return $this->type
== 'END';