|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#if NETCOREAPP |
| 5 | +#nullable enable |
| 6 | +#endif |
| 7 | + |
| 8 | +using System.Globalization; |
| 9 | + |
| 10 | +namespace MonoDevelop.MSBuild.Language; |
| 11 | + |
| 12 | +static class TypeNameValidation |
| 13 | +{ |
| 14 | + public static bool IsValidCSharpTypeOrNamespace (string value) => IsValidCSharpTypeOrNamespace(value, out _, out _); |
| 15 | + |
| 16 | + public static bool IsValidCSharpTypeOrNamespace (string value, out int componentCount, out bool isGenericType) |
| 17 | + { |
| 18 | + int offset = 0; |
| 19 | + if (!ConsumeCSharpGenericTypeOrNamespace(value, ref offset, out componentCount, out isGenericType)) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + return offset == value.Length; |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + static bool ConsumeCSharpGenericTypeOrNamespace (string value, ref int offset, out int componentCount, out bool isGenericType) |
| 27 | + { |
| 28 | + isGenericType = false; |
| 29 | + |
| 30 | + componentCount = ConsumeIdentifiers(value, ref offset); |
| 31 | + |
| 32 | + if (componentCount == 0) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + if (offset == value.Length) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + // try to consume generics in the form SomeType<SomeOtherType,AnotherType> |
| 41 | + if (value[offset] != '<') { |
| 42 | + // complete but not a generic type, we are done consuming, caller decides what to do with next char |
| 43 | + return true; |
| 44 | + } |
| 45 | + offset++; |
| 46 | + |
| 47 | + isGenericType = true; |
| 48 | + do { |
| 49 | + // Ignore whitespace after a comma as that's pretty common e.g. SomeType<SomeOtherType, AnotherType> |
| 50 | + // and is valid if this is going to be injected verbatim into a C# file. |
| 51 | + // We could be more strict and disallow whitespace, but that will likely trip people up. |
| 52 | + // We could also be more liberal and allow general whitespace around the angle brackets and commas, |
| 53 | + // but that's a bit of a rabbit hole and not likely to be very useful. |
| 54 | + if (value[offset] == ',') { |
| 55 | + offset++; |
| 56 | + ConsumeSpaces(value, ref offset); |
| 57 | + } |
| 58 | + if (!ConsumeCSharpGenericTypeOrNamespace(value, ref offset, out int consumedComponents, out _)) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } while (value[offset] == ','); |
| 62 | + |
| 63 | + if (offset < value.Length && value[offset] == '>') { |
| 64 | + offset++; |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + static void ConsumeSpaces(string value, ref int offset) |
| 72 | + { |
| 73 | + while (value[offset] == ' ') { |
| 74 | + offset++; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static bool IsValidClrNamespace (string value) => IsValidClrTypeOrNamespace(value, out _, out var hasGenerics) && !hasGenerics; |
| 79 | + |
| 80 | + public static bool IsValidClrTypeName (string value) => IsValidClrTypeOrNamespace(value, out var componentCount, out var hasGenerics) && componentCount == 1 && !hasGenerics; |
| 81 | + |
| 82 | + public static bool IsValidClrTypeOrNamespace (string value) => IsValidClrTypeOrNamespace(value, out _, out _); |
| 83 | + |
| 84 | + public static bool IsValidClrTypeOrNamespace (string value, out int componentCount, out bool isGenericType) |
| 85 | + { |
| 86 | + int offset = 0; |
| 87 | + if (!ConsumeClrGenericTypeOrNamespace(value, ref offset, out componentCount, out isGenericType)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + return offset == value.Length; |
| 91 | + } |
| 92 | + |
| 93 | + static bool ConsumeClrGenericTypeOrNamespace (string value, ref int offset, out int componentCount, out bool isGenericType) |
| 94 | + { |
| 95 | + isGenericType = false; |
| 96 | + |
| 97 | + componentCount = ConsumeIdentifiers(value, ref offset); |
| 98 | + |
| 99 | + if (componentCount == 0) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + if (offset == value.Length) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + // try to consume generics in the form SomeType`2[SomeOtherType,AnotherType] |
| 108 | + if (value[offset] != '`') { |
| 109 | + // complete but not a generic type, we are done consuming, caller decides what to do with next char |
| 110 | + return true; |
| 111 | + } |
| 112 | + offset++; |
| 113 | + |
| 114 | + int argCountOffset = offset; |
| 115 | + if(!ConsumeNumbers(value, ref offset) || !int.TryParse(value.Substring(argCountOffset, offset - argCountOffset), out int genericArgCount)) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + isGenericType = true; |
| 120 | + if (value[offset++] != '[') { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + for (int i = 0; i < genericArgCount; i++) { |
| 125 | + // recursively consume the generic type arguments |
| 126 | + if (!ConsumeClrGenericTypeOrNamespace(value, ref offset, out int consumedComponents, out _)) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + if (i < genericArgCount - 1) { |
| 130 | + if (offset < value.Length && value[offset] == ',') { |
| 131 | + offset++; |
| 132 | + } else { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (offset < value.Length && value[offset] == ']') { |
| 139 | + offset++; |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + static bool ConsumeNumbers (string value, ref int offset) |
| 147 | + { |
| 148 | + int start = offset; |
| 149 | + while(char.IsDigit(value[offset])) { |
| 150 | + offset++; |
| 151 | + } |
| 152 | + return offset > start; |
| 153 | + } |
| 154 | + |
| 155 | + static int ConsumeIdentifiers (string value, ref int offset) |
| 156 | + { |
| 157 | + int componentCount = 0; |
| 158 | + |
| 159 | + while(ConsumeIdentifier(value, ref offset)) { |
| 160 | + componentCount++; |
| 161 | + if (offset >= value.Length || value[offset] != '.') { |
| 162 | + return componentCount; |
| 163 | + } |
| 164 | + offset++; |
| 165 | + } |
| 166 | + |
| 167 | + if (componentCount > 0 && value[offset - 1] == '.') { |
| 168 | + // we consumed a dot but no identifier followed it, so walk it back |
| 169 | + offset--; |
| 170 | + } |
| 171 | + |
| 172 | + return componentCount; |
| 173 | + } |
| 174 | + |
| 175 | + static bool ConsumeIdentifier (string value, ref int offset) |
| 176 | + { |
| 177 | + bool nextMustBeStartChar = true; |
| 178 | + |
| 179 | + while (offset < value.Length) { |
| 180 | + char ch = value[offset]; |
| 181 | + // based on https://github.com/dotnet/runtime/blob/96be510135829ff199c6c341ad461c36bab4809e/src/libraries/Common/src/System/CSharpHelpers.cs#L141 |
| 182 | + UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(ch); |
| 183 | + switch (uc) { |
| 184 | + case UnicodeCategory.UppercaseLetter: // Lu |
| 185 | + case UnicodeCategory.LowercaseLetter: // Ll |
| 186 | + case UnicodeCategory.TitlecaseLetter: // Lt |
| 187 | + case UnicodeCategory.ModifierLetter: // Lm |
| 188 | + case UnicodeCategory.LetterNumber: // Lm |
| 189 | + case UnicodeCategory.OtherLetter: // Lo |
| 190 | + nextMustBeStartChar = false; |
| 191 | + offset++; |
| 192 | + continue; |
| 193 | + |
| 194 | + case UnicodeCategory.NonSpacingMark: // Mn |
| 195 | + case UnicodeCategory.SpacingCombiningMark: // Mc |
| 196 | + case UnicodeCategory.ConnectorPunctuation: // Pc |
| 197 | + case UnicodeCategory.DecimalDigitNumber: // Nd |
| 198 | + // Underscore is a valid starting character, even though it is a ConnectorPunctuation. |
| 199 | + if (nextMustBeStartChar && ch != '_') |
| 200 | + return false; |
| 201 | + offset++; |
| 202 | + continue; |
| 203 | + |
| 204 | + default: |
| 205 | + // unlike CSharpHelpers.cs, don't check IsSpecialTypeChar here because we're only looking |
| 206 | + // for identifier components that are valid for all languages, and generic syntax is |
| 207 | + // handled separately. |
| 208 | + // |
| 209 | + break; |
| 210 | + } |
| 211 | + break; |
| 212 | + } |
| 213 | + |
| 214 | + // return true if we have been able to consume a valid identifier. it need |
| 215 | + // not be the entire string. |
| 216 | + return !nextMustBeStartChar; |
| 217 | + } |
| 218 | + |
| 219 | + public static bool IsValidCSharpType (string value, out int componentCount) |
| 220 | + { |
| 221 | + string[] components = value.Split ('.'); |
| 222 | + componentCount = components.Length; |
| 223 | + foreach (var component in components) { |
| 224 | + if (!System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier (component)) { |
| 225 | + return false; |
| 226 | + } |
| 227 | + } |
| 228 | + return true; |
| 229 | + } |
| 230 | +} |
0 commit comments