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.
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
19 /** The text to parse */
22 /** The token array from token_get_all() */
25 /** The current position in the token array */
28 /** The current 1-based line number */
31 /** The current 1-based column number */
34 /** The current 0-based byte number */
37 /** The current ConfEditorToken object */
40 /** The previous ConfEditorToken object */
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.
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
69 * The elements of the top of the pathStack for every path encountered, indexed
70 * by slash-separated path.
75 * Next serial number for whitespace placeholder paths (\@extra-N)
80 * Editor state. This consists of the internal copy/insert operations which
81 * are applied to the source string to obtain the destination string.
86 * Simple entry point for command-line testing
88 static function test( $text ) {
90 $ce = new self( $text );
92 } catch ( ConfEditorParseError
$e ) {
93 return $e->getMessage() . "\n" . $e->highlight( $text );
99 * Construct a new parser
101 public function __construct( $text ) {
106 * Edit the text. Returns the edited text.
107 * @param $ops Array of operations.
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
116 * Deletes an array element or statement with the specified path.
118 * array('type' => 'delete', 'path' => '$foo/bar/baz' )
119 * is equivalent to the runtime PHP code:
120 * unset( $foo['bar']['baz'] );
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.
128 * Appends a new element to the end of the array. Adds a trailing comma.
130 * array( 'type' => 'append', 'path', '$foo/bar',
131 * 'key' => 'baz', 'value' => "'x'" )
132 * is like the PHP code:
133 * $foo['bar']['baz'] = 'x';
136 * Insert a new element at the start of the array.
139 public function edit( $ops ) {
142 $this->edits
= array(
143 array( 'copy', 0, strlen( $this->text
) )
145 foreach ( $ops as $op ) {
148 $value = isset( $op['value'] ) ?
$op['value'] : null;
149 $key = isset( $op['key'] ) ?
$op['key'] : null;
153 list( $start, $end ) = $this->findDeletionRegion( $path );
154 $this->replaceSourceRegion( $start, $end, false );
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 );
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 );
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\"" );
174 $lastEltInfo = $this->pathInfo
[$lastEltPath];
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, ',' );
183 // Make the text to insert
184 list( $start, $end ) = $this->findDeletionRegion( $lastEltPath );
186 if ( $key === null ) {
187 list( $indent, ) = $this->getIndent( $start );
188 $textToInsert = "$indent$value,";
190 list( $indent, $arrowIndent ) =
191 $this->getIndent( $start, $key, $lastEltInfo['arrowByte'] );
192 $textToInsert = "$indent$key$arrowIndent=> $value,";
194 $textToInsert .= ( $indent === false ?
' ' : "\n" );
197 $this->replaceSourceRegion( $end, $end, $textToInsert );
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\"" );
205 list( $start, ) = $this->findDeletionRegion( $firstEltPath );
206 $info = $this->pathInfo
[$firstEltPath];
208 // Make the text to insert
209 if ( $key === null ) {
210 list( $indent, ) = $this->getIndent( $start );
211 $textToInsert = "$indent$value,";
213 list( $indent, $arrowIndent ) =
214 $this->getIndent( $start, $key, $info['arrowByte'] );
215 $textToInsert = "$indent$key$arrowIndent=> $value,";
217 $textToInsert .= ( $indent === false ?
' ' : "\n" );
220 $this->replaceSourceRegion( $start, $start, $textToInsert );
223 throw new MWException( "Unrecognised operation: \"$type\"" );
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' )
237 // Do a second parse as a sanity check
241 } catch ( ConfEditorParseError
$e ) {
242 throw new MWException(
243 "Sorry, ConfEditor broke the file during editing and it won't parse anymore: " .
250 * Get the variables defined in the text
251 * @return array( varname => value )
256 foreach( $this->pathInfo
as $path => $data ) {
257 if ( $path[0] != '$' )
259 $trimmedPath = substr( $path, 1 );
260 $name = $data['name'];
261 if ( $name[0] == '@' )
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 );
270 $value = substr( $this->text
, $data['valueStartByte'],
271 $data['valueEndByte'] - $data['valueStartByte']
273 $this->setVar( $vars, $parentPath, $name,
274 $this->parseScalar( $value ) );
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
288 function setVar( &$array, $path, $key, $value ) {
289 $pathArr = explode( '/', $path );
291 if ( $path !== '' ) {
292 foreach ( $pathArr as $p ) {
293 if( !isset( $target[$p] ) )
294 $target[$p] = array();
295 $target =& $target[$p];
298 if ( !isset( $target[$key] ) )
299 $target[$key] = $value;
303 * Parse a scalar value in PHP
304 * @return mixed Parsed value
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' )
322 if ( substr( $str, 0, 5 ) == 'false' )
324 if ( substr( $str, 0, 4 ) == 'null' )
326 // Must be some kind of numeric value, so let PHP's weak typing
327 // be useful for a change
332 * Replace the byte offset region of the source with $newText.
333 * Works by adding elements to the $this->edits array.
335 function replaceSourceRegion( $start, $end, $newText = false ) {
336 // Split all copy operations with a source corresponding to the region
339 foreach ( $this->edits
as $edit ) {
340 if ( $edit[0] !== 'copy' ) {
344 $copyStart = $edit[1];
346 if ( $start >= $copyEnd ||
$end <= $copyStart ) {
347 // Outside this region
351 if ( ( $start < $copyStart && $end > $copyStart )
352 ||
( $start < $copyEnd && $end > $copyEnd )
354 throw new MWException( "Overlapping regions found, can't do the edit" );
357 $newEdits[] = array( 'copy', $copyStart, $start );
358 if ( $newText !== false ) {
359 $newEdits[] = array( 'insert', $newText );
361 $newEdits[] = array( 'copy', $end, $copyEnd );
363 $this->edits
= $newEdits;
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.
371 function findDeletionRegion( $pathName ) {
372 if ( !isset( $this->pathInfo
[$pathName] ) ) {
373 throw new MWException( "Can't find path \"$pathName\"" );
375 $path = $this->pathInfo
[$pathName];
378 while ( $this->pos
!= $path['startToken'] ) {
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'];
390 $lfPos = strrpos( $token->text
, "\n" );
391 if ( $lfPos === false ) {
392 $regionStart -= strlen( $token->text
);
394 // The line start does not include the LF
395 $regionStart -= strlen( $token->text
) - $lfPos - 1;
400 while ( $this->pos
!= $path['endToken'] ) {
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() ) {
409 $lfPos = strpos( $token->text
, "\n" );
410 if ( $lfPos === false ) {
411 $regionEnd +
= strlen( $token->text
);
413 // This should point past the LF
414 $regionEnd +
= $lfPos +
1;
418 return array( $regionStart, $regionEnd );
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
426 * The end position is the past-the-end (end + 1) value as per convention.
428 function findValueRegion( $pathName ) {
429 if ( !isset( $this->pathInfo
[$pathName] ) ) {
430 throw new MWException( "Can't find path \"$pathName\"" );
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\"" );
436 return array( $path['valueStartByte'], $path['valueEndByte'] );
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.
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 == '@' ) {
452 } elseif ( $part1 == "$path/" ) {
453 $lastEltPath = $candidatePath;
454 } elseif ( $lastEltPath !== false ) {
458 if ( $lastEltPath !== false ) {
462 // Try for an interstitial element
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 ) {
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.
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;
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;
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.
504 function getIndent( $pos, $key = false, $arrowPos = false ) {
506 if ( $pos == 0 ||
$this->text
[$pos-1] == "\n" ) {
507 $indentLength = strspn( $this->text
, " \t", $pos );
508 $indent = substr( $this->text
, $pos, $indentLength );
512 if ( $indent !== false && $arrowPos !== false ) {
513 $arrowIndentLength = $arrowPos - $pos - $indentLength - strlen( $key );
514 if ( $arrowIndentLength > 0 ) {
515 $arrowIndent = str_repeat( ' ', $arrowIndentLength );
518 return array( $indent, $arrowIndent );
522 * Run the parser on the text. Throws an exception if the string does not
523 * match our defined subset of PHP syntax.
525 public function parse() {
527 $this->pushState( 'file' );
528 $this->pushPath( '@extra-' . ($this->serial++
) );
529 $token = $this->firstToken();
531 while ( !$token->isEnd() ) {
532 $state = $this->popState();
534 $this->error( 'internal error: empty state stack' );
539 $this->expect( T_OPEN_TAG
);
540 $token = $this->skipSpace();
541 if ( $token->isEnd() ) {
544 $this->pushState( 'statement', 'file 2' );
547 $token = $this->skipSpace();
548 if ( $token->isEnd() ) {
551 $this->pushState( 'statement', 'file 2' );
554 $token = $this->skipSpace();
555 if ( !$this->validatePath( $token->text
) ) {
556 $this->error( "Invalid variable name \"{$token->text}\"" );
558 $this->nextPath( $token->text
);
559 $this->expect( T_VARIABLE
);
561 $arrayAssign = false;
562 if ( $this->currentToken()->type
== '[' ) {
564 $token = $this->skipSpace();
565 if ( !$token->isScalar() ) {
566 $this->error( "expected a string or number for the array key" );
568 if ( $token->type
== T_CONSTANT_ENCAPSED_STRING
) {
569 $text = $this->parseScalar( $token->text
);
571 $text = $token->text
;
573 if ( !$this->validatePath( $text ) ) {
574 $this->error( "Invalid associative array name \"$text\"" );
576 $this->pushPath( $text );
579 $this->expect( ']' );
583 $this->expect( '=' );
585 $this->startPathValue();
587 $this->pushState( 'expression', 'array assign end' );
589 $this->pushState( 'expression', 'statement end' );
591 case 'array assign end':
592 case 'statement end':
593 $this->endPathValue();
594 if ( $state == 'array assign end' )
597 $this->expect( ';' );
598 $this->nextPath( '@extra-' . ($this->serial++
) );
601 $token = $this->skipSpace();
602 if ( $token->type
== T_ARRAY
) {
603 $this->pushState( 'array' );
604 } elseif ( $token->isScalar() ) {
606 } elseif ( $token->type
== T_VARIABLE
) {
609 $this->error( "expected simple expression" );
614 $this->expect( T_ARRAY
);
616 $this->expect( '(' );
618 $this->pushPath( '@extra-' . ($this->serial++
) );
619 if ( $this->isAhead( ')' ) ) {
621 $this->pushState( 'array end' );
623 $this->pushState( 'element', 'array end' );
629 $this->expect( ')' );
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' );
639 $this->nextPath( '@next' );
640 $this->startPathValue();
641 $this->pushState( 'expression', 'element end' );
645 $token = $this->skipSpace();
646 if ( $token->type
== ',' ) {
647 $this->endPathValue();
650 $this->nextPath( '@extra-' . ($this->serial++
) );
651 // Look ahead to find ending bracket
652 if ( $this->isAhead( ")" ) ) {
653 // Found ending bracket, no continuation
656 // No ending bracket, continue to next element
657 $this->pushState( 'element' );
659 } elseif ( $token->type
== ')' ) {
661 $this->endPathValue();
663 $this->error( "expected the next array element or the end of the array" );
666 case 'assoc-element':
667 $token = $this->skipSpace();
668 if ( !$token->isScalar() ) {
669 $this->error( "expected a string or number for the array key" );
671 if ( $token->type
== T_CONSTANT_ENCAPSED_STRING
) {
672 $text = $this->parseScalar( $token->text
);
674 $text = $token->text
;
676 if ( !$this->validatePath( $text ) ) {
677 $this->error( "Invalid associative array name \"$text\"" );
679 $this->nextPath( $text );
683 $this->expect( T_DOUBLE_ARROW
);
685 $this->startPathValue();
686 $this->pushState( 'expression' );
690 if ( count( $this->stateStack
) ) {
691 $this->error( 'unexpected end of file' );
697 * Initialise a parse.
699 protected function initParse() {
700 $this->tokens
= token_get_all( $this->text
);
701 $this->stateStack
= array();
702 $this->pathStack
= array();
704 $this->pathInfo
= array();
709 * Set the parse position. Do not call this except from firstToken() and
710 * nextToken(), there is more to update than just the position.
712 protected function setPos( $pos ) {
714 if ( $this->pos
>= count( $this->tokens
) ) {
715 $this->currentToken
= ConfEditorToken
::newEnd();
717 $this->currentToken
= $this->newTokenObj( $this->tokens
[$this->pos
] );
719 return $this->currentToken
;
723 * Create a ConfEditorToken from an element of token_get_all()
725 function newTokenObj( $internalToken ) {
726 if ( is_array( $internalToken ) ) {
727 return new ConfEditorToken( $internalToken[0], $internalToken[1] );
729 return new ConfEditorToken( $internalToken, $internalToken );
734 * Reset the parse position
736 function firstToken() {
738 $this->prevToken
= ConfEditorToken
::newEnd();
742 return $this->currentToken
;
746 * Get the current token
748 function currentToken() {
749 return $this->currentToken
;
753 * Advance the current position and return the resulting next token
755 function nextToken() {
756 if ( $this->currentToken
) {
757 $text = $this->currentToken
->text
;
758 $lfCount = substr_count( $text, "\n" );
760 $this->lineNum +
= $lfCount;
761 $this->colNum
= strlen( $text ) - strrpos( $text, "\n" );
763 $this->colNum +
= strlen( $text );
765 $this->byteNum +
= strlen( $text );
767 $this->prevToken
= $this->currentToken
;
768 $this->setPos( $this->pos +
1 );
769 return $this->currentToken
;
773 * Get the token $offset steps ahead of the current position.
774 * $offset may be negative, to get tokens behind the current position.
776 function getTokenAhead( $offset ) {
777 $pos = $this->pos +
$offset;
778 if ( $pos >= count( $this->tokens
) ||
$pos < 0 ) {
779 return ConfEditorToken
::newEnd();
781 return $this->newTokenObj( $this->tokens
[$pos] );
786 * Advances the current position past any whitespace or comments
788 function skipSpace() {
789 while ( $this->currentToken
&& $this->currentToken
->isSkip() ) {
792 return $this->currentToken
;
796 * Throws an error if the current token is not of the given type, and
797 * then advances to the next position.
799 function expect( $type ) {
800 if ( $this->currentToken
&& $this->currentToken
->type
== $type ) {
801 return $this->nextToken();
803 $this->error( "expected " . $this->getTypeName( $type ) .
804 ", got " . $this->getTypeName( $this->currentToken
->type
) );
809 * Push a state or two on to the state stack.
811 function pushState( $nextState, $stateAfterThat = null ) {
812 if ( $stateAfterThat !== null ) {
813 $this->stateStack
[] = $stateAfterThat;
815 $this->stateStack
[] = $nextState;
819 * Pop a state from the state stack.
821 function popState() {
822 return array_pop( $this->stateStack
);
826 * Returns true if the user input path is valid.
827 * This exists to allow "/" and "@" to be reserved for string path keys
829 function validatePath( $path ) {
830 return strpos( $path, '/' ) === false && substr( $path, 0, 1 ) != '@';
834 * Internal function to update some things at the end of a path region. Do
835 * not call except from popPath() or nextPath().
839 foreach ( $this->pathStack
as $pathInfo ) {
843 $key .= $pathInfo['name'];
845 $pathInfo['endByte'] = $this->byteNum
;
846 $pathInfo['endToken'] = $this->pos
;
847 $this->pathInfo
[$key] = $pathInfo;
851 * Go up to a new path level, for example at the start of an array.
853 function pushPath( $path ) {
854 $this->pathStack
[] = array(
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,
870 * Go down a path level, for example at the end of an array.
874 array_pop( $this->pathStack
);
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.
882 function nextPath( $path ) {
884 $i = count( $this->pathStack
) - 1;
885 if ( $path == '@next' ) {
886 $nextArrayIndex =& $this->pathStack
[$i]['nextArrayIndex'];
887 $this->pathStack
[$i]['name'] = $nextArrayIndex;
890 $this->pathStack
[$i]['name'] = $path;
892 $this->pathStack
[$i] =
894 'startByte' => $this->byteNum
,
895 'startToken' => $this->pos
,
896 'valueStartToken' => false,
897 'valueStartByte' => false,
898 'valueEndToken' => false,
899 'valueEndByte' => false,
901 'arrowByte' => false,
902 ) +
$this->pathStack
[$i];
906 * Mark the start of the value part of a path.
908 function startPathValue() {
909 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
910 $path['valueStartToken'] = $this->pos
;
911 $path['valueStartByte'] = $this->byteNum
;
915 * Mark the end of the value part of a path.
917 function endPathValue() {
918 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
919 $path['valueEndToken'] = $this->pos
;
920 $path['valueEndByte'] = $this->byteNum
;
924 * Mark the comma separator in an array element
926 function markComma() {
927 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
928 $path['hasComma'] = true;
932 * Mark the arrow separator in an associative array element
934 function markArrow() {
935 $path =& $this->pathStack
[count( $this->pathStack
) - 1];
936 $path['arrowByte'] = $this->byteNum
;
940 * Generate a parse error
942 function error( $msg ) {
943 throw new ConfEditorParseError( $this, $msg );
947 * Get a readable name for the given token type.
949 function getTypeName( $type ) {
950 if ( is_int( $type ) ) {
951 return token_name( $type );
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
962 function isAhead( $type, $offset = 0 ) {
964 $token = $this->getTokenAhead( $offset );
965 while ( !$token->isEnd() ) {
966 if ( $token->isSkip() ) {
968 $token = $this->getTokenAhead( $ahead );
970 } elseif ( $token->type
== $type ) {
982 * Get the previous token object
984 function prevToken() {
985 return $this->prevToken
;
989 * Echo a reasonably readable representation of the tokenizer array.
991 function dumpTokens() {
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" ) );
999 echo "<pre>" . htmlspecialchars( $out ) . "</pre>";
1004 * Exception class for parse errors
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" );
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";
1027 * Class to wrap a token from the tokenizer.
1029 class ConfEditorToken
{
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
);
1035 static function newEnd() {
1036 return new self( 'END', '' );
1039 function __construct( $type, $text ) {
1040 $this->type
= $type;
1041 $this->text
= $text;
1045 return in_array( $this->type
, self
::$skipTypes );
1048 function isScalar() {
1049 return in_array( $this->type
, self
::$scalarTypes );
1053 return $this->type
== 'END';