/// <inheritdoc /> public void Regiser(IHost host) { host.RegisterParseFunc((parser, tc) => { if (tc != null && parser != null && tc.Count > 2) { var tags = new List <ITag>(); var 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 ParseException($"syntax error near '.': {tc.ToString()}", 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 { var 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 ParseException($"syntax error near '[': {tc.ToString()}", tcs[i][0].BeginLine, tcs[i][0].BeginColumn); } var 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); }, -1); var options = host.HostEnvironment.Options; if (options.Mode == EngineMode.Compiled) { RegiserCompile(host); } else { RegiserExcutor(host); } }
/// <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); }
/// <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) { return(null); } //支持写法:简写格式: //常规格式: if (tc.Count > 5 && Utility.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; tag.Value = parser.Read(tc[4, -1]); return(tag); } 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; var c = new ArithmeticTag(); c.AddChild(new VariableTag() { FirstToken = tc.First, Name = tc.First.Text }); c.AddChild(new OperatorTag(new Token(TokenKind.Operator, tc.Last.Text[0].ToString()))); //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); } if (tc.Count > 2 && tc.First.TokenKind == TokenKind.TextData && tc[1].Text == "=") { SetTag tag = new SetTag(); tag.Name = tc.First.Text; tag.Value = parser.Read(tc[2, tc.Count]); return(tag); } return(null); }
/// <inheritdoc /> public override Func <TemplateParser, TokenCollection, ITag> BuildParseMethod() { return((parser, tc) => { if (tc == null || parser == null) { return null; } if (tc.Count > 5 && Utility.IsEqual(tc.First.Text, Const.KEY_SET) && tc[1].TokenKind == TokenKind.LeftParentheses && tc[3].Text == "=" && tc.Last.TokenKind == TokenKind.RightParentheses) { var tag = new SetTag(); tag.Name = tc[2].Text; tag.Value = parser.Read(tc[4, -1]); return tag; } if (tc.Count == 2 && tc.First.TokenKind == TokenKind.TextData && tc.Last.TokenKind == TokenKind.Arithmetic && (tc.Last.Text == "++" || tc.Last.Text == "--")) { var tag = new SetTag(); tag.Name = tc.First.Text; var c = new ArithmeticTag(); c.AddChild(new VariableTag() { FirstToken = tc.First, Name = tc.First.Text }); c.AddChild(new OperatorTag(new Token(TokenKind.Arithmetic, tc.Last.Text[0].ToString()))); //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; } if (tc.Count > 2 && tc.First.TokenKind == TokenKind.TextData && tc[1].Text == "=") { var tag = new SetTag(); tag.Name = tc.First.Text; tag.Value = parser.Read(tc[2, tc.Count]); return tag; } return null; }); }