zephir开发的扩展“wudimei框架”之模板词法扫描完成代码切分

Posted wudimei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zephir开发的扩展“wudimei框架”之模板词法扫描完成代码切分相关的知识,希望对你有一定的参考价值。

{% if 
user_name == "YangQing-rong‘s username" || user_name == ‘admin‘ %}
 {{10*20.043123+8*times}}
{% endif %}

以上是待切分的模板view/test1.html

以下是lexer.php,用来测试切分效果的。

<?php

use WudimeiViewLexer;


 

$viewName = ‘view/test1.html‘;
$c = file_get_contents(__DIR__.‘/‘ . $viewName );

$l =new Lexer();
$dt = $l->scan($viewName , $c );
print_r( $dt );

切分是1.先按{%%}和{{}}切分成文本块和代码块,2.然后再对代码块进行切分,主要切分的有 数字,运算符,字符串,名称。

以下是切分后的输出。有tokens元素的是代码块。

Array
(
    [0] => WudimeiViewToken Object
        (
            [line:protected] => 1
            [column:protected] => 0
            [position:protected] => 0
            [content:protected] => {%
            [length:protected] => 2
            [tokenValue:protected] => 1
            [tokens:protected] => Array
                (
                )

        )

    [1] => WudimeiViewToken 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] => WudimeiViewToken Object
                        (
                            [line:protected] => 1
                            [column:protected] => 3
                            [position:protected] => 2
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [1] => WudimeiViewToken Object
                        (
                            [line:protected] => 1
                            [column:protected] => 4
                            [position:protected] => 3
                            [content:protected] => if
                            [length:protected] => 2
                            [tokenValue:protected] => 15
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [2] => WudimeiViewToken Object
                        (
                            [line:protected] => 1
                            [column:protected] => 6
                            [position:protected] => 5
                            [content:protected] =>  

                            [length:protected] => 2
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [3] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 1
                            [position:protected] => 7
                            [content:protected] => user_name
                            [length:protected] => 9
                            [tokenValue:protected] => 15
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [4] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 10
                            [position:protected] => 16
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [5] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 11
                            [position:protected] => 17
                            [content:protected] => ==
                            [length:protected] => 2
                            [tokenValue:protected] => 13
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [6] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 13
                            [position:protected] => 19
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [7] => WudimeiViewToken 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] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 40
                            [position:protected] => 46
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [9] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 41
                            [position:protected] => 47
                            [content:protected] => ||
                            [length:protected] => 2
                            [tokenValue:protected] => 13
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [10] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 43
                            [position:protected] => 49
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [11] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 44
                            [position:protected] => 50
                            [content:protected] => user_name
                            [length:protected] => 9
                            [tokenValue:protected] => 15
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [12] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 53
                            [position:protected] => 59
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [13] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 54
                            [position:protected] => 60
                            [content:protected] => ==
                            [length:protected] => 2
                            [tokenValue:protected] => 13
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [14] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 56
                            [position:protected] => 62
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [15] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 57
                            [position:protected] => 63
                            [content:protected] => ‘admin‘
                            [length:protected] => 7
                            [tokenValue:protected] => 13
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [16] => WudimeiViewToken Object
                        (
                            [line:protected] => 2
                            [column:protected] => 64
                            [position:protected] => 70
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                )

        )

    [2] => WudimeiViewToken Object
        (
            [line:protected] => 2
            [column:protected] => 65
            [position:protected] => 71
            [content:protected] => %}
            [length:protected] => 2
            [tokenValue:protected] => 2
            [tokens:protected] => Array
                (
                )

        )

    [3] => WudimeiViewToken Object
        (
            [line:protected] => 2
            [column:protected] => 67
            [position:protected] => 73
            [content:protected] => 
 
            [length:protected] => 2
            [tokenValue:protected] => 10
            [tokens:protected] => Array
                (
                )

        )

    [4] => WudimeiViewToken Object
        (
            [line:protected] => 3
            [column:protected] => 2
            [position:protected] => 75
            [content:protected] => {{
            [length:protected] => 2
            [tokenValue:protected] => 3
            [tokens:protected] => Array
                (
                )

        )

    [5] => WudimeiViewToken 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] => WudimeiViewToken Object
                        (
                            [line:protected] => 3
                            [column:protected] => 5
                            [position:protected] => 77
                            [content:protected] => 10
                            [length:protected] => 2
                            [tokenValue:protected] => 11
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [1] => WudimeiViewToken Object
                        (
                            [line:protected] => 3
                            [column:protected] => 7
                            [position:protected] => 79
                            [content:protected] => *
                            [length:protected] => 1
                            [tokenValue:protected] => 13
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [2] => WudimeiViewToken Object
                        (
                            [line:protected] => 3
                            [column:protected] => 8
                            [position:protected] => 80
                            [content:protected] => 20.043123
                            [length:protected] => 9
                            [tokenValue:protected] => 11
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [3] => WudimeiViewToken Object
                        (
                            [line:protected] => 3
                            [column:protected] => 17
                            [position:protected] => 89
                            [content:protected] => +
                            [length:protected] => 1
                            [tokenValue:protected] => 13
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [4] => WudimeiViewToken Object
                        (
                            [line:protected] => 3
                            [column:protected] => 18
                            [position:protected] => 90
                            [content:protected] => 8
                            [length:protected] => 1
                            [tokenValue:protected] => 11
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [5] => WudimeiViewToken Object
                        (
                            [line:protected] => 3
                            [column:protected] => 19
                            [position:protected] => 91
                            [content:protected] => *
                            [length:protected] => 1
                            [tokenValue:protected] => 13
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [6] => WudimeiViewToken Object
                        (
                            [line:protected] => 3
                            [column:protected] => 20
                            [position:protected] => 92
                            [content:protected] => times
                            [length:protected] => 5
                            [tokenValue:protected] => 15
                            [tokens:protected] => Array
                                (
                                )

                        )

                )

        )

    [6] => WudimeiViewToken Object
        (
            [line:protected] => 3
            [column:protected] => 24
            [position:protected] => 97
            [content:protected] => }}
            [length:protected] => 2
            [tokenValue:protected] => 4
            [tokens:protected] => Array
                (
                )

        )

    [7] => WudimeiViewToken Object
        (
            [line:protected] => 3
            [column:protected] => 26
            [position:protected] => 99
            [content:protected] => 

            [length:protected] => 1
            [tokenValue:protected] => 10
            [tokens:protected] => Array
                (
                )

        )

    [8] => WudimeiViewToken Object
        (
            [line:protected] => 4
            [column:protected] => 1
            [position:protected] => 100
            [content:protected] => {%
            [length:protected] => 2
            [tokenValue:protected] => 1
            [tokens:protected] => Array
                (
                )

        )

    [9] => WudimeiViewToken Object
        (
            [line:protected] => 4
            [column:protected] => 3
            [position:protected] => 102
            [content:protected] =>  endif 
            [length:protected] => 7
            [tokenValue:protected] => 7
            [tokens:protected] => Array
                (
                    [0] => WudimeiViewToken Object
                        (
                            [line:protected] => 4
                            [column:protected] => 4
                            [position:protected] => 102
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [1] => WudimeiViewToken Object
                        (
                            [line:protected] => 4
                            [column:protected] => 5
                            [position:protected] => 103
                            [content:protected] => endif
                            [length:protected] => 5
                            [tokenValue:protected] => 15
                            [tokens:protected] => Array
                                (
                                )

                        )

                    [2] => WudimeiViewToken Object
                        (
                            [line:protected] => 4
                            [column:protected] => 10
                            [position:protected] => 108
                            [content:protected] =>  
                            [length:protected] => 1
                            [tokenValue:protected] => 14
                            [tokens:protected] => Array
                                (
                                )

                        )

                )

        )

    [10] => WudimeiViewToken Object
        (
            [line:protected] => 4
            [column:protected] => 10
            [position:protected] => 109
            [content:protected] => %}
            [length:protected] => 2
            [tokenValue:protected] => 2
            [tokens:protected] => Array
                (
                )

        )

    [11] => WudimeiViewToken Object
        (
            [line:protected] => 4
            [column:protected] => 12
            [position:protected] => 111
            [content:protected] => 


            [length:protected] => 2
            [tokenValue:protected] => 10
            [tokens:protected] => Array
                (
                )

        )

)

 

以上是关于zephir开发的扩展“wudimei框架”之模板词法扫描完成代码切分的主要内容,如果未能解决你的问题,请参考以下文章

PHP Extension开发(Zephir版本)

PHP7 学习笔记如何使用zephir编译一个扩展记录

php安装phalcon扩展

Zephir

ecshop二次开发系统缓存优化之扩展数据缓存的必要性与方法

Flask扩展之http客户端开发