MediaWiki  1.23.2
ConfEditor Class Reference

This is a state machine style parser with two internal stacks: More...

Public Member Functions

 __construct ( $text)
 Construct a new parser. More...
 
 currentToken ()
 Get the current token. More...
 
 dumpTokens ()
 Echo a reasonably readable representation of the tokenizer array. More...
 
 edit ( $ops)
 Edit the text. More...
 
 endPath ()
 Internal function to update some things at the end of a path region. More...
 
 endPathValue ()
 Mark the end of the value part of a path. More...
 
 error ( $msg)
 Generate a parse error. More...
 
 expect ( $type)
 Throws an error if the current token is not of the given type, and then advances to the next position. More...
 
 findDeletionRegion ( $pathName)
 Finds the source byte region which you would want to delete, if $pathName was to be deleted. More...
 
 findFirstArrayElement ( $path)
 Find the path name of first element in the array. More...
 
 findLastArrayElement ( $path)
 Find the path name of the last element in the array. More...
 
 findValueRegion ( $pathName)
 Find the byte region in the source corresponding to the value part. More...
 
 firstToken ()
 Reset the parse position. More...
 
 getIndent ( $pos, $key=false, $arrowPos=false)
 Get the indent string which sits after a given start position. More...
 
 getTokenAhead ( $offset)
 Get the token $offset steps ahead of the current position. More...
 
 getTypeName ( $type)
 Get a readable name for the given token type. More...
 
 getVars ()
 Get the variables defined in the text. More...
 
 isAhead ( $type, $offset=0)
 Looks ahead to see if the given type is the next token type, starting from the current position plus the given offset. More...
 
 markArrow ()
 Mark the arrow separator in an associative array element. More...
 
 markComma ()
 Mark the comma separator in an array element. More...
 
 newTokenObj ( $internalToken)
 Create a ConfEditorToken from an element of token_get_all() More...
 
 nextPath ( $path)
 Go to the next path on the same level. More...
 
 nextToken ()
 Advance the current position and return the resulting next token. More...
 
 parse ()
 Run the parser on the text. More...
 
 parseScalar ( $str)
 Parse a scalar value in PHP. More...
 
 popPath ()
 Go down a path level, for example at the end of an array. More...
 
 popState ()
 Pop a state from the state stack. More...
 
 prevToken ()
 Get the previous token object. More...
 
 pushPath ( $path)
 Go up to a new path level, for example at the start of an array. More...
 
 pushState ( $nextState, $stateAfterThat=null)
 Push a state or two on to the state stack. More...
 
 replaceSourceRegion ( $start, $end, $newText=false)
 Replace the byte offset region of the source with $newText. More...
 
 setVar (&$array, $path, $key, $value)
 Set a value in an array, unless it's set already. More...
 
 skipSpace ()
 Advances the current position past any whitespace or comments. More...
 
 startPathValue ()
 Mark the start of the value part of a path. More...
 
 validatePath ( $path)
 Returns true if the user input path is valid. More...
 

Static Public Member Functions

static test ( $text)
 Simple entry point for command-line testing. More...
 

Public Attributes

 $byteNum
 The current 0-based byte number. More...
 
 $colNum
 The current 1-based column number. More...
 
 $currentToken
 The current ConfEditorToken object. More...
 
 $edits
 Editor state. More...
 
 $lineNum
 The current 1-based line number. More...
 
 $pathInfo
 The elements of the top of the pathStack for every path encountered, indexed by slash-separated path. More...
 
 $pathStack
 The path stack is a stack of associative arrays with the following elements: name The name of top level of the path level The level (number of elements) of the path startByte The byte offset of the start of the path startToken The token offset of the start endByte The byte offset of thee endToken The token offset of the end, plus one valueStartToken The start token offset of the value part valueStartByte The start byte offset of the value part valueEndToken The end token offset of the value part, plus one valueEndByte The end byte offset of the value part, plus one nextArrayIndex The next numeric array index at this level hasComma True if the array element ends with a comma arrowByte The byte offset of the "=>", or false if there isn't one. More...
 
 $pos
 The current position in the token array. More...
 
 $prevToken
 The previous ConfEditorToken object. More...
 
 $serial
 Next serial number for whitespace placeholder paths (@extra-N) More...
 
 $stateStack
 The state machine stack. More...
 
 $text
 The text to parse. More...
 
 $tokens
 The token array from token_get_all() More...
 

Protected Member Functions

 initParse ()
 Initialise a parse. More...
 
 setPos ( $pos)
 Set the parse position. More...
 

Detailed Description

This is a state machine style parser with two internal stacks:

  • A next state stack, which determines the state the machine will progress to next
  • A path stack, which keeps track of the logical location in the file.

Reference grammar:

file = T_OPEN_TAG *statement statement = T_VARIABLE "=" expression ";" expression = array / scalar / T_VARIABLE array = T_ARRAY "(" [ element *( "," element ) [ "," ] ] ")" element = assoc-element / expression assoc-element = scalar T_DOUBLE_ARROW expression scalar = T_LNUMBER / T_DNUMBER / T_STRING / T_CONSTANT_ENCAPSED_STRING

Definition at line 38 of file ConfEditor.php.

Constructor & Destructor Documentation

◆ __construct()

ConfEditor::__construct (   $text)

Construct a new parser.

Definition at line 125 of file ConfEditor.php.

References $text, and text.

Member Function Documentation

◆ currentToken()

ConfEditor::currentToken ( )

Get the current token.

Definition at line 808 of file ConfEditor.php.

References $currentToken.

Referenced by expect(), nextToken(), parse(), setPos(), and skipSpace().

◆ dumpTokens()

ConfEditor::dumpTokens ( )

Echo a reasonably readable representation of the tokenizer array.

Definition at line 1059 of file ConfEditor.php.

References $out, as, getTypeName(), and newTokenObj().

◆ edit()

ConfEditor::edit (   $ops)

Edit the text.

Returns the edited text.

Parameters
array$opsof operations.

Operations are given as an associative array, with members: type: One of delete, set, append or insert (required) path: The path to operate on (required) key: The array key to insert/append, with PHP quotes value: The value, with PHP quotes

delete Deletes an array element or statement with the specified path. e.g. array('type' => 'delete', 'path' => '$foo/bar/baz' ) is equivalent to the runtime PHP code: unset( $foo['bar']['baz'] );

set Sets the value of an array element. If the element doesn't exist, it is appended to the array. If it does exist, the value is set, with comments and indenting preserved.

append Appends a new element to the end of the array. Adds a trailing comma. e.g. array( 'type' => 'append', 'path', '$foo/bar', 'key' => 'baz', 'value' => "'x'" ) is like the PHP code: $foo['bar']['baz'] = 'x';

insert Insert a new element at the start of the array.

Exceptions
MWException
Returns
string

Definition at line 165 of file ConfEditor.php.

References $e, $out, $path, $type, $value, array(), as, edits, findDeletionRegion(), findFirstArrayElement(), findLastArrayElement(), findValueRegion(), getIndent(), list, parse(), replaceSourceRegion(), and text.

◆ endPath()

ConfEditor::endPath ( )

Internal function to update some things at the end of a path region.

Do not call except from popPath() or nextPath().

Definition at line 902 of file ConfEditor.php.

References $byteNum, $pathInfo, $pos, and as.

Referenced by nextPath(), and popPath().

◆ endPathValue()

ConfEditor::endPathValue ( )

Mark the end of the value part of a path.

Definition at line 982 of file ConfEditor.php.

References $byteNum, $path, and $pos.

Referenced by parse().

◆ error()

ConfEditor::error (   $msg)

Generate a parse error.

Definition at line 1007 of file ConfEditor.php.

Referenced by expect(), and parse().

◆ expect()

ConfEditor::expect (   $type)

Throws an error if the current token is not of the given type, and then advances to the next position.

Definition at line 862 of file ConfEditor.php.

References $type, currentToken(), error(), getTypeName(), and nextToken().

Referenced by parse().

◆ findDeletionRegion()

ConfEditor::findDeletionRegion (   $pathName)

Finds the source byte region which you would want to delete, if $pathName was to be deleted.

Includes the leading spaces and tabs, the trailing line break, and any comments in between.

Parameters
$pathName
Exceptions
MWException
Returns
array

Definition at line 414 of file ConfEditor.php.

References $count, $path, $pos, array(), firstToken(), getTokenAhead(), and nextToken().

Referenced by edit().

◆ findFirstArrayElement()

ConfEditor::findFirstArrayElement (   $path)

Find the path name of first element in the array.

If the array is empty, this will return the @extra interstitial element. If the specified path is not found or is not an array, it will return false.

Returns
bool|int|string

Definition at line 532 of file ConfEditor.php.

References $path, and as.

Referenced by edit().

◆ findLastArrayElement()

ConfEditor::findLastArrayElement (   $path)

Find the path name of the last element in the array.

If the array is empty, this will return the @extra interstitial element. If the specified path is not found or is not an array, it will return false.

Returns
bool|int|string

Definition at line 494 of file ConfEditor.php.

References $path, and as.

Referenced by edit().

◆ findValueRegion()

ConfEditor::findValueRegion (   $pathName)

Find the byte region in the source corresponding to the value part.

This includes the quotes, but does not include the trailing comma or semicolon.

The end position is the past-the-end (end + 1) value as per convention.

Parameters
$pathName
Exceptions
MWException
Returns
array

Definition at line 476 of file ConfEditor.php.

References $path, and array().

Referenced by edit().

◆ firstToken()

ConfEditor::firstToken ( )

Reset the parse position.

Definition at line 795 of file ConfEditor.php.

References $currentToken, ConfEditorToken\newEnd(), prevToken(), and setPos().

Referenced by findDeletionRegion(), initParse(), and parse().

◆ getIndent()

ConfEditor::getIndent (   $pos,
  $key = false,
  $arrowPos = false 
)

Get the indent string which sits after a given start position.

Returns false if the position is not at the start of the line.

Returns
array

Definition at line 558 of file ConfEditor.php.

References $pos, array(), and text.

Referenced by edit().

◆ getTokenAhead()

ConfEditor::getTokenAhead (   $offset)

Get the token $offset steps ahead of the current position.

$offset may be negative, to get tokens behind the current position.

Returns
ConfEditorToken

Definition at line 838 of file ConfEditor.php.

References $pos, ConfEditorToken\newEnd(), and newTokenObj().

Referenced by findDeletionRegion(), and isAhead().

◆ getTypeName()

ConfEditor::getTypeName (   $type)

Get a readable name for the given token type.

Returns
string

Definition at line 1015 of file ConfEditor.php.

References $type.

Referenced by dumpTokens(), and expect().

◆ getVars()

ConfEditor::getVars ( )

Get the variables defined in the text.

Returns
array( varname => value )

Definition at line 280 of file ConfEditor.php.

References $name, $path, $value, $vars, array(), as, parse(), parseScalar(), setVar(), and text.

◆ initParse()

ConfEditor::initParse ( )
protected

Initialise a parse.

Definition at line 756 of file ConfEditor.php.

References array(), firstToken(), and text.

Referenced by parse().

◆ isAhead()

ConfEditor::isAhead (   $type,
  $offset = 0 
)

Looks ahead to see if the given type is the next token type, starting from the current position plus the given offset.

Skips any intervening whitespace.

Returns
bool

Definition at line 1029 of file ConfEditor.php.

References $type, and getTokenAhead().

Referenced by parse().

◆ markArrow()

ConfEditor::markArrow ( )

Mark the arrow separator in an associative array element.

Definition at line 999 of file ConfEditor.php.

References $byteNum, and $path.

Referenced by parse().

◆ markComma()

ConfEditor::markComma ( )

Mark the comma separator in an array element.

Definition at line 991 of file ConfEditor.php.

References $path.

Referenced by parse().

◆ newTokenObj()

ConfEditor::newTokenObj (   $internalToken)

Create a ConfEditorToken from an element of token_get_all()

Returns
ConfEditorToken

Definition at line 784 of file ConfEditor.php.

Referenced by dumpTokens(), getTokenAhead(), and setPos().

◆ nextPath()

ConfEditor::nextPath (   $path)

Go to the next path on the same level.

This ends the current path and starts a new one. If $path is @next, the new path is set to the next numeric array element.

Definition at line 947 of file ConfEditor.php.

References $path, array(), and endPath().

Referenced by parse().

◆ nextToken()

ConfEditor::nextToken ( )

Advance the current position and return the resulting next token.

Definition at line 815 of file ConfEditor.php.

References $currentToken, $text, currentToken(), prevToken(), and setPos().

Referenced by expect(), findDeletionRegion(), parse(), and skipSpace().

◆ parse()

ConfEditor::parse ( )

Run the parser on the text.

Throws an exception if the string does not match our defined subset of PHP syntax.

Definition at line 580 of file ConfEditor.php.

References $text, currentToken(), endPathValue(), error(), expect(), firstToken(), initParse(), isAhead(), markArrow(), markComma(), nextPath(), nextToken(), parseScalar(), popPath(), popState(), pushPath(), pushState(), skipSpace(), startPathValue(), type, and validatePath().

Referenced by edit(), and getVars().

◆ parseScalar()

ConfEditor::parseScalar (   $str)

Parse a scalar value in PHP.

Returns
mixed Parsed value

Definition at line 340 of file ConfEditor.php.

References array().

Referenced by getVars(), and parse().

◆ popPath()

ConfEditor::popPath ( )

Go down a path level, for example at the end of an array.

Definition at line 937 of file ConfEditor.php.

References endPath().

Referenced by parse().

◆ popState()

ConfEditor::popState ( )

Pop a state from the state stack.

Returns
mixed

Definition at line 885 of file ConfEditor.php.

Referenced by parse().

◆ prevToken()

ConfEditor::prevToken ( )

Get the previous token object.

Definition at line 1052 of file ConfEditor.php.

References $prevToken.

Referenced by firstToken(), and nextToken().

◆ pushPath()

ConfEditor::pushPath (   $path)

Go up to a new path level, for example at the start of an array.

Definition at line 918 of file ConfEditor.php.

References $path, and array().

Referenced by parse().

◆ pushState()

ConfEditor::pushState (   $nextState,
  $stateAfterThat = null 
)

Push a state or two on to the state stack.

Definition at line 874 of file ConfEditor.php.

Referenced by parse().

◆ replaceSourceRegion()

ConfEditor::replaceSourceRegion (   $start,
  $end,
  $newText = false 
)

Replace the byte offset region of the source with $newText.

Works by adding elements to the $this->edits array.

Definition at line 375 of file ConfEditor.php.

References array(), as, and edits.

Referenced by edit().

◆ setPos()

ConfEditor::setPos (   $pos)
protected

Set the parse position.

Do not call this except from firstToken() and nextToken(), there is more to update than just the position.

Definition at line 769 of file ConfEditor.php.

References $currentToken, $pos, currentToken(), ConfEditorToken\newEnd(), and newTokenObj().

Referenced by firstToken(), and nextToken().

◆ setVar()

ConfEditor::setVar ( $array,
  $path,
  $key,
  $value 
)

Set a value in an array, unless it's set already.

For instance, setVar( $arr, 'foo/bar', 'baz', 3 ); will set $arr['foo']['bar']['baz'] = 3;

Parameters
$arrayarray
string$pathslash-delimited path
$keymixed Key
$valuemixed Value

Definition at line 320 of file ConfEditor.php.

References $path, $value, array(), and as.

Referenced by getVars().

◆ skipSpace()

ConfEditor::skipSpace ( )

Advances the current position past any whitespace or comments.

Definition at line 850 of file ConfEditor.php.

References $currentToken, currentToken(), and nextToken().

Referenced by parse().

◆ startPathValue()

ConfEditor::startPathValue ( )

Mark the start of the value part of a path.

Definition at line 973 of file ConfEditor.php.

References $byteNum, $path, and $pos.

Referenced by parse().

◆ test()

static ConfEditor::test (   $text)
static

Simple entry point for command-line testing.

Parameters
$textstring
Returns
string

Definition at line 111 of file ConfEditor.php.

References $e, and $text.

◆ validatePath()

ConfEditor::validatePath (   $path)

Returns true if the user input path is valid.

This exists to allow "/" and "@" to be reserved for string path keys

Returns
bool

Definition at line 894 of file ConfEditor.php.

References $path.

Referenced by parse().

Member Data Documentation

◆ $byteNum

ConfEditor::$byteNum

The current 0-based byte number.

Definition at line 55 of file ConfEditor.php.

Referenced by endPath(), endPathValue(), markArrow(), and startPathValue().

◆ $colNum

ConfEditor::$colNum

The current 1-based column number.

Definition at line 52 of file ConfEditor.php.

◆ $currentToken

ConfEditor::$currentToken

The current ConfEditorToken object.

Definition at line 58 of file ConfEditor.php.

Referenced by currentToken(), firstToken(), nextToken(), setPos(), and skipSpace().

◆ $edits

ConfEditor::$edits

Editor state.

This consists of the internal copy/insert operations which are applied to the source string to obtain the destination string.

Definition at line 102 of file ConfEditor.php.

◆ $lineNum

ConfEditor::$lineNum

The current 1-based line number.

Definition at line 49 of file ConfEditor.php.

◆ $pathInfo

ConfEditor::$pathInfo

The elements of the top of the pathStack for every path encountered, indexed by slash-separated path.

Definition at line 91 of file ConfEditor.php.

Referenced by endPath().

◆ $pathStack

ConfEditor::$pathStack

The path stack is a stack of associative arrays with the following elements: name The name of top level of the path level The level (number of elements) of the path startByte The byte offset of the start of the path startToken The token offset of the start endByte The byte offset of thee endToken The token offset of the end, plus one valueStartToken The start token offset of the value part valueStartByte The start byte offset of the value part valueEndToken The end token offset of the value part, plus one valueEndByte The end byte offset of the value part, plus one nextArrayIndex The next numeric array index at this level hasComma True if the array element ends with a comma arrowByte The byte offset of the "=>", or false if there isn't one.

Definition at line 85 of file ConfEditor.php.

◆ $pos

ConfEditor::$pos

The current position in the token array.

Definition at line 46 of file ConfEditor.php.

Referenced by endPath(), endPathValue(), findDeletionRegion(), getIndent(), getTokenAhead(), setPos(), and startPathValue().

◆ $prevToken

ConfEditor::$prevToken

The previous ConfEditorToken object.

Definition at line 61 of file ConfEditor.php.

Referenced by prevToken().

◆ $serial

ConfEditor::$serial

Next serial number for whitespace placeholder paths (@extra-N)

Definition at line 96 of file ConfEditor.php.

◆ $stateStack

ConfEditor::$stateStack

The state machine stack.

This is an array of strings where the topmost element will be popped off and become the next parser state.

Definition at line 67 of file ConfEditor.php.

◆ $text

ConfEditor::$text

The text to parse.

Definition at line 40 of file ConfEditor.php.

Referenced by __construct(), nextToken(), parse(), and test().

◆ $tokens

ConfEditor::$tokens

The token array from token_get_all()

Definition at line 43 of file ConfEditor.php.


The documentation for this class was generated from the following file: