##// END OF EJS Templates
refactoring
refactoring

File last commit:

r269:ff581cff7003 v3
r275:6fefd5811b9b v3
Show More
TypeReferenceParser.cs
160 lines | 4.5 KiB | text/x-csharp | CSharpLexer
cin
Working on Unity xml configuration
r267 using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Implab.ServiceHost.Unity {
cin
Implemented typereference parser
r268 internal class TypeReferenceParser {
cin
Working on Unity xml configuration
r267 enum TokenType {
None,
Word,
Dot,
Comma,
OpenList,
CloseList,
Eof
}
cin
Working on Unity container xml configuration
r269 readonly Regex _tokens = new Regex(@"([\w\+]+)|\s*([\.{},])\s*");
cin
Working on Unity xml configuration
r267
TokenType m_token;
string m_tokenValue;
int m_pos;
cin
Implemented typereference parser
r268 int m_tokenPos;
cin
Working on Unity xml configuration
r267 readonly string m_text;
TokenType Token { get { return m_token; } }
string TokenValue { get { return m_tokenValue; } }
cin
Implemented typereference parser
r268 int TokenPos { get { return m_tokenPos; } }
cin
Working on Unity xml configuration
r267 public TypeReferenceParser(string text) {
Safe.ArgumentNotEmpty(text, nameof(text));
m_text = text;
}
bool ReadToken() {
if (m_pos >= m_text.Length) {
m_token = TokenType.Eof;
m_tokenValue = null;
return false;
}
var m = _tokens.Match(m_text, m_pos);
if (m.Success) {
cin
Implemented typereference parser
r268 m_tokenPos = m_pos;
cin
Working on Unity xml configuration
r267 m_pos += m.Length;
if (m.Groups[1].Success) {
m_token = TokenType.Word;
m_tokenValue = m.Groups[1].Value;
} else if (m.Groups[2].Success) {
m_tokenValue = null;
switch (m.Groups[2].Value) {
case "{":
m_token = TokenType.OpenList;
break;
case "}":
m_token = TokenType.CloseList;
break;
case ".":
m_token = TokenType.Dot;
break;
case ",":
m_token = TokenType.Comma;
break;
}
}
return true;
}
throw new FormatException($"Failed to parse '{m_text}' at pos {m_pos}");
}
cin
Implemented typereference parser
r268 public TypeReference Parse() {
var result = ReadTypeReference();
if (ReadToken())
ThrowUnexpectedToken();
return result;
cin
Working on Unity xml configuration
r267 }
string[] ReadTypeName() {
var parts = new List<string>();
string current = null;
bool stop = false;
while ((!stop) && ReadToken()) {
switch (Token) {
case TokenType.Word:
if (current != null)
ThrowUnexpectedToken();
current = TokenValue;
break;
case TokenType.Dot:
if (current == null)
ThrowUnexpectedToken();
parts.Add(current);
current = null;
break;
default:
stop = true;
break;
}
}
if (current != null)
parts.Add(current);
if (parts.Count == 0)
return null;
return parts.ToArray();
}
TypeReference ReadTypeReference() {
var parts = ReadTypeName();
if (parts == null)
return null;
var typeReference = new TypeReference {
cin
Implemented typereference parser
r268 Namespace = string.Join(".", parts, 0, parts.Length - 1),
TypeName = parts[parts.Length - 1]
};
cin
Working on Unity xml configuration
r267
switch (Token) {
case TokenType.OpenList:
typeReference.GenericParameters = ReadTypeReferenceList();
cin
Implemented typereference parser
r268 if (Token != TokenType.CloseList)
cin
Working on Unity xml configuration
r267 ThrowUnexpectedToken();
cin
Implemented typereference parser
r268 ReadToken();
cin
Working on Unity xml configuration
r267 break;
}
return typeReference;
}
TypeReference[] ReadTypeReferenceList() {
cin
Implemented typereference parser
r268 var list = new List<TypeReference>();
cin
Working on Unity xml configuration
r267
cin
Implemented typereference parser
r268 do {
var typeReference = ReadTypeReference();
list.Add(typeReference);
} while (Token == TokenType.Comma);
return list.ToArray();
cin
Working on Unity xml configuration
r267 }
void ThrowUnexpectedToken() {
cin
Implemented typereference parser
r268 throw new FormatException($"Unexpected '{Token}' at pos {TokenPos}: -->{m_text.Substring(TokenPos, Math.Min(m_text.Length - TokenPos, 10))}");
cin
Working on Unity xml configuration
r267 }
}
}