Documentation

Parser extends Token
in package

LightnCandy Parser

Table of Contents

BLOCKPARAM  = 9999
LITERAL  = -1
PARTIALBLOCK  = 9998
POS_BACKFILL  = 13
POS_BEGINRAW  = 5
POS_BEGINTAG  = 3
POS_ENDRAW  = 8
POS_ENDTAG  = 10
POS_INNERTAG  = 7
POS_LOTHER  = 1
POS_LSPACE  = 2
POS_LSPACECTL  = 4
POS_OP  = 6
POS_ROTHER  = 12
POS_RSPACE  = 11
POS_RSPACECTL  = 9
SUBEXP  = -2
VARNAME_SEARCH  = '/(\[[^\]]+\]|[^\[\]\.]+)/'
getBlockParams()  : array<string|int, string>|null
Get block params and fix the variable list
getPartialBlock()  : int
Get partial block id and fix the variable list
getPartialName()  : array<string|int, string>|null
Get partial name from "foo" or [foo] or \'foo\'
isSubExp()  : bool
Check a parsed result is a subexpression or not
parse()  : array<string|int, bool|int|array>
Parse the token and return parsed result.
setDelimiter()  : mixed
Setup delimiter by default or provided string
subexpression()  : array<string|int, bool|int|array>
Parse a subexpression then return parsed result.
toString()  : string
return token string
advancedVariable()  : array<string|int, bool|int|array>
Analyze parsed token for advanved variables.
analyze()  : array<string|int, bool|int|array>
Analyze a token string and return parsed result.
detectQuote()  : array<string, int>|null
Detect quote charactors
getExpression()  : array<int, string>
Return array presentation for an expression
getLiteral()  : array<string|int, int|string>
Return array presentation for a literal

Constants

BLOCKPARAM

public mixed BLOCKPARAM = 9999

LITERAL

public mixed LITERAL = -1

PARTIALBLOCK

public mixed PARTIALBLOCK = 9998

POS_BACKFILL

public mixed POS_BACKFILL = 13

POS_BEGINRAW

public mixed POS_BEGINRAW = 5

POS_BEGINTAG

public mixed POS_BEGINTAG = 3

POS_ENDRAW

public mixed POS_ENDRAW = 8

POS_ENDTAG

public mixed POS_ENDTAG = 10

POS_INNERTAG

public mixed POS_INNERTAG = 7

POS_LOTHER

public mixed POS_LOTHER = 1

POS_LSPACE

public mixed POS_LSPACE = 2

POS_LSPACECTL

public mixed POS_LSPACECTL = 4

POS_OP

public mixed POS_OP = 6

POS_ROTHER

public mixed POS_ROTHER = 12

POS_RSPACE

public mixed POS_RSPACE = 11

POS_RSPACECTL

public mixed POS_RSPACECTL = 9
public mixed VARNAME_SEARCH = '/(\[[^\]]+\]|[^\[\]\.]+)/'

Methods

getBlockParams()

Get block params and fix the variable list

public static getBlockParams(array<string|int, bool|int|string|array> &$vars) : array<string|int, string>|null
Parameters
$vars : array<string|int, bool|int|string|array>

parsed token

Return values
array<string|int, string>|null

Return list of block params or null

getPartialBlock()

Get partial block id and fix the variable list

public static getPartialBlock(array<string|int, bool|int|string|array> &$vars) : int
Parameters
$vars : array<string|int, bool|int|string|array>

parsed token

Return values
int

Return partial block id

getPartialName()

Get partial name from "foo" or [foo] or \'foo\'

public static getPartialName(array<string|int, bool|int|array> &$vars, int $pos) : array<string|int, string>|null
Parameters
$vars : array<string|int, bool|int|array>

parsed token

$pos : int

position of partial name

Tags
expect

null when input array()

expect

array('foo') when input array('foo')

expect

array('foo') when input array('"foo"')

expect

array('foo') when input array('[foo]')

expect

array('foo') when input array("\'foo\'")

expect

array('foo') when input array(0, 'foo'), 1

Return values
array<string|int, string>|null

Return one element partial name array

isSubExp()

Check a parsed result is a subexpression or not

public static isSubExp(array<string|int, string|int|array> $var) : bool
Parameters
$var : array<string|int, string|int|array>
Tags
expect

false when input 0

expect

false when input array()

expect

false when input array(\LightnCandy\Parser::SUBEXP, 0)

expect

false when input array(\LightnCandy\Parser::SUBEXP, 0, 0)

expect

false when input array(\LightnCandy\Parser::SUBEXP, 0, '', 0)

expect

true when input array(\LightnCandy\Parser::SUBEXP, 0, '')

Return values
bool

return true when input is a subexpression

parse()

Parse the token and return parsed result.

public static parse(array<string|int, string> &$token, array<string, array|string|int> &$context) : array<string|int, bool|int|array>
Parameters
$token : array<string|int, string>

preg_match results

$context : array<string, array|string|int>

current compile context

Tags
expect

array(false, array(array())) when input array(0,0,0,0,0,0,0,''), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(true, array(array())) when input array(0,0,0,'{{',0,'{',0,''), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(true, array(array())) when input array(0,0,0,0,0,0,0,''), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 0, 'noesc' => 1), 'rawblock' => false)

expect

array(false, array(array('a'))) when input array(0,0,0,0,0,0,0,'a'), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array('b'))) when input array(0,0,0,0,0,0,0,'a b'), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array('"b'), array('c"'))) when input array(0,0,0,0,0,0,0,'a "b c"'), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array(-1, ''b c''))) when input array(0,0,0,0,0,0,0,'a "b c"'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array('[b'), array('c]'))) when input array(0,0,0,0,0,0,0,'a [b c]'), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array('[b'), array('c]'))) when input array(0,0,0,0,0,0,0,'a [b c]'), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array('b c'))) when input array(0,0,0,0,0,0,0,'a [b c]'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array('b c'))) when input array(0,0,0,0,0,0,0,'a [b c]'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), 'q' => array('b c'))) when input array(0,0,0,0,0,0,0,'a q=[b c]'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array('q=[b c'))) when input array(0,0,0,0,0,0,0,'a [q=[b c]'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), 'q' => array('[b'), array('c]'))) when input array(0,0,0,0,0,0,0,'a q=[b c]'), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), 'q' => array('b'), array('c'))) when input array(0,0,0,0,0,0,0,'a [q]=b c'), array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), 'q' => array(-1, ''b c''))) when input array(0,0,0,0,0,0,0,'a q="b c"'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array(-2, array(array('foo'), array('bar')), '(foo bar)'))) when input array(0,0,0,0,0,0,0,'(foo bar)'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 1, 'lambda' => 0), 'ops' => array('seperator' => ''), 'usedFeature' => array('subexp' => 0), 'rawblock' => false)

expect

array(false, array(array('foo'), array("'=='"), array('bar'))) when input array(0,0,0,0,0,0,0,"foo '==' bar"), array('flags' => array('strpar' => 0, 'advar' => 1, 'namev' => 1, 'noesc' => 0, 'this' => 0), 'rawblock' => false)

expect

array(false, array(array(-2, array(array('foo'), array('bar')), '( foo bar)'))) when input array(0,0,0,0,0,0,0,'( foo bar)'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 1, 'lambda' => 0), 'ops' => array('seperator' => ''), 'usedFeature' => array('subexp' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array(-1, '' b c''))) when input array(0,0,0,0,0,0,0,'a " b c"'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 0, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), 'q' => array(-1, '' b c''))) when input array(0,0,0,0,0,0,0,'a q=" b c"'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('foo'), array(-1, "' =='"), array('bar'))) when input array(0,0,0,0,0,0,0,"foo ' ==' bar"), array('flags' => array('strpar' => 0, 'advar' => 1, 'namev' => 1, 'noesc' => 0, 'this' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), array(' b c'))) when input array(0,0,0,0,0,0,0,'a [ b c]'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array(array('a'), 'q' => array(-1, "' d e'"))) when input array(0,0,0,0,0,0,0,"a q=' d e'"), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0), 'rawblock' => false)

expect

array(false, array('q' => array(-2, array(array('foo'), array('bar')), '( foo bar)'))) when input array(0,0,0,0,0,0,0,'q=( foo bar)'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false, 'helperresolver' => 0)

expect

array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','foo'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false)

expect

array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','"foo"'), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false)

expect

array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','[foo] '), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false)

expect

array(false, array(array('foo'))) when input array(0,0,0,0,0,0,'>','\'foo\''), array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'namev' => 1, 'noesc' => 0, 'exhlp' => 0, 'lambda' => 0), 'usedFeature' => array('subexp' => 0), 'ops' => array('seperator' => 0), 'rawblock' => false)

Return values
array<string|int, bool|int|array>

Return parsed result

setDelimiter()

Setup delimiter by default or provided string

public static setDelimiter(array<string, array|string|int> &$context[, string|null $left = null ][, string|null $right = null ]) : mixed
Parameters
$context : array<string, array|string|int>

Current context

$left : string|null = null

left string of a token

$right : string|null = null

right string of a token

Return values
mixed

subexpression()

Parse a subexpression then return parsed result.

public static subexpression(string $expression, array<string, array|string|int> &$context) : array<string|int, bool|int|array>
Parameters
$expression : string

the full string of a sub expression

$context : array<string, array|string|int>

current compile context

Tags
expect

array(\LightnCandy\Parser::SUBEXP, array(array('a'), array('b')), '(a b)') when input '(a b)', array('usedFeature' => array('subexp' => 0), 'flags' => array('advar' => 0, 'namev' => 0, 'this' => 0, 'exhlp' => 1, 'strpar' => 0))

Return values
array<string|int, bool|int|array>

Return parsed result

toString()

return token string

public static toString(array<string|int, string> $token[, array<string|int, string>|null $merge = null ]) : string
Parameters
$token : array<string|int, string>

detected handlebars {{ }} token

$merge : array<string|int, string>|null = null

list of token strings to be merged

Tags
expect

'c' when input array(0, 'a', 'b', 'c', 'd', 'e')

expect

'cd' when input array(0, 'a', 'b', 'c', 'd', 'e', 'f')

expect

'qd' when input array(0, 'a', 'b', 'c', 'd', 'e', 'f'), array(3 => 'q')

Return values
string

Return whole token

advancedVariable()

Analyze parsed token for advanved variables.

protected static advancedVariable(array<string|int, bool|int|array> $vars, array<string, array|string|int> &$context, string $token) : array<string|int, bool|int|array>
Parameters
$vars : array<string|int, bool|int|array>

parsed token

$context : array<string, array|string|int>

current compile context

$token : string

original token

Tags
expect

array(array('this')) when input array('this'), array('flags' => array('advar' => 1, 'namev' => 1, 'this' => 0,)), 0

expect

array(array()) when input array('this'), array('flags' => array('advar' => 1, 'namev' => 1, 'this' => 1)), 0

expect

array(array('a')) when input array('a'), array('flags' => array('advar' => 1, 'namev' => 1, 'this' => 0, 'strpar' => 0)), 0

expect

array(array('a'), array('b')) when input array('a', 'b'), array('flags' => array('advar' => 1, 'namev' => 1, 'this' => 0, 'strpar' => 0)), 0

expect

array('a' => array('b')) when input array('a=b'), array('flags' => array('advar' => 1, 'namev' => 1, 'this' => 0, 'strpar' => 0)), 0

expect

array('fo o' => array(\LightnCandy\Parser::LITERAL, '123')) when input array('[fo o]=123'), array('flags' => array('advar' => 1, 'namev' => 1, 'this' => 0)), 0

expect

array('fo o' => array(\LightnCandy\Parser::LITERAL, ''bar'')) when input array('[fo o]="bar"'), array('flags' => array('advar' => 1, 'namev' => 1, 'this' => 0)), 0

Return values
array<string|int, bool|int|array>

Return parsed result

analyze()

Analyze a token string and return parsed result.

protected static analyze(string $token, array<string, array|string|int> &$context) : array<string|int, bool|int|array>
Parameters
$token : string

preg_match results

$context : array<string, array|string|int>

current compile context

Tags
expect

array('foo', 'bar') when input 'foo bar', array('flags' => array('advar' => 1))

expect

array('foo', "'bar'") when input "foo 'bar'", array('flags' => array('advar' => 1))

expect

array('[fo o]', '"bar"') when input '[fo o] "bar"', array('flags' => array('advar' => 1))

expect

array('fo=123', 'bar="45', '6"') when input 'fo=123 bar="45 6"', array('flags' => array('advar' => 0))

expect

array('fo=123', 'bar="45 6"') when input 'fo=123 bar="45 6"', array('flags' => array('advar' => 1))

expect

array('[fo', 'o]=123') when input '[fo o]=123', array('flags' => array('advar' => 0))

expect

array('[fo o]=123') when input '[fo o]=123', array('flags' => array('advar' => 1))

expect

array('[fo o]=123', 'bar="456"') when input '[fo o]=123 bar="456"', array('flags' => array('advar' => 1))

expect

array('[fo o]="1 2 3"') when input '[fo o]="1 2 3"', array('flags' => array('advar' => 1))

expect

array('foo', 'a=(foo a=(foo a="ok"))') when input 'foo a=(foo a=(foo a="ok"))', array('flags' => array('advar' => 1))

Return values
array<string|int, bool|int|array>

Return parsed result

detectQuote()

Detect quote charactors

protected static detectQuote(string $string) : array<string, int>|null
Parameters
$string : string

the string to be detect the quote charactors

Return values
array<string, int>|null

Expected ending string when quote charactor be detected

getExpression()

Return array presentation for an expression

protected static getExpression(string $v, array<string, array|string|int> &$context, int $pos) : array<int, string>
Parameters
$v : string

analyzed expression names.

$context : array<string, array|string|int>

Current compile content.

$pos : int

expression position

Tags
expect

array('this') when input 'this', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 0)), 0

expect

array() when input 'this', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1)), 0

expect

array(1) when input '..', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(1) when input '../', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(1) when input '../.', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(1) when input '../this', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(1, 'a') when input '../a', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(2, 'a', 'b') when input '../../a.b', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(2, '[a]', 'b') when input '../../[a].b', array('flags' => array('strpar' => 0, 'advar' => 0, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(2, 'a', 'b') when input '../../[a].b', array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(0, 'id') when input 'this.id', array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 1, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array('this', 'id') when input 'this.id', array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(0, 'id') when input './id', array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 0

expect

array(\LightnCandy\Parser::LITERAL, ''a.b'') when input '"a.b"', array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 1

expect

array(\LightnCandy\Parser::LITERAL, '123') when input '123', array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 1

expect

array(\LightnCandy\Parser::LITERAL, 'null') when input 'null', array('flags' => array('strpar' => 0, 'advar' => 1, 'this' => 0, 'parent' => 1), 'usedFeature' => array('parent' => 0)), 1

Return values
array<int, string>

Return variable name array

getLiteral()

Return array presentation for a literal

protected static getLiteral(string $name, bool $asis[, bool $quote = false ]) : array<string|int, int|string>
Parameters
$name : string

variable name.

$asis : bool

keep the name as is or not

$quote : bool = false

add single quote or not

Return values
array<string|int, int|string>

Return variable name array

Search results