PHP Класс pQuery\TokenizerBase

Can convert any string into tokens. The base class only supports identifier/whitespace tokens. For more tokens, the class can be easily extended. Use like: next() !== $a::TOK_NULL) { echo $a->token, ': ',$a->getTokenString(), "
\n"; } ?>
Показать файл Открыть проект

Открытые свойства

Свойство Тип Описание
$char_map array Automatically built character map. Built using {@link $identifiers}, {@link $whitespace} and {@link $custom_char_map}
$custom_char_map array All characters that should be mapped to a token/function that cannot be considered as whitespace or identifier
$doc string The document that is being tokenized
$errors array All errors found while parsing the document
$identifiers array | string List with all the character that can be considered as identifier
$line_pos Current (Line/Column) position in document
$pos integer Current (character) position in the document
$size integer The size of the document (length of string)
$token integer Current token
$token_start integer Start position of token. If NULL, then current position is used.
$whitespace array | string List with all the character that can be considered as whitespace

Открытые методы

Метод Описание
__construct ( string $doc = '', integer $pos ) Class constructor
addError ( string $error ) Add error to the array and appends current position
getDoc ( ) : string Returns target document
getIdentifiers ( boolean $as_string = true ) : string | array Returns identifier characters as string/array
getLinePos ( ) : array Returns current position in document (Line/Char)
getPos ( ) : integer Returns current position in document (Index)
getToken ( ) : integer Returns current token
getTokenString ( integer $start_offset, integer $end_offset ) : string Returns current token as string
getWhitespace ( boolean $as_string = true ) : string | array Returns whitespace characters as string/array
mapChar ( string $char, integer | string $map ) Maps a custom character to a token/function
next ( ) : integer Continues to the next token
next_no_whitespace ( ) : integer Finds the next token, but skips whitespace
next_pos ( string $needle, boolean $callback = true ) : integer Finds the next token by searching for a string
next_search ( string | array $characters, boolean $callback = true ) : integer Finds the next token using stop characters.
setDoc ( string $doc, integer $pos ) Sets target document
setIdentifiers ( string | array $ident ) Sets characters to be recognized as identifier
setPos ( integer $pos ) Sets position in document
setWhitespace ( string | array $ws ) Sets characters to be recognized as whitespace
unmapChar ( string $char ) Removes a char mapped with {@link mapChar()}

Защищенные методы

Метод Описание
buildCharMap ( ) Builds the {@link $map_char} array
expect ( string | integer $token, boolean | integer $do_next = true, boolean | integer $try_next = false, boolean | integer $next_on_match = 1 ) : boolean Expect a specific token or character. Adds error if token doesn't match.
parse_identifier ( ) : integer Parse identifiers
parse_linebreak ( ) Parse line breaks and increase line number
parse_whitespace ( ) : integer Parse whitespace

Описание методов

__construct() публичный Метод

Class constructor
См. также: setDoc()
См. также: setPos()
public __construct ( string $doc = '', integer $pos )
$doc string Document to be tokenized
$pos integer Position to start parsing

addError() публичный Метод

Add error to the array and appends current position
public addError ( string $error )
$error string

buildCharMap() защищенный Метод

Builds the {@link $map_char} array
protected buildCharMap ( )

expect() защищенный Метод

Expect a specific token or character. Adds error if token doesn't match.
protected expect ( string | integer $token, boolean | integer $do_next = true, boolean | integer $try_next = false, boolean | integer $next_on_match = 1 ) : boolean
$token string | integer Character or token to expect
$do_next boolean | integer Go to next character before evaluating. 1 for next char, true to ignore whitespace
$try_next boolean | integer Try next character if current doesn't match. 1 for next char, true to ignore whitespace
$next_on_match boolean | integer Go to next character after evaluating. 1 for next char, true to ignore whitespace
Результат boolean

getDoc() публичный Метод

Returns target document
См. также: setDoc()
public getDoc ( ) : string
Результат string

getIdentifiers() публичный Метод

Returns identifier characters as string/array
См. также: setIdentifiers()
public getIdentifiers ( boolean $as_string = true ) : string | array
$as_string boolean Should the result be a string or an array?
Результат string | array

getLinePos() публичный Метод

Returns current position in document (Line/Char)
public getLinePos ( ) : array
Результат array array(Line, Column)

getPos() публичный Метод

Returns current position in document (Index)
См. также: setPos()
public getPos ( ) : integer
Результат integer

getToken() публичный Метод

Returns current token
public getToken ( ) : integer
Результат integer

getTokenString() публичный Метод

Returns current token as string
public getTokenString ( integer $start_offset, integer $end_offset ) : string
$start_offset integer Offset from token start
$end_offset integer Offset from token end
Результат string

getWhitespace() публичный Метод

Returns whitespace characters as string/array
См. также: setWhitespace()
public getWhitespace ( boolean $as_string = true ) : string | array
$as_string boolean Should the result be a string or an array?
Результат string | array

mapChar() публичный Метод

Used like: mapChar('a', self::{@link TOK_IDENTIFIER}) or mapChar('a', 'parse_identifier');
См. также: unmapChar()
public mapChar ( string $char, integer | string $map )
$char string Character that should be mapped. If set, it will be overridden
$map integer | string If function name, then $this->function will be called, otherwise token is set to $map

next() публичный Метод

Continues to the next token
public next ( ) : integer
Результат integer Next token ({@link TOK_NULL} if none)

next_no_whitespace() публичный Метод

Finds the next token, but skips whitespace
public next_no_whitespace ( ) : integer
Результат integer Next token ({@link TOK_NULL} if none)

next_pos() публичный Метод

Finds the next token by searching for a string
public next_pos ( string $needle, boolean $callback = true ) : integer
$needle string The needle that's being searched for
$callback boolean Should the function check the charmap after finding the needle?
Результат integer Next token ({@link TOK_NULL} if none)

parse_identifier() защищенный Метод

Parse identifiers
protected parse_identifier ( ) : integer
Результат integer Token

parse_linebreak() защищенный Метод

Parse line breaks and increase line number
protected parse_linebreak ( )

parse_whitespace() защищенный Метод

Parse whitespace
protected parse_whitespace ( ) : integer
Результат integer Token

setDoc() публичный Метод

Sets target document
См. также: getDoc()
См. также: setPos()
public setDoc ( string $doc, integer $pos )
$doc string Document to be tokenized
$pos integer Position to start parsing

setIdentifiers() публичный Метод

Used like: setIdentifiers('ab') or setIdentifiers(array('a' => true, 'b', 'c'));
public setIdentifiers ( string | array $ident )
$ident string | array

setPos() публичный Метод

Sets position in document
См. также: getPos()
public setPos ( integer $pos )
$pos integer

setWhitespace() публичный Метод

Used like: setWhitespace('ab') or setWhitespace(array('a' => true, 'b', 'c'));
public setWhitespace ( string | array $ws )
$ws string | array

unmapChar() публичный Метод

Removes a char mapped with {@link mapChar()}
См. также: mapChar()
public unmapChar ( string $char )
$char string Character that should be unmapped

Описание свойств

$char_map публичное свойство

Automatically built character map. Built using {@link $identifiers}, {@link $whitespace} and {@link $custom_char_map}
public array $char_map
Результат array

$custom_char_map публичное свойство

All characters that should be mapped to a token/function that cannot be considered as whitespace or identifier
См. также: mapChar()
См. также: unmapChar()
public array $custom_char_map
Результат array

$doc публичное свойство

The document that is being tokenized
См. также: setDoc()
См. также: getDoc()
public string $doc
Результат string

$errors публичное свойство

All errors found while parsing the document
См. также: addError()
public array $errors
Результат array

$identifiers публичное свойство

List with all the character that can be considered as identifier
См. также: setIdentifiers()
См. также: getIdentifiers()
public array|string $identifiers
Результат array | string

$line_pos публичное свойство

Current (Line/Column) position in document
См. также: getLinePos()
public $line_pos

$pos публичное свойство

Current (character) position in the document
См. также: setPos()
См. также: getPos()
public int $pos
Результат integer

$size публичное свойство

The size of the document (length of string)
public int $size
Результат integer

$token публичное свойство

Current token
См. также: getToken()
public int $token
Результат integer

$token_start публичное свойство

Start position of token. If NULL, then current position is used.
См. также: getTokenString()
public int $token_start
Результат integer

$whitespace публичное свойство

List with all the character that can be considered as whitespace
См. также: setWhitespace()
См. также: getWhitespace()
public array|string $whitespace
Результат array | string