##// 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,40 +1,53
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;
5 using Unity.Injection;
7 using Unity.Injection;
6
8
7 namespace Implab.Playground {
9 namespace Implab.Playground {
8
10
9 public class Foo {
11 public class Foo {
10 public int IntValue { get; set; }
12 public int IntValue { get; set; }
11
13
12 public string StringValue { get; set; }
14 public string StringValue { get; set; }
13
15
14 }
16 }
15
17
16 public class Container<T> {
18 public class Container<T> {
17 public Container() {
19 public Container() {
18
20
19 }
21 }
20
22
21 public Container(T instance) {
23 public Container(T instance) {
22 Instance = instance;
24 Instance = instance;
23 }
25 }
24
26
25 public T Instance { get; set; }
27 public T Instance { get; set; }
26 }
28 }
27
29
28 public class Program {
30 public class Program {
29
31
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
39 }
52 }
40 }
53 }
@@ -1,9 +1,58
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; }
4
8
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 }
@@ -1,156 +1,162
1 using System;
1 using System;
2 using System.Collections.Generic;
2 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
10 Word,
10 Word,
11
11
12 Dot,
12 Dot,
13
13
14 Comma,
14 Comma,
15
15
16 OpenList,
16 OpenList,
17
17
18 CloseList,
18 CloseList,
19
19
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
27 string m_tokenValue;
27 string m_tokenValue;
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;
40 }
44 }
41
45
42 bool ReadToken() {
46 bool ReadToken() {
43 if (m_pos >= m_text.Length) {
47 if (m_pos >= m_text.Length) {
44 m_token = TokenType.Eof;
48 m_token = TokenType.Eof;
45 m_tokenValue = null;
49 m_tokenValue = null;
46 return false;
50 return false;
47 }
51 }
48
52
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;
55 m_tokenValue = m.Groups[1].Value;
60 m_tokenValue = m.Groups[1].Value;
56 } else if (m.Groups[2].Success) {
61 } else if (m.Groups[2].Success) {
57 m_tokenValue = null;
62 m_tokenValue = null;
58 switch (m.Groups[2].Value) {
63 switch (m.Groups[2].Value) {
59 case "{":
64 case "{":
60 m_token = TokenType.OpenList;
65 m_token = TokenType.OpenList;
61 break;
66 break;
62 case "}":
67 case "}":
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 ",":
69 m_token = TokenType.Comma;
75 m_token = TokenType.Comma;
70 break;
76 break;
71 }
77 }
72 }
78 }
73 return true;
79 return true;
74 }
80 }
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() {
83 var parts = new List<string>();
92 var parts = new List<string>();
84
93
85 string current = null;
94 string current = null;
86 bool stop = false;
95 bool stop = false;
87 while ((!stop) && ReadToken()) {
96 while ((!stop) && ReadToken()) {
88 switch (Token) {
97 switch (Token) {
89 case TokenType.Word:
98 case TokenType.Word:
90 if (current != null)
99 if (current != null)
91 ThrowUnexpectedToken();
100 ThrowUnexpectedToken();
92 current = TokenValue;
101 current = TokenValue;
93 break;
102 break;
94 case TokenType.Dot:
103 case TokenType.Dot:
95 if (current == null)
104 if (current == null)
96 ThrowUnexpectedToken();
105 ThrowUnexpectedToken();
97 parts.Add(current);
106 parts.Add(current);
98 current = null;
107 current = null;
99 break;
108 break;
100 default:
109 default:
101 stop = true;
110 stop = true;
102 break;
111 break;
103 }
112 }
104 }
113 }
105 if (current != null)
114 if (current != null)
106 parts.Add(current);
115 parts.Add(current);
107
116
108 if (parts.Count == 0)
117 if (parts.Count == 0)
109 return null;
118 return null;
110
119
111 return parts.ToArray();
120 return parts.ToArray();
112 }
121 }
113
122
114 TypeReference ReadTypeReference() {
123 TypeReference ReadTypeReference() {
115
124
116 var parts = ReadTypeName();
125 var parts = ReadTypeName();
117 if (parts == null)
126 if (parts == null)
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
138 return typeReference;
143 return typeReference;
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