Ejemplo n.º 1
0
        /// <summary>
        /// 转换描述文本为表达式数组
        /// </summary>
        /// <remarks>
        /// a: {#$ TrimHTML(%Title%) $#}
        /// b: {#$ Repeat(5,%Title%) $#}
        /// c: {#$ Length(TrimHTML(%Title%)) $#}
        /// d: {#$ Length(TrimHTML(%Title%)) > 3 ? "太长" : "OK" $#}
        /// e: {#$ Replace(TrimHTML(%Title%), "电子"," ") $#}
        /// f: {#$ ReplaceX(TrimHTML(%Title%), "\w","") $#}
        /// </remarks>
        public static List<InnerExpression> GetInnerExpressions(string ExpPaper, IResourceDependency Res)
        {
            //OleDbHelper.AppendToFile("~/debug.txt", "\n\n 解析:" + ExpPaper);
            List<InnerExpression> expList = new List<InnerExpression>();

            int lenT = ExpPaper.Length;
            int cursor = 0, iflag = 0;
            char chr = ExpPaper[cursor];
            string strTemp = "", strExpression = "";
            ReservedWords words = null;

            while (cursor < lenT)
            {
                #region 字符扫描
                chr = ExpPaper[cursor];
                if (!ReservedWords.IsReservedChar(chr))
                {
                    ++cursor;
                    continue;
                }
                else
                {
                    if (cursor > iflag)
                    {
                        strTemp = ExpPaper.Substring(iflag, cursor - iflag).Trim();
                    }
                    iflag = cursor;

                    words = new ReservedWords(chr);
                    if (words.IsBraceChar())
                    {
                        #region 配对字符解析
                        ReservedWords.MoveToCharBrace(chr, ReservedWords.GetBraceChar(chr),
                                ref cursor, ref ExpPaper);

                        if (chr == '(')
                        {
                            //Function
                            strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1);
                            //OleDbHelper.AppendToFile("~/debug.txt", "\n 函数体:" + strExpression);
                            if (strTemp.Length == 0) strTemp = null;
                            expList.Add(new InnerExpression(strTemp, strExpression, Res));
                        }
                        else if (chr == '?')
                        {
                            strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1).Trim();
                            #region 跳出双引号里面的 : 操作符号
                            if (strExpression.IndexOf('"') != -1 && strExpression[0] != '"')
                            {
                                ReservedWords.MoveToCharBrace('"', '"', ref cursor, ref ExpPaper);
                                ReservedWords.MoveToCharBrace(ExpPaper[cursor], ':', ref cursor, ref ExpPaper);
                                strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1).Trim();
                            }
                            #endregion

                            #region 跳出单引号里面的 : 操作符号
                            if (strExpression.IndexOf('\'') != -1 && strExpression[0] != '\'')
                            {
                                ReservedWords.MoveToCharBrace('\'', '\'', ref cursor, ref ExpPaper);
                                ReservedWords.MoveToCharBrace(ExpPaper[cursor], ':', ref cursor, ref ExpPaper);
                                strExpression = ExpPaper.Substring(iflag + 1, cursor - iflag - 1).Trim();
                            }
                            #endregion

                            if (strTemp.Length > 0)
                            {
                                expList.Add(new InnerExpression(strTemp));
                            }
                            expList.Add(new InnerExpression("?"));
                            expList.Add(new InnerExpression(strExpression));
                            expList.Add(new InnerExpression(":"));

                            //Util.Debug(false, ExpPaper.Substring(cursor));
                        }
                        else if (chr == '[')
                        {
                            // {#$["首页","新闻","动态","联系"][2]$#}	= "动态"
                            #region 数组情况
                            if (cursor < lenT - 1)
                            {
                                char aIdx = ExpPaper[cursor + 1];
                                while (aIdx == '[')
                                {
                                    cursor++;
                                    ReservedWords.MoveToCharBrace(aIdx, ReservedWords.GetBraceChar(aIdx), ref cursor, ref ExpPaper);
                                    if (cursor < (lenT - 1))
                                    {
                                        aIdx = ExpPaper[cursor + 1];
                                    }
                                    else { break; }
                                }
                                strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1);
                                expList.Add(new InnerExpression(strExpression, ',', Res));
                            }
                            else
                            {
                                #region 获取数组下标操作TODO
                                strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1);
                                expList.Add(new InnerExpression(strExpression, Res));
                                #endregion
                            }
                            #endregion
                        }
                        else if (chr == '$')
                        {
                            #region 内置系统标签
                            strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1);
                            SystemTag sysTag = new SystemTag(string.Concat("{#", strExpression, "#}"));
                            sysTag.SetResourceDependency(Res);
                            //OleDbHelper.AppendToFile("~/debug.log", System.Environment.NewLine + string.Concat("{#", strExpression, "#}", "\n", sysTag.ToString()));
                            expList.Add(new InnerExpression(sysTag.ToString()));
                            #endregion
                        }
                        else if (chr == '"' || chr == '\'')
                        {
                            strExpression = ExpPaper.Substring(iflag, cursor - iflag + 1);
                            //Util.Debug(false, "Find String:" + strExpression);
                            expList.Add(new InnerExpression(strExpression));
                        }
                        #endregion

                        iflag = cursor + 1;
                    }
                    else if (words.IsOperator())
                    {
                        strExpression = strTemp;
                        if (strExpression.Length > 0)
                        {
                            InnerExpression exp = new InnerExpression(strExpression);
                            expList.Add(exp);
                        }

                        #region 处理操作符号

                    ParseOperator:

                        char chrNext = ExpPaper[cursor + 1];
                        if ((chr == '+' || chr == '-') && char.IsNumber(chrNext))
                        {
                            #region 正负号处理
                            ++cursor;
                            if (cursor < lenT)
                            {
                                ReservedWords.MoveToCharInRange(ref cursor, ref ExpPaper, ' ', '*', '/', '%', '+', '-', '>', '<', '=', '!', '&', '^', '|');
                                expList.Add(new InnerExpression(ExpPaper.Substring(iflag, cursor - iflag)));

                                #region 如遇操作符
                                if (cursor < lenT && ExpPaper[cursor] != ' ')
                                {
                                    iflag = cursor;
                                    chr = ExpPaper[cursor];
                                    //Util.Debug(false, "new char: = [" + chr.ToString() + "]");
                                    goto ParseOperator;
                                }
                                #endregion
                            }
                            #endregion
                        }
                        else
                        {
                            // *= += -= ++ -- <>
                            if (ReservedWords.IsCharInRange(chrNext, '=', '+', '-', '>'))
                            {
                                expList.Add(new InnerExpression(chr.ToString() + chrNext.ToString()));
                                ++cursor;
                            }
                            else
                            {
                                expList.Add(new InnerExpression(chr.ToString()));
                            }
                        }
                        #endregion

                        iflag = cursor + 1;
                    }
                    else
                    {
                        if (strTemp.Length > 0)
                        {
                            expList.Add(new InnerExpression(strTemp));
                        }
                        //Util.Debug(false, "11 - [" + strTemp.Trim() + "]" + "chr=[" + chr.ToString() + "]");
                    }
                }
                ++cursor;
                #endregion
            }

            if (iflag < cursor)
            {
                expList.Add(new InnerExpression(ExpPaper.Substring(iflag, cursor - iflag).Trim()));
            }

            //#region 解析结果查看
            //foreach (InnerExpression ext in expList)
            //{
            //    //Util.Debug(false, string.Concat("Exp定义:", ext.TagDefinition));
            //    OleDbHelper.AppendToFile("~/debug.log", System.Environment.NewLine + string.Concat("Exp定义:", ext.TagDefinition));
            //}
            //#endregion
            return expList;
        }