/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public Tag Parse(TemplateParser parser, TokenCollection tc) { if (tc.First.TokenKind == TokenKind.TextData && tc.Count > 2 && (tc[1].TokenKind == TokenKind.LeftParentheses) && tc.Last.TokenKind == TokenKind.RightParentheses) { FunctaionTag tag = new FunctaionTag(); tag.Name = tc.First.Text; Int32 pos = 0, start = 2, end; for (Int32 i = 2; i < tc.Count; i++) { end = i; switch (tc[i].TokenKind) { case TokenKind.Comma: if (pos == 0) { TokenCollection coll = new TokenCollection(); coll.Add(tc, start, end - 1); if (coll.Count > 0) { tag.AddChild(parser.Read(coll)); } start = i + 1; } break; default: if (tc[i].TokenKind == TokenKind.LeftParentheses) { pos++; } else if (tc[i].TokenKind == TokenKind.RightParentheses) { pos--; } if (i == tc.Count - 1) { TokenCollection coll = new TokenCollection(); coll.Add(tc, start, end - 1); if (coll.Count > 0) { tag.AddChild(parser.Read(coll)); } } break; } } return(tag); } return(null); }
public Tag Parse(TemplateParser parser, TokenCollection tc) { if (tc.Count > 2 && tc.First.TokenKind == TokenKind.TextData && HasDot(tc)) { ReferenceTag tag = new ReferenceTag(); Int32 start, end, pos; start = end = pos = 0; for (Int32 i = 0; i < tc.Count; i++) { end = i; switch (tc[i].TokenKind) { case TokenKind.Dot: if (pos == 0) { TokenCollection coll = new TokenCollection(); coll.Add(tc, start, end - 1); tag.AddChild(parser.Read(coll)); start = i + 1; } break; default: if (tc[i].TokenKind == TokenKind.LeftParentheses) { pos++; } else if (tc[i].TokenKind == TokenKind.RightParentheses) { pos--; } if (i == tc.Count - 1) { TokenCollection coll = new TokenCollection(); coll.Add(tc, start, end); tag.AddChild(parser.Read(coll)); } break; } } if (tag.Children.Count > 0) { if (tag.Children.Count == 1) { return(tag.Children[0]); } } return(tag); } return(null); }
public Tag Parse(TemplateParser parser, TokenCollection tc) { if (tc.Count > 0 && Common.ParserHelpers.IsEqual(Field.KEY_FOREACH, tc.First.Text)) { if (tc.Count > 5 && tc[1].TokenKind == TokenKind.LeftParentheses && tc[2].TokenKind == TokenKind.TextData && Common.ParserHelpers.IsEqual(tc[3].Text, Field.KEY_IN) && tc.Last.TokenKind == TokenKind.RightParentheses) { ForeachTag tag = new ForeachTag(); tag.Name = tc[2].Text; TokenCollection coll = new TokenCollection(); coll.Add(tc, 4, tc.Count - 2); tag.Source = parser.Read(coll); while (parser.MoveNext()) { tag.Children.Add(parser.Current); if (parser.Current is EndTag) { return(tag); } } throw new Exception.ParseException(String.Concat("foreach is not properly closed by a end tag:", tc), tc.First.BeginLine, tc.First.BeginColumn); } else { throw new Exception.ParseException(String.Concat("syntax error near foreach:", tc), tc.First.BeginLine, tc.First.BeginColumn); } } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public Tag Parse(TemplateParser parser, TokenCollection tc) { if (tc != null && parser != null && tc.Count > 3 && Common.Utility.IsEqual(tc.First.Text, Field.KEY_ELSEIF)) { if (tc[1].TokenKind == TokenKind.LeftParentheses && tc.Last.TokenKind == TokenKind.RightParentheses) { ElseifTag tag = new ElseifTag(); TokenCollection coll = new TokenCollection(); coll.Add(tc, 2, tc.Count - 2); tag.Test = parser.Read(coll); return(tag); } else { throw new Exception.ParseException(String.Concat("syntax error near if:", tc), tc.First.BeginLine, tc.First.BeginColumn); } } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public Tag Parse(TemplateParser parser, TokenCollection tc) { if (tc != null && parser != null && tc.Count > 3 && Common.Utility.IsEqual(tc.First.Text, Field.KEY_IF)) { if (tc[1].TokenKind == TokenKind.LeftParentheses && tc.Last.TokenKind == TokenKind.RightParentheses) { IfTag tag = new IfTag(); ElseifTag t = new ElseifTag(); TokenCollection coll = new TokenCollection(); coll.Add(tc, 2, tc.Count - 2); t.Test = parser.Read(coll); t.FirstToken = coll.First; //t.LastToken = coll.Last; tag.AddChild(t); while (parser.MoveNext()) { if (parser.Current is EndTag) { tag.AddChild(parser.Current); return(tag); } else if (parser.Current is ElseifTag || parser.Current is ElseTag) { tag.AddChild(parser.Current); } else { tag.Children[tag.Children.Count - 1].AddChild(parser.Current); } } throw new Exception.ParseException(String.Concat("if is not properly closed by a end tag:", tc), tc.First.BeginLine, tc.First.BeginColumn); } else { throw new Exception.ParseException(String.Concat("syntax error near if:", tc), tc.First.BeginLine, tc.First.BeginColumn); } } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public Tag Parse(TemplateParser parser, TokenCollection tc) { if (Common.ParserHelpers.IsEqual(tc.First.Text, Field.KEY_LOAD)) { if (tc.Count > 2 && (tc[1].TokenKind == TokenKind.LeftParentheses) && tc.Last.TokenKind == TokenKind.RightParentheses) { LoadTag tag = new LoadTag(); tag.Path = parser.Read(new TokenCollection(tc, 2, tc.Count - 2)); return(tag); } } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public Tag Parse(TemplateParser parser, TokenCollection tc) { if (Common.Utility.IsEqual(tc.First.Text, Field.KEY_INCLUDE)) { if (tc != null && parser != null && tc.Count > 2 && (tc[1].TokenKind == TokenKind.LeftParentheses) && tc.Last.TokenKind == TokenKind.RightParentheses) { IncludeTag tag = new IncludeTag(); tag.Path = parser.Read(new TokenCollection(tc, 2, tc.Count - 2)); return(tag); } } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public Tag Parse(TemplateParser parser, TokenCollection tc) { if (tc.Count > 3 && Common.ParserHelpers.IsEqual(Field.KEY_FOR, tc.First.Text)) { if (tc[1].TokenKind == TokenKind.LeftParentheses && tc.Last.TokenKind == TokenKind.RightParentheses) { Int32 pos = 0, start = 2, end; List <Tag> ts = new List <Tag>(3); ForTag tag = new ForTag(); for (Int32 i = 2; i < tc.Count - 1; i++) { end = i; if (tc[i].TokenKind == TokenKind.Punctuation && tc[i].Text == ";") { if (pos == 0) { TokenCollection coll = new TokenCollection(); coll.Add(tc, start, end - 1); if (coll.Count > 0) { ts.Add(parser.Read(coll)); } else { ts.Add(null); } start = i + 1; continue; } } if (tc[i].TokenKind == TokenKind.LeftParentheses) { pos++; } else if (tc[i].TokenKind == TokenKind.RightParentheses) { pos--; } if (i == tc.Count - 2) { TokenCollection coll = new TokenCollection(); coll.Add(tc, start, end); if (coll.Count > 0) { ts.Add(parser.Read(coll)); } else { ts.Add(null); } } } if (ts.Count != 3) { throw new Exception.ParseException(String.Concat("syntax error near for:", tc), tc.First.BeginLine, tc.First.BeginColumn); } tag.Initial = ts[0]; tag.Test = ts[1]; tag.Do = ts[2]; while (parser.MoveNext()) { tag.Children.Add(parser.Current); if (parser.Current is EndTag) { return(tag); } } throw new Exception.ParseException(String.Concat("for is not properly closed by a end tag:", tc), tc.First.BeginLine, tc.First.BeginColumn); } else { throw new Exception.ParseException(String.Concat("syntax error near for:", tc), tc.First.BeginLine, tc.First.BeginColumn); } } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public Tag Parse(TemplateParser parser, TokenCollection tc) { //支持写法:简写格式: //常规格式: if (tc.Count > 5 && Common.ParserHelpers.IsEqual(tc.First.Text, Field.KEY_SET) && tc[1].TokenKind == TokenKind.LeftParentheses && tc[3].Text == "=" && tc.Last.TokenKind == TokenKind.RightParentheses) { SetTag tag = new SetTag(); tag.Name = tc[2].Text; TokenCollection coll = new TokenCollection(); coll.Add(tc, 4, tc.Count - 2); tag.Value = parser.Read(coll); return(tag); } else if (tc.Count == 2 && tc.First.TokenKind == TokenKind.TextData && tc.Last.TokenKind == TokenKind.Operator && (tc.Last.Text == "++" || tc.Last.Text == "--")) { SetTag tag = new SetTag(); tag.Name = tc.First.Text; ExpressionTag c = new ExpressionTag(); c.AddChild(new VariableTag() { FirstToken = tc.First, Name = tc.First.Text }); c.AddChild(new TextTag() { FirstToken = new Token(TokenKind.Operator, tc.Last.Text[0].ToString()) }); c.AddChild(new NumberTag() { Value = 1, FirstToken = new Token(TokenKind.Number, "1") }); tag.Value = c; return(tag); } else if (tc.Count > 2 && tc.First.TokenKind == TokenKind.TextData && tc[1].Text == "=") { SetTag tag = new SetTag(); tag.Name = tc.First.Text; TokenCollection coll = new TokenCollection(); coll.Add(tc, 2, tc.Count - 1); tag.Value = parser.Read(coll); return(tag); } return(null); }
public Tag Parse(TemplateParser parser, TokenCollection tc) { if (tc.Count > 2 && HasOperator(tc)) { Int32 start, end, pos; start = end = pos = 0; #region 去括号 //(8+2) ==》 8+2 //(8+2) * (10-5) ==》(8+2) * (10-5) if (tc.First.TokenKind == TokenKind.LeftParentheses && tc.Last.TokenKind == TokenKind.RightParentheses) { for (Int32 i = 1; i < tc.Count - 1; i++) { switch (tc[i].TokenKind) { case TokenKind.LeftParentheses: pos++; break; case TokenKind.RightParentheses: if (pos > 0) { pos--; } break; } } if (pos == 0) { tc = new TokenCollection(tc, 1, tc.Count - 2); } else { pos = 0; } } #endregion ExpressionTag tag = new ExpressionTag(); #region 执行表达式折分 for (Int32 i = 0; i < tc.Count; i++) { end = i; switch (tc[i].TokenKind) { case TokenKind.Operator: if (pos == 0) { if (start != end) { TokenCollection coll = new TokenCollection(); coll.Add(tc, start, end - 1); tag.AddChild(parser.Read(coll)); } tag.AddChild(new TextTag()); tag.Children[tag.Children.Count - 1].FirstToken = tc[i]; start = i + 1; } break; default: if (tc[i].TokenKind == TokenKind.LeftParentheses) { pos++; } else if (tc[i].TokenKind == TokenKind.RightParentheses) { pos--; } if (i == tc.Count - 1) { TokenCollection coll = new TokenCollection(); if (tc[start].TokenKind == TokenKind.RightParentheses) { coll.Add(tc, start + 1, end - 1); } else { coll.Add(tc, start, end); } start = i + 1; if (coll.Count > 0) { tag.AddChild(parser.Read(coll)); } } break; } } #endregion if (tag.Children.Count > 0) { if (tag.Children.Count == 1) { return(tag.Children[0]); } return(tag); } } return(null); }