diff Implab.ServiceHost/Unity/TypeReferenceParser.cs @ 268:0be8a6ae8307 v3

Implemented typereference parser
author cin
date Sun, 22 Apr 2018 15:29:10 +0300
parents 6b3e5c48131b
children ff581cff7003
line wrap: on
line diff
--- 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