{% if user_name == "YangQing-rong's username" || user_name == 'admin' %} { {10*20.043123+8*times}}{% endif %}
以上是待切分的模板view/test1.html。
以下是lexer.php,用来测试切分效果的。
scan($viewName , $c );print_r( $dt );
切分是1.先按{%%}和{
{}}切分成文本块和代码块,2.然后再对代码块进行切分,主要切分的有 数字,运算符,字符串,名称。以下是切分后的输出。有tokens元素的是代码块。
Array( [0] => Wudimei\View\Token Object ( [line:protected] => 1 [column:protected] => 0 [position:protected] => 0 [content:protected] => {% [length:protected] => 2 [tokenValue:protected] => 1 [tokens:protected] => Array ( ) ) [1] => Wudimei\View\Token Object ( [line:protected] => 1 [column:protected] => 2 [position:protected] => 2 [content:protected] => if user_name == "YangQing-rong's username" || user_name == 'admin' [length:protected] => 69 [tokenValue:protected] => 7 [tokens:protected] => Array ( [0] => Wudimei\View\Token Object ( [line:protected] => 1 [column:protected] => 3 [position:protected] => 2 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [1] => Wudimei\View\Token Object ( [line:protected] => 1 [column:protected] => 4 [position:protected] => 3 [content:protected] => if [length:protected] => 2 [tokenValue:protected] => 15 [tokens:protected] => Array ( ) ) [2] => Wudimei\View\Token Object ( [line:protected] => 1 [column:protected] => 6 [position:protected] => 5 [content:protected] => [length:protected] => 2 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [3] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 1 [position:protected] => 7 [content:protected] => user_name [length:protected] => 9 [tokenValue:protected] => 15 [tokens:protected] => Array ( ) ) [4] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 10 [position:protected] => 16 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [5] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 11 [position:protected] => 17 [content:protected] => == [length:protected] => 2 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [6] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 13 [position:protected] => 19 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [7] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 14 [position:protected] => 20 [content:protected] => "YangQing-rong's username" [length:protected] => 26 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [8] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 40 [position:protected] => 46 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [9] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 41 [position:protected] => 47 [content:protected] => || [length:protected] => 2 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [10] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 43 [position:protected] => 49 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [11] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 44 [position:protected] => 50 [content:protected] => user_name [length:protected] => 9 [tokenValue:protected] => 15 [tokens:protected] => Array ( ) ) [12] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 53 [position:protected] => 59 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [13] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 54 [position:protected] => 60 [content:protected] => == [length:protected] => 2 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [14] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 56 [position:protected] => 62 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [15] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 57 [position:protected] => 63 [content:protected] => 'admin' [length:protected] => 7 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [16] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 64 [position:protected] => 70 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) ) ) [2] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 65 [position:protected] => 71 [content:protected] => %} [length:protected] => 2 [tokenValue:protected] => 2 [tokens:protected] => Array ( ) ) [3] => Wudimei\View\Token Object ( [line:protected] => 2 [column:protected] => 67 [position:protected] => 73 [content:protected] => [length:protected] => 2 [tokenValue:protected] => 10 [tokens:protected] => Array ( ) ) [4] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 2 [position:protected] => 75 [content:protected] => { { [length:protected] => 2 [tokenValue:protected] => 3 [tokens:protected] => Array ( ) ) [5] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 4 [position:protected] => 77 [content:protected] => 10*20.043123+8*times [length:protected] => 20 [tokenValue:protected] => 8 [tokens:protected] => Array ( [0] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 5 [position:protected] => 77 [content:protected] => 10 [length:protected] => 2 [tokenValue:protected] => 11 [tokens:protected] => Array ( ) ) [1] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 7 [position:protected] => 79 [content:protected] => * [length:protected] => 1 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [2] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 8 [position:protected] => 80 [content:protected] => 20.043123 [length:protected] => 9 [tokenValue:protected] => 11 [tokens:protected] => Array ( ) ) [3] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 17 [position:protected] => 89 [content:protected] => + [length:protected] => 1 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [4] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 18 [position:protected] => 90 [content:protected] => 8 [length:protected] => 1 [tokenValue:protected] => 11 [tokens:protected] => Array ( ) ) [5] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 19 [position:protected] => 91 [content:protected] => * [length:protected] => 1 [tokenValue:protected] => 13 [tokens:protected] => Array ( ) ) [6] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 20 [position:protected] => 92 [content:protected] => times [length:protected] => 5 [tokenValue:protected] => 15 [tokens:protected] => Array ( ) ) ) ) [6] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 24 [position:protected] => 97 [content:protected] => }} [length:protected] => 2 [tokenValue:protected] => 4 [tokens:protected] => Array ( ) ) [7] => Wudimei\View\Token Object ( [line:protected] => 3 [column:protected] => 26 [position:protected] => 99 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 10 [tokens:protected] => Array ( ) ) [8] => Wudimei\View\Token Object ( [line:protected] => 4 [column:protected] => 1 [position:protected] => 100 [content:protected] => {% [length:protected] => 2 [tokenValue:protected] => 1 [tokens:protected] => Array ( ) ) [9] => Wudimei\View\Token Object ( [line:protected] => 4 [column:protected] => 3 [position:protected] => 102 [content:protected] => endif [length:protected] => 7 [tokenValue:protected] => 7 [tokens:protected] => Array ( [0] => Wudimei\View\Token Object ( [line:protected] => 4 [column:protected] => 4 [position:protected] => 102 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) [1] => Wudimei\View\Token Object ( [line:protected] => 4 [column:protected] => 5 [position:protected] => 103 [content:protected] => endif [length:protected] => 5 [tokenValue:protected] => 15 [tokens:protected] => Array ( ) ) [2] => Wudimei\View\Token Object ( [line:protected] => 4 [column:protected] => 10 [position:protected] => 108 [content:protected] => [length:protected] => 1 [tokenValue:protected] => 14 [tokens:protected] => Array ( ) ) ) ) [10] => Wudimei\View\Token Object ( [line:protected] => 4 [column:protected] => 10 [position:protected] => 109 [content:protected] => %} [length:protected] => 2 [tokenValue:protected] => 2 [tokens:protected] => Array ( ) ) [11] => Wudimei\View\Token Object ( [line:protected] => 4 [column:protected] => 12 [position:protected] => 111 [content:protected] => [length:protected] => 2 [tokenValue:protected] => 10 [tokens:protected] => Array ( ) ))