/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public ITag Parse(TemplateParser parser, TokenCollection tc) { if (tc != null && parser != null && tc.First.TokenKind == TokenKind.TextData && tc.Count > 2 && tc[1].TokenKind == TokenKind.LeftParentheses && tc.Last.TokenKind == TokenKind.RightParentheses && tc.Split(0, tc.Count, TokenKind.Operator).Length == 1) { FunctaionTag tag = new FunctaionTag(); tag.Name = tc.First.Text; //tag.Func = (BasisTag)parser.Read(tc[0, 1]); TokenCollection[] tcs = tc.Split(2, tc.Count - 1, TokenKind.Comma); for (int i = 0; i < tcs.Length; i++) { if (tcs[i].Count == 1 && tcs[i][0].TokenKind == TokenKind.Comma) { continue; } tag.AddChild(parser.Read(tcs[i])); } return(tag); } return(null); }
/// <summary> /// JSON分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public ITag Parse(TemplateParser parser, TokenCollection tc) { if (tc.Count > 2 && (tc[0].TokenKind == TokenKind.LeftBrace) && tc.Last.TokenKind == TokenKind.RightBrace) { JsonTag tag = new JsonTag(); TokenCollection[] tcs = tc.Split(1, tc.Count - 1, TokenKind.Comma); for (int i = 0; i < tcs.Length; i++) { if (tcs[i].Count == 1 && tcs[i][0].TokenKind == TokenKind.Comma) { continue; } TokenCollection[] keyValuePair = tcs[i].Split(0, tcs[i].Count, TokenKind.Colon); if (keyValuePair.Length != 3) { //不符合规范 return(null); } var key = parser.Read(keyValuePair[0]); var value = parser.Read(keyValuePair[2]); tag.Dict.Add(key, value); } return(tag); } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public ITag Parse(TemplateParser parser, TokenCollection tc) { if (tc != null && parser != null && tc.Count > 2) { List <ITag> tags = new List <ITag>(); TokenCollection[] tcs = tc.Split(0, tc.Count, TokenKind.Dot, TokenKind.Operator); if (tcs.Length == 1) { return(null); } for (int i = 0; i < tcs.Length; i++) { if (tcs[i].Count == 1 && tcs[i][0].TokenKind == TokenKind.Dot) { if (tags.Count == 0 || i == tcs[i].Count - 1 || (tcs[i + 1].Count == 1 && (tcs[i + 1][0].TokenKind == TokenKind.Dot || tcs[i + 1][0].TokenKind == TokenKind.Operator))) { throw new Exception.ParseException(string.Concat("syntax error near .:", tc), tcs[i][0].BeginLine, tcs[i][0].BeginColumn); } if (tags[tags.Count - 1] is ReferenceTag) { tags[tags.Count - 1].AddChild(parser.Read(tcs[i + 1])); } else { ReferenceTag t = new ReferenceTag(); t.AddChild(tags[tags.Count - 1]); t.AddChild(parser.Read(tcs[i + 1])); tags[tags.Count - 1] = t; } i++; } else if (tcs[i].Count == 1 && tcs[i][0].TokenKind == TokenKind.Operator) { tags.Add(new TextTag()); tags[tags.Count - 1].FirstToken = tcs[i][0]; } else if (tcs[i].Count > 0) { if (tcs[i].First.TokenKind == TokenKind.LeftParentheses && tcs[i].Last.TokenKind == TokenKind.RightParentheses) { tcs[i].RemoveAt(0); tcs[i].RemoveAt(tcs[i].Count - 1); } tags.Add(parser.Read(tcs[i])); } } /*****************************************************/ //int start, end, pos; //start = end = pos = 0; //List<Token> data = new List<Token>(); //Queue<TokenCollection> queue = new Queue<TokenCollection>(); //for (int i = 0; i < tc.Count; i++) //{ // end = i; // if (tc[i].TokenKind == TokenKind.LeftParentheses) // { // pos++; // } // else if (tc[i].TokenKind == TokenKind.RightParentheses) // { // if (pos > 0) // { // pos--; // } // else // { // throw new Exception.ParseException(string.Concat("syntax error near ):", tc), data[i].BeginLine, data[i].BeginColumn); // } // } // else if (pos == 0 && (tc[i].TokenKind == TokenKind.Dot || tc[i].TokenKind == TokenKind.Operator)) // { // if (end > start) // { // queue.Enqueue(tc[start, end]); // data.Add(null); // } // start = i + 1; // data.Add(tc[i]); // } // if (i == tc.Count - 1 && end >= start) // { // if (start == 0 && end == i) // { // throw new Exception.ParseException(string.Concat("Unexpected tag:", tc), tc[0].BeginLine, tc[0].BeginColumn); // } // queue.Enqueue(tc[start, end + 1]); // data.Add(null); // start = i + 1; // } //} ////=============================================================== //if (queue.Count == 1 && queue.Peek().Equals(tc)) //{ // return null; //} //for (int i = 0; i < data.Count; i++) //{ // if (data[i] == null) // { // TokenCollection tmpColl = queue.Dequeue(); // if (tmpColl.First.TokenKind == TokenKind.LeftParentheses && tmpColl.Last.TokenKind == TokenKind.RightParentheses) // { // tmpColl.RemoveAt(0); // tmpColl.RemoveAt(tmpColl.Count - 1); // } // tags.Add(parser.Read(tmpColl)); // } // else if (data[i].TokenKind == TokenKind.Dot) // { // if (tags.Count == 0 || i == data.Count - 1 || data[i + 1] != null) // { // throw new Exception.ParseException(string.Concat("syntax error near .:", tc), data[i].BeginLine, data[i].BeginColumn); // } // if (tags[tags.Count - 1] is ReferenceTag) // { // tags[tags.Count - 1].AddChild(parser.Read(queue.Dequeue())); // } // else // { // ReferenceTag t = new ReferenceTag(); // t.AddChild(tags[tags.Count - 1]); // t.AddChild(parser.Read(queue.Dequeue())); // tags[tags.Count - 1] = t; // } // i++; // } // else if (data[i].TokenKind == TokenKind.Operator) // { // tags.Add(new TextTag()); // tags[tags.Count - 1].FirstToken = data[i]; // } //} if (tags.Count == 1) { return(tags[0]); } if (tags.Count > 1) { ExpressionTag t = new ExpressionTag(); for (int i = 0; i < tags.Count; i++) { t.AddChild(tags[i]); } tags.Clear(); return(t); } } return(null); }
/// <summary> /// 分析标签 /// </summary> /// <param name="parser">TemplateParser</param> /// <param name="tc">Token集合</param> /// <returns></returns> public ITag Parse(TemplateParser parser, TokenCollection tc) { if (tc != null && parser != null && tc.Count > 2) { List <ITag> tags = new List <ITag>(); TokenCollection[] tcs = tc.Split(0, tc.Count, TokenKind.Dot, TokenKind.Operator); bool isLogical = false; if (tcs.Length == 1) { return(null); } for (int i = 0; i < tcs.Length; i++) { if (tcs[i].Count == 1 && tcs[i][0].TokenKind == TokenKind.Dot) { if (tags.Count == 0 || i == tcs[i].Count - 1 || (tcs[i + 1].Count == 1 && (tcs[i + 1][0].TokenKind == TokenKind.Dot || tcs[i + 1][0].TokenKind == TokenKind.Operator))) { throw new Exception.ParseException(string.Concat("syntax error near .:", tc), tcs[i][0].BeginLine, tcs[i][0].BeginColumn); } if (tags[tags.Count - 1] is ReferenceTag) { tags[tags.Count - 1].AddChild(parser.Read(tcs[i + 1])); } else { ReferenceTag t = new ReferenceTag(); t.AddChild(tags[tags.Count - 1]); t.AddChild(parser.Read(tcs[i + 1])); tags[tags.Count - 1] = t; } i++; } else if (tcs[i].Count == 1 && tcs[i][0].TokenKind == TokenKind.Operator) { tags.Add(new OperatorTag(tcs[i][0])); Operator op = Dynamic.OperatorConvert.Parse(tcs[i][0].Text); switch (op) { case Operator.Or: case Operator.And: case Operator.LessThan: case Operator.LessThanOrEqual: case Operator.Equal: case Operator.GreaterThan: case Operator.GreaterThanOrEqual: case Operator.NotEqual: isLogical = true; break; } } else if (tcs[i][0].TokenKind == TokenKind.LeftBracket) { if (tags[tags.Count - 1] is ReferenceTag) { tags[tags.Count - 1].AddChild(parser.Read(tcs[i])); } else { if (tags.Count == 0) { throw new Exception.ParseException(string.Concat("syntax error near [:", tc), tcs[i][0].BeginLine, tcs[i][0].BeginColumn); } ReferenceTag t = new ReferenceTag(); t.AddChild(tags[tags.Count - 1]); t.AddChild(parser.Read(tcs[i])); tags[tags.Count - 1] = t; } } else if (tcs[i].Count > 0) { if (tcs[i].First.TokenKind == TokenKind.LeftParentheses && tcs[i].Last.TokenKind == TokenKind.RightParentheses) { tcs[i].RemoveAt(0); tcs[i].RemoveAt(tcs[i].Count - 1); } tags.Add(parser.Read(tcs[i])); } } if (tags.Count == 1) { return(tags[0]); } if (tags.Count > 1) { var list = new List <List <ITag> >(); ITag t; if (isLogical) { t = new LogicTag(); var arr = Analysis(tags, new List <Operator>(new Operator[] { Operator.And, Operator.Or })); if (arr.Length == 1) { return(arr[0]); } AddRange(t, arr); } else { t = new ArithmeticTag(); for (int i = 0; i < tags.Count; i++) { t.AddChild(tags[i]); } } tags.Clear(); return(t); } } return(null); }