Scribunto parser support
authorTim Starling <tstarling@wikimedia.org>
Tue, 22 May 2012 03:26:25 +0000 (13:26 +1000)
committerTim Starling <tstarling@wikimedia.org>
Tue, 22 May 2012 03:53:41 +0000 (13:53 +1000)
Add $indexOffset parameter to PPFrame::newChild(). This makes it
possible to use newChild() for interpreting named parameters to
invoke in Scribunto -- otherwise I would have had to duplicate its
functionality, which would have been tricky given that I wanted to
make a real frame with an expand() method. Setting $indexOffset allows
newChild() to start counting numbered parameters from somewhere other
than the first pipe character, leaving room for the Scribunto function
name.

Fixed PPCustomFrame_*::getArguments(), was missing for no apparent
reason. I didn't end up using it in Scribunto, but there's no harm in
adding it anyway.

Change-Id: I0c761aab8a7f1ae74e8d151a1346febb5c466e18

includes/parser/Preprocessor.php
includes/parser/Preprocessor_DOM.php
includes/parser/Preprocessor_Hash.php

index 3d2f2f8..19bcbf2 100644 (file)
@@ -78,15 +78,19 @@ interface PPFrame {
 
        const RECOVER_ORIG = 27; // = 1|2|8|16 no constant expression support in PHP yet
 
+       /** This constant exists when $indexOffset is supported in newChild() */
+       const SUPPORTS_INDEX_OFFSET = 1;
+
        /**
         * Create a child frame
         *
         * @param $args array
         * @param $title Title
+        * @param $indexOffset A number subtracted from the index attributes of the arguments
         *
         * @return PPFrame
         */
-       function newChild( $args = false, $title = false );
+       function newChild( $args = false, $title = false, $indexOffset = 0 );
 
        /**
         * Expand a document tree node
index 5bb951b..f991df2 100644 (file)
@@ -926,7 +926,7 @@ class PPFrame_DOM implements PPFrame {
         *
         * @return PPTemplateFrame_DOM
         */
-       function newChild( $args = false, $title = false ) {
+       function newChild( $args = false, $title = false, $indexOffset = 0 ) {
                $namedArgs = array();
                $numberedArgs = array();
                if ( $title === false ) {
@@ -938,6 +938,9 @@ class PPFrame_DOM implements PPFrame {
                                $args = $args->node;
                        }
                        foreach ( $args as $arg ) {
+                               if ( $arg instanceof PPNode ) {
+                                       $arg = $arg->node;
+                               }
                                if ( !$xpath ) {
                                        $xpath = new DOMXPath( $arg->ownerDocument );
                                }
@@ -947,6 +950,7 @@ class PPFrame_DOM implements PPFrame {
                                if ( $nameNodes->item( 0 )->hasAttributes() ) {
                                        // Numbered parameter
                                        $index = $nameNodes->item( 0 )->attributes->getNamedItem( 'index' )->textContent;
+                                       $index = $index - $indexOffset;
                                        $numberedArgs[$index] = $value->item( 0 );
                                        unset( $namedArgs[$index] );
                                } else {
@@ -1549,6 +1553,10 @@ class PPCustomFrame_DOM extends PPFrame_DOM {
                }
                return $this->args[$index];
        }
+
+       function getArguments() {
+               return $this->args;
+       }
 }
 
 /**
index 32ba707..f455a1d 100644 (file)
@@ -888,7 +888,7 @@ class PPFrame_Hash implements PPFrame {
         *
         * @return PPTemplateFrame_Hash
         */
-       function newChild( $args = false, $title = false ) {
+       function newChild( $args = false, $title = false, $indexOffset = 0 ) {
                $namedArgs = array();
                $numberedArgs = array();
                if ( $title === false ) {
@@ -904,8 +904,9 @@ class PPFrame_Hash implements PPFrame {
                                $bits = $arg->splitArg();
                                if ( $bits['index'] !== '' ) {
                                        // Numbered parameter
-                                       $numberedArgs[$bits['index']] = $bits['value'];
-                                       unset( $namedArgs[$bits['index']] );
+                                       $index = $bits['index'] - $indexOffset;
+                                       $numberedArgs[$index] = $bits['value'];
+                                       unset( $namedArgs[$index] );
                                } else {
                                        // Named parameter
                                        $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
@@ -1496,6 +1497,10 @@ class PPCustomFrame_Hash extends PPFrame_Hash {
                }
                return $this->args[$index];
        }
+
+       function getArguments() {
+               return $this->args;
+       }
 }
 
 /**