Пример #1
0
        /// <summary>
        /// Reads the next tag from the tokens.
        /// </summary>
        private ITag Read()
        {
            ITag t = null;

            if (IsTagStart())
            {
                Token t1, t2;
                t1 = t2 = GetToken();
                TokenCollection tc = new TokenCollection();
                if (t1 == null)
                {
                    return(null);
                }
                do
                {
                    this.index++;
                    t2.Next = GetToken();
                    t2      = t2.Next;
                    if (t2 == null)
                    {
                        break;
                    }
                    tc.Add(t2);
                } while (!IsTagEnd());

                if (tc.Last != null && (tc.Last.TokenKind == TokenKind.TagEnd))
                {
                    tc.Remove(tc.Last);
                }

                this.index++;

                //if (tc.Count == 1 && tc[0] != null && tc[0].TokenKind == TokenKind.Comment)
                //{
                //    return new TextTag();
                //}

                try
                {
                    t = Read(tc);
                }
                catch (TemplateException)
                {
                    throw;
                }
                catch (System.Exception e)
                {
                    throw new ParseException($"Parse error:{tc.ToString()}\r\nError message:{e.Message}", tc.First.BeginLine, tc.First.BeginColumn);//标签分析异常
                }

                if (t != null)
                {
                    t.FirstToken = t1;
                    if (t.Children.Count == 0 || t.LastToken == null || t2.CompareTo(t.LastToken) > 0)
                    {
                        t.LastToken = t2;
                    }
                }
                else
                {
                    throw new ParseException($"Unexpected  tag: {tc.ToString()}", tc.First.BeginLine, tc.First.BeginColumn); //未知的标签
                }
            }
            else
            {
                t            = new TextTag();
                t.FirstToken = GetToken();
                t.LastToken  = null;
                this.index++;
            }
            return(t);
        }