CodeDom.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #if MOBILE
  2. using System;
  3. using System.Globalization;
  4. namespace Microsoft.CSharp
  5. {
  6. internal class CodeDomProvider
  7. {
  8. public string CreateEscapedIdentifier (string name)
  9. {
  10. // Any identifier started with two consecutive underscores are
  11. // reserved by CSharp.
  12. if (IsKeyword(name) || IsPrefixTwoUnderscore(name)) {
  13. return "@" + name;
  14. }
  15. return name;
  16. }
  17. static bool IsKeyword(string value) {
  18. return false;
  19. }
  20. static bool IsPrefixTwoUnderscore(string value) {
  21. if( value.Length < 3) {
  22. return false;
  23. }
  24. else {
  25. return ((value[0] == '_') && (value[1] == '_') && (value[2] != '_'));
  26. }
  27. }
  28. }
  29. internal class CSharpCodeProvider : CodeDomProvider
  30. {
  31. }
  32. class CodeGenerator
  33. {
  34. public static bool IsValidLanguageIndependentIdentifier(string value)
  35. {
  36. return IsValidTypeNameOrIdentifier(value, false);
  37. }
  38. private static bool IsValidTypeNameOrIdentifier(string value, bool isTypeName) {
  39. bool nextMustBeStartChar = true;
  40. if (value.Length == 0)
  41. return false;
  42. // each char must be Lu, Ll, Lt, Lm, Lo, Nd, Mn, Mc, Pc
  43. //
  44. for(int i = 0; i < value.Length; i++) {
  45. char ch = value[i];
  46. UnicodeCategory uc = Char.GetUnicodeCategory(ch);
  47. switch (uc) {
  48. case UnicodeCategory.UppercaseLetter: // Lu
  49. case UnicodeCategory.LowercaseLetter: // Ll
  50. case UnicodeCategory.TitlecaseLetter: // Lt
  51. case UnicodeCategory.ModifierLetter: // Lm
  52. case UnicodeCategory.LetterNumber: // Lm
  53. case UnicodeCategory.OtherLetter: // Lo
  54. nextMustBeStartChar = false;
  55. break;
  56. case UnicodeCategory.NonSpacingMark: // Mn
  57. case UnicodeCategory.SpacingCombiningMark: // Mc
  58. case UnicodeCategory.ConnectorPunctuation: // Pc
  59. case UnicodeCategory.DecimalDigitNumber: // Nd
  60. // Underscore is a valid starting character, even though it is a ConnectorPunctuation.
  61. if (nextMustBeStartChar && ch != '_')
  62. return false;
  63. nextMustBeStartChar = false;
  64. break;
  65. default:
  66. // We only check the special Type chars for type names.
  67. if (isTypeName && IsSpecialTypeChar(ch, ref nextMustBeStartChar)) {
  68. break;
  69. }
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. private static bool IsSpecialTypeChar(char ch, ref bool nextMustBeStartChar) {
  76. switch(ch) {
  77. case ':':
  78. case '.':
  79. case '$':
  80. case '+':
  81. case '<':
  82. case '>':
  83. case '-':
  84. case '[':
  85. case ']':
  86. case ',':
  87. case '&':
  88. case '*':
  89. nextMustBeStartChar = true;
  90. return true;
  91. case '`':
  92. return true;
  93. }
  94. return false;
  95. }
  96. }
  97. }
  98. #endif