##// END OF EJS Templates
Implemented typereference parser
cin -
r268:0be8a6ae8307 v3
parent child
Show More
@@ -0,0 +1,81
1 using System;
2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
4 using Implab.Diagnostics;
5
6 namespace Implab.ServiceHost.Unity {
7 using System.Linq;
8 using System.Text;
9 using static Trace<ConfigurationContext>;
10
11 public class ConfigurationContext {
12 Regex _nsRx = new Regex(@"^\w+(\.\w+)*$", RegexOptions.Compiled);
13 readonly LinkedList<string> m_namespases = new LinkedList<string>();
14
15 LinkedListNode<string> m_insertAt;
16
17 public ConfigurationContext() {
18 m_insertAt = new LinkedListNode<string>(string.Empty);
19 m_namespases.AddFirst(m_insertAt);
20 }
21
22 public void AddNamespace(string ns) {
23 Safe.ArgumentMatch(ns, nameof(ns), _nsRx);
24 if (m_insertAt != null)
25 m_namespases.AddAfter(m_insertAt, ns);
26 else
27 m_namespases.AddFirst(ns);
28 }
29
30 public Type Resolve(TypeReference reference) {
31 Safe.ArgumentNotNull(reference, nameof(reference));
32
33 var args = reference.IsGeneric && !reference.IsOpenGeneric ? reference.GenericParameters?.Select(Resolve).ToArray() : null;
34 var argc = reference.IsGeneric ? reference.GenericParameters.Length : 0;
35
36 foreach (var ns in m_namespases) {
37 var typeName = FormatName(new [] { ns, reference.Namespace, reference.TypeName}, argc, args, reference.IsArray);
38
39 var resolved = Type.GetType(typeName, false);
40 if (resolved != null) {
41 Log("Probe succeed {0} in '{1}': {2} -> {3}", reference, ns, typeName, resolved.FullName);
42 return resolved;
43 } else {
44 Log("Probe failed {0} in '{1}': {2}", reference, ns, typeName);
45 }
46 }
47
48 throw new Exception($"Failed to resolve: {reference}");
49 }
50
51 string FormatName(string[] parts, int argc, Type[] args, bool isArray) {
52 var builder = new StringBuilder();
53
54 builder.Append(String.Join(".", parts.Where(x => !string.IsNullOrEmpty(x))));
55 if (argc > 0) {
56 builder.Append('`');
57 builder.Append(argc);
58 }
59
60 if (args!= null && args.Length > 0) {
61 builder.Append('[');
62 builder.Append(string.Join(",", args.Select(x => x.FullName)));
63 builder.Append(']');
64 }
65
66 if(isArray)
67 builder.Append("[]");
68
69 return builder.ToString();
70 }
71
72 public Type Resolve(string typeReference) {
73 return Resolve(TypeReference.Parse(typeReference));
74 }
75
76 public void Register(ServiceElement descriptor) {
77
78 }
79
80 }
81 } No newline at end of file
@@ -1,4 +1,6
1 using System;
1 using System;
2 using System.Diagnostics;
3 using Implab.Diagnostics;
2 using Implab.ServiceHost.Unity;
4 using Implab.ServiceHost.Unity;
3 using Implab.Xml;
5 using Implab.Xml;
4 using Unity;
6 using Unity;
@@ -30,9 +32,20 namespace Implab.Playground {
30 static void Main(string[] args) {
32 static void Main(string[] args) {
31 var container = new UnityContainer();
33 var container = new UnityContainer();
32
34
33 var containerConfig = SerializationHelpers.DeserializeFromFile<ContainerElement>("data/sample.xml");
35 var listener = new SimpleTraceListener(Console.Out);
36 Trace<ConfigurationContext>.TraceSource.Switch.Level = SourceLevels.All;
37 Trace<ConfigurationContext>.TraceSource.Listeners.Add(listener);
38
39 var c = new Container<int>();
34
40
35 Console.WriteLine($"container: {containerConfig.Registrations.Count}");
41 var cts = new ConfigurationContext();
42 cts.AddNamespace("System");
43 cts.AddNamespace("System.Collections.Generic");
44 cts.AddNamespace("Implab.Playground");
45
46 Console.WriteLine(c.GetType().FullName);
47
48 cts.Resolve("Container{Int32}");
36 }
49 }
37
50
38
51
@@ -1,3 +1,7
1 using System;
2 using System.Linq;
3 using System.Text;
4
1 namespace Implab.ServiceHost.Unity {
5 namespace Implab.ServiceHost.Unity {
2 public class TypeReference {
6 public class TypeReference {
3 public string TypeName { get; set; }
7 public string TypeName { get; set; }
@@ -5,5 +9,50 namespace Implab.ServiceHost.Unity {
5 public string Namespace { get; set; }
9 public string Namespace { get; set; }
6
10
7 public TypeReference[] GenericParameters { get; set; }
11 public TypeReference[] GenericParameters { get; set; }
12
13 public bool IsArray { get; set; }
14
15 public bool IsOpenGeneric {
16 get {
17 return GenericParameters!=null && GenericParameters.Contains(null);
18 }
19 }
20
21 public bool IsGeneric {
22 get {
23 return GenericParameters != null && GenericParameters.Length > 0;
24 }
25 }
26
27 public override string ToString() {
28 var builder = new StringBuilder();
29
30 if (!string.IsNullOrEmpty(Namespace)) {
31 builder.Append(Namespace);
32 builder.Append('.');
33 }
34
35 if (!string.IsNullOrEmpty(TypeName)) {
36 builder.Append(TypeName);
37 } else {
38 builder.Append("__unnamed__");
39 }
40
41 if (GenericParameters != null && GenericParameters.Length > 0) {
42 builder.Append('{');
43 for(var i = 0; i < GenericParameters.Length; i++) {
44 if (i > 0)
45 builder.Append(',');
46 builder.Append(GenericParameters[i]);
47 }
48 builder.Append('}');
49 }
50
51 return builder.ToString();
52 }
53 public static TypeReference Parse(string text) {
54 var parser = new TypeReferenceParser(text);
55 return parser.Parse();
56 }
8 }
57 }
9 } No newline at end of file
58 }
@@ -3,7 +3,7 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
3 using System.Text.RegularExpressions;
4
4
5 namespace Implab.ServiceHost.Unity {
5 namespace Implab.ServiceHost.Unity {
6 public class TypeReferenceParser {
6 internal class TypeReferenceParser {
7 enum TokenType {
7 enum TokenType {
8 None,
8 None,
9
9
@@ -20,7 +20,7 namespace Implab.ServiceHost.Unity {
20 Eof
20 Eof
21 }
21 }
22
22
23 readonly Regex _tokens = new Regex(@"(\w+)|\s*([\.{},])\s*");
23 readonly Regex _tokens = new Regex(@"(\w+)|\s*([\.{},\+])\s*");
24
24
25 TokenType m_token;
25 TokenType m_token;
26
26
@@ -28,12 +28,16 namespace Implab.ServiceHost.Unity {
28
28
29 int m_pos;
29 int m_pos;
30
30
31 int m_tokenPos;
32
31 readonly string m_text;
33 readonly string m_text;
32
34
33 TokenType Token { get { return m_token; } }
35 TokenType Token { get { return m_token; } }
34
36
35 string TokenValue { get { return m_tokenValue; } }
37 string TokenValue { get { return m_tokenValue; } }
36
38
39 int TokenPos { get { return m_tokenPos; } }
40
37 public TypeReferenceParser(string text) {
41 public TypeReferenceParser(string text) {
38 Safe.ArgumentNotEmpty(text, nameof(text));
42 Safe.ArgumentNotEmpty(text, nameof(text));
39 m_text = text;
43 m_text = text;
@@ -49,6 +53,7 namespace Implab.ServiceHost.Unity {
49 var m = _tokens.Match(m_text, m_pos);
53 var m = _tokens.Match(m_text, m_pos);
50
54
51 if (m.Success) {
55 if (m.Success) {
56 m_tokenPos = m_pos;
52 m_pos += m.Length;
57 m_pos += m.Length;
53 if (m.Groups[1].Success) {
58 if (m.Groups[1].Success) {
54 m_token = TokenType.Word;
59 m_token = TokenType.Word;
@@ -63,6 +68,7 namespace Implab.ServiceHost.Unity {
63 m_token = TokenType.CloseList;
68 m_token = TokenType.CloseList;
64 break;
69 break;
65 case ".":
70 case ".":
71 case "+":
66 m_token = TokenType.Dot;
72 m_token = TokenType.Dot;
67 break;
73 break;
68 case ",":
74 case ",":
@@ -75,8 +81,11 namespace Implab.ServiceHost.Unity {
75 throw new FormatException($"Failed to parse '{m_text}' at pos {m_pos}");
81 throw new FormatException($"Failed to parse '{m_text}' at pos {m_pos}");
76 }
82 }
77
83
78 public TypeRerefence Pase() {
84 public TypeReference Parse() {
79
85 var result = ReadTypeReference();
86 if (ReadToken())
87 ThrowUnexpectedToken();
88 return result;
80 }
89 }
81
90
82 string[] ReadTypeName() {
91 string[] ReadTypeName() {
@@ -118,20 +127,16 namespace Implab.ServiceHost.Unity {
118 return null;
127 return null;
119
128
120 var typeReference = new TypeReference {
129 var typeReference = new TypeReference {
121 Namespace = string.Join(",", parts, 0, parts.Length - 1),
130 Namespace = string.Join(".", parts, 0, parts.Length - 1),
122 TypeName = parts[parts.Length - 1]
131 TypeName = parts[parts.Length - 1]
123 };
132 };
124
133
125 switch (Token) {
134 switch (Token) {
126 case TokenType.Eof:
127 break;
128 case TokenType.OpenList:
135 case TokenType.OpenList:
129 typeReference.GenericParameters = ReadTypeReferenceList();
136 typeReference.GenericParameters = ReadTypeReferenceList();
130 if(Token != TokenType.CloseList)
137 if (Token != TokenType.CloseList)
131 ThrowUnexpectedToken();
138 ThrowUnexpectedToken();
132 break;
139 ReadToken();
133 default:
134 ThrowUnexpectedToken();
135 break;
140 break;
136 }
141 }
137
142
@@ -139,18 +144,19 namespace Implab.ServiceHost.Unity {
139 }
144 }
140
145
141 TypeReference[] ReadTypeReferenceList() {
146 TypeReference[] ReadTypeReferenceList() {
142 throw new NotImplementedException();
147 var list = new List<TypeReference>();
143 }
144
148
145 void ReadDot() {
149 do {
146 if (!ReadToken() || Token != TokenType.Dot)
150 var typeReference = ReadTypeReference();
147 ThrowUnexpectedToken();
151 list.Add(typeReference);
152 } while (Token == TokenType.Comma);
153
154 return list.ToArray();
148 }
155 }
149
156
150 void ThrowUnexpectedToken() {
157 void ThrowUnexpectedToken() {
151 throw new FormatException($"Unexpected '{Token}' at {m_pos}");
158 throw new FormatException($"Unexpected '{Token}' at pos {TokenPos}: -->{m_text.Substring(TokenPos, Math.Min(m_text.Length - TokenPos, 10))}");
152 }
159 }
153
160
154
155 }
161 }
156 } No newline at end of file
162 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now