annotate Implab.ServiceHost/Unity/TypeReferenceParser.cs @ 277:963b17c275be v3

Refactoring Added <array> element to injection parameters Working on registrations of factories
author cin
date Sat, 28 Apr 2018 18:48:09 +0300
parents ff581cff7003
children 6691aff01de1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
1 using System;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
2 using System.Collections.Generic;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
3 using System.Text.RegularExpressions;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
4
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
5 namespace Implab.ServiceHost.Unity {
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
6 internal class TypeReferenceParser {
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
7 enum TokenType {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
8 None,
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
9
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
10 Word,
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
11
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
12 Dot,
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
13
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
14 Comma,
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
15
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
16 OpenList,
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
17
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
18 CloseList,
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
19
277
963b17c275be Refactoring
cin
parents: 269
diff changeset
20 OpenArray,
963b17c275be Refactoring
cin
parents: 269
diff changeset
21
963b17c275be Refactoring
cin
parents: 269
diff changeset
22 CloseArray,
963b17c275be Refactoring
cin
parents: 269
diff changeset
23
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
24 Eof
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
25 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
26
277
963b17c275be Refactoring
cin
parents: 269
diff changeset
27 readonly Regex _tokens = new Regex(@"([\w\+]+)|\s*([\.{},\[\]])\s*");
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
28
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
29 TokenType m_token;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
30
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
31 string m_tokenValue;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
32
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
33 int m_pos;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
34
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
35 int m_tokenPos;
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
36
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
37 readonly string m_text;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
38
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
39 TokenType Token { get { return m_token; } }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
40
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
41 string TokenValue { get { return m_tokenValue; } }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
42
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
43 int TokenPos { get { return m_tokenPos; } }
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
44
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
45 public TypeReferenceParser(string text) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
46 Safe.ArgumentNotEmpty(text, nameof(text));
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
47 m_text = text;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
48 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
49
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
50 bool ReadToken() {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
51 if (m_pos >= m_text.Length) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
52 m_token = TokenType.Eof;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
53 m_tokenValue = null;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
54 return false;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
55 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
56
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
57 var m = _tokens.Match(m_text, m_pos);
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
58
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
59 if (m.Success) {
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
60 m_tokenPos = m_pos;
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
61 m_pos += m.Length;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
62 if (m.Groups[1].Success) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
63 m_token = TokenType.Word;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
64 m_tokenValue = m.Groups[1].Value;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
65 } else if (m.Groups[2].Success) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
66 m_tokenValue = null;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
67 switch (m.Groups[2].Value) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
68 case "{":
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
69 m_token = TokenType.OpenList;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
70 break;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
71 case "}":
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
72 m_token = TokenType.CloseList;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
73 break;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
74 case ".":
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
75 m_token = TokenType.Dot;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
76 break;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
77 case ",":
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
78 m_token = TokenType.Comma;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
79 break;
277
963b17c275be Refactoring
cin
parents: 269
diff changeset
80 case "[":
963b17c275be Refactoring
cin
parents: 269
diff changeset
81 m_token = TokenType.OpenArray;
963b17c275be Refactoring
cin
parents: 269
diff changeset
82 break;
963b17c275be Refactoring
cin
parents: 269
diff changeset
83 case "]":
963b17c275be Refactoring
cin
parents: 269
diff changeset
84 m_token = TokenType.CloseArray;
963b17c275be Refactoring
cin
parents: 269
diff changeset
85 break;
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
86 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
87 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
88 return true;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
89 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
90 throw new FormatException($"Failed to parse '{m_text}' at pos {m_pos}");
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
91 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
92
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
93 public TypeReference Parse() {
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
94 var result = ReadTypeReference();
277
963b17c275be Refactoring
cin
parents: 269
diff changeset
95 if (Token != TokenType.Eof)
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
96 ThrowUnexpectedToken();
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
97 return result;
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
98 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
99
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
100 string[] ReadTypeName() {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
101 var parts = new List<string>();
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
102
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
103 string current = null;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
104 bool stop = false;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
105 while ((!stop) && ReadToken()) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
106 switch (Token) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
107 case TokenType.Word:
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
108 if (current != null)
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
109 ThrowUnexpectedToken();
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
110 current = TokenValue;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
111 break;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
112 case TokenType.Dot:
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
113 if (current == null)
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
114 ThrowUnexpectedToken();
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
115 parts.Add(current);
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
116 current = null;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
117 break;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
118 default:
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
119 stop = true;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
120 break;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
121 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
122 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
123 if (current != null)
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
124 parts.Add(current);
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
125
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
126 if (parts.Count == 0)
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
127 return null;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
128
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
129 return parts.ToArray();
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
130 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
131
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
132 TypeReference ReadTypeReference() {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
133
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
134 var parts = ReadTypeName();
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
135 if (parts == null)
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
136 return null;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
137
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
138 var typeReference = new TypeReference {
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
139 Namespace = string.Join(".", parts, 0, parts.Length - 1),
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
140 TypeName = parts[parts.Length - 1]
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
141 };
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
142
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
143 switch (Token) {
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
144 case TokenType.OpenList:
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
145 typeReference.GenericParameters = ReadTypeReferenceList();
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
146 if (Token != TokenType.CloseList)
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
147 ThrowUnexpectedToken();
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
148 ReadToken();
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
149 break;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
150 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
151
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
152 return typeReference;
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
153 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
154
277
963b17c275be Refactoring
cin
parents: 269
diff changeset
155 int CountDimentions() {
963b17c275be Refactoring
cin
parents: 269
diff changeset
156 return 0;
963b17c275be Refactoring
cin
parents: 269
diff changeset
157 }
963b17c275be Refactoring
cin
parents: 269
diff changeset
158
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
159 TypeReference[] ReadTypeReferenceList() {
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
160 var list = new List<TypeReference>();
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
161
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
162 do {
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
163 var typeReference = ReadTypeReference();
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
164 list.Add(typeReference);
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
165 } while (Token == TokenType.Comma);
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
166
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
167 return list.ToArray();
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
168 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
169
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
170 void ThrowUnexpectedToken() {
268
0be8a6ae8307 Implemented typereference parser
cin
parents: 267
diff changeset
171 throw new FormatException($"Unexpected '{Token}' at pos {TokenPos}: -->{m_text.Substring(TokenPos, Math.Min(m_text.Length - TokenPos, 10))}");
267
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
172 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
173
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
174 }
6b3e5c48131b Working on Unity xml configuration
cin
parents:
diff changeset
175 }