r52055 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52054‎ | r52055 | r52056 >
Date:17:13, 17 June 2009
Author:catrope
Status:ok
Tags:
Comment:
switch-master: Extend parser to handle statements of the form $variable[scalar] = expression;
Modified paths:
  • /trunk/tools/switch-master/ConfEditor.php (modified) (history)

Diff [purge]

Index: trunk/tools/switch-master/ConfEditor.php
@@ -286,7 +286,6 @@
287287 * @param $value mixed Value
288288 */
289289 function setVar( &$array, $path, $key, $value ) {
290 - echo "$path [$key] = $value\n";
291290 $pathArr = explode( '/', $path );
292291 $target =& $array;
293292 if ( $path !== '' ) {
@@ -305,11 +304,11 @@
306305 * @return mixed Parsed value
307306 */
308307 function parseScalar( $str ) {
309 - if ( @$str[0] == '\'' )
 308+ if ( $str !== '' && $str[0] == '\'' )
310309 // Single-quoted string
311310 return strtr( substr( $str, 1, -1 ),
312311 array( '\\\'' => '\'', '\\\\' => '\\' ) );
313 - if ( @$str[0] == '"' )
 312+ if ( $str !== '' && @$str[0] == '"' )
314313 // Double-quoted string
315314 return strtr( stripcslashes( substr( $str, 1, -1 ) ),
316315 array( '\'' => '\\\'' ) );
@@ -555,15 +554,43 @@
556555 $this->nextPath( $token->text );
557556 $this->expect( T_VARIABLE );
558557 $this->skipSpace();
 558+ $arrayAssign = false;
 559+ if ( $this->currentToken()->type == '[' ) {
 560+ $this->nextToken();
 561+ $token = $this->skipSpace();
 562+ if ( !$token->isScalar() ) {
 563+ $this->error( "expected a string or number for the array key" );
 564+ }
 565+ if ( $token->type == T_CONSTANT_ENCAPSED_STRING ) {
 566+ $text = $this->parseScalar( $token->text );
 567+ } else {
 568+ $text = $token->text;
 569+ }
 570+ if ( !$this->validatePath( $text ) ) {
 571+ $this->error( "Invalid associative array name \"$text\"" );
 572+ }
 573+ $this->pushPath( $text );
 574+ $this->nextToken();
 575+ $this->skipSpace();
 576+ $this->expect( ']' );
 577+ $this->skipSpace();
 578+ $arrayAssign = true;
 579+ }
559580 $this->expect( '=' );
560581 $this->skipSpace();
561582 $this->startPathValue();
562 - $this->pushState( 'expression', 'statement end' );
 583+ if ( $arrayAssign )
 584+ $this->pushState( 'expression', 'array assign end' );
 585+ else
 586+ $this->pushState( 'expression', 'statement end' );
563587 break;
 588+ case 'array assign end':
564589 case 'statement end':
 590+ $this->endPathValue();
 591+ if ( $state == 'array assign end' )
 592+ $this->popPath();
565593 $this->skipSpace();
566 - $this->endPathValue();
567 - $this->expect( ';');
 594+ $this->expect( ';' );
568595 $this->nextPath( '@extra-' . ($this->serial++) );
569596 break;
570597 case 'expression':

Status & tagging log