[NUI] Add Tizen.NUI.XamlBuild module
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / internal / Xaml / TypeArgumentsParser.cs
1
2 using System.Collections.Generic;
3 using System.Xml;
4
5 namespace Tizen.NUI.Xaml
6 {
7     internal static class TypeArgumentsParser
8     {
9         public static IList<XmlType> ParseExpression(string expression, IXmlNamespaceResolver resolver, IXmlLineInfo lineInfo)
10         {
11             var typeList = new List<XmlType>();
12             while (!string.IsNullOrWhiteSpace(expression))
13             {
14                 var match = expression;
15                 typeList.Add(Parse(match, ref expression, resolver, lineInfo));
16             }
17             return typeList;
18         }
19
20         static XmlType Parse(string match, ref string remaining, IXmlNamespaceResolver resolver, IXmlLineInfo lineinfo)
21         {
22             remaining = null;
23             int parensCount = 0;
24             int pos = 0;
25             bool isGeneric = false;
26
27             for (pos = 0; pos < match.Length; pos++)
28             {
29                 if (match[pos] == '(')
30                 {
31                     parensCount++;
32                     isGeneric = true;
33                 }
34                 else if (match[pos] == ')')
35                     parensCount--;
36                 else if (match[pos] == ',' && parensCount == 0)
37                 {
38                     remaining = match.Substring(pos + 1);
39                     break;
40                 }
41             }
42             var type = match.Substring(0, pos).Trim();
43
44             IList<XmlType> typeArguments = null;
45             if (isGeneric)
46             {
47                 typeArguments = ParseExpression(
48                     type.Substring(type.IndexOf('(') + 1, type.LastIndexOf(')') - type.IndexOf('(') - 1), resolver, lineinfo);
49                 type = type.Substring(0, type.IndexOf('('));
50             }
51
52             var split = type.Split(':');
53             if (split.Length > 2)
54                 return null;
55
56             string prefix, name;
57             if (split.Length == 2) {
58                 prefix = split [0];
59                 name = split [1];
60             } else {
61                 prefix = "";
62                 name = split [0];
63             }
64
65             var namespaceuri = resolver.LookupNamespace(prefix);
66             if (namespaceuri == null)
67                 throw new XamlParseException($"No xmlns declaration for prefix '{prefix}'.", lineinfo, null);
68             return new XmlType(namespaceuri, name, typeArguments);
69         }
70     }
71 }