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

// 取元素的值
                        } else {
                            if (pm.isSingleValue())
                                obj = new ElementTerminalExpression(key);
                            else
                                obj = new ElementsTerminalExpression(key);
                        }
                    }

list.push(obj);
                }

return list;
            }

// 开始实现第三步-------------------------------------

/**
             * 构建抽象语法树
             * @param  {[type]} list [把每个元素转换成为相应的解释器对象后的数组]
             * @return {[type]}      [description]
             */
            function buildTree(list) {
                // 第一个对象,也是返回去的对象,就是抽象语法树的根
                var returnReadXMLExpr = null;
                // 定义上一个对象
                var preReadXmlExpr = null;
                var readXml, ele, eles;

for (var i = 0, len = list.length; i < len; i++) {
                    readXml = list[i];
                    // 说明是第一个元素
                    if (preReadXmlExpr === null) {
                        preReadXmlExpr = readXml;
                        returnReadXMLExpr = readXml;

// 把元素添加到上一个对象下面,同时把本对象设置成为oldRe
                        // 作为下一个对象的父节点
                    } else {
                        if (preReadXmlExpr instanceof ElementExpression) {
                            ele = preReadXmlExpr;
                            ele.addEle(readXml);
                            preReadXmlExpr = readXml;
                        } else if (preReadXmlExpr instanceof ElementsExpression) {
                            eles = preReadXmlExpr;
                            eles.addEle(readXml);
                            preReadXmlExpr = readXml;
                        }
                    }
                }

return returnReadXMLExpr;
            }

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

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