private Dictionary <string, PropertyTemplate> parseParameter() { var dic = new Dictionary <string, PropertyTemplate>(); while (true) { var key = parseKey(); throwIfHasNoNextOrNext(); if (_reader.Current() == '@' && _reader.HasNext() && _reader.Forwards() == '{') { _reader.Next(); dic.Add(key, parse()); throwIfHasNoNextOrNext(); if (_reader.Current() == ']') { return(dic); } else { throwIfNotMatch(',', "require char ','"); } } else { _reader.Back(); var parameterNode = new PropertyTemplate(); parameterNode.Value = parseSegment(',', ']'); dic.Add(key, parameterNode); if (_reader.Current() == ']') { return(dic); } else { throwIfNotMatch(',', "require char ','"); } } } }
private string parseVariable() { var builder = new StringBuilder(); while (_reader.HasNext()) { _reader.Next(); if (_reader.Current() == '}' && _reader.HasNext() && _reader.Forwards() == '$') { _reader.Next(); return(builder.ToString()); } else { builder.Append(_reader.Current()); } } throw new ExpressionIncorrectException($"expression varible incompleted!"); }
public PropertySegement[] Parse(string input) { _reader = new CharSequenceReader(input); var ls = new List <PropertySegement>(); while (_reader.HasNext()) { _reader.Next(); if (_reader.Current() == '@' && _reader.HasNext() && _reader.Forwards() == '{') { _reader.Next(); if (_builder.Length != 0) { ls.Add(new PropertySegement() { Value = _builder.ToString() }); _builder.Clear(); } var template = new PropertySegement(); template.Template = parseProperty(); ls.Add(template); } else if (_reader.Current() == '$' && _reader.HasNext() && _reader.Forwards() == '{') { _reader.Next(); if (_builder.Length != 0) { ls.Add(new PropertySegement() { Value = _builder.ToString() }); _builder.Clear(); } var template = new PropertySegement(); template.Value = parseVariable(); template.IsVariable = true; ls.Add(template); } else { _builder.Append(_reader.Current()); } } if (_builder.Length != 0) { ls.Add(new PropertySegement() { Value = _builder.ToString() }); } return(ls.ToArray()); }
public static SqlTemplate Parse(string input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } var reader = new CharSequenceReader(input); bool parsingParameter = false; var template = new SqlTemplate(); template.RawString = input; var ls = new List <SqlTemplateSegment>(); var segmentBuilder = new StringBuilder(); while (reader.HasNext()) { reader.Next(); if (reader.Current() == '@' && !parsingParameter) { if (segmentBuilder.Length != 0) { ls.Add(new SqlTemplateSegment(segmentBuilder.ToString(), false)); segmentBuilder.Clear(); } parsingParameter = true; } else if (parsingParameter && (reader.Current() == ' ' || reader.Current() == '\r' || reader.Current() == '\n' || reader.Current() == '\t')) { bool followIn = ls.Count == 0 ? false : ls[ls.Count - 1].Value.ToLower().Trim().EndsWith(" in") ? true : false; ls.Add(new SqlTemplateSegment(segmentBuilder.ToString(), true, followIn)); segmentBuilder.Clear(); parsingParameter = false; } else { segmentBuilder.Append(reader.Current()); } } if (segmentBuilder.Length != 0) { if (parsingParameter) { bool followIn = ls.Count == 0 ? false : ls[ls.Count - 1].Value.ToLower().Trim().EndsWith(" in") ? true : false; ls.Add(new SqlTemplateSegment(segmentBuilder.ToString(), true, followIn)); } else { ls.Add(new SqlTemplateSegment(segmentBuilder.ToString(), false)); } } template.Segments = ls.ToArray(); return(template); }