MediaWiki  REL1_19
ConfEditor Class Reference

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. More...

List of all members.

Public Member Functions

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

Static Public Member Functions

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

Public Attributes

 $byteNum
 The current 0-based byte number.
 $colNum
 The current 1-based column number.
 $currentToken
 The current ConfEditorToken object.
 $edits
 Editor state.
 $lineNum
 The current 1-based line number.
 $pathInfo
 The elements of the top of the pathStack for every path encountered, indexed by slash-separated path.
 $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.
 $pos
 The current position in the token array.
 $prevToken
 The previous ConfEditorToken object.
 $serial
 Next serial number for whitespace placeholder paths (@extra-N)
 $stateStack
 The state machine stack.
 $text
 The text to parse.
 $tokens
 The token array from token_get_all()

Protected Member Functions

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

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 18 of file ConfEditor.php.


Constructor & Destructor Documentation

Construct a new parser.

Definition at line 105 of file ConfEditor.php.

References $text.


Member Function Documentation

Get the current token.

Definition at line 752 of file ConfEditor.php.

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

Here is the caller graph for this function:

Echo a reasonably readable representation of the tokenizer array.

Definition at line 995 of file ConfEditor.php.

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

Here is the call graph for this function:

ConfEditor::edit ( ops)

Edit the text.

Returns the edited text.

Parameters:
$opsArray of 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.

Definition at line 143 of file ConfEditor.php.

References $out, $path, findDeletionRegion(), findFirstArrayElement(), findLastArrayElement(), findValueRegion(), getIndent(), parse(), and replaceSourceRegion().

Here is the call graph for this function:

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

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

Definition at line 841 of file ConfEditor.php.

References $pathInfo.

Referenced by nextPath(), and popPath().

Here is the caller graph for this function:

Mark the end of the value part of a path.

Definition at line 921 of file ConfEditor.php.

References $path.

Referenced by parse().

Here is the caller graph for this function:

ConfEditor::error ( msg)

Generate a parse error.

Definition at line 946 of file ConfEditor.php.

Referenced by expect(), and parse().

Here is the caller graph for this function:

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 803 of file ConfEditor.php.

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

Referenced by parse().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Definition at line 375 of file ConfEditor.php.

References $path, firstToken(), getTokenAhead(), and nextToken().

Referenced by edit().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Definition at line 484 of file ConfEditor.php.

References $path.

Referenced by edit().

Here is the caller graph for this function:

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.

Definition at line 448 of file ConfEditor.php.

References $path.

Referenced by edit().

Here is the caller graph for this function:

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.

Definition at line 432 of file ConfEditor.php.

References $path.

Referenced by edit().

Here is the caller graph for this function:

Reset the parse position.

Definition at line 740 of file ConfEditor.php.

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Definition at line 508 of file ConfEditor.php.

References $pos.

Referenced by edit().

Here is the caller graph for this function:

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

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

Definition at line 780 of file ConfEditor.php.

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

Referenced by findDeletionRegion(), and isAhead().

Here is the call graph for this function:

Here is the caller graph for this function:

Get a readable name for the given token type.

Definition at line 953 of file ConfEditor.php.

Referenced by dumpTokens(), and expect().

Here is the caller graph for this function:

Get the variables defined in the text.

Returns:
array( varname => value )

Definition at line 257 of file ConfEditor.php.

References $path, parse(), parseScalar(), and setVar().

Here is the call graph for this function:

ConfEditor::initParse ( ) [protected]

Initialise a parse.

Definition at line 703 of file ConfEditor.php.

References firstToken().

Referenced by parse().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Definition at line 966 of file ConfEditor.php.

References getTokenAhead().

Referenced by parse().

Here is the call graph for this function:

Here is the caller graph for this function:

Mark the arrow separator in an associative array element.

Definition at line 938 of file ConfEditor.php.

References $path.

Referenced by parse().

Here is the caller graph for this function:

Mark the comma separator in an array element.

Definition at line 930 of file ConfEditor.php.

References $path.

Referenced by parse().

Here is the caller graph for this function:

ConfEditor::newTokenObj ( internalToken)

Create a ConfEditorToken from an element of token_get_all()

Definition at line 729 of file ConfEditor.php.

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

Here is the caller graph for this function:

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 886 of file ConfEditor.php.

References $path, and endPath().

Referenced by parse().

Here is the call graph for this function:

Here is the caller graph for this function:

Advance the current position and return the resulting next token.

Definition at line 759 of file ConfEditor.php.

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

Run the parser on the text.

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

Definition at line 529 of file ConfEditor.php.

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

Referenced by edit(), and getVars().

Here is the call graph for this function:

Here is the caller graph for this function:

Parse a scalar value in PHP.

Returns:
mixed Parsed value

Definition at line 310 of file ConfEditor.php.

Referenced by getVars(), and parse().

Here is the caller graph for this function:

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

Definition at line 876 of file ConfEditor.php.

References endPath().

Referenced by parse().

Here is the call graph for this function:

Here is the caller graph for this function:

Pop a state from the state stack.

Definition at line 825 of file ConfEditor.php.

Referenced by parse().

Here is the caller graph for this function:

Get the previous token object.

Definition at line 988 of file ConfEditor.php.

Referenced by firstToken(), and nextToken().

Here is the caller graph for this function:

ConfEditor::pushPath ( path)

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

Definition at line 857 of file ConfEditor.php.

References $path.

Referenced by parse().

Here is the caller graph for this function:

ConfEditor::pushState ( nextState,
stateAfterThat = null 
)

Push a state or two on to the state stack.

Definition at line 815 of file ConfEditor.php.

Referenced by parse().

Here is the caller graph for this function:

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 339 of file ConfEditor.php.

Referenced by edit().

Here is the caller graph for this function:

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 716 of file ConfEditor.php.

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

Referenced by firstToken(), and nextToken().

Here is the call graph for this function:

Here is the caller graph for this function:

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
$pathstring slash-delimited path
$keymixed Key
$valuemixed Value

Definition at line 292 of file ConfEditor.php.

References $path.

Referenced by getVars().

Here is the caller graph for this function:

Advances the current position past any whitespace or comments.

Definition at line 792 of file ConfEditor.php.

References currentToken(), and nextToken().

Referenced by parse().

Here is the call graph for this function:

Here is the caller graph for this function:

Mark the start of the value part of a path.

Definition at line 912 of file ConfEditor.php.

References $path.

Referenced by parse().

Here is the caller graph for this function:

static ConfEditor::test ( text) [static]

Simple entry point for command-line testing.

Parameters:
$textstring
Returns:
string

Definition at line 92 of file ConfEditor.php.

References $text, and ConfEditorParseError\highlight().

Here is the call graph for this function:

Returns true if the user input path is valid.

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

Definition at line 833 of file ConfEditor.php.

References $path.

Referenced by parse().

Here is the caller graph for this function:


Member Data Documentation

ConfEditor::$byteNum

The current 0-based byte number.

Definition at line 35 of file ConfEditor.php.

ConfEditor::$colNum

The current 1-based column number.

Definition at line 32 of file ConfEditor.php.

ConfEditor::$currentToken

The current ConfEditorToken object.

Definition at line 38 of file ConfEditor.php.

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 83 of file ConfEditor.php.

ConfEditor::$lineNum

The current 1-based line number.

Definition at line 29 of file ConfEditor.php.

ConfEditor::$pathInfo

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

Definition at line 72 of file ConfEditor.php.

Referenced by endPath().

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 66 of file ConfEditor.php.

ConfEditor::$pos

The current position in the token array.

Definition at line 26 of file ConfEditor.php.

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

ConfEditor::$prevToken

The previous ConfEditorToken object.

Definition at line 41 of file ConfEditor.php.

ConfEditor::$serial

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

Definition at line 77 of file ConfEditor.php.

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 47 of file ConfEditor.php.

ConfEditor::$text

The text to parse.

Definition at line 20 of file ConfEditor.php.

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

ConfEditor::$tokens

The token array from token_get_all()

Definition at line 23 of file ConfEditor.php.


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