javascript设计模式之解释器模式详解(14)

return {
                // 公共方法
                parse: function (expr) {
                    listEle = [];

var mapPath = parseMapPath(expr);
                    var list = mapPath2Interpreter(mapPath);

return buildTree(list);
                }
            };
        }();

void function () {
            // 准备上下文
            var c = new Context('Interpreter.xml');
            // 通过解析其获取抽象语法树
            var readXmlExpr = Parser.parse('root/a/b/d$.id$');
            // 请求解析,获取返回值
            var ss = readXmlExpr.interpret(c);

console.log('------------parsing--------------');
            for (var i = 0, len = ss.length; i < len; i++) {
                console.log('d的属性id的值是 = ' + ss[i]);
            }
            console.log('---------------parsed--------------');

// 如果要使用同一个上下文,连续进行解析,需要重新初始化上下文对象
            c.reInit();
            var readxmlExpr2 = Parser.parse('root/a/b/d$');
            var ss2 = readxmlExpr2.interpret(c);
            console.log('------------parsing--------------');
            for (i = 0, len = ss2.length; i < len; i++) {
                console.log('d的值是 = ' + ss2[i]);
            }
            console.log('---------------parsed--------------');

c.reInit();
            var readxmlExpr3 = Parser.parse('root/a/b/c');
            var ss3 = readxmlExpr3.interpret(c);
            console.log('------------parsing--------------');
            console.log('c的name属性值是 = ' + ss3);
            console.log('---------------parsed--------------');

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgpjfd.html