Mercurial > pub > ImplabNet
changeset 268:0be8a6ae8307 v3
Implemented typereference parser
author | cin |
---|---|
date | Sun, 22 Apr 2018 15:29:10 +0300 |
parents | 6b3e5c48131b |
children | ff581cff7003 |
files | Implab.Playground/Program.cs Implab.ServiceHost/Unity/ConfigurationContext.cs Implab.ServiceHost/Unity/TypeReference.cs Implab.ServiceHost/Unity/TypeReferenceParser.cs |
diffstat | 4 files changed, 171 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/Implab.Playground/Program.cs Fri Apr 20 19:05:12 2018 +0300 +++ b/Implab.Playground/Program.cs Sun Apr 22 15:29:10 2018 +0300 @@ -1,4 +1,6 @@ using System; +using System.Diagnostics; +using Implab.Diagnostics; using Implab.ServiceHost.Unity; using Implab.Xml; using Unity; @@ -30,9 +32,20 @@ static void Main(string[] args) { var container = new UnityContainer(); - var containerConfig = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml"); + var listener = new SimpleTraceListener(Console.Out); + Trace<ConfigurationContext>.TraceSource.Switch.Level = SourceLevels.All; + Trace<ConfigurationContext>.TraceSource.Listeners.Add(listener); + + var c = new Container<int>(); - Console.WriteLine($"container: {containerConfig.Registrations.Count}"); + var cts = new ConfigurationContext(); + cts.AddNamespace("System"); + cts.AddNamespace("System.Collections.Generic"); + cts.AddNamespace("Implab.Playground"); + + Console.WriteLine(c.GetType().FullName); + + cts.Resolve("Container{Int32}"); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab.ServiceHost/Unity/ConfigurationContext.cs Sun Apr 22 15:29:10 2018 +0300 @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using Implab.Diagnostics; + +namespace Implab.ServiceHost.Unity { + using System.Linq; + using System.Text; + using static Trace<ConfigurationContext>; + + public class ConfigurationContext { + Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled); + readonly LinkedList<string> m_namespases = new LinkedList<string>(); + + LinkedListNode<string> m_insertAt; + + public ConfigurationContext() { + m_insertAt = new LinkedListNode<string>(string.Empty); + m_namespases.AddFirst(m_insertAt); + } + + public void AddNamespace(string ns) { + Safe.ArgumentMatch(ns, nameof(ns), _nsRx); + if (m_insertAt != null) + m_namespases.AddAfter(m_insertAt, ns); + else + m_namespases.AddFirst(ns); + } + + public Type Resolve(TypeReference reference) { + Safe.ArgumentNotNull(reference, nameof(reference)); + + var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null; + var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0; + + foreach (var ns in m_namespases) { + var typeName = FormatName(new [] { ns, reference.Namespace, reference.TypeName}, argc, args, reference.IsArray); + + var resolved = Type.GetType(typeName, false); + if (resolved != null) { + Log("Probe succeed {0} in '{1}': {2} -> {3}", reference, ns, typeName, resolved.FullName); + return resolved; + } else { + Log("Probe failed {0} in '{1}': {2}", reference, ns, typeName); + } + } + + throw new Exception($"Failed to resolve: {reference}"); + } + + string FormatName(string[] parts, int argc, Type[] args, bool isArray) { + var builder = new StringBuilder(); + + builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x)))); + if (argc > 0) { + builder.Append('`'); + builder.Append(argc); + } + + if (args!= null && args.Length > 0) { + builder.Append('['); + builder.Append(string.Join(",", args.Select(x => x.FullName))); + builder.Append(']'); + } + + if(isArray) + builder.Append("[]"); + + return builder.ToString(); + } + + public Type Resolve(string typeReference) { + return Resolve(TypeReference.Parse(typeReference)); + } + + public void Register(ServiceElement descriptor) { + + } + + } +} \ No newline at end of file
--- a/Implab.ServiceHost/Unity/TypeReference.cs Fri Apr 20 19:05:12 2018 +0300 +++ b/Implab.ServiceHost/Unity/TypeReference.cs Sun Apr 22 15:29:10 2018 +0300 @@ -1,3 +1,7 @@ +using System; +using System.Linq; +using System.Text; + namespace Implab.ServiceHost.Unity { public class TypeReference { public string TypeName { get; set; } @@ -5,5 +9,50 @@ public string Namespace { get; set; } public TypeReference[] GenericParameters { get; set; } + + public bool IsArray { get; set; } + + public bool IsOpenGeneric { + get { + return GenericParameters!=null && GenericParameters.Contains(null); + } + } + + public bool IsGeneric { + get { + return GenericParameters != null && GenericParameters.Length > 0; + } + } + + public override string ToString() { + var builder = new StringBuilder(); + + if (!string.IsNullOrEmpty(Namespace)) { + builder.Append(Namespace); + builder.Append('.'); + } + + if (!string.IsNullOrEmpty(TypeName)) { + builder.Append(TypeName); + } else { + builder.Append("__unnamed__"); + } + + if (GenericParameters != null && GenericParameters.Length > 0) { + builder.Append('{'); + for(var i = 0; i < GenericParameters.Length; i++) { + if (i > 0) + builder.Append(','); + builder.Append(GenericParameters[i]); + } + builder.Append('}'); + } + + return builder.ToString(); + } + public static TypeReference Parse(string text) { + var parser = new TypeReferenceParser(text); + return parser.Parse(); + } } } \ No newline at end of file
--- a/Implab.ServiceHost/Unity/TypeReferenceParser.cs Fri Apr 20 19:05:12 2018 +0300 +++ b/Implab.ServiceHost/Unity/TypeReferenceParser.cs Sun Apr 22 15:29:10 2018 +0300 @@ -3,7 +3,7 @@ using System.Text.RegularExpressions; namespace Implab.ServiceHost.Unity { - public class TypeReferenceParser { + internal class TypeReferenceParser { enum TokenType { None, @@ -20,7 +20,7 @@ Eof } - readonly Regex _tokens = new Regex(@"(\w+)|\s*([\.{},])\s*"); + readonly Regex _tokens = new Regex(@"(\w+)|\s*([\.{},\+])\s*"); TokenType m_token; @@ -28,12 +28,16 @@ int m_pos; + int m_tokenPos; + readonly string m_text; TokenType Token { get { return m_token; } } string TokenValue { get { return m_tokenValue; } } + int TokenPos { get { return m_tokenPos; } } + public TypeReferenceParser(string text) { Safe.ArgumentNotEmpty(text, nameof(text)); m_text = text; @@ -49,6 +53,7 @@ var m = _tokens.Match(m_text, m_pos); if (m.Success) { + m_tokenPos = m_pos; m_pos += m.Length; if (m.Groups[1].Success) { m_token = TokenType.Word; @@ -63,6 +68,7 @@ m_token = TokenType.CloseList; break; case ".": + case "+": m_token = TokenType.Dot; break; case ",": @@ -75,8 +81,11 @@ throw new FormatException($"Failed to parse '{m_text}' at pos {m_pos}"); } - public TypeRerefence Pase() { - + public TypeReference Parse() { + var result = ReadTypeReference(); + if (ReadToken()) + ThrowUnexpectedToken(); + return result; } string[] ReadTypeName() { @@ -118,20 +127,16 @@ return null; var typeReference = new TypeReference { - Namespace = string.Join(",", parts, 0, parts.Length - 1), - TypeName = parts[parts.Length - 1] - }; + Namespace = string.Join(".", parts, 0, parts.Length - 1), + TypeName = parts[parts.Length - 1] + }; switch (Token) { - case TokenType.Eof: - break; case TokenType.OpenList: typeReference.GenericParameters = ReadTypeReferenceList(); - if(Token != TokenType.CloseList) + if (Token != TokenType.CloseList) ThrowUnexpectedToken(); - break; - default: - ThrowUnexpectedToken(); + ReadToken(); break; } @@ -139,18 +144,19 @@ } TypeReference[] ReadTypeReferenceList() { - throw new NotImplementedException(); - } + var list = new List<TypeReference>(); - void ReadDot() { - if (!ReadToken() || Token != TokenType.Dot) - ThrowUnexpectedToken(); + do { + var typeReference = ReadTypeReference(); + list.Add(typeReference); + } while (Token == TokenType.Comma); + + return list.ToArray(); } void ThrowUnexpectedToken() { - throw new FormatException($"Unexpected '{Token}' at {m_pos}"); + throw new FormatException($"Unexpected '{Token}' at pos {TokenPos}: -->{m_text.Substring(TokenPos, Math.Min(m_text.Length - TokenPos, 10))}"); } - } } \ No newline at end of file