예제 #1
0
 public void ClearEmpty(AnalyzeBlock root)
 {
     foreach (var unit in root.ResetElements())
     {
         if (unit.IsEmpty)
         {
             continue;
         }
         var block = unit as AnalyzeBlock;
         if (block != null)
         {
             block.Parent = root;
             ClearEmpty(block);
             if (block.Elements.Count == 0)
             {
                 continue;
             }
             block.Release();
             root.Append(block.Elements.Count == 1 ? block.Elements[0] : unit);
         }
         else
         {
             root.Append(unit);
         }
     }
 }
예제 #2
0
        private static void LinkBarket(AnalyzeBlock root)
        {
            AnalyzeUnitBase pre = null;
            AnalyzeBlock    cur = null;

            // CodeItemType pretype = CodeItemType.None;
            foreach (var unit in root.ResetElements())
            {
                var block = unit as AnalyzeBlock;
                if (block == null)
                {
                    var word = (WordUnit)unit;
                    pre = word.IsPunctuate || word.IsKeyWord ? null : unit;
                    root.Append(unit);
                    //pretype = CodeItemType.None;
                    continue;
                }
                LinkBarket(block);
                switch (block.ItemType)
                {
                case CodeItemType.Brackets21:
                    //case CodeItemType.Brackets31:
                    //if (pretype == CodeItemType.None || cur == null)
                {
                    if (pre == null)
                    {
                        root.Append(unit);
                        break;
                    }
                    if (pre != cur)
                    {
                        cur?.Release();
                        cur = new AnalyzeBlock();
                        var wd = pre as WordUnit;
                        cur.Primary = wd ?? ((AnalyzeBlock)pre).Primary;
                        root.Elements.Remove(pre);
                        cur.Elements.Add(pre);
                        cur.ItemRace   = CodeItemRace.Value;
                        cur.ItemFamily = CodeItemFamily.ValueSentence;
                        cur.ItemType   = CodeItemType.Brackets21 == block.ItemType
                                    ? CodeItemType.Call
                                    : CodeItemType.Table_Child;
                        root.Elements.Add(cur);
                        pre = cur;
                    }
                    unit.Name = "_call_";
                    cur.Elements.Add(unit);
                }
                    //root.Elements.Add(block);
                    //pretype = block.ItemType;
                    continue;
                }
                root.Append(unit);
                pre = unit;
            }
            cur?.Release();
        }
예제 #3
0
        /// <summary>
        ///     分级组合
        ///     0 括号块组合(不使用此方法)
        ///     1 .;单词粘连组合
        ///     2 一元操作符组合(not # -)
        ///     3 串联组合(.. ^)
        ///     4 乘除余指数(* / %)
        ///     5 加减(+ -)
        ///     6 比较(== ~大小于)
        ///     7 逻辑(and)
        ///     8 逻辑(or)
        ///     9 赋值(=)
        /// </summary>
        /// <param name="root"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public void MergeSimpleBlock(AnalyzeBlock root, int level)
        {
            AnalyzeUnitBase preUnit  = null;
            var             joinNext = false;
            var             array    = root.Elements.ToArray();

            root.Elements.Clear();
            foreach (var unit in array)
            {
                if (unit.IsEmpty)
                {
                    continue;
                }
                if (unit.IsContent)
                {
                    root.Append(unit);
                    joinNext = false;
                    preUnit  = null;
                    continue;
                }

                var block = unit as AnalyzeBlock;
                if (block != null)
                {
                    if (!unit.IsLock && !unit.IsUnit)
                    {
                        MergeSimpleBlock(block, level);
                    }
                    if (!joinNext)
                    {
                        root.Append(block);
                        preUnit = block;
                    }
                    else
                    {
                        ((AnalyzeBlock)preUnit).Append(block);
                        joinNext = false;
                    }
                    continue;
                }
                var word = (WordUnit)unit;
                if (word.JoinLevel == level)
                {
                    switch (word.JoinFeature)
                    {
                    case JoinFeature.Front:
                        var newBlock = new AnalyzeBlock
                        {
                            Primary = word
                        };
                        root.Append(newBlock);
                        newBlock.Append(unit);
                        preUnit  = newBlock;
                        joinNext = true;
                        continue;

                    case JoinFeature.TowWay:
                        newBlock = new AnalyzeBlock
                        {
                            Primary = word
                        };
                        if (preUnit == null)
                        {
                            newBlock.IsError = true;
                        }
                        else
                        {
                            root.Elements.Remove(preUnit);
                            newBlock.Append(preUnit);
                        }
                        root.Append(newBlock);
                        newBlock.Append(unit);
                        preUnit  = newBlock;
                        joinNext = true;
                        continue;
                    }
                }
                if (!joinNext)
                {
                    root.Append(word);
                    if (!word.IsSpace && unit.ItemRace != CodeItemRace.Assist)
                    {
                        preUnit = level <= 6 && unit.ItemRace == CodeItemRace.Range ? null : word;
                    }
                    continue;
                }
                if (word.Char == ';')//终止符号出错
                {
                    ((AnalyzeBlock)preUnit).IsError = true;
                    joinNext = false;
                    preUnit  = null;
                    continue;
                }
                ((AnalyzeBlock)preUnit).Append(unit);
                if (word.IsSpace || unit.ItemRace == CodeItemRace.Assist)
                {
                    continue;
                }
                if (level < 6)
                {
                    preUnit.IsError = unit.ItemRace == CodeItemRace.Range;
                }
                joinNext = false;
            }
        }
예제 #4
0
        /// <summary>
        ///     串联
        /// </summary>
        /// <param name="root"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public void ConnectBlock(AnalyzeBlock root, int level)
        {
            AnalyzeUnitBase pre  = null;
            var             step = 0;
            AnalyzeBlock    cur  = root;

            foreach (var unit in root.ResetElements())
            {
                if (unit.IsEmpty)
                {
                    continue;
                }
                var block = unit as AnalyzeBlock;
                if (block != null)
                {
                    if (!unit.IsLock && !unit.IsUnit)
                    {
                        ConnectBlock(block, level);
                    }
                    pre = block;
                }
                else
                {
                    if (unit.JoinLevel == level && unit.JoinFeature == JoinFeature.Connect)
                    {
                        if (step == 0)
                        {
                            cur = new AnalyzeBlock
                            {
                                Primary = (WordUnit)unit
                            };
                            if (pre != null)
                            {
                                cur.Append(pre);
                                root.Elements.RemoveAt(root.Elements.Count - 1);
                            }
                            else
                            {
                                cur.IsError = true;
                            }
                            root.Append(cur);
                        }
                        step = 1;
                        cur.Append(unit);
                        continue;
                    }
                    pre = ((WordUnit)unit).IsPunctuate || ((WordUnit)unit).IsKeyWord ? null : unit;
                }
                switch (step)
                {
                case 0:
                    root.Append(unit);
                    break;

                case 1:
                    step = 2;
                    cur.Append(unit);
                    break;

                default:
                    step = 0;
                    root.Append(unit);
                    break;
                }
            }
        }