jenkins-bot has submitted this change and it was merged.

Change subject: add static variables (version 0.3.2)
......................................................................


add static variables (version 0.3.2)

Change-Id: I70d4f6fd15673adb2dabf91c6070c2543623aaca
---
M Foxway.php
M includes/Debug.php
M includes/ErrorMessage.php
M includes/Interpreter.php
M includes/Runtime.php
M tests/phpunit/includes/InterpreterTest.php
6 files changed, 133 insertions(+), 22 deletions(-)

Approvals:
  Pastakhov: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Foxway.php b/Foxway.php
index 3ce54f1..bc90aac 100644
--- a/Foxway.php
+++ b/Foxway.php
@@ -15,7 +15,7 @@
        die( 'This file is an extension to MediaWiki and thus not a valid entry 
point.' );
 }
 
-define( 'Foxway_VERSION' , '0.3.1' );
+define( 'Foxway_VERSION' , '0.3.2' );
 
 // Register this extension on Special:Version
 $wgExtensionCredits['parserhook'][] = array(
diff --git a/includes/Debug.php b/includes/Debug.php
index 44a56e6..fc21e7f 100644
--- a/includes/Debug.php
+++ b/includes/Debug.php
@@ -80,6 +80,7 @@
                        case T_ELSE:
                        case T_ELSEIF:
                        case T_ARRAY:
+                       case T_STATIC:
                                $class = 'foxway_construct';
                                break;
                        case T_VARIABLE:
diff --git a/includes/ErrorMessage.php b/includes/ErrorMessage.php
index f826428..0ac0e40 100644
--- a/includes/ErrorMessage.php
+++ b/includes/ErrorMessage.php
@@ -7,21 +7,34 @@
  * @ingroup Foxway
  * @author Pavel Astakhov <pastak...@yandex.ru>
  * @licence GNU General Public Licence 2.0 or later
- *
- * @property-read int $type Type of error
- * @property-read mixed $params Params of error
  */
 class ErrorMessage implements iRawOutput {
-       private $line;
-       private $tokenLine;
-       private $type;
-       private $params;
+       /**
+        * __LINE__ of source code returned error
+        * @var int
+        */
+       public $line;
+       /**
+        * line parser source code that contains the error
+        * @var int
+        */
+       public $tokenLine;
+       /**
+        * Type of error
+        * @var int
+        */
+       public $type;
+       /**
+        * Params of error
+        * @var mixed
+        */
+       public $params;
        private $caller;
 
        public function __construct( $line, $tokenLine, $type, $params ) {
                $this->line = $line;
-               $this->type = $type;
                $this->tokenLine = $tokenLine;
+               $this->type = $type;
                $this->params = $params;
                $this->caller = wfGetCaller();
 
@@ -34,17 +47,6 @@
                                array( 'class' => 'error', 'title' => 'Report 
from ' .htmlspecialchars($this->caller). 'line '.htmlspecialchars($this->line) 
), // TODO wfMessage
                                $this->getMessage()
                                );
-       }
-
-       public function __get($name) {
-               switch ($name) {
-                       case 'type':
-                               return $this->type;
-                               break;
-                       case 'params':
-                               return $this->params;
-                               break;
-               }
        }
 
        public function getMessage() {
diff --git a/includes/Interpreter.php b/includes/Interpreter.php
index 34ea40a..a395fd4 100644
--- a/includes/Interpreter.php
+++ b/includes/Interpreter.php
@@ -21,6 +21,7 @@
 define( 'FOXWAY_EXPECT_PARENTHES_WITH_DOUBLE_ARROW', 1 << 12 );
 define( 'FOXWAY_ALLOW_DOUBLE_ARROW', 1 << 13 );
 define( 'FOXWAY_NEED_CONCATENATION_OPERATOR', 1 << 14 );
+define( 'FOXWAY_EXPECT_STATIC_VARIABLE', 1 << 15 );
 
 /**
  * Interpreter class of Foxway extension.
@@ -155,6 +156,9 @@
                                        $commandResult = 
$runtime->getCommandResult();
                                        break;
                                case ',':
+                                       if( $parenthesFlags & 
FOXWAY_EXPECT_STATIC_VARIABLE ) {
+                                               continue;
+                                       }
                                        if ( !($parenthesFlags & 
FOXWAY_EXPECT_LIST_PARAMS) ) {
                                                $return[] = new 
ErrorMessage(__LINE__, $tokenLine, E_PARSE, $id);
                                                break 2;
@@ -306,6 +310,32 @@
                                        if( $expected && in_array(T_VARIABLE, 
$expected) ) {
                                                if( $parenthesFlags & 
FOXWAY_EXPECT_CURLY_CLOSE ) {
                                                        $expected = array( '}' 
);
+                                               } elseif( $parenthesFlags & 
FOXWAY_EXPECT_STATIC_VARIABLE ) {
+                                                       $r = 
$runtime->addParamVariable($text, T_STATIC);
+                                                       if( $r instanceof 
ErrorMessage ) {
+                                                               $r->tokenLine = 
$tokenLine;
+                                                               $return[] = $r;
+                                                               break 2;
+                                                       }
+                                                       if( $r === false ) {
+                                                               $count = 
count($tokens);
+                                                               for( ; $index < 
$count; $index++ ) { // skip already initialized static variable;
+                                                                       switch 
($tokens[$index]) {
+                                                                          case 
',':
+                                                                               
   $expected = array(T_VARIABLE);
+                                                                               
   continue 3;
+                                                                          case 
';':
+                                                                               
   $index--;
+                                                                               
   $expected = array(';');
+                                                                               
   continue 3;
+                                                                  }
+                                                               }
+                                                               $return[] = new 
ErrorMessage(__LINE__, $tokenLine, E_PARSE, '$end');
+                                                               break 2;
+                                                       } else {
+                                                               $expected = 
array( '=', ',', ';' );
+                                                       }
+                                                       continue;
                                                } else {
                                                        $expected = 
array_merge( self::$arrayOperators, self::$assigmentOperators );
                                                        $parenthesFlags |= 
FOXWAY_ALLOW_ASSIGMENT;
@@ -479,7 +509,6 @@
                                        // break is not necessary here
                                case T_ECHO:
                                case ',':
-                               case '=':
                                case T_CONCAT_EQUAL:    // .=
                                case T_PLUS_EQUAL:              // +=
                                case T_MINUS_EQUAL:             // -=
@@ -514,6 +543,13 @@
                                case '?':
                                case '[':
                                        $expected = self::$arrayParams;
+                                       break;
+                               case '=':
+                                       if( $parenthesFlags & 
FOXWAY_EXPECT_STATIC_VARIABLE ) {
+                                               $expected = array( 
T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_DNUMBER ); // static and global 
variables
+                                       }else{
+                                               $expected = self::$arrayParams;
+                                       }
                                        break;
                                case ':':
                                        $expected = self::$arrayParams;
@@ -552,6 +588,10 @@
                                case '{':
                                        $curlyLever++;
                                        break;
+                               case T_STATIC:
+                                       $parenthesFlags |= 
FOXWAY_EXPECT_STATIC_VARIABLE;
+                                       $expected = array(T_VARIABLE);
+                                       break;
                        }
 
                        /*****************   EXPECT  PHASE  TWO  
**************************/
diff --git a/includes/Runtime.php b/includes/Runtime.php
index 5fa1b12..5834521 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -29,6 +29,7 @@
 
        protected $stack = array();
        protected static $variables = array();
+       protected static $staticVariables = array();
        protected $thisVariables;
        protected $args;
        protected $scope;
@@ -126,8 +127,32 @@
                $this->lastCommand = $name;
        }
 
-       public function addParamVariable( $variable ) {
+       /**
+        *
+        * @param string $variable Variable name
+        * @param integer $scope Variable scope (default, static, global)
+        * @return boolean Normally return true, false for already initialized 
static variables
+        */
+       public function addParamVariable( $variable, $scope = T_VARIABLE ) {
+               $return = true;
+               switch ($scope) {
+                       case T_STATIC:
+                               if( isset($this->thisVariables[$variable]) ) {
+                                       return new ErrorMessage(__LINE__, null, 
E_PARSE, T_STATIC);
+                               }
+                               $args0 = isset($this->args[0]) ? $this->args[0] 
: '';
+                               if( !isset(self::$staticVariables[$args0]) ) {
+                                       self::$staticVariables[$args0] = 
array();
+                               }
+                               if( 
!isset(self::$staticVariables[$args0][$variable]) ) {
+                                       
self::$staticVariables[$args0][$variable] = null;
+                               }else{
+                                       $return = false;
+                               }
+                               $this->thisVariables[$variable] = 
&self::$staticVariables[$args0][$variable];
+               }
                $this->addParam( new RVariable($variable, $this->thisVariables) 
);
+               return $return;
        }
 
        public function addParamValue( $value ) {
diff --git a/tests/phpunit/includes/InterpreterTest.php 
b/tests/phpunit/includes/InterpreterTest.php
index 034926e..11cd1c7 100644
--- a/tests/phpunit/includes/InterpreterTest.php
+++ b/tests/phpunit/includes/InterpreterTest.php
@@ -1011,4 +1011,47 @@
                                );
        }
 
+       /**
+        * Test static variable $stat in testTemplate
+        */
+       public function testRun_echo_scope_static_1() {
+               // start testScope
+               $this->assertEquals(
+                               Interpreter::run('$foo = "local foo variable 
from testScope";', array('testScope'), 0),
+                               array()
+                               );
+               // {{testTemplate|HELLO!}}
+               $this->assertEquals(
+                               Interpreter::run('
+$foo = $argv[1];
+static $stat = 0;
+$bar++; $stat++;
+echo $foo, $argv[0], $argc, $bar, $stat, $argv["test"];', 
array('testTemplate', 'HELLO!'), 1),
+                               array('HELLO!', 'testTemplate', 2, 1, 1, null)
+                               );
+               // {{testTemplate|HELLO!|test="TEST!!!"}}
+               $this->assertEquals(
+                               Interpreter::run('
+$foo = $argv[1];
+static $stat = 0;
+$bar++; $stat++;
+echo $foo, $argv[0], $argc, $bar, $stat, $argv["test"];', 
array('testTemplate', 'HELLO!', 'test'=>'TEST!!!'), 2),
+                               array('HELLO!', 'testTemplate', 3, 1, 2, 
'TEST!!!')
+                               );
+               // {{testTemplate|HELLO!}}
+               $this->assertEquals(
+                               Interpreter::run('
+$foo = $argv[1];
+static $stat = 0;
+$bar++; $stat++;
+echo $foo, $argv[0], $argc, $bar, $stat, $argv["test"];', 
array('testTemplate', 'HELLO!'), 3),
+                               array('HELLO!', 'testTemplate', 2, 1, 3, null)
+                               );
+               // end testScope
+               $this->assertEquals(
+                               Interpreter::run('echo $foo;', 
array('testScope'), 0),
+                               array('local foo variable from testScope')
+                               );
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/65667
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I70d4f6fd15673adb2dabf91c6070c2543623aaca
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Foxway
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <pastak...@yandex.ru>
Gerrit-Reviewer: Pastakhov <pastak...@yandex.ru>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to